1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* X pop-up deck-of-cards menu facility for GNU Emacs.
24 * Written by Jon Arnold and Roman Budzianowski
25 * Mods and rewrite by Robert Krawitz
29 /* Modified by Fred Pierresteguy on December 93
30 to make the popup menus and menubar use the Xt. */
32 /* Rewritten for clarity and GC protection by rms in Feb 94. */
36 #if 0 /* Why was this included? And without syssignal.h? */
37 /* On 4.3 this loses if it comes after xterm.h. */
44 #include "termhooks.h"
49 #include "blockinput.h"
53 #include "sysselect.h"
60 /* This may include sys/types.h, and that somehow loses
61 if this is not done before the other system files. */
65 /* Load sys/types.h if not already loaded.
66 In some systems loading it twice is suicidal. */
68 #include <sys/types.h>
71 #include "dispextern.h"
74 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
75 code accepts the Emacs internal encoding. */
76 #undef HAVE_MULTILINGUAL_MENU
80 #include <X11/IntrinsicP.h>
81 #include <X11/CoreP.h>
82 #include <X11/StringDefs.h>
83 #include <X11/Shell.h>
85 #include <X11/Xaw/Paned.h>
86 #endif /* USE_LUCID */
87 #include "../lwlib/lwlib.h"
88 #else /* not USE_X_TOOLKIT */
90 #include "../oldXMenu/XMenu.h"
92 #endif /* not USE_X_TOOLKIT */
93 #endif /* HAVE_X_WINDOWS */
100 Lisp_Object Vmenu_updating_frame
;
102 Lisp_Object Qdebug_on_next_call
;
104 extern Lisp_Object Qmenu_bar
;
106 extern Lisp_Object QCtoggle
, QCradio
;
108 extern Lisp_Object Voverriding_local_map
;
109 extern Lisp_Object Voverriding_local_map_menu_flag
;
111 extern Lisp_Object Qoverriding_local_map
, Qoverriding_terminal_local_map
;
113 extern Lisp_Object Qmenu_bar_update_hook
;
116 extern void set_frame_menubar
P_ ((FRAME_PTR
, int, int));
117 extern XtAppContext Xt_app_con
;
119 static Lisp_Object xdialog_show
P_ ((FRAME_PTR
, int, Lisp_Object
, Lisp_Object
,
121 static void popup_get_selection
P_ ((XEvent
*, struct x_display_info
*,
124 /* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
127 #endif /* USE_X_TOOLKIT */
132 extern void set_frame_menubar
P_ ((FRAME_PTR
, int, int));
133 static Lisp_Object xdialog_show
P_ ((FRAME_PTR
, int, Lisp_Object
, Lisp_Object
,
137 /* This is how to deal with multibyte text if HAVE_MULTILINGUAL_MENU
138 isn't defined. The use of HAVE_MULTILINGUAL_MENU could probably be
139 confined to an extended version of this with sections of code below
140 using it unconditionally. */
142 /* gtk just uses utf-8. */
143 # define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
144 #elif defined HAVE_X_I18N
145 # define ENCODE_MENU_STRING(str) ENCODE_SYSTEM (str)
147 # define ENCODE_MENU_STRING(str) string_make_unibyte (str)
150 static void push_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
151 Lisp_Object
, Lisp_Object
, Lisp_Object
,
152 Lisp_Object
, Lisp_Object
));
153 static int update_frame_menubar
P_ ((struct frame
*));
154 static Lisp_Object xmenu_show
P_ ((struct frame
*, int, int, int, int,
155 Lisp_Object
, char **));
156 static void keymap_panes
P_ ((Lisp_Object
*, int, int));
157 static void single_keymap_panes
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
159 static void list_of_panes
P_ ((Lisp_Object
));
160 static void list_of_items
P_ ((Lisp_Object
));
163 /* This holds a Lisp vector that holds the results of decoding
164 the keymaps or alist-of-alists that specify a menu.
166 It describes the panes and items within the panes.
168 Each pane is described by 3 elements in the vector:
169 t, the pane name, the pane's prefix key.
170 Then follow the pane's items, with 5 elements per item:
171 the item string, the enable flag, the item's value,
172 the definition, and the equivalent keyboard key's description string.
174 In some cases, multiple levels of menus may be described.
175 A single vector slot containing nil indicates the start of a submenu.
176 A single vector slot containing lambda indicates the end of a submenu.
177 The submenu follows a menu item which is the way to reach the submenu.
179 A single vector slot containing quote indicates that the
180 following items should appear on the right of a dialog box.
182 Using a Lisp vector to hold this information while we decode it
183 takes care of protecting all the data from GC. */
185 #define MENU_ITEMS_PANE_NAME 1
186 #define MENU_ITEMS_PANE_PREFIX 2
187 #define MENU_ITEMS_PANE_LENGTH 3
191 MENU_ITEMS_ITEM_NAME
= 0,
192 MENU_ITEMS_ITEM_ENABLE
,
193 MENU_ITEMS_ITEM_VALUE
,
194 MENU_ITEMS_ITEM_EQUIV_KEY
,
195 MENU_ITEMS_ITEM_DEFINITION
,
196 MENU_ITEMS_ITEM_TYPE
,
197 MENU_ITEMS_ITEM_SELECTED
,
198 MENU_ITEMS_ITEM_HELP
,
199 MENU_ITEMS_ITEM_LENGTH
202 static Lisp_Object menu_items
;
204 /* If non-nil, means that the global vars defined here are already in use.
205 Used to detect cases where we try to re-enter this non-reentrant code. */
206 static Lisp_Object menu_items_inuse
;
208 /* Number of slots currently allocated in menu_items. */
209 static int menu_items_allocated
;
211 /* This is the index in menu_items of the first empty slot. */
212 static int menu_items_used
;
214 /* The number of panes currently recorded in menu_items,
215 excluding those within submenus. */
216 static int menu_items_n_panes
;
218 /* Current depth within submenus. */
219 static int menu_items_submenu_depth
;
221 /* Flag which when set indicates a dialog or menu has been posted by
222 Xt on behalf of one of the widget sets. */
223 static int popup_activated_flag
;
225 static int next_menubar_widget_id
;
227 /* This is set nonzero after the user activates the menu bar, and set
228 to zero again after the menu bars are redisplayed by prepare_menu_bar.
229 While it is nonzero, all calls to set_frame_menubar go deep.
231 I don't understand why this is needed, but it does seem to be
232 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
234 int pending_menu_activation
;
238 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
240 static struct frame
*
241 menubar_id_to_frame (id
)
244 Lisp_Object tail
, frame
;
247 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
250 if (!GC_FRAMEP (frame
))
253 if (!FRAME_WINDOW_P (f
))
255 if (f
->output_data
.x
->id
== id
)
263 /* Initialize the menu_items structure if we haven't already done so.
264 Also mark it as currently empty. */
269 if (!NILP (menu_items_inuse
))
270 error ("Trying to use a menu from within a menu-entry");
272 if (NILP (menu_items
))
274 menu_items_allocated
= 60;
275 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
278 menu_items_inuse
= Qt
;
280 menu_items_n_panes
= 0;
281 menu_items_submenu_depth
= 0;
284 /* Call at the end of generating the data in menu_items. */
292 unuse_menu_items (dummy
)
295 return menu_items_inuse
= Qnil
;
298 /* Call when finished using the data for the current menu
302 discard_menu_items ()
304 /* Free the structure if it is especially large.
305 Otherwise, hold on to it, to save time. */
306 if (menu_items_allocated
> 200)
309 menu_items_allocated
= 0;
311 xassert (NILP (menu_items_inuse
));
314 /* This undoes save_menu_items, and it is called by the specpdl unwind
318 restore_menu_items (saved
)
321 menu_items
= XCAR (saved
);
322 menu_items_inuse
= (! NILP (menu_items
) ? Qt
: Qnil
);
323 menu_items_allocated
= (VECTORP (menu_items
) ? ASIZE (menu_items
) : 0);
324 saved
= XCDR (saved
);
325 menu_items_used
= XINT (XCAR (saved
));
326 saved
= XCDR (saved
);
327 menu_items_n_panes
= XINT (XCAR (saved
));
328 saved
= XCDR (saved
);
329 menu_items_submenu_depth
= XINT (XCAR (saved
));
332 /* Push the whole state of menu_items processing onto the specpdl.
333 It will be restored when the specpdl is unwound. */
338 Lisp_Object saved
= list4 (!NILP (menu_items_inuse
) ? menu_items
: Qnil
,
339 make_number (menu_items_used
),
340 make_number (menu_items_n_panes
),
341 make_number (menu_items_submenu_depth
));
342 record_unwind_protect (restore_menu_items
, saved
);
343 menu_items_inuse
= Qnil
;
347 /* Make the menu_items vector twice as large. */
353 int old_size
= menu_items_allocated
;
356 menu_items_allocated
*= 2;
358 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
359 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
360 old_size
* sizeof (Lisp_Object
));
363 /* Begin a submenu. */
366 push_submenu_start ()
368 if (menu_items_used
+ 1 > menu_items_allocated
)
371 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
372 menu_items_submenu_depth
++;
380 if (menu_items_used
+ 1 > menu_items_allocated
)
383 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
384 menu_items_submenu_depth
--;
387 /* Indicate boundary between left and right. */
390 push_left_right_boundary ()
392 if (menu_items_used
+ 1 > menu_items_allocated
)
395 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
398 /* Start a new menu pane in menu_items.
399 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
402 push_menu_pane (name
, prefix_vec
)
403 Lisp_Object name
, prefix_vec
;
405 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
408 if (menu_items_submenu_depth
== 0)
409 menu_items_n_panes
++;
410 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
411 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
412 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
415 /* Push one menu item into the current pane. NAME is the string to
416 display. ENABLE if non-nil means this item can be selected. KEY
417 is the key generated by choosing this item, or nil if this item
418 doesn't really have a definition. DEF is the definition of this
419 item. EQUIV is the textual description of the keyboard equivalent
420 for this item (or nil if none). TYPE is the type of this menu
421 item, one of nil, `toggle' or `radio'. */
424 push_menu_item (name
, enable
, key
, def
, equiv
, type
, selected
, help
)
425 Lisp_Object name
, enable
, key
, def
, equiv
, type
, selected
, help
;
427 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
430 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
431 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
432 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
433 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
434 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
435 XVECTOR (menu_items
)->contents
[menu_items_used
++] = type
;
436 XVECTOR (menu_items
)->contents
[menu_items_used
++] = selected
;
437 XVECTOR (menu_items
)->contents
[menu_items_used
++] = help
;
440 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
441 and generate menu panes for them in menu_items.
442 If NOTREAL is nonzero,
443 don't bother really computing whether an item is enabled. */
446 keymap_panes (keymaps
, nmaps
, notreal
)
447 Lisp_Object
*keymaps
;
455 /* Loop over the given keymaps, making a pane for each map.
456 But don't make a pane that is empty--ignore that map instead.
457 P is the number of panes we have made so far. */
458 for (mapno
= 0; mapno
< nmaps
; mapno
++)
459 single_keymap_panes (keymaps
[mapno
],
460 Fkeymap_prompt (keymaps
[mapno
]), Qnil
, notreal
, 10);
462 finish_menu_items ();
465 /* Args passed between single_keymap_panes and single_menu_item. */
468 Lisp_Object pending_maps
;
469 int maxdepth
, notreal
;
473 static void single_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
476 /* This is a recursive subroutine of keymap_panes.
477 It handles one keymap, KEYMAP.
478 The other arguments are passed along
479 or point to local variables of the previous function.
480 If NOTREAL is nonzero, only check for equivalent key bindings, don't
481 evaluate expressions in menu items and don't make any menu.
483 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
486 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
488 Lisp_Object pane_name
;
496 skp
.pending_maps
= Qnil
;
497 skp
.maxdepth
= maxdepth
;
498 skp
.notreal
= notreal
;
504 push_menu_pane (pane_name
, prefix
);
507 /* Remember index for first item in this pane so we can go back and
508 add a prefix when (if) we see the first button. After that, notbuttons
509 is set to 0, to mark that we have seen a button and all non button
510 items need a prefix. */
511 skp
.notbuttons
= menu_items_used
;
514 GCPRO1 (skp
.pending_maps
);
515 map_keymap (keymap
, single_menu_item
, Qnil
, &skp
, 1);
518 /* Process now any submenus which want to be panes at this level. */
519 while (CONSP (skp
.pending_maps
))
521 Lisp_Object elt
, eltcdr
, string
;
522 elt
= XCAR (skp
.pending_maps
);
524 string
= XCAR (eltcdr
);
525 /* We no longer discard the @ from the beginning of the string here.
526 Instead, we do this in xmenu_show. */
527 single_keymap_panes (Fcar (elt
), string
,
528 XCDR (eltcdr
), notreal
, maxdepth
- 1);
529 skp
.pending_maps
= XCDR (skp
.pending_maps
);
533 /* This is a subroutine of single_keymap_panes that handles one
535 KEY is a key in a keymap and ITEM is its binding.
536 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
538 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
539 evaluate expressions in menu items and don't make any menu.
540 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them.
541 SKP->NOTBUTTONS is only used when simulating toggle boxes and radio
542 buttons. It keeps track of if we have seen a button in this menu or
546 single_menu_item (key
, item
, dummy
, skp_v
)
547 Lisp_Object key
, item
, dummy
;
550 Lisp_Object map
, item_string
, enabled
;
551 struct gcpro gcpro1
, gcpro2
;
553 struct skp
*skp
= skp_v
;
555 /* Parse the menu item and leave the result in item_properties. */
557 res
= parse_menu_item (item
, skp
->notreal
, 0);
560 return; /* Not a menu item. */
562 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
566 /* We don't want to make a menu, just traverse the keymaps to
567 precompute equivalent key bindings. */
569 single_keymap_panes (map
, Qnil
, key
, 1, skp
->maxdepth
- 1);
573 enabled
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_ENABLE
];
574 item_string
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_NAME
];
576 if (!NILP (map
) && SREF (item_string
, 0) == '@')
579 /* An enabled separate pane. Remember this to handle it later. */
580 skp
->pending_maps
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
586 /* Simulate radio buttons and toggle boxes by putting a prefix in
589 Lisp_Object prefix
= Qnil
;
590 Lisp_Object type
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
];
594 = XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
];
597 /* The first button. Line up previous items in this menu. */
599 int index
= skp
->notbuttons
; /* Index for first item this menu. */
602 while (index
< menu_items_used
)
605 = XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
];
609 submenu
++; /* Skip sub menu. */
611 else if (EQ (tem
, Qlambda
))
614 submenu
--; /* End sub menu. */
616 else if (EQ (tem
, Qt
))
617 index
+= 3; /* Skip new pane marker. */
618 else if (EQ (tem
, Qquote
))
619 index
++; /* Skip a left, right divider. */
622 if (!submenu
&& SREF (tem
, 0) != '\0'
623 && SREF (tem
, 0) != '-')
624 XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
]
625 = concat2 (build_string (" "), tem
);
626 index
+= MENU_ITEMS_ITEM_LENGTH
;
632 /* Calculate prefix, if any, for this item. */
633 if (EQ (type
, QCtoggle
))
634 prefix
= build_string (NILP (selected
) ? "[ ] " : "[X] ");
635 else if (EQ (type
, QCradio
))
636 prefix
= build_string (NILP (selected
) ? "( ) " : "(*) ");
638 /* Not a button. If we have earlier buttons, then we need a prefix. */
639 else if (!skp
->notbuttons
&& SREF (item_string
, 0) != '\0'
640 && SREF (item_string
, 0) != '-')
641 prefix
= build_string (" ");
644 item_string
= concat2 (prefix
, item_string
);
646 #endif /* not HAVE_BOXES */
648 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
650 /* Indicate visually that this is a submenu. */
651 item_string
= concat2 (item_string
, build_string (" >"));
654 push_menu_item (item_string
, enabled
, key
,
655 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_DEF
],
656 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_KEYEQ
],
657 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
],
658 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
],
659 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_HELP
]);
661 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
662 /* Display a submenu using the toolkit. */
663 if (! (NILP (map
) || NILP (enabled
)))
665 push_submenu_start ();
666 single_keymap_panes (map
, Qnil
, key
, 0, skp
->maxdepth
- 1);
672 /* Push all the panes and items of a menu described by the
673 alist-of-alists MENU.
674 This handles old-fashioned calls to x-popup-menu. */
684 for (tail
= menu
; CONSP (tail
); tail
= XCDR (tail
))
686 Lisp_Object elt
, pane_name
, pane_data
;
688 pane_name
= Fcar (elt
);
689 CHECK_STRING (pane_name
);
690 push_menu_pane (ENCODE_MENU_STRING (pane_name
), Qnil
);
691 pane_data
= Fcdr (elt
);
692 CHECK_CONS (pane_data
);
693 list_of_items (pane_data
);
696 finish_menu_items ();
699 /* Push the items in a single pane defined by the alist PANE. */
705 Lisp_Object tail
, item
, item1
;
707 for (tail
= pane
; CONSP (tail
); tail
= XCDR (tail
))
711 push_menu_item (ENCODE_MENU_STRING (item
), Qnil
, Qnil
, Qt
,
712 Qnil
, Qnil
, Qnil
, Qnil
);
713 else if (CONSP (item
))
716 CHECK_STRING (item1
);
717 push_menu_item (ENCODE_MENU_STRING (item1
), Qt
, XCDR (item
),
718 Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
721 push_left_right_boundary ();
726 #ifdef HAVE_X_WINDOWS
727 /* Return the mouse position in *X and *Y. The coordinates are window
728 relative for the edit window in frame F.
729 This is for Fx_popup_menu. The mouse_position_hook can not
730 be used for X, as it returns window relative coordinates
731 for the window where the mouse is in. This could be the menu bar,
732 the scroll bar or the edit window. Fx_popup_menu needs to be
733 sure it is the edit window. */
735 mouse_position_for_popup (f
, x
, y
)
740 Window root
, dummy_window
;
745 XQueryPointer (FRAME_X_DISPLAY (f
),
746 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
748 /* The root window which contains the pointer. */
751 /* Window pointer is on, not used */
754 /* The position on that root window. */
757 /* x/y in dummy_window coordinates, not used. */
760 /* Modifier keys and pointer buttons, about which
762 (unsigned int *) &dummy
);
766 /* xmenu_show expects window coordinates, not root window
767 coordinates. Translate. */
768 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
769 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
772 #endif /* HAVE_X_WINDOWS */
774 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
775 doc
: /* Pop up a deck-of-cards menu and return user's selection.
776 POSITION is a position specification. This is either a mouse button event
777 or a list ((XOFFSET YOFFSET) WINDOW)
778 where XOFFSET and YOFFSET are positions in pixels from the top left
779 corner of WINDOW. (WINDOW may be a window or a frame object.)
780 This controls the position of the top left of the menu as a whole.
781 If POSITION is t, it means to use the current mouse position.
783 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
784 The menu items come from key bindings that have a menu string as well as
785 a definition; actually, the "definition" in such a key binding looks like
786 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
787 the keymap as a top-level element.
789 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
790 Otherwise, REAL-DEFINITION should be a valid key binding definition.
792 You can also use a list of keymaps as MENU.
793 Then each keymap makes a separate pane.
795 When MENU is a keymap or a list of keymaps, the return value is the
796 list of events corresponding to the user's choice. Note that
797 `x-popup-menu' does not actually execute the command bound to that
800 Alternatively, you can specify a menu of multiple panes
801 with a list of the form (TITLE PANE1 PANE2...),
802 where each pane is a list of form (TITLE ITEM1 ITEM2...).
803 Each ITEM is normally a cons cell (STRING . VALUE);
804 but a string can appear as an item--that makes a nonselectable line
806 With this form of menu, the return value is VALUE from the chosen item.
808 If POSITION is nil, don't display the menu at all, just precalculate the
809 cached information about equivalent key sequences.
811 If the user gets rid of the menu without making a valid choice, for
812 instance by clicking the mouse away from a valid choice or by typing
813 keyboard input, then this normally results in a quit and
814 `x-popup-menu' does not return. But if POSITION is a mouse button
815 event (indicating that the user invoked the menu with the mouse) then
816 no quit occurs and `x-popup-menu' returns nil. */)
818 Lisp_Object position
, menu
;
820 Lisp_Object keymap
, tem
;
821 int xpos
= 0, ypos
= 0;
823 char *error_name
= NULL
;
824 Lisp_Object selection
;
826 Lisp_Object x
, y
, window
;
829 int specpdl_count
= SPECPDL_INDEX ();
833 if (! NILP (position
))
835 int get_current_pos_p
= 0;
838 /* Decode the first argument: find the window and the coordinates. */
839 if (EQ (position
, Qt
)
840 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
841 || EQ (XCAR (position
), Qtool_bar
))))
843 get_current_pos_p
= 1;
847 tem
= Fcar (position
);
850 window
= Fcar (Fcdr (position
));
852 y
= Fcar (XCDR (tem
));
857 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
858 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
859 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
864 /* If a click happens in an external tool bar or a detached
865 tool bar, x and y is NIL. In that case, use the current
866 mouse position. This happens for the help button in the
867 tool bar. Ideally popup-menu should pass NIL to
868 this function, but it doesn't. */
869 if (NILP (x
) && NILP (y
))
870 get_current_pos_p
= 1;
873 if (get_current_pos_p
)
875 /* Use the mouse's current position. */
876 FRAME_PTR new_f
= SELECTED_FRAME ();
877 #ifdef HAVE_X_WINDOWS
878 /* Can't use mouse_position_hook for X since it returns
879 coordinates relative to the window the mouse is in,
880 we need coordinates relative to the edit widget always. */
885 mouse_position_for_popup (new_f
, &cur_x
, &cur_y
);
886 /* cur_x/y may be negative, so use make_number. */
887 x
= make_number (cur_x
);
888 y
= make_number (cur_y
);
891 #else /* not HAVE_X_WINDOWS */
892 Lisp_Object bar_window
;
893 enum scroll_bar_part part
;
896 if (mouse_position_hook
)
897 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
898 &part
, &x
, &y
, &time
);
899 #endif /* not HAVE_X_WINDOWS */
902 XSETFRAME (window
, new_f
);
905 window
= selected_window
;
914 /* Decode where to put the menu. */
922 else if (WINDOWP (window
))
924 CHECK_LIVE_WINDOW (window
);
925 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
927 xpos
= WINDOW_LEFT_EDGE_X (XWINDOW (window
));
928 ypos
= WINDOW_TOP_EDGE_Y (XWINDOW (window
));
931 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
932 but I don't want to make one now. */
933 CHECK_WINDOW (window
);
938 XSETFRAME (Vmenu_updating_frame
, f
);
941 Vmenu_updating_frame
= Qnil
;
942 #endif /* HAVE_MENUS */
944 record_unwind_protect (unuse_menu_items
, Qnil
);
948 /* Decode the menu items from what was specified. */
950 keymap
= get_keymap (menu
, 0, 0);
953 /* We were given a keymap. Extract menu info from the keymap. */
956 /* Extract the detailed info to make one pane. */
957 keymap_panes (&menu
, 1, NILP (position
));
959 /* Search for a string appearing directly as an element of the keymap.
960 That string is the title of the menu. */
961 prompt
= Fkeymap_prompt (keymap
);
962 if (NILP (title
) && !NILP (prompt
))
965 /* Make that be the pane title of the first pane. */
966 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
967 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
971 else if (CONSP (menu
) && KEYMAPP (XCAR (menu
)))
973 /* We were given a list of keymaps. */
974 int nmaps
= XFASTINT (Flength (menu
));
976 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
981 /* The first keymap that has a prompt string
982 supplies the menu title. */
983 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= XCDR (tem
))
987 maps
[i
++] = keymap
= get_keymap (XCAR (tem
), 1, 0);
989 prompt
= Fkeymap_prompt (keymap
);
990 if (NILP (title
) && !NILP (prompt
))
994 /* Extract the detailed info to make one pane. */
995 keymap_panes (maps
, nmaps
, NILP (position
));
997 /* Make the title be the pane title of the first pane. */
998 if (!NILP (title
) && menu_items_n_panes
>= 0)
999 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
1005 /* We were given an old-fashioned menu. */
1006 title
= Fcar (menu
);
1007 CHECK_STRING (title
);
1009 list_of_panes (Fcdr (menu
));
1014 unbind_to (specpdl_count
, Qnil
);
1016 if (NILP (position
))
1018 discard_menu_items ();
1024 /* Display them in a menu. */
1027 selection
= xmenu_show (f
, xpos
, ypos
, for_click
,
1028 keymaps
, title
, &error_name
);
1031 discard_menu_items ();
1034 #endif /* HAVE_MENUS */
1036 if (error_name
) error (error_name
);
1042 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 3, 0,
1043 doc
: /* Pop up a dialog box and return user's selection.
1044 POSITION specifies which frame to use.
1045 This is normally a mouse button event or a window or frame.
1046 If POSITION is t, it means to use the frame the mouse is on.
1047 The dialog box appears in the middle of the specified frame.
1049 CONTENTS specifies the alternatives to display in the dialog box.
1050 It is a list of the form (DIALOG ITEM1 ITEM2...).
1051 Each ITEM is a cons cell (STRING . VALUE).
1052 The return value is VALUE from the chosen item.
1054 An ITEM may also be just a string--that makes a nonselectable item.
1055 An ITEM may also be nil--that means to put all preceding items
1056 on the left of the dialog box and all following items on the right.
1057 \(By default, approximately half appear on each side.)
1059 If HEADER is non-nil, the frame title for the box is "Information",
1060 otherwise it is "Question".
1062 If the user gets rid of the dialog box without making a valid choice,
1063 for instance using the window manager, then this produces a quit and
1064 `x-popup-dialog' does not return. */)
1065 (position
, contents
, header
)
1066 Lisp_Object position
, contents
, header
;
1073 /* Decode the first argument: find the window or frame to use. */
1074 if (EQ (position
, Qt
)
1075 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
1076 || EQ (XCAR (position
), Qtool_bar
))))
1078 #if 0 /* Using the frame the mouse is on may not be right. */
1079 /* Use the mouse's current position. */
1080 FRAME_PTR new_f
= SELECTED_FRAME ();
1081 Lisp_Object bar_window
;
1082 enum scroll_bar_part part
;
1086 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
1089 XSETFRAME (window
, new_f
);
1091 window
= selected_window
;
1093 window
= selected_window
;
1095 else if (CONSP (position
))
1098 tem
= Fcar (position
);
1100 window
= Fcar (Fcdr (position
));
1103 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
1104 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
1107 else if (WINDOWP (position
) || FRAMEP (position
))
1112 /* Decode where to put the menu. */
1114 if (FRAMEP (window
))
1115 f
= XFRAME (window
);
1116 else if (WINDOWP (window
))
1118 CHECK_LIVE_WINDOW (window
);
1119 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
1122 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1123 but I don't want to make one now. */
1124 CHECK_WINDOW (window
);
1126 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1127 /* Display a menu with these alternatives
1128 in the middle of frame F. */
1130 Lisp_Object x
, y
, frame
, newpos
;
1131 XSETFRAME (frame
, f
);
1132 XSETINT (x
, x_pixel_width (f
) / 2);
1133 XSETINT (y
, x_pixel_height (f
) / 2);
1134 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
1136 return Fx_popup_menu (newpos
,
1137 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
1143 Lisp_Object selection
;
1144 int specpdl_count
= SPECPDL_INDEX ();
1146 /* Decode the dialog items from what was specified. */
1147 title
= Fcar (contents
);
1148 CHECK_STRING (title
);
1149 record_unwind_protect (unuse_menu_items
, Qnil
);
1151 if (NILP (Fcar (Fcdr (contents
))))
1152 /* No buttons specified, add an "Ok" button so users can pop down
1153 the dialog. Also, the lesstif/motif version crashes if there are
1155 contents
= Fcons (title
, Fcons (Fcons (build_string ("Ok"), Qt
), Qnil
));
1157 list_of_panes (Fcons (contents
, Qnil
));
1159 /* Display them in a dialog box. */
1161 selection
= xdialog_show (f
, 0, title
, header
, &error_name
);
1164 unbind_to (specpdl_count
, Qnil
);
1165 discard_menu_items ();
1167 if (error_name
) error (error_name
);
1176 /* Set menu_items_inuse so no other popup menu or dialog is created. */
1179 x_menu_set_in_use (in_use
)
1182 menu_items_inuse
= in_use
? Qt
: Qnil
;
1183 popup_activated_flag
= in_use
;
1186 /* Wait for an X event to arrive or for a timer to expire. */
1189 x_menu_wait_for_event (void *data
)
1191 extern EMACS_TIME timer_check
P_ ((int));
1193 /* Another way to do this is to register a timer callback, that can be
1194 done in GTK and Xt. But we have to do it like this when using only X
1195 anyway, and with callbacks we would have three variants for timer handling
1196 instead of the small ifdefs below. */
1199 #ifdef USE_X_TOOLKIT
1200 ! XtAppPending (Xt_app_con
)
1201 #elif defined USE_GTK
1202 ! gtk_events_pending ()
1204 ! XPending ((Display
*) data
)
1208 EMACS_TIME next_time
= timer_check (1);
1209 long secs
= EMACS_SECS (next_time
);
1210 long usecs
= EMACS_USECS (next_time
);
1211 SELECT_TYPE read_fds
;
1212 struct x_display_info
*dpyinfo
;
1215 FD_ZERO (&read_fds
);
1216 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
1218 int fd
= ConnectionNumber (dpyinfo
->display
);
1219 FD_SET (fd
, &read_fds
);
1223 if (secs
< 0 || (secs
== 0 && usecs
== 0))
1225 /* Sometimes timer_check returns -1 (no timers) even if there are
1226 timers. So do a timeout anyway. */
1227 EMACS_SET_SECS (next_time
, 1);
1228 EMACS_SET_USECS (next_time
, 0);
1231 select (n
+ 1, &read_fds
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0, &next_time
);
1234 #endif /* ! MSDOS */
1237 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1239 #ifdef USE_X_TOOLKIT
1241 /* Loop in Xt until the menu pulldown or dialog popup has been
1242 popped down (deactivated). This is used for x-popup-menu
1243 and x-popup-dialog; it is not used for the menu bar.
1245 NOTE: All calls to popup_get_selection should be protected
1246 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1249 popup_get_selection (initial_event
, dpyinfo
, id
, do_timers
)
1250 XEvent
*initial_event
;
1251 struct x_display_info
*dpyinfo
;
1257 while (popup_activated_flag
)
1261 event
= *initial_event
;
1266 if (do_timers
) x_menu_wait_for_event (0);
1267 XtAppNextEvent (Xt_app_con
, &event
);
1270 /* Make sure we don't consider buttons grabbed after menu goes.
1271 And make sure to deactivate for any ButtonRelease,
1272 even if XtDispatchEvent doesn't do that. */
1273 if (event
.type
== ButtonRelease
1274 && dpyinfo
->display
== event
.xbutton
.display
)
1276 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1277 #ifdef USE_MOTIF /* Pretending that the event came from a
1278 Btn1Down seems the only way to convince Motif to
1279 activate its callbacks; setting the XmNmenuPost
1280 isn't working. --marcus@sysc.pdx.edu. */
1281 event
.xbutton
.button
= 1;
1282 /* Motif only pops down menus when no Ctrl, Alt or Mod
1283 key is pressed and the button is released. So reset key state
1284 so Motif thinks this is the case. */
1285 event
.xbutton
.state
= 0;
1288 /* Pop down on C-g and Escape. */
1289 else if (event
.type
== KeyPress
1290 && dpyinfo
->display
== event
.xbutton
.display
)
1292 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
1294 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
1295 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
1296 popup_activated_flag
= 0;
1299 x_dispatch_event (&event
, event
.xany
.display
);
1303 #endif /* USE_X_TOOLKIT */
1306 /* Loop util popup_activated_flag is set to zero in a callback.
1307 Used for popup menus and dialogs. */
1310 popup_widget_loop (do_timers
, widget
)
1314 ++popup_activated_flag
;
1316 /* Process events in the Gtk event loop until done. */
1317 while (popup_activated_flag
)
1319 if (do_timers
) x_menu_wait_for_event (0);
1320 gtk_main_iteration ();
1325 /* Activate the menu bar of frame F.
1326 This is called from keyboard.c when it gets the
1327 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1329 To activate the menu bar, we use the X button-press event
1330 that was saved in saved_menu_event.
1331 That makes the toolkit do its thing.
1333 But first we recompute the menu bar contents (the whole tree).
1335 The reason for saving the button event until here, instead of
1336 passing it to the toolkit right away, is that we can safely
1337 execute Lisp code. */
1340 x_activate_menubar (f
)
1343 if (!f
->output_data
.x
->saved_menu_event
->type
)
1347 if (! xg_win_to_widget (FRAME_X_DISPLAY (f
),
1348 f
->output_data
.x
->saved_menu_event
->xany
.window
))
1352 set_frame_menubar (f
, 0, 1);
1355 XPutBackEvent (f
->output_data
.x
->display_info
->display
,
1356 f
->output_data
.x
->saved_menu_event
);
1357 popup_activated_flag
= 1;
1359 XtDispatchEvent (f
->output_data
.x
->saved_menu_event
);
1363 if (f
->output_data
.x
->saved_menu_event
->type
== ButtonRelease
)
1364 pending_menu_activation
= 1;
1367 /* Ignore this if we get it a second time. */
1368 f
->output_data
.x
->saved_menu_event
->type
= 0;
1371 /* Detect if a dialog or menu has been posted. */
1376 return popup_activated_flag
;
1379 /* This callback is invoked when the user selects a menubar cascade
1380 pushbutton, but before the pulldown menu is posted. */
1384 popup_activate_callback (widget
, id
, client_data
)
1387 XtPointer client_data
;
1389 popup_activated_flag
= 1;
1393 /* This callback is invoked when a dialog or menu is finished being
1394 used and has been unposted. */
1398 popup_deactivate_callback (widget
, client_data
)
1400 gpointer client_data
;
1402 popup_activated_flag
= 0;
1406 popup_deactivate_callback (widget
, id
, client_data
)
1409 XtPointer client_data
;
1411 popup_activated_flag
= 0;
1416 /* Function that finds the frame for WIDGET and shows the HELP text
1418 F is the frame if known, or NULL if not known. */
1420 show_help_event (f
, widget
, help
)
1422 xt_or_gtk_widget widget
;
1429 XSETFRAME (frame
, f
);
1430 kbd_buffer_store_help_event (frame
, help
);
1434 #if 0 /* This code doesn't do anything useful. ++kfs */
1435 /* WIDGET is the popup menu. It's parent is the frame's
1436 widget. See which frame that is. */
1437 xt_or_gtk_widget frame_widget
= XtParent (widget
);
1440 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
1442 frame
= XCAR (tail
);
1443 if (GC_FRAMEP (frame
)
1444 && (f
= XFRAME (frame
),
1445 FRAME_X_P (f
) && f
->output_data
.x
->widget
== frame_widget
))
1449 show_help_echo (help
, Qnil
, Qnil
, Qnil
, 1);
1453 /* Callback called when menu items are highlighted/unhighlighted
1454 while moving the mouse over them. WIDGET is the menu bar or menu
1455 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1456 the data structure for the menu item, or null in case of
1461 menu_highlight_callback (widget
, call_data
)
1465 xg_menu_item_cb_data
*cb_data
;
1468 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (widget
),
1470 if (! cb_data
) return;
1472 help
= call_data
? cb_data
->help
: Qnil
;
1474 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1475 Don't show help for them, they won't appear before the
1476 popup is popped down. */
1477 if (popup_activated_flag
<= 1)
1478 show_help_event (cb_data
->cl_data
->f
, widget
, help
);
1482 menu_highlight_callback (widget
, id
, call_data
)
1490 widget_value
*wv
= (widget_value
*) call_data
;
1492 help
= wv
? wv
->help
: Qnil
;
1494 /* Determine the frame for the help event. */
1495 f
= menubar_id_to_frame (id
);
1497 show_help_event (f
, widget
, help
);
1501 /* Find the menu selection and store it in the keyboard buffer.
1502 F is the frame the menu is on.
1503 MENU_BAR_ITEMS_USED is the length of VECTOR.
1504 VECTOR is an array of menu events for the whole menu. */
1507 find_and_call_menu_selection (f
, menu_bar_items_used
, vector
, client_data
)
1509 int menu_bar_items_used
;
1513 Lisp_Object prefix
, entry
;
1514 Lisp_Object
*subprefix_stack
;
1515 int submenu_depth
= 0;
1519 subprefix_stack
= (Lisp_Object
*) alloca (menu_bar_items_used
* sizeof (Lisp_Object
));
1523 while (i
< menu_bar_items_used
)
1525 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1527 subprefix_stack
[submenu_depth
++] = prefix
;
1531 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1533 prefix
= subprefix_stack
[--submenu_depth
];
1536 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1538 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1539 i
+= MENU_ITEMS_PANE_LENGTH
;
1543 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1544 /* The EMACS_INT cast avoids a warning. There's no problem
1545 as long as pointers have enough bits to hold small integers. */
1546 if ((int) (EMACS_INT
) client_data
== i
)
1549 struct input_event buf
;
1553 XSETFRAME (frame
, f
);
1554 buf
.kind
= MENU_BAR_EVENT
;
1555 buf
.frame_or_window
= frame
;
1557 kbd_buffer_store_event (&buf
);
1559 for (j
= 0; j
< submenu_depth
; j
++)
1560 if (!NILP (subprefix_stack
[j
]))
1562 buf
.kind
= MENU_BAR_EVENT
;
1563 buf
.frame_or_window
= frame
;
1564 buf
.arg
= subprefix_stack
[j
];
1565 kbd_buffer_store_event (&buf
);
1570 buf
.kind
= MENU_BAR_EVENT
;
1571 buf
.frame_or_window
= frame
;
1573 kbd_buffer_store_event (&buf
);
1576 buf
.kind
= MENU_BAR_EVENT
;
1577 buf
.frame_or_window
= frame
;
1579 kbd_buffer_store_event (&buf
);
1583 i
+= MENU_ITEMS_ITEM_LENGTH
;
1590 /* Gtk calls callbacks just because we tell it what item should be
1591 selected in a radio group. If this variable is set to a non-zero
1592 value, we are creating menus and don't want callbacks right now.
1594 static int xg_crazy_callback_abort
;
1596 /* This callback is called from the menu bar pulldown menu
1597 when the user makes a selection.
1598 Figure out what the user chose
1599 and put the appropriate events into the keyboard buffer. */
1601 menubar_selection_callback (widget
, client_data
)
1603 gpointer client_data
;
1605 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
1607 if (xg_crazy_callback_abort
)
1610 if (! cb_data
|| ! cb_data
->cl_data
|| ! cb_data
->cl_data
->f
)
1613 /* For a group of radio buttons, GTK calls the selection callback first
1614 for the item that was active before the selection and then for the one that
1615 is active after the selection. For C-h k this means we get the help on
1616 the deselected item and then the selected item is executed. Prevent that
1617 by ignoring the non-active item. */
1618 if (GTK_IS_RADIO_MENU_ITEM (widget
)
1619 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget
)))
1622 /* When a menu is popped down, X generates a focus event (i.e. focus
1623 goes back to the frame below the menu). Since GTK buffers events,
1624 we force it out here before the menu selection event. Otherwise
1625 sit-for will exit at once if the focus event follows the menu selection
1629 while (gtk_events_pending ())
1630 gtk_main_iteration ();
1633 find_and_call_menu_selection (cb_data
->cl_data
->f
,
1634 cb_data
->cl_data
->menu_bar_items_used
,
1635 cb_data
->cl_data
->menu_bar_vector
,
1636 cb_data
->call_data
);
1639 #else /* not USE_GTK */
1641 /* This callback is called from the menu bar pulldown menu
1642 when the user makes a selection.
1643 Figure out what the user chose
1644 and put the appropriate events into the keyboard buffer. */
1646 menubar_selection_callback (widget
, id
, client_data
)
1649 XtPointer client_data
;
1653 f
= menubar_id_to_frame (id
);
1656 find_and_call_menu_selection (f
, f
->menu_bar_items_used
,
1657 f
->menu_bar_vector
, client_data
);
1659 #endif /* not USE_GTK */
1661 /* Allocate a widget_value, blocking input. */
1664 xmalloc_widget_value ()
1666 widget_value
*value
;
1669 value
= malloc_widget_value ();
1675 /* This recursively calls free_widget_value on the tree of widgets.
1676 It must free all data that was malloc'ed for these widget_values.
1677 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1678 must be left alone. */
1681 free_menubar_widget_value_tree (wv
)
1686 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1688 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1690 free_menubar_widget_value_tree (wv
->contents
);
1691 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1695 free_menubar_widget_value_tree (wv
->next
);
1696 wv
->next
= (widget_value
*) 0xDEADBEEF;
1699 free_widget_value (wv
);
1703 /* Set up data in menu_items for a menu bar item
1704 whose event type is ITEM_KEY (with string ITEM_NAME)
1705 and whose contents come from the list of keymaps MAPS. */
1708 parse_single_submenu (item_key
, item_name
, maps
)
1709 Lisp_Object item_key
, item_name
, maps
;
1713 Lisp_Object
*mapvec
;
1715 int top_level_items
= 0;
1717 length
= Flength (maps
);
1718 len
= XINT (length
);
1720 /* Convert the list MAPS into a vector MAPVEC. */
1721 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1722 for (i
= 0; i
< len
; i
++)
1724 mapvec
[i
] = Fcar (maps
);
1728 /* Loop over the given keymaps, making a pane for each map.
1729 But don't make a pane that is empty--ignore that map instead. */
1730 for (i
= 0; i
< len
; i
++)
1732 if (!KEYMAPP (mapvec
[i
]))
1734 /* Here we have a command at top level in the menu bar
1735 as opposed to a submenu. */
1736 top_level_items
= 1;
1737 push_menu_pane (Qnil
, Qnil
);
1738 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
1739 Qnil
, Qnil
, Qnil
, Qnil
);
1744 prompt
= Fkeymap_prompt (mapvec
[i
]);
1745 single_keymap_panes (mapvec
[i
],
1746 !NILP (prompt
) ? prompt
: item_name
,
1751 return top_level_items
;
1754 /* Create a tree of widget_value objects
1755 representing the panes and items
1756 in menu_items starting at index START, up to index END. */
1758 static widget_value
*
1759 digest_single_submenu (start
, end
, top_level_items
)
1760 int start
, end
, top_level_items
;
1762 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1764 int submenu_depth
= 0;
1765 widget_value
**submenu_stack
;
1769 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1770 wv
= xmalloc_widget_value ();
1774 wv
->button_type
= BUTTON_TYPE_NONE
;
1780 /* Loop over all panes and items made by the preceding call
1781 to parse_single_submenu and construct a tree of widget_value objects.
1782 Ignore the panes and items used by previous calls to
1783 digest_single_submenu, even though those are also in menu_items. */
1787 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1789 submenu_stack
[submenu_depth
++] = save_wv
;
1794 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1797 save_wv
= submenu_stack
[--submenu_depth
];
1800 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1801 && submenu_depth
!= 0)
1802 i
+= MENU_ITEMS_PANE_LENGTH
;
1803 /* Ignore a nil in the item list.
1804 It's meaningful only for dialog boxes. */
1805 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1807 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1809 /* Create a new pane. */
1810 Lisp_Object pane_name
, prefix
;
1815 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1816 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1818 #ifndef HAVE_MULTILINGUAL_MENU
1819 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1821 pane_name
= ENCODE_MENU_STRING (pane_name
);
1822 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1825 pane_string
= (NILP (pane_name
)
1826 ? "" : (char *) SDATA (pane_name
));
1827 /* If there is just one top-level pane, put all its items directly
1828 under the top-level menu. */
1829 if (menu_items_n_panes
== 1)
1832 /* If the pane has a meaningful name,
1833 make the pane a top-level menu item
1834 with its items as a submenu beneath it. */
1835 if (strcmp (pane_string
, ""))
1837 wv
= xmalloc_widget_value ();
1841 first_wv
->contents
= wv
;
1842 wv
->lname
= pane_name
;
1843 /* Set value to 1 so update_submenu_strings can handle '@' */
1844 wv
->value
= (char *)1;
1846 wv
->button_type
= BUTTON_TYPE_NONE
;
1854 i
+= MENU_ITEMS_PANE_LENGTH
;
1858 /* Create a new item within current pane. */
1859 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1862 /* All items should be contained in panes. */
1863 if (panes_seen
== 0)
1866 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1867 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1868 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1869 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1870 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1871 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1872 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1874 #ifndef HAVE_MULTILINGUAL_MENU
1875 if (STRING_MULTIBYTE (item_name
))
1877 item_name
= ENCODE_MENU_STRING (item_name
);
1878 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1881 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1883 descrip
= ENCODE_MENU_STRING (descrip
);
1884 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
1886 #endif /* not HAVE_MULTILINGUAL_MENU */
1888 wv
= xmalloc_widget_value ();
1892 save_wv
->contents
= wv
;
1894 wv
->lname
= item_name
;
1895 if (!NILP (descrip
))
1898 /* The EMACS_INT cast avoids a warning. There's no problem
1899 as long as pointers have enough bits to hold small integers. */
1900 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1901 wv
->enabled
= !NILP (enable
);
1904 wv
->button_type
= BUTTON_TYPE_NONE
;
1905 else if (EQ (type
, QCradio
))
1906 wv
->button_type
= BUTTON_TYPE_RADIO
;
1907 else if (EQ (type
, QCtoggle
))
1908 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1912 wv
->selected
= !NILP (selected
);
1913 if (! STRINGP (help
))
1920 i
+= MENU_ITEMS_ITEM_LENGTH
;
1924 /* If we have just one "menu item"
1925 that was originally a button, return it by itself. */
1926 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
1928 wv
= first_wv
->contents
;
1929 free_widget_value (first_wv
);
1936 /* Walk through the widget_value tree starting at FIRST_WV and update
1937 the char * pointers from the corresponding lisp values.
1938 We do this after building the whole tree, since GC may happen while the
1939 tree is constructed, and small strings are relocated. So we must wait
1940 until no GC can happen before storing pointers into lisp values. */
1942 update_submenu_strings (first_wv
)
1943 widget_value
*first_wv
;
1947 for (wv
= first_wv
; wv
; wv
= wv
->next
)
1949 if (STRINGP (wv
->lname
))
1951 wv
->name
= (char *) SDATA (wv
->lname
);
1953 /* Ignore the @ that means "separate pane".
1954 This is a kludge, but this isn't worth more time. */
1955 if (wv
->value
== (char *)1)
1957 if (wv
->name
[0] == '@')
1963 if (STRINGP (wv
->lkey
))
1964 wv
->key
= (char *) SDATA (wv
->lkey
);
1967 update_submenu_strings (wv
->contents
);
1972 /* Recompute all the widgets of frame F, when the menu bar has been
1973 changed. Value is non-zero if widgets were updated. */
1976 update_frame_menubar (f
)
1980 return xg_update_frame_menubar (f
);
1982 struct x_output
*x
= f
->output_data
.x
;
1985 if (!x
->menubar_widget
|| XtIsManaged (x
->menubar_widget
))
1989 /* Save the size of the frame because the pane widget doesn't accept
1990 to resize itself. So force it. */
1991 columns
= FRAME_COLS (f
);
1992 rows
= FRAME_LINES (f
);
1994 /* Do the voodoo which means "I'm changing lots of things, don't try
1995 to refigure sizes until I'm done." */
1996 lw_refigure_widget (x
->column_widget
, False
);
1998 /* The order in which children are managed is the top to bottom
1999 order in which they are displayed in the paned window. First,
2000 remove the text-area widget. */
2001 XtUnmanageChild (x
->edit_widget
);
2003 /* Remove the menubar that is there now, and put up the menubar that
2005 XtManageChild (x
->menubar_widget
);
2006 XtMapWidget (x
->menubar_widget
);
2007 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, NULL
);
2009 /* Re-manage the text-area widget, and then thrash the sizes. */
2010 XtManageChild (x
->edit_widget
);
2011 lw_refigure_widget (x
->column_widget
, True
);
2013 /* Force the pane widget to resize itself with the right values. */
2014 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
2020 /* Set the contents of the menubar widgets of frame F.
2021 The argument FIRST_TIME is currently ignored;
2022 it is set the first time this is called, from initialize_frame_menubar. */
2025 set_frame_menubar (f
, first_time
, deep_p
)
2030 xt_or_gtk_widget menubar_widget
= f
->output_data
.x
->menubar_widget
;
2031 #ifdef USE_X_TOOLKIT
2035 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
2037 int *submenu_start
, *submenu_end
;
2038 int *submenu_top_level_items
, *submenu_n_panes
;
2041 XSETFRAME (Vmenu_updating_frame
, f
);
2043 #ifdef USE_X_TOOLKIT
2044 if (f
->output_data
.x
->id
== 0)
2045 f
->output_data
.x
->id
= next_menubar_widget_id
++;
2046 id
= f
->output_data
.x
->id
;
2049 if (! menubar_widget
)
2051 else if (pending_menu_activation
&& !deep_p
)
2053 /* Make the first call for any given frame always go deep. */
2054 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
2057 f
->output_data
.x
->saved_menu_event
= (XEvent
*)xmalloc (sizeof (XEvent
));
2058 f
->output_data
.x
->saved_menu_event
->type
= 0;
2062 /* If we have detached menus, we must update deep so detached menus
2063 also gets updated. */
2064 deep_p
= deep_p
|| xg_have_tear_offs ();
2069 /* Make a widget-value tree representing the entire menu trees. */
2071 struct buffer
*prev
= current_buffer
;
2073 int specpdl_count
= SPECPDL_INDEX ();
2074 int previous_menu_items_used
= f
->menu_bar_items_used
;
2075 Lisp_Object
*previous_items
2076 = (Lisp_Object
*) alloca (previous_menu_items_used
2077 * sizeof (Lisp_Object
));
2079 /* If we are making a new widget, its contents are empty,
2080 do always reinitialize them. */
2081 if (! menubar_widget
)
2082 previous_menu_items_used
= 0;
2084 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
2085 specbind (Qinhibit_quit
, Qt
);
2086 /* Don't let the debugger step into this code
2087 because it is not reentrant. */
2088 specbind (Qdebug_on_next_call
, Qnil
);
2090 record_unwind_save_match_data ();
2091 if (NILP (Voverriding_local_map_menu_flag
))
2093 specbind (Qoverriding_terminal_local_map
, Qnil
);
2094 specbind (Qoverriding_local_map
, Qnil
);
2097 set_buffer_internal_1 (XBUFFER (buffer
));
2099 /* Run the Lucid hook. */
2100 safe_run_hooks (Qactivate_menubar_hook
);
2102 /* If it has changed current-menubar from previous value,
2103 really recompute the menubar from the value. */
2104 if (! NILP (Vlucid_menu_bar_dirty_flag
))
2105 call0 (Qrecompute_lucid_menubar
);
2106 safe_run_hooks (Qmenu_bar_update_hook
);
2107 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2109 items
= FRAME_MENU_BAR_ITEMS (f
);
2111 /* Save the frame's previous menu bar contents data. */
2112 if (previous_menu_items_used
)
2113 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
2114 previous_menu_items_used
* sizeof (Lisp_Object
));
2116 /* Fill in menu_items with the current menu bar contents.
2117 This can evaluate Lisp code. */
2120 menu_items
= f
->menu_bar_vector
;
2121 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
2122 submenu_start
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2123 submenu_end
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2124 submenu_n_panes
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int));
2125 submenu_top_level_items
2126 = (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2128 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2130 Lisp_Object key
, string
, maps
;
2134 key
= XVECTOR (items
)->contents
[i
];
2135 string
= XVECTOR (items
)->contents
[i
+ 1];
2136 maps
= XVECTOR (items
)->contents
[i
+ 2];
2140 submenu_start
[i
] = menu_items_used
;
2142 menu_items_n_panes
= 0;
2143 submenu_top_level_items
[i
]
2144 = parse_single_submenu (key
, string
, maps
);
2145 submenu_n_panes
[i
] = menu_items_n_panes
;
2147 submenu_end
[i
] = menu_items_used
;
2150 finish_menu_items ();
2152 /* Convert menu_items into widget_value trees
2153 to display the menu. This cannot evaluate Lisp code. */
2155 wv
= xmalloc_widget_value ();
2156 wv
->name
= "menubar";
2159 wv
->button_type
= BUTTON_TYPE_NONE
;
2163 for (i
= 0; i
< last_i
; i
+= 4)
2165 menu_items_n_panes
= submenu_n_panes
[i
];
2166 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
2167 submenu_top_level_items
[i
]);
2171 first_wv
->contents
= wv
;
2172 /* Don't set wv->name here; GC during the loop might relocate it. */
2174 wv
->button_type
= BUTTON_TYPE_NONE
;
2178 set_buffer_internal_1 (prev
);
2180 /* If there has been no change in the Lisp-level contents
2181 of the menu bar, skip redisplaying it. Just exit. */
2183 /* Compare the new menu items with the ones computed last time. */
2184 for (i
= 0; i
< previous_menu_items_used
; i
++)
2185 if (menu_items_used
== i
2186 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
2188 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
2190 /* The menu items have not changed. Don't bother updating
2191 the menus in any form, since it would be a no-op. */
2192 free_menubar_widget_value_tree (first_wv
);
2193 discard_menu_items ();
2194 unbind_to (specpdl_count
, Qnil
);
2198 /* The menu items are different, so store them in the frame. */
2199 f
->menu_bar_vector
= menu_items
;
2200 f
->menu_bar_items_used
= menu_items_used
;
2202 /* This calls restore_menu_items to restore menu_items, etc.,
2203 as they were outside. */
2204 unbind_to (specpdl_count
, Qnil
);
2206 /* Now GC cannot happen during the lifetime of the widget_value,
2207 so it's safe to store data from a Lisp_String. */
2208 wv
= first_wv
->contents
;
2209 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2212 string
= XVECTOR (items
)->contents
[i
+ 1];
2215 wv
->name
= (char *) SDATA (string
);
2216 update_submenu_strings (wv
->contents
);
2223 /* Make a widget-value tree containing
2224 just the top level menu bar strings. */
2226 wv
= xmalloc_widget_value ();
2227 wv
->name
= "menubar";
2230 wv
->button_type
= BUTTON_TYPE_NONE
;
2234 items
= FRAME_MENU_BAR_ITEMS (f
);
2235 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2239 string
= XVECTOR (items
)->contents
[i
+ 1];
2243 wv
= xmalloc_widget_value ();
2244 wv
->name
= (char *) SDATA (string
);
2247 wv
->button_type
= BUTTON_TYPE_NONE
;
2249 /* This prevents lwlib from assuming this
2250 menu item is really supposed to be empty. */
2251 /* The EMACS_INT cast avoids a warning.
2252 This value just has to be different from small integers. */
2253 wv
->call_data
= (void *) (EMACS_INT
) (-1);
2258 first_wv
->contents
= wv
;
2262 /* Forget what we thought we knew about what is in the
2263 detailed contents of the menu bar menus.
2264 Changing the top level always destroys the contents. */
2265 f
->menu_bar_items_used
= 0;
2268 /* Create or update the menu bar widget. */
2273 xg_crazy_callback_abort
= 1;
2276 /* The fourth arg is DEEP_P, which says to consider the entire
2277 menu trees we supply, rather than just the menu bar item names. */
2278 xg_modify_menubar_widgets (menubar_widget
,
2282 G_CALLBACK (menubar_selection_callback
),
2283 G_CALLBACK (popup_deactivate_callback
),
2284 G_CALLBACK (menu_highlight_callback
));
2288 GtkWidget
*wvbox
= f
->output_data
.x
->vbox_widget
;
2291 = xg_create_widget ("menubar", "menubar", f
, first_wv
,
2292 G_CALLBACK (menubar_selection_callback
),
2293 G_CALLBACK (popup_deactivate_callback
),
2294 G_CALLBACK (menu_highlight_callback
));
2296 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2300 #else /* not USE_GTK */
2303 /* Disable resizing (done for Motif!) */
2304 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
2306 /* The third arg is DEEP_P, which says to consider the entire
2307 menu trees we supply, rather than just the menu bar item names. */
2308 lw_modify_all_widgets (id
, first_wv
, deep_p
);
2310 /* Re-enable the edit widget to resize. */
2311 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
2315 char menuOverride
[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2316 XtTranslations override
= XtParseTranslationTable (menuOverride
);
2318 menubar_widget
= lw_create_widget ("menubar", "menubar", id
, first_wv
,
2319 f
->output_data
.x
->column_widget
,
2321 popup_activate_callback
,
2322 menubar_selection_callback
,
2323 popup_deactivate_callback
,
2324 menu_highlight_callback
);
2325 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2327 /* Make menu pop down on C-g. */
2328 XtOverrideTranslations (menubar_widget
, override
);
2333 = (f
->output_data
.x
->menubar_widget
2334 ? (f
->output_data
.x
->menubar_widget
->core
.height
2335 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
2338 #if 0 /* Experimentally, we now get the right results
2339 for -geometry -0-0 without this. 24 Aug 96, rms. */
2341 if (FRAME_EXTERNAL_MENU_BAR (f
))
2344 XtVaGetValues (f
->output_data
.x
->column_widget
,
2345 XtNinternalBorderWidth
, &ibw
, NULL
);
2346 menubar_size
+= ibw
;
2348 #endif /* USE_LUCID */
2351 f
->output_data
.x
->menubar_height
= menubar_size
;
2353 #endif /* not USE_GTK */
2355 free_menubar_widget_value_tree (first_wv
);
2356 update_frame_menubar (f
);
2359 xg_crazy_callback_abort
= 0;
2365 /* Called from Fx_create_frame to create the initial menubar of a frame
2366 before it is mapped, so that the window is mapped with the menubar already
2367 there instead of us tacking it on later and thrashing the window after it
2371 initialize_frame_menubar (f
)
2374 /* This function is called before the first chance to redisplay
2375 the frame. It has to be, so the frame will have the right size. */
2376 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2377 set_frame_menubar (f
, 1, 1);
2381 /* Get rid of the menu bar of frame F, and free its storage.
2382 This is used when deleting a frame, and when turning off the menu bar.
2383 For GTK this function is in gtkutil.c. */
2387 free_frame_menubar (f
)
2390 Widget menubar_widget
;
2392 menubar_widget
= f
->output_data
.x
->menubar_widget
;
2394 f
->output_data
.x
->menubar_height
= 0;
2399 /* Removing the menu bar magically changes the shell widget's x
2400 and y position of (0, 0) which, when the menu bar is turned
2401 on again, leads to pull-down menuss appearing in strange
2402 positions near the upper-left corner of the display. This
2403 happens only with some window managers like twm and ctwm,
2404 but not with other like Motif's mwm or kwm, because the
2405 latter generate ConfigureNotify events when the menu bar
2406 is switched off, which fixes the shell position. */
2407 Position x0
, y0
, x1
, y1
;
2413 if (f
->output_data
.x
->widget
)
2414 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x0
, XtNy
, &y0
, NULL
);
2417 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
2418 f
->output_data
.x
->menubar_widget
= NULL
;
2421 if (f
->output_data
.x
->widget
)
2423 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x1
, XtNy
, &y1
, NULL
);
2424 if (x1
== 0 && y1
== 0)
2425 XtVaSetValues (f
->output_data
.x
->widget
, XtNx
, x0
, XtNy
, y0
, NULL
);
2432 #endif /* not USE_GTK */
2434 #endif /* USE_X_TOOLKIT || USE_GTK */
2436 /* xmenu_show actually displays a menu using the panes and items in menu_items
2437 and returns the value selected from it.
2438 There are two versions of xmenu_show, one for Xt and one for Xlib.
2439 Both assume input is blocked by the caller. */
2441 /* F is the frame the menu is for.
2442 X and Y are the frame-relative specified position,
2443 relative to the inside upper left corner of the frame F.
2444 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2445 KEYMAPS is 1 if this menu was specified with keymaps;
2446 in that case, we return a list containing the chosen item's value
2447 and perhaps also the pane's prefix.
2448 TITLE is the specified menu title.
2449 ERROR is a place to store an error message string in case of failure.
2450 (We return nil on failure, but the value doesn't actually matter.) */
2452 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2454 /* The item selected in the popup menu. */
2455 static Lisp_Object
*volatile menu_item_selection
;
2459 /* Used when position a popup menu. See menu_position_func and
2460 create_and_show_popup_menu below. */
2461 struct next_popup_x_y
2468 /* The menu position function to use if we are not putting a popup
2469 menu where the pointer is.
2470 MENU is the menu to pop up.
2471 X and Y shall on exit contain x/y where the menu shall pop up.
2472 PUSH_IN is not documented in the GTK manual.
2473 USER_DATA is any data passed in when calling gtk_menu_popup.
2474 Here it points to a struct next_popup_x_y where the coordinates
2475 to store in *X and *Y are as well as the frame for the popup.
2477 Here only X and Y are used. */
2479 menu_position_func (menu
, x
, y
, push_in
, user_data
)
2486 struct next_popup_x_y
* data
= (struct next_popup_x_y
*)user_data
;
2488 int disp_width
= FRAME_X_DISPLAY_INFO (data
->f
)->width
;
2489 int disp_height
= FRAME_X_DISPLAY_INFO (data
->f
)->height
;
2494 /* Check if there is room for the menu. If not, adjust x/y so that
2495 the menu is fully visible. */
2496 gtk_widget_size_request (GTK_WIDGET (menu
), &req
);
2497 if (data
->x
+ req
.width
> disp_width
)
2498 *x
-= data
->x
+ req
.width
- disp_width
;
2499 if (data
->y
+ req
.height
> disp_height
)
2500 *y
-= data
->y
+ req
.height
- disp_height
;
2504 popup_selection_callback (widget
, client_data
)
2506 gpointer client_data
;
2508 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
2510 if (xg_crazy_callback_abort
) return;
2511 if (cb_data
) menu_item_selection
= (Lisp_Object
*) cb_data
->call_data
;
2518 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
2520 popup_activated_flag
= 0;
2522 gtk_widget_destroy (GTK_WIDGET (p
->pointer
));
2527 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2529 menu_item_selection will be set to the selection. */
2531 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2533 widget_value
*first_wv
;
2540 GtkMenuPositionFunc pos_func
= 0; /* Pop up at pointer. */
2541 struct next_popup_x_y popup_x_y
;
2542 int specpdl_count
= SPECPDL_INDEX ();
2544 xg_crazy_callback_abort
= 1;
2545 menu
= xg_create_widget ("popup", first_wv
->name
, f
, first_wv
,
2546 G_CALLBACK (popup_selection_callback
),
2547 G_CALLBACK (popup_deactivate_callback
),
2548 G_CALLBACK (menu_highlight_callback
));
2549 xg_crazy_callback_abort
= 0;
2553 /* Not invoked by a click. pop up at x/y. */
2554 pos_func
= menu_position_func
;
2556 /* Adjust coordinates to be root-window-relative. */
2557 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2558 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2564 i
= 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2568 for (i
= 0; i
< 5; i
++)
2569 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2573 /* Display the menu. */
2574 gtk_widget_show_all (menu
);
2575 gtk_menu_popup (GTK_MENU (menu
), 0, 0, pos_func
, &popup_x_y
, i
, 0);
2577 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
2579 if (GTK_WIDGET_MAPPED (menu
))
2581 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2582 two. show_help_echo uses this to detect popup menus. */
2583 popup_activated_flag
= 1;
2584 /* Process events that apply to the menu. */
2585 popup_widget_loop (1, menu
);
2588 unbind_to (specpdl_count
, Qnil
);
2590 /* Must reset this manually because the button release event is not passed
2591 to Emacs event loop. */
2592 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2595 #else /* not USE_GTK */
2597 /* We need a unique id for each widget handled by the Lucid Widget
2600 For the main windows, and popup menus, we use this counter,
2601 which we increment each time after use. This starts from 1<<16.
2603 For menu bars, we use numbers starting at 0, counted in
2604 next_menubar_widget_id. */
2605 LWLIB_ID widget_id_tick
;
2608 popup_selection_callback (widget
, id
, client_data
)
2611 XtPointer client_data
;
2613 menu_item_selection
= (Lisp_Object
*) client_data
;
2616 /* ARG is the LWLIB ID of the dialog box, represented
2617 as a Lisp object as (HIGHPART . LOWPART). */
2623 LWLIB_ID id
= (XINT (XCAR (arg
)) << 4 * sizeof (LWLIB_ID
)
2624 | XINT (XCDR (arg
)));
2627 lw_destroy_all_widgets (id
);
2629 popup_activated_flag
= 0;
2634 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2636 menu_item_selection will be set to the selection. */
2638 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2640 widget_value
*first_wv
;
2648 XButtonPressedEvent dummy
;
2652 menu_id
= widget_id_tick
++;
2653 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
2654 f
->output_data
.x
->widget
, 1, 0,
2655 popup_selection_callback
,
2656 popup_deactivate_callback
,
2657 menu_highlight_callback
);
2659 dummy
.type
= ButtonPress
;
2661 dummy
.send_event
= 0;
2662 dummy
.display
= FRAME_X_DISPLAY (f
);
2663 dummy
.time
= CurrentTime
;
2664 dummy
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
2665 dummy
.window
= dummy
.root
;
2666 dummy
.subwindow
= dummy
.root
;
2670 /* Adjust coordinates to be root-window-relative. */
2671 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2672 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2679 for (i
= 0; i
< 5; i
++)
2680 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2683 /* Don't allow any geometry request from the user. */
2684 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
2685 XtSetValues (menu
, av
, ac
);
2687 /* Display the menu. */
2688 lw_popup_menu (menu
, (XEvent
*) &dummy
);
2689 popup_activated_flag
= 1;
2692 int fact
= 4 * sizeof (LWLIB_ID
);
2693 int specpdl_count
= SPECPDL_INDEX ();
2694 record_unwind_protect (pop_down_menu
,
2695 Fcons (make_number (menu_id
>> (fact
)),
2696 make_number (menu_id
& ~(-1 << (fact
)))));
2698 /* Process events that apply to the menu. */
2699 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
, 1);
2701 unbind_to (specpdl_count
, Qnil
);
2705 #endif /* not USE_GTK */
2708 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
2718 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
2719 widget_value
**submenu_stack
2720 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
2721 Lisp_Object
*subprefix_stack
2722 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
2723 int submenu_depth
= 0;
2729 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2731 *error
= "Empty menu";
2735 /* Create a tree of widget_value objects
2736 representing the panes and their items. */
2737 wv
= xmalloc_widget_value ();
2741 wv
->button_type
= BUTTON_TYPE_NONE
;
2746 /* Loop over all panes and items, filling in the tree. */
2748 while (i
< menu_items_used
)
2750 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2752 submenu_stack
[submenu_depth
++] = save_wv
;
2758 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2761 save_wv
= submenu_stack
[--submenu_depth
];
2765 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
2766 && submenu_depth
!= 0)
2767 i
+= MENU_ITEMS_PANE_LENGTH
;
2768 /* Ignore a nil in the item list.
2769 It's meaningful only for dialog boxes. */
2770 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2772 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2774 /* Create a new pane. */
2775 Lisp_Object pane_name
, prefix
;
2778 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
2779 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2781 #ifndef HAVE_MULTILINGUAL_MENU
2782 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
2784 pane_name
= ENCODE_MENU_STRING (pane_name
);
2785 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
2788 pane_string
= (NILP (pane_name
)
2789 ? "" : (char *) SDATA (pane_name
));
2790 /* If there is just one top-level pane, put all its items directly
2791 under the top-level menu. */
2792 if (menu_items_n_panes
== 1)
2795 /* If the pane has a meaningful name,
2796 make the pane a top-level menu item
2797 with its items as a submenu beneath it. */
2798 if (!keymaps
&& strcmp (pane_string
, ""))
2800 wv
= xmalloc_widget_value ();
2804 first_wv
->contents
= wv
;
2805 wv
->name
= pane_string
;
2806 if (keymaps
&& !NILP (prefix
))
2810 wv
->button_type
= BUTTON_TYPE_NONE
;
2815 else if (first_pane
)
2821 i
+= MENU_ITEMS_PANE_LENGTH
;
2825 /* Create a new item within current pane. */
2826 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
2827 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
2828 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
2829 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
2830 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
2831 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
2832 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
2833 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
2835 #ifndef HAVE_MULTILINGUAL_MENU
2836 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
2838 item_name
= ENCODE_MENU_STRING (item_name
);
2839 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
2842 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
2844 descrip
= ENCODE_MENU_STRING (descrip
);
2845 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
2847 #endif /* not HAVE_MULTILINGUAL_MENU */
2849 wv
= xmalloc_widget_value ();
2853 save_wv
->contents
= wv
;
2854 wv
->name
= (char *) SDATA (item_name
);
2855 if (!NILP (descrip
))
2856 wv
->key
= (char *) SDATA (descrip
);
2858 /* If this item has a null value,
2859 make the call_data null so that it won't display a box
2860 when the mouse is on it. */
2862 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
2863 wv
->enabled
= !NILP (enable
);
2866 wv
->button_type
= BUTTON_TYPE_NONE
;
2867 else if (EQ (type
, QCtoggle
))
2868 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
2869 else if (EQ (type
, QCradio
))
2870 wv
->button_type
= BUTTON_TYPE_RADIO
;
2874 wv
->selected
= !NILP (selected
);
2876 if (! STRINGP (help
))
2883 i
+= MENU_ITEMS_ITEM_LENGTH
;
2887 /* Deal with the title, if it is non-nil. */
2890 widget_value
*wv_title
= xmalloc_widget_value ();
2891 widget_value
*wv_sep1
= xmalloc_widget_value ();
2892 widget_value
*wv_sep2
= xmalloc_widget_value ();
2894 wv_sep2
->name
= "--";
2895 wv_sep2
->next
= first_wv
->contents
;
2896 wv_sep2
->help
= Qnil
;
2898 wv_sep1
->name
= "--";
2899 wv_sep1
->next
= wv_sep2
;
2900 wv_sep1
->help
= Qnil
;
2902 #ifndef HAVE_MULTILINGUAL_MENU
2903 if (STRING_MULTIBYTE (title
))
2904 title
= ENCODE_MENU_STRING (title
);
2907 wv_title
->name
= (char *) SDATA (title
);
2908 wv_title
->enabled
= TRUE
;
2909 wv_title
->button_type
= BUTTON_TYPE_NONE
;
2910 wv_title
->next
= wv_sep1
;
2911 wv_title
->help
= Qnil
;
2912 first_wv
->contents
= wv_title
;
2915 /* No selection has been chosen yet. */
2916 menu_item_selection
= 0;
2918 /* Actually create and show the menu until popped down. */
2919 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
);
2921 /* Free the widget_value objects we used to specify the contents. */
2922 free_menubar_widget_value_tree (first_wv
);
2924 /* Find the selected item, and its pane, to return
2925 the proper value. */
2926 if (menu_item_selection
!= 0)
2928 Lisp_Object prefix
, entry
;
2930 prefix
= entry
= Qnil
;
2932 while (i
< menu_items_used
)
2934 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2936 subprefix_stack
[submenu_depth
++] = prefix
;
2940 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2942 prefix
= subprefix_stack
[--submenu_depth
];
2945 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2948 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2949 i
+= MENU_ITEMS_PANE_LENGTH
;
2951 /* Ignore a nil in the item list.
2952 It's meaningful only for dialog boxes. */
2953 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2958 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2959 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2965 entry
= Fcons (entry
, Qnil
);
2967 entry
= Fcons (prefix
, entry
);
2968 for (j
= submenu_depth
- 1; j
>= 0; j
--)
2969 if (!NILP (subprefix_stack
[j
]))
2970 entry
= Fcons (subprefix_stack
[j
], entry
);
2974 i
+= MENU_ITEMS_ITEM_LENGTH
;
2978 else if (!for_click
)
2979 /* Make "Cancel" equivalent to C-g. */
2980 Fsignal (Qquit
, Qnil
);
2987 dialog_selection_callback (widget
, client_data
)
2989 gpointer client_data
;
2991 /* The EMACS_INT cast avoids a warning. There's no problem
2992 as long as pointers have enough bits to hold small integers. */
2993 if ((int) (EMACS_INT
) client_data
!= -1)
2994 menu_item_selection
= (Lisp_Object
*) client_data
;
2996 popup_activated_flag
= 0;
2999 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3001 menu_item_selection will be set to the selection. */
3003 create_and_show_dialog (f
, first_wv
)
3005 widget_value
*first_wv
;
3009 menu
= xg_create_widget ("dialog", first_wv
->name
, f
, first_wv
,
3010 G_CALLBACK (dialog_selection_callback
),
3011 G_CALLBACK (popup_deactivate_callback
),
3016 int specpdl_count
= SPECPDL_INDEX ();
3017 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
3019 /* Display the menu. */
3020 gtk_widget_show_all (menu
);
3022 /* Process events that apply to the menu. */
3023 popup_widget_loop (1, menu
);
3025 unbind_to (specpdl_count
, Qnil
);
3029 #else /* not USE_GTK */
3031 dialog_selection_callback (widget
, id
, client_data
)
3034 XtPointer client_data
;
3036 /* The EMACS_INT cast avoids a warning. There's no problem
3037 as long as pointers have enough bits to hold small integers. */
3038 if ((int) (EMACS_INT
) client_data
!= -1)
3039 menu_item_selection
= (Lisp_Object
*) client_data
;
3042 lw_destroy_all_widgets (id
);
3044 popup_activated_flag
= 0;
3048 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3050 menu_item_selection will be set to the selection. */
3052 create_and_show_dialog (f
, first_wv
)
3054 widget_value
*first_wv
;
3058 dialog_id
= widget_id_tick
++;
3059 lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
3060 f
->output_data
.x
->widget
, 1, 0,
3061 dialog_selection_callback
, 0, 0);
3062 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
3064 /* Display the dialog box. */
3065 lw_pop_up_all_widgets (dialog_id
);
3066 popup_activated_flag
= 1;
3068 /* Process events that apply to the dialog box.
3069 Also handle timers. */
3071 int count
= SPECPDL_INDEX ();
3072 int fact
= 4 * sizeof (LWLIB_ID
);
3074 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3075 record_unwind_protect (pop_down_menu
,
3076 Fcons (make_number (dialog_id
>> (fact
)),
3077 make_number (dialog_id
& ~(-1 << (fact
)))));
3079 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
),
3082 unbind_to (count
, Qnil
);
3086 #endif /* not USE_GTK */
3088 static char * button_names
[] = {
3089 "button1", "button2", "button3", "button4", "button5",
3090 "button6", "button7", "button8", "button9", "button10" };
3093 xdialog_show (f
, keymaps
, title
, header
, error_name
)
3096 Lisp_Object title
, header
;
3099 int i
, nb_buttons
=0;
3100 char dialog_name
[6];
3102 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
3104 /* Number of elements seen so far, before boundary. */
3106 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3107 int boundary_seen
= 0;
3111 if (menu_items_n_panes
> 1)
3113 *error_name
= "Multiple panes in dialog box";
3117 /* Create a tree of widget_value objects
3118 representing the text label and buttons. */
3120 Lisp_Object pane_name
, prefix
;
3122 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
3123 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
3124 pane_string
= (NILP (pane_name
)
3125 ? "" : (char *) SDATA (pane_name
));
3126 prev_wv
= xmalloc_widget_value ();
3127 prev_wv
->value
= pane_string
;
3128 if (keymaps
&& !NILP (prefix
))
3130 prev_wv
->enabled
= 1;
3131 prev_wv
->name
= "message";
3132 prev_wv
->help
= Qnil
;
3135 /* Loop over all panes and items, filling in the tree. */
3136 i
= MENU_ITEMS_PANE_LENGTH
;
3137 while (i
< menu_items_used
)
3140 /* Create a new item within current pane. */
3141 Lisp_Object item_name
, enable
, descrip
;
3142 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3143 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3145 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3147 if (NILP (item_name
))
3149 free_menubar_widget_value_tree (first_wv
);
3150 *error_name
= "Submenu in dialog items";
3153 if (EQ (item_name
, Qquote
))
3155 /* This is the boundary between left-side elts
3156 and right-side elts. Stop incrementing right_count. */
3161 if (nb_buttons
>= 9)
3163 free_menubar_widget_value_tree (first_wv
);
3164 *error_name
= "Too many dialog items";
3168 wv
= xmalloc_widget_value ();
3170 wv
->name
= (char *) button_names
[nb_buttons
];
3171 if (!NILP (descrip
))
3172 wv
->key
= (char *) SDATA (descrip
);
3173 wv
->value
= (char *) SDATA (item_name
);
3174 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
3175 wv
->enabled
= !NILP (enable
);
3179 if (! boundary_seen
)
3183 i
+= MENU_ITEMS_ITEM_LENGTH
;
3186 /* If the boundary was not specified,
3187 by default put half on the left and half on the right. */
3188 if (! boundary_seen
)
3189 left_count
= nb_buttons
- nb_buttons
/ 2;
3191 wv
= xmalloc_widget_value ();
3192 wv
->name
= dialog_name
;
3195 /* Frame title: 'Q' = Question, 'I' = Information.
3196 Can also have 'E' = Error if, one day, we want
3197 a popup for errors. */
3199 dialog_name
[0] = 'Q';
3201 dialog_name
[0] = 'I';
3203 /* Dialog boxes use a really stupid name encoding
3204 which specifies how many buttons to use
3205 and how many buttons are on the right. */
3206 dialog_name
[1] = '0' + nb_buttons
;
3207 dialog_name
[2] = 'B';
3208 dialog_name
[3] = 'R';
3209 /* Number of buttons to put on the right. */
3210 dialog_name
[4] = '0' + nb_buttons
- left_count
;
3212 wv
->contents
= first_wv
;
3216 /* No selection has been chosen yet. */
3217 menu_item_selection
= 0;
3219 /* Actually create and show the dialog. */
3220 create_and_show_dialog (f
, first_wv
);
3222 /* Free the widget_value objects we used to specify the contents. */
3223 free_menubar_widget_value_tree (first_wv
);
3225 /* Find the selected item, and its pane, to return
3226 the proper value. */
3227 if (menu_item_selection
!= 0)
3233 while (i
< menu_items_used
)
3237 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3240 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3241 i
+= MENU_ITEMS_PANE_LENGTH
;
3243 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3245 /* This is the boundary between left-side elts and
3252 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3253 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
3257 entry
= Fcons (entry
, Qnil
);
3259 entry
= Fcons (prefix
, entry
);
3263 i
+= MENU_ITEMS_ITEM_LENGTH
;
3268 /* Make "Cancel" equivalent to C-g. */
3269 Fsignal (Qquit
, Qnil
);
3274 #else /* not USE_X_TOOLKIT && not USE_GTK */
3276 /* The frame of the last activated non-toolkit menu bar.
3277 Used to generate menu help events. */
3279 static struct frame
*menu_help_frame
;
3282 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3284 PANE is the pane number, and ITEM is the menu item number in
3285 the menu (currently not used).
3287 This cannot be done with generating a HELP_EVENT because
3288 XMenuActivate contains a loop that doesn't let Emacs process
3292 menu_help_callback (help_string
, pane
, item
)
3296 extern Lisp_Object Qmenu_item
;
3297 Lisp_Object
*first_item
;
3298 Lisp_Object pane_name
;
3299 Lisp_Object menu_object
;
3301 first_item
= XVECTOR (menu_items
)->contents
;
3302 if (EQ (first_item
[0], Qt
))
3303 pane_name
= first_item
[MENU_ITEMS_PANE_NAME
];
3304 else if (EQ (first_item
[0], Qquote
))
3305 /* This shouldn't happen, see xmenu_show. */
3306 pane_name
= empty_string
;
3308 pane_name
= first_item
[MENU_ITEMS_ITEM_NAME
];
3310 /* (menu-item MENU-NAME PANE-NUMBER) */
3311 menu_object
= Fcons (Qmenu_item
,
3313 Fcons (make_number (pane
), Qnil
)));
3314 show_help_echo (help_string
? build_string (help_string
) : Qnil
,
3315 Qnil
, menu_object
, make_number (item
), 1);
3322 struct Lisp_Save_Value
*p1
= XSAVE_VALUE (Fcar (arg
));
3323 struct Lisp_Save_Value
*p2
= XSAVE_VALUE (Fcdr (arg
));
3325 FRAME_PTR f
= p1
->pointer
;
3326 XMenu
*menu
= p2
->pointer
;
3330 XUngrabPointer (FRAME_X_DISPLAY (f
), CurrentTime
);
3331 XUngrabKeyboard (FRAME_X_DISPLAY (f
), CurrentTime
);
3333 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3335 #ifdef HAVE_X_WINDOWS
3336 /* Assume the mouse has moved out of the X window.
3337 If it has actually moved in, we will get an EnterNotify. */
3338 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
3340 /* State that no mouse buttons are now held.
3341 (The oldXMenu code doesn't track this info for us.)
3342 That is not necessarily true, but the fiction leads to reasonable
3343 results, and it is a pain to ask which are actually held now. */
3344 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
3346 #endif /* HAVE_X_WINDOWS */
3355 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
3365 int pane
, selidx
, lpane
, status
;
3366 Lisp_Object entry
, pane_prefix
;
3368 int ulx
, uly
, width
, height
;
3369 int dispwidth
, dispheight
;
3370 int i
, j
, lines
, maxlines
;
3373 unsigned int dummy_uint
;
3374 int specpdl_count
= SPECPDL_INDEX ();
3377 if (menu_items_n_panes
== 0)
3380 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
3382 *error
= "Empty menu";
3386 /* Figure out which root window F is on. */
3387 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
3388 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
3389 &dummy_uint
, &dummy_uint
);
3391 /* Make the menu on that window. */
3392 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
3395 *error
= "Can't create menu";
3399 /* Don't GC while we prepare and show the menu,
3400 because we give the oldxmenu library pointers to the
3401 contents of strings. */
3402 inhibit_garbage_collection ();
3404 #ifdef HAVE_X_WINDOWS
3405 /* Adjust coordinates to relative to the outer (window manager) window. */
3406 x
+= FRAME_OUTER_TO_INNER_DIFF_X (f
);
3407 y
+= FRAME_OUTER_TO_INNER_DIFF_Y (f
);
3408 #endif /* HAVE_X_WINDOWS */
3410 /* Adjust coordinates to be root-window-relative. */
3414 /* Create all the necessary panes and their items. */
3415 maxlines
= lines
= i
= 0;
3416 while (i
< menu_items_used
)
3418 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3420 /* Create a new pane. */
3421 Lisp_Object pane_name
, prefix
;
3424 maxlines
= max (maxlines
, lines
);
3426 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
3427 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3428 pane_string
= (NILP (pane_name
)
3429 ? "" : (char *) SDATA (pane_name
));
3430 if (keymaps
&& !NILP (prefix
))
3433 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
3434 if (lpane
== XM_FAILURE
)
3436 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3437 *error
= "Can't create pane";
3440 i
+= MENU_ITEMS_PANE_LENGTH
;
3442 /* Find the width of the widest item in this pane. */
3445 while (j
< menu_items_used
)
3448 item
= XVECTOR (menu_items
)->contents
[j
];
3456 width
= SBYTES (item
);
3457 if (width
> maxwidth
)
3460 j
+= MENU_ITEMS_ITEM_LENGTH
;
3463 /* Ignore a nil in the item list.
3464 It's meaningful only for dialog boxes. */
3465 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3469 /* Create a new item within current pane. */
3470 Lisp_Object item_name
, enable
, descrip
, help
;
3471 unsigned char *item_data
;
3474 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3475 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3477 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3478 help
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_HELP
];
3479 help_string
= STRINGP (help
) ? SDATA (help
) : NULL
;
3481 if (!NILP (descrip
))
3483 int gap
= maxwidth
- SBYTES (item_name
);
3486 spacer
= Fmake_string (make_number (gap
), make_number (' '));
3487 item_name
= concat2 (item_name
, spacer
);
3488 item_name
= concat2 (item_name
, descrip
);
3489 item_data
= SDATA (item_name
);
3491 /* if alloca is fast, use that to make the space,
3492 to reduce gc needs. */
3494 = (unsigned char *) alloca (maxwidth
3495 + SBYTES (descrip
) + 1);
3496 bcopy (SDATA (item_name
), item_data
,
3497 SBYTES (item_name
));
3498 for (j
= SCHARS (item_name
); j
< maxwidth
; j
++)
3500 bcopy (SDATA (descrip
), item_data
+ j
,
3502 item_data
[j
+ SBYTES (descrip
)] = 0;
3506 item_data
= SDATA (item_name
);
3508 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
3509 menu
, lpane
, 0, item_data
,
3510 !NILP (enable
), help_string
)
3513 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3514 *error
= "Can't add selection to menu";
3517 i
+= MENU_ITEMS_ITEM_LENGTH
;
3522 maxlines
= max (maxlines
, lines
);
3524 /* All set and ready to fly. */
3525 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
3526 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3527 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3528 x
= min (x
, dispwidth
);
3529 y
= min (y
, dispheight
);
3532 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
3533 &ulx
, &uly
, &width
, &height
);
3534 if (ulx
+width
> dispwidth
)
3536 x
-= (ulx
+ width
) - dispwidth
;
3537 ulx
= dispwidth
- width
;
3539 if (uly
+height
> dispheight
)
3541 y
-= (uly
+ height
) - dispheight
;
3542 uly
= dispheight
- height
;
3544 if (ulx
< 0) x
-= ulx
;
3545 if (uly
< 0) y
-= uly
;
3549 /* If position was not given by a mouse click, adjust so upper left
3550 corner of the menu as a whole ends up at given coordinates. This
3551 is what x-popup-menu says in its documentation. */
3553 y
+= 1.5*height
/(maxlines
+2);
3556 XMenuSetAEQ (menu
, TRUE
);
3557 XMenuSetFreeze (menu
, TRUE
);
3561 XMenuActivateSetWaitFunction (x_menu_wait_for_event
, FRAME_X_DISPLAY (f
));
3564 record_unwind_protect (pop_down_menu
,
3565 Fcons (make_save_value (f
, 0),
3566 make_save_value (menu
, 0)));
3568 /* Help display under X won't work because XMenuActivate contains
3569 a loop that doesn't give Emacs a chance to process it. */
3570 menu_help_frame
= f
;
3571 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
3572 x
, y
, ButtonReleaseMask
, &datap
,
3573 menu_help_callback
);
3579 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
3582 /* Find the item number SELIDX in pane number PANE. */
3584 while (i
< menu_items_used
)
3586 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3590 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3592 i
+= MENU_ITEMS_PANE_LENGTH
;
3601 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3604 entry
= Fcons (entry
, Qnil
);
3605 if (!NILP (pane_prefix
))
3606 entry
= Fcons (pane_prefix
, entry
);
3612 i
+= MENU_ITEMS_ITEM_LENGTH
;
3618 *error
= "Can't activate menu";
3623 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3624 the menu was invoked with a mouse event as POSITION). */
3626 Fsignal (Qquit
, Qnil
);
3631 unbind_to (specpdl_count
, Qnil
);
3636 #endif /* not USE_X_TOOLKIT */
3638 #endif /* HAVE_MENUS */
3643 staticpro (&menu_items
);
3645 menu_items_inuse
= Qnil
;
3647 Qdebug_on_next_call
= intern ("debug-on-next-call");
3648 staticpro (&Qdebug_on_next_call
);
3650 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame
,
3651 doc
: /* Frame for which we are updating a menu.
3652 The enable predicate for a menu command should check this variable. */);
3653 Vmenu_updating_frame
= Qnil
;
3655 #ifdef USE_X_TOOLKIT
3656 widget_id_tick
= (1<<16);
3657 next_menubar_widget_id
= 1;
3660 defsubr (&Sx_popup_menu
);
3662 defsubr (&Sx_popup_dialog
);
3666 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3667 (do not change this comment) */