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. */
47 #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
));
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
;
749 XQueryPointer (FRAME_X_DISPLAY (f
),
750 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
752 /* The root window which contains the pointer. */
755 /* Window pointer is on, not used */
758 /* The position on that root window. */
761 /* x/y in dummy_window coordinates, not used. */
764 /* Modifier keys and pointer buttons, about which
766 (unsigned int *) &dummy
);
770 /* xmenu_show expects window coordinates, not root window
771 coordinates. Translate. */
772 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
773 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
776 #endif /* HAVE_X_WINDOWS */
778 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
779 doc
: /* Pop up a deck-of-cards menu and return user's selection.
780 POSITION is a position specification. This is either a mouse button event
781 or a list ((XOFFSET YOFFSET) WINDOW)
782 where XOFFSET and YOFFSET are positions in pixels from the top left
783 corner of WINDOW. (WINDOW may be a window or a frame object.)
784 This controls the position of the top left of the menu as a whole.
785 If POSITION is t, it means to use the current mouse position.
787 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
788 The menu items come from key bindings that have a menu string as well as
789 a definition; actually, the "definition" in such a key binding looks like
790 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
791 the keymap as a top-level element.
793 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
794 Otherwise, REAL-DEFINITION should be a valid key binding definition.
796 You can also use a list of keymaps as MENU.
797 Then each keymap makes a separate pane.
799 When MENU is a keymap or a list of keymaps, the return value is the
800 list of events corresponding to the user's choice. Note that
801 `x-popup-menu' does not actually execute the command bound to that
804 Alternatively, you can specify a menu of multiple panes
805 with a list of the form (TITLE PANE1 PANE2...),
806 where each pane is a list of form (TITLE ITEM1 ITEM2...).
807 Each ITEM is normally a cons cell (STRING . VALUE);
808 but a string can appear as an item--that makes a nonselectable line
810 With this form of menu, the return value is VALUE from the chosen item.
812 If POSITION is nil, don't display the menu at all, just precalculate the
813 cached information about equivalent key sequences.
815 If the user gets rid of the menu without making a valid choice, for
816 instance by clicking the mouse away from a valid choice or by typing
817 keyboard input, then this normally results in a quit and
818 `x-popup-menu' does not return. But if POSITION is a mouse button
819 event (indicating that the user invoked the menu with the mouse) then
820 no quit occurs and `x-popup-menu' returns nil. */)
822 Lisp_Object position
, menu
;
824 Lisp_Object keymap
, tem
;
825 int xpos
= 0, ypos
= 0;
827 char *error_name
= NULL
;
828 Lisp_Object selection
;
830 Lisp_Object x
, y
, window
;
833 int specpdl_count
= SPECPDL_INDEX ();
837 if (! NILP (position
))
839 int get_current_pos_p
= 0;
842 /* Decode the first argument: find the window and the coordinates. */
843 if (EQ (position
, Qt
)
844 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
845 || EQ (XCAR (position
), Qtool_bar
))))
847 get_current_pos_p
= 1;
851 tem
= Fcar (position
);
854 window
= Fcar (Fcdr (position
));
856 y
= Fcar (XCDR (tem
));
861 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
862 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
863 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
868 /* If a click happens in an external tool bar or a detached
869 tool bar, x and y is NIL. In that case, use the current
870 mouse position. This happens for the help button in the
871 tool bar. Ideally popup-menu should pass NIL to
872 this function, but it doesn't. */
873 if (NILP (x
) && NILP (y
))
874 get_current_pos_p
= 1;
877 if (get_current_pos_p
)
879 /* Use the mouse's current position. */
880 FRAME_PTR new_f
= SELECTED_FRAME ();
881 #ifdef HAVE_X_WINDOWS
882 /* Can't use mouse_position_hook for X since it returns
883 coordinates relative to the window the mouse is in,
884 we need coordinates relative to the edit widget always. */
889 mouse_position_for_popup (new_f
, &cur_x
, &cur_y
);
890 /* cur_x/y may be negative, so use make_number. */
891 x
= make_number (cur_x
);
892 y
= make_number (cur_y
);
895 #else /* not HAVE_X_WINDOWS */
896 Lisp_Object bar_window
;
897 enum scroll_bar_part part
;
900 if (mouse_position_hook
)
901 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
902 &part
, &x
, &y
, &time
);
903 #endif /* not HAVE_X_WINDOWS */
906 XSETFRAME (window
, new_f
);
909 window
= selected_window
;
918 /* Decode where to put the menu. */
926 else if (WINDOWP (window
))
928 CHECK_LIVE_WINDOW (window
);
929 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
931 xpos
= WINDOW_LEFT_EDGE_X (XWINDOW (window
));
932 ypos
= WINDOW_TOP_EDGE_Y (XWINDOW (window
));
935 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
936 but I don't want to make one now. */
937 CHECK_WINDOW (window
);
943 error ("Can not put X menu on non-X terminal");
945 XSETFRAME (Vmenu_updating_frame
, f
);
948 Vmenu_updating_frame
= Qnil
;
949 #endif /* HAVE_MENUS */
951 record_unwind_protect (unuse_menu_items
, Qnil
);
955 /* Decode the menu items from what was specified. */
957 keymap
= get_keymap (menu
, 0, 0);
960 /* We were given a keymap. Extract menu info from the keymap. */
963 /* Extract the detailed info to make one pane. */
964 keymap_panes (&menu
, 1, NILP (position
));
966 /* Search for a string appearing directly as an element of the keymap.
967 That string is the title of the menu. */
968 prompt
= Fkeymap_prompt (keymap
);
969 if (NILP (title
) && !NILP (prompt
))
972 /* Make that be the pane title of the first pane. */
973 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
974 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
978 else if (CONSP (menu
) && KEYMAPP (XCAR (menu
)))
980 /* We were given a list of keymaps. */
981 int nmaps
= XFASTINT (Flength (menu
));
983 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
988 /* The first keymap that has a prompt string
989 supplies the menu title. */
990 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= XCDR (tem
))
994 maps
[i
++] = keymap
= get_keymap (XCAR (tem
), 1, 0);
996 prompt
= Fkeymap_prompt (keymap
);
997 if (NILP (title
) && !NILP (prompt
))
1001 /* Extract the detailed info to make one pane. */
1002 keymap_panes (maps
, nmaps
, NILP (position
));
1004 /* Make the title be the pane title of the first pane. */
1005 if (!NILP (title
) && menu_items_n_panes
>= 0)
1006 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
1012 /* We were given an old-fashioned menu. */
1013 title
= Fcar (menu
);
1014 CHECK_STRING (title
);
1016 list_of_panes (Fcdr (menu
));
1021 unbind_to (specpdl_count
, Qnil
);
1023 if (NILP (position
))
1025 discard_menu_items ();
1031 /* Display them in a menu. */
1034 selection
= xmenu_show (f
, xpos
, ypos
, for_click
,
1035 keymaps
, title
, &error_name
);
1038 discard_menu_items ();
1041 #endif /* HAVE_MENUS */
1043 if (error_name
) error (error_name
);
1049 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 3, 0,
1050 doc
: /* Pop up a dialog box and return user's selection.
1051 POSITION specifies which frame to use.
1052 This is normally a mouse button event or a window or frame.
1053 If POSITION is t, it means to use the frame the mouse is on.
1054 The dialog box appears in the middle of the specified frame.
1056 CONTENTS specifies the alternatives to display in the dialog box.
1057 It is a list of the form (DIALOG ITEM1 ITEM2...).
1058 Each ITEM is a cons cell (STRING . VALUE).
1059 The return value is VALUE from the chosen item.
1061 An ITEM may also be just a string--that makes a nonselectable item.
1062 An ITEM may also be nil--that means to put all preceding items
1063 on the left of the dialog box and all following items on the right.
1064 \(By default, approximately half appear on each side.)
1066 If HEADER is non-nil, the frame title for the box is "Information",
1067 otherwise it is "Question".
1069 If the user gets rid of the dialog box without making a valid choice,
1070 for instance using the window manager, then this produces a quit and
1071 `x-popup-dialog' does not return. */)
1072 (position
, contents
, header
)
1073 Lisp_Object position
, contents
, header
;
1080 /* Decode the first argument: find the window or frame to use. */
1081 if (EQ (position
, Qt
)
1082 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
1083 || EQ (XCAR (position
), Qtool_bar
))))
1085 #if 0 /* Using the frame the mouse is on may not be right. */
1086 /* Use the mouse's current position. */
1087 FRAME_PTR new_f
= SELECTED_FRAME ();
1088 Lisp_Object bar_window
;
1089 enum scroll_bar_part part
;
1093 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
1096 XSETFRAME (window
, new_f
);
1098 window
= selected_window
;
1100 window
= selected_window
;
1102 else if (CONSP (position
))
1105 tem
= Fcar (position
);
1107 window
= Fcar (Fcdr (position
));
1110 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
1111 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
1114 else if (WINDOWP (position
) || FRAMEP (position
))
1119 /* Decode where to put the menu. */
1121 if (FRAMEP (window
))
1122 f
= XFRAME (window
);
1123 else if (WINDOWP (window
))
1125 CHECK_LIVE_WINDOW (window
);
1126 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
1129 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1130 but I don't want to make one now. */
1131 CHECK_WINDOW (window
);
1133 if (! FRAME_X_P (f
))
1134 error ("Can not put X dialog on non-X terminal");
1136 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1137 /* Display a menu with these alternatives
1138 in the middle of frame F. */
1140 Lisp_Object x
, y
, frame
, newpos
;
1141 XSETFRAME (frame
, f
);
1142 XSETINT (x
, x_pixel_width (f
) / 2);
1143 XSETINT (y
, x_pixel_height (f
) / 2);
1144 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
1146 return Fx_popup_menu (newpos
,
1147 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
1153 Lisp_Object selection
;
1154 int specpdl_count
= SPECPDL_INDEX ();
1156 /* Decode the dialog items from what was specified. */
1157 title
= Fcar (contents
);
1158 CHECK_STRING (title
);
1159 record_unwind_protect (unuse_menu_items
, Qnil
);
1161 if (NILP (Fcar (Fcdr (contents
))))
1162 /* No buttons specified, add an "Ok" button so users can pop down
1163 the dialog. Also, the lesstif/motif version crashes if there are
1165 contents
= Fcons (title
, Fcons (Fcons (build_string ("Ok"), Qt
), Qnil
));
1167 list_of_panes (Fcons (contents
, Qnil
));
1169 /* Display them in a dialog box. */
1171 selection
= xdialog_show (f
, 0, title
, header
, &error_name
);
1174 unbind_to (specpdl_count
, Qnil
);
1175 discard_menu_items ();
1177 if (error_name
) error (error_name
);
1186 /* Set menu_items_inuse so no other popup menu or dialog is created. */
1189 x_menu_set_in_use (in_use
)
1192 menu_items_inuse
= in_use
? Qt
: Qnil
;
1193 popup_activated_flag
= in_use
;
1196 /* Wait for an X event to arrive or for a timer to expire. */
1199 x_menu_wait_for_event (void *data
)
1201 extern EMACS_TIME timer_check
P_ ((int));
1203 /* Another way to do this is to register a timer callback, that can be
1204 done in GTK and Xt. But we have to do it like this when using only X
1205 anyway, and with callbacks we would have three variants for timer handling
1206 instead of the small ifdefs below. */
1209 #ifdef USE_X_TOOLKIT
1210 ! XtAppPending (Xt_app_con
)
1211 #elif defined USE_GTK
1212 ! gtk_events_pending ()
1214 ! XPending ((Display
*) data
)
1218 EMACS_TIME next_time
= timer_check (1);
1219 long secs
= EMACS_SECS (next_time
);
1220 long usecs
= EMACS_USECS (next_time
);
1221 SELECT_TYPE read_fds
;
1222 struct x_display_info
*dpyinfo
;
1225 FD_ZERO (&read_fds
);
1226 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
1228 int fd
= ConnectionNumber (dpyinfo
->display
);
1229 FD_SET (fd
, &read_fds
);
1233 if (secs
< 0 || (secs
== 0 && usecs
== 0))
1235 /* Sometimes timer_check returns -1 (no timers) even if there are
1236 timers. So do a timeout anyway. */
1237 EMACS_SET_SECS (next_time
, 1);
1238 EMACS_SET_USECS (next_time
, 0);
1241 select (n
+ 1, &read_fds
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0, &next_time
);
1244 #endif /* ! MSDOS */
1247 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1249 #ifdef USE_X_TOOLKIT
1251 /* Loop in Xt until the menu pulldown or dialog popup has been
1252 popped down (deactivated). This is used for x-popup-menu
1253 and x-popup-dialog; it is not used for the menu bar.
1255 NOTE: All calls to popup_get_selection should be protected
1256 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1259 popup_get_selection (initial_event
, dpyinfo
, id
, do_timers
)
1260 XEvent
*initial_event
;
1261 struct x_display_info
*dpyinfo
;
1267 while (popup_activated_flag
)
1271 event
= *initial_event
;
1276 if (do_timers
) x_menu_wait_for_event (0);
1277 XtAppNextEvent (Xt_app_con
, &event
);
1280 /* Make sure we don't consider buttons grabbed after menu goes.
1281 And make sure to deactivate for any ButtonRelease,
1282 even if XtDispatchEvent doesn't do that. */
1283 if (event
.type
== ButtonRelease
1284 && dpyinfo
->display
== event
.xbutton
.display
)
1286 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1287 #ifdef USE_MOTIF /* Pretending that the event came from a
1288 Btn1Down seems the only way to convince Motif to
1289 activate its callbacks; setting the XmNmenuPost
1290 isn't working. --marcus@sysc.pdx.edu. */
1291 event
.xbutton
.button
= 1;
1292 /* Motif only pops down menus when no Ctrl, Alt or Mod
1293 key is pressed and the button is released. So reset key state
1294 so Motif thinks this is the case. */
1295 event
.xbutton
.state
= 0;
1298 /* Pop down on C-g and Escape. */
1299 else if (event
.type
== KeyPress
1300 && dpyinfo
->display
== event
.xbutton
.display
)
1302 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
1304 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
1305 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
1306 popup_activated_flag
= 0;
1309 x_dispatch_event (&event
, event
.xany
.display
);
1313 #endif /* USE_X_TOOLKIT */
1316 /* Loop util popup_activated_flag is set to zero in a callback.
1317 Used for popup menus and dialogs. */
1320 popup_widget_loop (do_timers
, widget
)
1324 ++popup_activated_flag
;
1326 /* Process events in the Gtk event loop until done. */
1327 while (popup_activated_flag
)
1329 if (do_timers
) x_menu_wait_for_event (0);
1330 gtk_main_iteration ();
1335 /* Activate the menu bar of frame F.
1336 This is called from keyboard.c when it gets the
1337 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1339 To activate the menu bar, we use the X button-press event
1340 that was saved in saved_menu_event.
1341 That makes the toolkit do its thing.
1343 But first we recompute the menu bar contents (the whole tree).
1345 The reason for saving the button event until here, instead of
1346 passing it to the toolkit right away, is that we can safely
1347 execute Lisp code. */
1350 x_activate_menubar (f
)
1353 if (! FRAME_X_P (f
))
1356 if (!f
->output_data
.x
->saved_menu_event
->type
)
1360 if (! xg_win_to_widget (FRAME_X_DISPLAY (f
),
1361 f
->output_data
.x
->saved_menu_event
->xany
.window
))
1365 set_frame_menubar (f
, 0, 1);
1368 XPutBackEvent (f
->output_data
.x
->display_info
->display
,
1369 f
->output_data
.x
->saved_menu_event
);
1370 popup_activated_flag
= 1;
1372 XtDispatchEvent (f
->output_data
.x
->saved_menu_event
);
1376 if (f
->output_data
.x
->saved_menu_event
->type
== ButtonRelease
)
1377 pending_menu_activation
= 1;
1380 /* Ignore this if we get it a second time. */
1381 f
->output_data
.x
->saved_menu_event
->type
= 0;
1384 /* Detect if a dialog or menu has been posted. */
1389 return popup_activated_flag
;
1392 /* This callback is invoked when the user selects a menubar cascade
1393 pushbutton, but before the pulldown menu is posted. */
1397 popup_activate_callback (widget
, id
, client_data
)
1400 XtPointer client_data
;
1402 popup_activated_flag
= 1;
1406 /* This callback is invoked when a dialog or menu is finished being
1407 used and has been unposted. */
1411 popup_deactivate_callback (widget
, client_data
)
1413 gpointer client_data
;
1415 popup_activated_flag
= 0;
1419 popup_deactivate_callback (widget
, id
, client_data
)
1422 XtPointer client_data
;
1424 popup_activated_flag
= 0;
1429 /* Function that finds the frame for WIDGET and shows the HELP text
1431 F is the frame if known, or NULL if not known. */
1433 show_help_event (f
, widget
, help
)
1435 xt_or_gtk_widget widget
;
1442 XSETFRAME (frame
, f
);
1443 kbd_buffer_store_help_event (frame
, help
);
1447 #if 0 /* This code doesn't do anything useful. ++kfs */
1448 /* WIDGET is the popup menu. It's parent is the frame's
1449 widget. See which frame that is. */
1450 xt_or_gtk_widget frame_widget
= XtParent (widget
);
1453 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
1455 frame
= XCAR (tail
);
1456 if (GC_FRAMEP (frame
)
1457 && (f
= XFRAME (frame
),
1458 FRAME_X_P (f
) && f
->output_data
.x
->widget
== frame_widget
))
1462 show_help_echo (help
, Qnil
, Qnil
, Qnil
, 1);
1466 /* Callback called when menu items are highlighted/unhighlighted
1467 while moving the mouse over them. WIDGET is the menu bar or menu
1468 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1469 the data structure for the menu item, or null in case of
1474 menu_highlight_callback (widget
, call_data
)
1478 xg_menu_item_cb_data
*cb_data
;
1481 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (widget
),
1483 if (! cb_data
) return;
1485 help
= call_data
? cb_data
->help
: Qnil
;
1487 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1488 Don't show help for them, they won't appear before the
1489 popup is popped down. */
1490 if (popup_activated_flag
<= 1)
1491 show_help_event (cb_data
->cl_data
->f
, widget
, help
);
1495 menu_highlight_callback (widget
, id
, call_data
)
1503 widget_value
*wv
= (widget_value
*) call_data
;
1505 help
= wv
? wv
->help
: Qnil
;
1507 /* Determine the frame for the help event. */
1508 f
= menubar_id_to_frame (id
);
1510 show_help_event (f
, widget
, help
);
1514 /* Find the menu selection and store it in the keyboard buffer.
1515 F is the frame the menu is on.
1516 MENU_BAR_ITEMS_USED is the length of VECTOR.
1517 VECTOR is an array of menu events for the whole menu. */
1520 find_and_call_menu_selection (f
, menu_bar_items_used
, vector
, client_data
)
1522 int menu_bar_items_used
;
1526 Lisp_Object prefix
, entry
;
1527 Lisp_Object
*subprefix_stack
;
1528 int submenu_depth
= 0;
1532 subprefix_stack
= (Lisp_Object
*) alloca (menu_bar_items_used
* sizeof (Lisp_Object
));
1536 while (i
< menu_bar_items_used
)
1538 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1540 subprefix_stack
[submenu_depth
++] = prefix
;
1544 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1546 prefix
= subprefix_stack
[--submenu_depth
];
1549 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1551 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1552 i
+= MENU_ITEMS_PANE_LENGTH
;
1556 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1557 /* The EMACS_INT cast avoids a warning. There's no problem
1558 as long as pointers have enough bits to hold small integers. */
1559 if ((int) (EMACS_INT
) client_data
== i
)
1562 struct input_event buf
;
1566 XSETFRAME (frame
, f
);
1567 buf
.kind
= MENU_BAR_EVENT
;
1568 buf
.frame_or_window
= frame
;
1570 kbd_buffer_store_event (&buf
);
1572 for (j
= 0; j
< submenu_depth
; j
++)
1573 if (!NILP (subprefix_stack
[j
]))
1575 buf
.kind
= MENU_BAR_EVENT
;
1576 buf
.frame_or_window
= frame
;
1577 buf
.arg
= subprefix_stack
[j
];
1578 kbd_buffer_store_event (&buf
);
1583 buf
.kind
= MENU_BAR_EVENT
;
1584 buf
.frame_or_window
= frame
;
1586 kbd_buffer_store_event (&buf
);
1589 buf
.kind
= MENU_BAR_EVENT
;
1590 buf
.frame_or_window
= frame
;
1592 kbd_buffer_store_event (&buf
);
1596 i
+= MENU_ITEMS_ITEM_LENGTH
;
1603 /* Gtk calls callbacks just because we tell it what item should be
1604 selected in a radio group. If this variable is set to a non-zero
1605 value, we are creating menus and don't want callbacks right now.
1607 static int xg_crazy_callback_abort
;
1609 /* This callback is called from the menu bar pulldown menu
1610 when the user makes a selection.
1611 Figure out what the user chose
1612 and put the appropriate events into the keyboard buffer. */
1614 menubar_selection_callback (widget
, client_data
)
1616 gpointer client_data
;
1618 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
1620 if (xg_crazy_callback_abort
)
1623 if (! cb_data
|| ! cb_data
->cl_data
|| ! cb_data
->cl_data
->f
)
1626 /* For a group of radio buttons, GTK calls the selection callback first
1627 for the item that was active before the selection and then for the one that
1628 is active after the selection. For C-h k this means we get the help on
1629 the deselected item and then the selected item is executed. Prevent that
1630 by ignoring the non-active item. */
1631 if (GTK_IS_RADIO_MENU_ITEM (widget
)
1632 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget
)))
1635 /* When a menu is popped down, X generates a focus event (i.e. focus
1636 goes back to the frame below the menu). Since GTK buffers events,
1637 we force it out here before the menu selection event. Otherwise
1638 sit-for will exit at once if the focus event follows the menu selection
1642 while (gtk_events_pending ())
1643 gtk_main_iteration ();
1646 find_and_call_menu_selection (cb_data
->cl_data
->f
,
1647 cb_data
->cl_data
->menu_bar_items_used
,
1648 cb_data
->cl_data
->menu_bar_vector
,
1649 cb_data
->call_data
);
1652 #else /* not USE_GTK */
1654 /* This callback is called from the menu bar pulldown menu
1655 when the user makes a selection.
1656 Figure out what the user chose
1657 and put the appropriate events into the keyboard buffer. */
1659 menubar_selection_callback (widget
, id
, client_data
)
1662 XtPointer client_data
;
1666 f
= menubar_id_to_frame (id
);
1669 find_and_call_menu_selection (f
, f
->menu_bar_items_used
,
1670 f
->menu_bar_vector
, client_data
);
1672 #endif /* not USE_GTK */
1674 /* Allocate a widget_value, blocking input. */
1677 xmalloc_widget_value ()
1679 widget_value
*value
;
1682 value
= malloc_widget_value ();
1688 /* This recursively calls free_widget_value on the tree of widgets.
1689 It must free all data that was malloc'ed for these widget_values.
1690 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1691 must be left alone. */
1694 free_menubar_widget_value_tree (wv
)
1699 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1701 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1703 free_menubar_widget_value_tree (wv
->contents
);
1704 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1708 free_menubar_widget_value_tree (wv
->next
);
1709 wv
->next
= (widget_value
*) 0xDEADBEEF;
1712 free_widget_value (wv
);
1716 /* Set up data in menu_items for a menu bar item
1717 whose event type is ITEM_KEY (with string ITEM_NAME)
1718 and whose contents come from the list of keymaps MAPS. */
1721 parse_single_submenu (item_key
, item_name
, maps
)
1722 Lisp_Object item_key
, item_name
, maps
;
1726 Lisp_Object
*mapvec
;
1728 int top_level_items
= 0;
1730 length
= Flength (maps
);
1731 len
= XINT (length
);
1733 /* Convert the list MAPS into a vector MAPVEC. */
1734 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1735 for (i
= 0; i
< len
; i
++)
1737 mapvec
[i
] = Fcar (maps
);
1741 /* Loop over the given keymaps, making a pane for each map.
1742 But don't make a pane that is empty--ignore that map instead. */
1743 for (i
= 0; i
< len
; i
++)
1745 if (!KEYMAPP (mapvec
[i
]))
1747 /* Here we have a command at top level in the menu bar
1748 as opposed to a submenu. */
1749 top_level_items
= 1;
1750 push_menu_pane (Qnil
, Qnil
);
1751 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
1752 Qnil
, Qnil
, Qnil
, Qnil
);
1757 prompt
= Fkeymap_prompt (mapvec
[i
]);
1758 single_keymap_panes (mapvec
[i
],
1759 !NILP (prompt
) ? prompt
: item_name
,
1764 return top_level_items
;
1767 /* Create a tree of widget_value objects
1768 representing the panes and items
1769 in menu_items starting at index START, up to index END. */
1771 static widget_value
*
1772 digest_single_submenu (start
, end
, top_level_items
)
1773 int start
, end
, top_level_items
;
1775 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1777 int submenu_depth
= 0;
1778 widget_value
**submenu_stack
;
1782 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1783 wv
= xmalloc_widget_value ();
1787 wv
->button_type
= BUTTON_TYPE_NONE
;
1793 /* Loop over all panes and items made by the preceding call
1794 to parse_single_submenu and construct a tree of widget_value objects.
1795 Ignore the panes and items used by previous calls to
1796 digest_single_submenu, even though those are also in menu_items. */
1800 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1802 submenu_stack
[submenu_depth
++] = save_wv
;
1807 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1810 save_wv
= submenu_stack
[--submenu_depth
];
1813 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1814 && submenu_depth
!= 0)
1815 i
+= MENU_ITEMS_PANE_LENGTH
;
1816 /* Ignore a nil in the item list.
1817 It's meaningful only for dialog boxes. */
1818 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1820 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1822 /* Create a new pane. */
1823 Lisp_Object pane_name
, prefix
;
1828 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1829 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1831 #ifndef HAVE_MULTILINGUAL_MENU
1832 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1834 pane_name
= ENCODE_MENU_STRING (pane_name
);
1835 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1838 pane_string
= (NILP (pane_name
)
1839 ? "" : (char *) SDATA (pane_name
));
1840 /* If there is just one top-level pane, put all its items directly
1841 under the top-level menu. */
1842 if (menu_items_n_panes
== 1)
1845 /* If the pane has a meaningful name,
1846 make the pane a top-level menu item
1847 with its items as a submenu beneath it. */
1848 if (strcmp (pane_string
, ""))
1850 wv
= xmalloc_widget_value ();
1854 first_wv
->contents
= wv
;
1855 wv
->lname
= pane_name
;
1856 /* Set value to 1 so update_submenu_strings can handle '@' */
1857 wv
->value
= (char *)1;
1859 wv
->button_type
= BUTTON_TYPE_NONE
;
1867 i
+= MENU_ITEMS_PANE_LENGTH
;
1871 /* Create a new item within current pane. */
1872 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1875 /* All items should be contained in panes. */
1876 if (panes_seen
== 0)
1879 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1880 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1881 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1882 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1883 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1884 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1885 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1887 #ifndef HAVE_MULTILINGUAL_MENU
1888 if (STRING_MULTIBYTE (item_name
))
1890 item_name
= ENCODE_MENU_STRING (item_name
);
1891 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1894 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1896 descrip
= ENCODE_MENU_STRING (descrip
);
1897 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
1899 #endif /* not HAVE_MULTILINGUAL_MENU */
1901 wv
= xmalloc_widget_value ();
1905 save_wv
->contents
= wv
;
1907 wv
->lname
= item_name
;
1908 if (!NILP (descrip
))
1911 /* The EMACS_INT cast avoids a warning. There's no problem
1912 as long as pointers have enough bits to hold small integers. */
1913 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1914 wv
->enabled
= !NILP (enable
);
1917 wv
->button_type
= BUTTON_TYPE_NONE
;
1918 else if (EQ (type
, QCradio
))
1919 wv
->button_type
= BUTTON_TYPE_RADIO
;
1920 else if (EQ (type
, QCtoggle
))
1921 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1925 wv
->selected
= !NILP (selected
);
1926 if (! STRINGP (help
))
1933 i
+= MENU_ITEMS_ITEM_LENGTH
;
1937 /* If we have just one "menu item"
1938 that was originally a button, return it by itself. */
1939 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
1941 wv
= first_wv
->contents
;
1942 free_widget_value (first_wv
);
1949 /* Walk through the widget_value tree starting at FIRST_WV and update
1950 the char * pointers from the corresponding lisp values.
1951 We do this after building the whole tree, since GC may happen while the
1952 tree is constructed, and small strings are relocated. So we must wait
1953 until no GC can happen before storing pointers into lisp values. */
1955 update_submenu_strings (first_wv
)
1956 widget_value
*first_wv
;
1960 for (wv
= first_wv
; wv
; wv
= wv
->next
)
1962 if (STRINGP (wv
->lname
))
1964 wv
->name
= (char *) SDATA (wv
->lname
);
1966 /* Ignore the @ that means "separate pane".
1967 This is a kludge, but this isn't worth more time. */
1968 if (wv
->value
== (char *)1)
1970 if (wv
->name
[0] == '@')
1976 if (STRINGP (wv
->lkey
))
1977 wv
->key
= (char *) SDATA (wv
->lkey
);
1980 update_submenu_strings (wv
->contents
);
1985 /* Recompute all the widgets of frame F, when the menu bar has been
1986 changed. Value is non-zero if widgets were updated. */
1989 update_frame_menubar (f
)
1993 return xg_update_frame_menubar (f
);
1998 if (! FRAME_X_P (f
))
2001 x
= f
->output_data
.x
;
2003 if (!x
->menubar_widget
|| XtIsManaged (x
->menubar_widget
))
2007 /* Save the size of the frame because the pane widget doesn't accept
2008 to resize itself. So force it. */
2009 columns
= FRAME_COLS (f
);
2010 rows
= FRAME_LINES (f
);
2012 /* Do the voodoo which means "I'm changing lots of things, don't try
2013 to refigure sizes until I'm done." */
2014 lw_refigure_widget (x
->column_widget
, False
);
2016 /* The order in which children are managed is the top to bottom
2017 order in which they are displayed in the paned window. First,
2018 remove the text-area widget. */
2019 XtUnmanageChild (x
->edit_widget
);
2021 /* Remove the menubar that is there now, and put up the menubar that
2023 XtManageChild (x
->menubar_widget
);
2024 XtMapWidget (x
->menubar_widget
);
2025 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, NULL
);
2027 /* Re-manage the text-area widget, and then thrash the sizes. */
2028 XtManageChild (x
->edit_widget
);
2029 lw_refigure_widget (x
->column_widget
, True
);
2031 /* Force the pane widget to resize itself with the right values. */
2032 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
2038 /* Set the contents of the menubar widgets of frame F.
2039 The argument FIRST_TIME is currently ignored;
2040 it is set the first time this is called, from initialize_frame_menubar. */
2043 set_frame_menubar (f
, first_time
, deep_p
)
2048 xt_or_gtk_widget menubar_widget
;
2049 #ifdef USE_X_TOOLKIT
2053 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
2055 int *submenu_start
, *submenu_end
;
2056 int *submenu_top_level_items
, *submenu_n_panes
;
2058 if (! FRAME_X_P (f
))
2061 menubar_widget
= f
->output_data
.x
->menubar_widget
;
2063 XSETFRAME (Vmenu_updating_frame
, f
);
2065 #ifdef USE_X_TOOLKIT
2066 if (f
->output_data
.x
->id
== 0)
2067 f
->output_data
.x
->id
= next_menubar_widget_id
++;
2068 id
= f
->output_data
.x
->id
;
2071 if (! menubar_widget
)
2073 else if (pending_menu_activation
&& !deep_p
)
2075 /* Make the first call for any given frame always go deep. */
2076 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
2079 f
->output_data
.x
->saved_menu_event
= (XEvent
*)xmalloc (sizeof (XEvent
));
2080 f
->output_data
.x
->saved_menu_event
->type
= 0;
2084 /* If we have detached menus, we must update deep so detached menus
2085 also gets updated. */
2086 deep_p
= deep_p
|| xg_have_tear_offs ();
2091 /* Make a widget-value tree representing the entire menu trees. */
2093 struct buffer
*prev
= current_buffer
;
2095 int specpdl_count
= SPECPDL_INDEX ();
2096 int previous_menu_items_used
= f
->menu_bar_items_used
;
2097 Lisp_Object
*previous_items
2098 = (Lisp_Object
*) alloca (previous_menu_items_used
2099 * sizeof (Lisp_Object
));
2101 /* If we are making a new widget, its contents are empty,
2102 do always reinitialize them. */
2103 if (! menubar_widget
)
2104 previous_menu_items_used
= 0;
2106 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
2107 specbind (Qinhibit_quit
, Qt
);
2108 /* Don't let the debugger step into this code
2109 because it is not reentrant. */
2110 specbind (Qdebug_on_next_call
, Qnil
);
2112 record_unwind_save_match_data ();
2113 if (NILP (Voverriding_local_map_menu_flag
))
2115 specbind (Qoverriding_terminal_local_map
, Qnil
);
2116 specbind (Qoverriding_local_map
, Qnil
);
2119 set_buffer_internal_1 (XBUFFER (buffer
));
2121 /* Run the Lucid hook. */
2122 safe_run_hooks (Qactivate_menubar_hook
);
2124 /* If it has changed current-menubar from previous value,
2125 really recompute the menubar from the value. */
2126 if (! NILP (Vlucid_menu_bar_dirty_flag
))
2127 call0 (Qrecompute_lucid_menubar
);
2128 safe_run_hooks (Qmenu_bar_update_hook
);
2129 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2131 items
= FRAME_MENU_BAR_ITEMS (f
);
2133 /* Save the frame's previous menu bar contents data. */
2134 if (previous_menu_items_used
)
2135 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
2136 previous_menu_items_used
* sizeof (Lisp_Object
));
2138 /* Fill in menu_items with the current menu bar contents.
2139 This can evaluate Lisp code. */
2142 menu_items
= f
->menu_bar_vector
;
2143 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
2144 submenu_start
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2145 submenu_end
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2146 submenu_n_panes
= (int *) alloca (XVECTOR (items
)->size
* sizeof (int));
2147 submenu_top_level_items
2148 = (int *) alloca (XVECTOR (items
)->size
* sizeof (int *));
2150 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2152 Lisp_Object key
, string
, maps
;
2156 key
= XVECTOR (items
)->contents
[i
];
2157 string
= XVECTOR (items
)->contents
[i
+ 1];
2158 maps
= XVECTOR (items
)->contents
[i
+ 2];
2162 submenu_start
[i
] = menu_items_used
;
2164 menu_items_n_panes
= 0;
2165 submenu_top_level_items
[i
]
2166 = parse_single_submenu (key
, string
, maps
);
2167 submenu_n_panes
[i
] = menu_items_n_panes
;
2169 submenu_end
[i
] = menu_items_used
;
2172 finish_menu_items ();
2174 /* Convert menu_items into widget_value trees
2175 to display the menu. This cannot evaluate Lisp code. */
2177 wv
= xmalloc_widget_value ();
2178 wv
->name
= "menubar";
2181 wv
->button_type
= BUTTON_TYPE_NONE
;
2185 for (i
= 0; i
< last_i
; i
+= 4)
2187 menu_items_n_panes
= submenu_n_panes
[i
];
2188 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
2189 submenu_top_level_items
[i
]);
2193 first_wv
->contents
= wv
;
2194 /* Don't set wv->name here; GC during the loop might relocate it. */
2196 wv
->button_type
= BUTTON_TYPE_NONE
;
2200 set_buffer_internal_1 (prev
);
2202 /* If there has been no change in the Lisp-level contents
2203 of the menu bar, skip redisplaying it. Just exit. */
2205 /* Compare the new menu items with the ones computed last time. */
2206 for (i
= 0; i
< previous_menu_items_used
; i
++)
2207 if (menu_items_used
== i
2208 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
2210 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
2212 /* The menu items have not changed. Don't bother updating
2213 the menus in any form, since it would be a no-op. */
2214 free_menubar_widget_value_tree (first_wv
);
2215 discard_menu_items ();
2216 unbind_to (specpdl_count
, Qnil
);
2220 /* The menu items are different, so store them in the frame. */
2221 f
->menu_bar_vector
= menu_items
;
2222 f
->menu_bar_items_used
= menu_items_used
;
2224 /* This calls restore_menu_items to restore menu_items, etc.,
2225 as they were outside. */
2226 unbind_to (specpdl_count
, Qnil
);
2228 /* Now GC cannot happen during the lifetime of the widget_value,
2229 so it's safe to store data from a Lisp_String. */
2230 wv
= first_wv
->contents
;
2231 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2234 string
= XVECTOR (items
)->contents
[i
+ 1];
2237 wv
->name
= (char *) SDATA (string
);
2238 update_submenu_strings (wv
->contents
);
2245 /* Make a widget-value tree containing
2246 just the top level menu bar strings. */
2248 wv
= xmalloc_widget_value ();
2249 wv
->name
= "menubar";
2252 wv
->button_type
= BUTTON_TYPE_NONE
;
2256 items
= FRAME_MENU_BAR_ITEMS (f
);
2257 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
2261 string
= XVECTOR (items
)->contents
[i
+ 1];
2265 wv
= xmalloc_widget_value ();
2266 wv
->name
= (char *) SDATA (string
);
2269 wv
->button_type
= BUTTON_TYPE_NONE
;
2271 /* This prevents lwlib from assuming this
2272 menu item is really supposed to be empty. */
2273 /* The EMACS_INT cast avoids a warning.
2274 This value just has to be different from small integers. */
2275 wv
->call_data
= (void *) (EMACS_INT
) (-1);
2280 first_wv
->contents
= wv
;
2284 /* Forget what we thought we knew about what is in the
2285 detailed contents of the menu bar menus.
2286 Changing the top level always destroys the contents. */
2287 f
->menu_bar_items_used
= 0;
2290 /* Create or update the menu bar widget. */
2295 xg_crazy_callback_abort
= 1;
2298 /* The fourth arg is DEEP_P, which says to consider the entire
2299 menu trees we supply, rather than just the menu bar item names. */
2300 xg_modify_menubar_widgets (menubar_widget
,
2304 G_CALLBACK (menubar_selection_callback
),
2305 G_CALLBACK (popup_deactivate_callback
),
2306 G_CALLBACK (menu_highlight_callback
));
2310 GtkWidget
*wvbox
= f
->output_data
.x
->vbox_widget
;
2313 = xg_create_widget ("menubar", "menubar", f
, first_wv
,
2314 G_CALLBACK (menubar_selection_callback
),
2315 G_CALLBACK (popup_deactivate_callback
),
2316 G_CALLBACK (menu_highlight_callback
));
2318 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2322 #else /* not USE_GTK */
2325 /* Disable resizing (done for Motif!) */
2326 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
2328 /* The third arg is DEEP_P, which says to consider the entire
2329 menu trees we supply, rather than just the menu bar item names. */
2330 lw_modify_all_widgets (id
, first_wv
, deep_p
);
2332 /* Re-enable the edit widget to resize. */
2333 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
2337 char menuOverride
[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2338 XtTranslations override
= XtParseTranslationTable (menuOverride
);
2340 menubar_widget
= lw_create_widget ("menubar", "menubar", id
, first_wv
,
2341 f
->output_data
.x
->column_widget
,
2343 popup_activate_callback
,
2344 menubar_selection_callback
,
2345 popup_deactivate_callback
,
2346 menu_highlight_callback
);
2347 f
->output_data
.x
->menubar_widget
= menubar_widget
;
2349 /* Make menu pop down on C-g. */
2350 XtOverrideTranslations (menubar_widget
, override
);
2355 = (f
->output_data
.x
->menubar_widget
2356 ? (f
->output_data
.x
->menubar_widget
->core
.height
2357 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
2360 #if 0 /* Experimentally, we now get the right results
2361 for -geometry -0-0 without this. 24 Aug 96, rms. */
2363 if (FRAME_EXTERNAL_MENU_BAR (f
))
2366 XtVaGetValues (f
->output_data
.x
->column_widget
,
2367 XtNinternalBorderWidth
, &ibw
, NULL
);
2368 menubar_size
+= ibw
;
2370 #endif /* USE_LUCID */
2373 f
->output_data
.x
->menubar_height
= menubar_size
;
2375 #endif /* not USE_GTK */
2377 free_menubar_widget_value_tree (first_wv
);
2378 update_frame_menubar (f
);
2381 xg_crazy_callback_abort
= 0;
2387 /* Called from Fx_create_frame to create the initial menubar of a frame
2388 before it is mapped, so that the window is mapped with the menubar already
2389 there instead of us tacking it on later and thrashing the window after it
2393 initialize_frame_menubar (f
)
2396 /* This function is called before the first chance to redisplay
2397 the frame. It has to be, so the frame will have the right size. */
2398 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
2399 set_frame_menubar (f
, 1, 1);
2403 /* Get rid of the menu bar of frame F, and free its storage.
2404 This is used when deleting a frame, and when turning off the menu bar.
2405 For GTK this function is in gtkutil.c. */
2409 free_frame_menubar (f
)
2412 Widget menubar_widget
;
2414 if (! FRAME_X_P (f
))
2417 menubar_widget
= f
->output_data
.x
->menubar_widget
;
2419 f
->output_data
.x
->menubar_height
= 0;
2424 /* Removing the menu bar magically changes the shell widget's x
2425 and y position of (0, 0) which, when the menu bar is turned
2426 on again, leads to pull-down menuss appearing in strange
2427 positions near the upper-left corner of the display. This
2428 happens only with some window managers like twm and ctwm,
2429 but not with other like Motif's mwm or kwm, because the
2430 latter generate ConfigureNotify events when the menu bar
2431 is switched off, which fixes the shell position. */
2432 Position x0
, y0
, x1
, y1
;
2438 if (f
->output_data
.x
->widget
)
2439 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x0
, XtNy
, &y0
, NULL
);
2442 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
2443 f
->output_data
.x
->menubar_widget
= NULL
;
2446 if (f
->output_data
.x
->widget
)
2448 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x1
, XtNy
, &y1
, NULL
);
2449 if (x1
== 0 && y1
== 0)
2450 XtVaSetValues (f
->output_data
.x
->widget
, XtNx
, x0
, XtNy
, y0
, NULL
);
2457 #endif /* not USE_GTK */
2459 #endif /* USE_X_TOOLKIT || USE_GTK */
2461 /* xmenu_show actually displays a menu using the panes and items in menu_items
2462 and returns the value selected from it.
2463 There are two versions of xmenu_show, one for Xt and one for Xlib.
2464 Both assume input is blocked by the caller. */
2466 /* F is the frame the menu is for.
2467 X and Y are the frame-relative specified position,
2468 relative to the inside upper left corner of the frame F.
2469 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2470 KEYMAPS is 1 if this menu was specified with keymaps;
2471 in that case, we return a list containing the chosen item's value
2472 and perhaps also the pane's prefix.
2473 TITLE is the specified menu title.
2474 ERROR is a place to store an error message string in case of failure.
2475 (We return nil on failure, but the value doesn't actually matter.) */
2477 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2479 /* The item selected in the popup menu. */
2480 static Lisp_Object
*volatile menu_item_selection
;
2484 /* Used when position a popup menu. See menu_position_func and
2485 create_and_show_popup_menu below. */
2486 struct next_popup_x_y
2493 /* The menu position function to use if we are not putting a popup
2494 menu where the pointer is.
2495 MENU is the menu to pop up.
2496 X and Y shall on exit contain x/y where the menu shall pop up.
2497 PUSH_IN is not documented in the GTK manual.
2498 USER_DATA is any data passed in when calling gtk_menu_popup.
2499 Here it points to a struct next_popup_x_y where the coordinates
2500 to store in *X and *Y are as well as the frame for the popup.
2502 Here only X and Y are used. */
2504 menu_position_func (menu
, x
, y
, push_in
, user_data
)
2511 struct next_popup_x_y
* data
= (struct next_popup_x_y
*)user_data
;
2513 int disp_width
= FRAME_X_DISPLAY_INFO (data
->f
)->width
;
2514 int disp_height
= FRAME_X_DISPLAY_INFO (data
->f
)->height
;
2519 /* Check if there is room for the menu. If not, adjust x/y so that
2520 the menu is fully visible. */
2521 gtk_widget_size_request (GTK_WIDGET (menu
), &req
);
2522 if (data
->x
+ req
.width
> disp_width
)
2523 *x
-= data
->x
+ req
.width
- disp_width
;
2524 if (data
->y
+ req
.height
> disp_height
)
2525 *y
-= data
->y
+ req
.height
- disp_height
;
2529 popup_selection_callback (widget
, client_data
)
2531 gpointer client_data
;
2533 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
2535 if (xg_crazy_callback_abort
) return;
2536 if (cb_data
) menu_item_selection
= (Lisp_Object
*) cb_data
->call_data
;
2543 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
2545 popup_activated_flag
= 0;
2547 gtk_widget_destroy (GTK_WIDGET (p
->pointer
));
2552 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2554 menu_item_selection will be set to the selection. */
2556 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2558 widget_value
*first_wv
;
2565 GtkMenuPositionFunc pos_func
= 0; /* Pop up at pointer. */
2566 struct next_popup_x_y popup_x_y
;
2567 int specpdl_count
= SPECPDL_INDEX ();
2569 if (! FRAME_X_P (f
))
2572 xg_crazy_callback_abort
= 1;
2573 menu
= xg_create_widget ("popup", first_wv
->name
, f
, first_wv
,
2574 G_CALLBACK (popup_selection_callback
),
2575 G_CALLBACK (popup_deactivate_callback
),
2576 G_CALLBACK (menu_highlight_callback
));
2577 xg_crazy_callback_abort
= 0;
2581 /* Not invoked by a click. pop up at x/y. */
2582 pos_func
= menu_position_func
;
2584 /* Adjust coordinates to be root-window-relative. */
2585 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2586 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2592 i
= 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2596 for (i
= 0; i
< 5; i
++)
2597 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2601 /* Display the menu. */
2602 gtk_widget_show_all (menu
);
2603 gtk_menu_popup (GTK_MENU (menu
), 0, 0, pos_func
, &popup_x_y
, i
, 0);
2605 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
2607 if (GTK_WIDGET_MAPPED (menu
))
2609 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2610 two. show_help_echo uses this to detect popup menus. */
2611 popup_activated_flag
= 1;
2612 /* Process events that apply to the menu. */
2613 popup_widget_loop (1, menu
);
2616 unbind_to (specpdl_count
, Qnil
);
2618 /* Must reset this manually because the button release event is not passed
2619 to Emacs event loop. */
2620 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2623 #else /* not USE_GTK */
2625 /* We need a unique id for each widget handled by the Lucid Widget
2628 For the main windows, and popup menus, we use this counter,
2629 which we increment each time after use. This starts from 1<<16.
2631 For menu bars, we use numbers starting at 0, counted in
2632 next_menubar_widget_id. */
2633 LWLIB_ID widget_id_tick
;
2636 popup_selection_callback (widget
, id
, client_data
)
2639 XtPointer client_data
;
2641 menu_item_selection
= (Lisp_Object
*) client_data
;
2644 /* ARG is the LWLIB ID of the dialog box, represented
2645 as a Lisp object as (HIGHPART . LOWPART). */
2651 LWLIB_ID id
= (XINT (XCAR (arg
)) << 4 * sizeof (LWLIB_ID
)
2652 | XINT (XCDR (arg
)));
2655 lw_destroy_all_widgets (id
);
2657 popup_activated_flag
= 0;
2662 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2664 menu_item_selection will be set to the selection. */
2666 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
)
2668 widget_value
*first_wv
;
2676 XButtonPressedEvent dummy
;
2680 if (! FRAME_X_P (f
))
2683 menu_id
= widget_id_tick
++;
2684 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
2685 f
->output_data
.x
->widget
, 1, 0,
2686 popup_selection_callback
,
2687 popup_deactivate_callback
,
2688 menu_highlight_callback
);
2690 dummy
.type
= ButtonPress
;
2692 dummy
.send_event
= 0;
2693 dummy
.display
= FRAME_X_DISPLAY (f
);
2694 dummy
.time
= CurrentTime
;
2695 dummy
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
2696 dummy
.window
= dummy
.root
;
2697 dummy
.subwindow
= dummy
.root
;
2701 /* Adjust coordinates to be root-window-relative. */
2702 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2703 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2710 for (i
= 0; i
< 5; i
++)
2711 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2714 /* Don't allow any geometry request from the user. */
2715 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
2716 XtSetValues (menu
, av
, ac
);
2718 /* Display the menu. */
2719 lw_popup_menu (menu
, (XEvent
*) &dummy
);
2720 popup_activated_flag
= 1;
2723 int fact
= 4 * sizeof (LWLIB_ID
);
2724 int specpdl_count
= SPECPDL_INDEX ();
2725 record_unwind_protect (pop_down_menu
,
2726 Fcons (make_number (menu_id
>> (fact
)),
2727 make_number (menu_id
& ~(-1 << (fact
)))));
2729 /* Process events that apply to the menu. */
2730 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
, 1);
2732 unbind_to (specpdl_count
, Qnil
);
2736 #endif /* not USE_GTK */
2739 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
2749 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
2750 widget_value
**submenu_stack
2751 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
2752 Lisp_Object
*subprefix_stack
2753 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
2754 int submenu_depth
= 0;
2758 if (! FRAME_X_P (f
))
2763 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2765 *error
= "Empty menu";
2769 /* Create a tree of widget_value objects
2770 representing the panes and their items. */
2771 wv
= xmalloc_widget_value ();
2775 wv
->button_type
= BUTTON_TYPE_NONE
;
2780 /* Loop over all panes and items, filling in the tree. */
2782 while (i
< menu_items_used
)
2784 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2786 submenu_stack
[submenu_depth
++] = save_wv
;
2792 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2795 save_wv
= submenu_stack
[--submenu_depth
];
2799 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
2800 && submenu_depth
!= 0)
2801 i
+= MENU_ITEMS_PANE_LENGTH
;
2802 /* Ignore a nil in the item list.
2803 It's meaningful only for dialog boxes. */
2804 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2806 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2808 /* Create a new pane. */
2809 Lisp_Object pane_name
, prefix
;
2812 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
2813 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2815 #ifndef HAVE_MULTILINGUAL_MENU
2816 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
2818 pane_name
= ENCODE_MENU_STRING (pane_name
);
2819 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
2822 pane_string
= (NILP (pane_name
)
2823 ? "" : (char *) SDATA (pane_name
));
2824 /* If there is just one top-level pane, put all its items directly
2825 under the top-level menu. */
2826 if (menu_items_n_panes
== 1)
2829 /* If the pane has a meaningful name,
2830 make the pane a top-level menu item
2831 with its items as a submenu beneath it. */
2832 if (!keymaps
&& strcmp (pane_string
, ""))
2834 wv
= xmalloc_widget_value ();
2838 first_wv
->contents
= wv
;
2839 wv
->name
= pane_string
;
2840 if (keymaps
&& !NILP (prefix
))
2844 wv
->button_type
= BUTTON_TYPE_NONE
;
2849 else if (first_pane
)
2855 i
+= MENU_ITEMS_PANE_LENGTH
;
2859 /* Create a new item within current pane. */
2860 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
2861 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
2862 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
2863 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
2864 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
2865 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
2866 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
2867 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
2869 #ifndef HAVE_MULTILINGUAL_MENU
2870 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
2872 item_name
= ENCODE_MENU_STRING (item_name
);
2873 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
2876 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
2878 descrip
= ENCODE_MENU_STRING (descrip
);
2879 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
2881 #endif /* not HAVE_MULTILINGUAL_MENU */
2883 wv
= xmalloc_widget_value ();
2887 save_wv
->contents
= wv
;
2888 wv
->name
= (char *) SDATA (item_name
);
2889 if (!NILP (descrip
))
2890 wv
->key
= (char *) SDATA (descrip
);
2892 /* If this item has a null value,
2893 make the call_data null so that it won't display a box
2894 when the mouse is on it. */
2896 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
2897 wv
->enabled
= !NILP (enable
);
2900 wv
->button_type
= BUTTON_TYPE_NONE
;
2901 else if (EQ (type
, QCtoggle
))
2902 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
2903 else if (EQ (type
, QCradio
))
2904 wv
->button_type
= BUTTON_TYPE_RADIO
;
2908 wv
->selected
= !NILP (selected
);
2910 if (! STRINGP (help
))
2917 i
+= MENU_ITEMS_ITEM_LENGTH
;
2921 /* Deal with the title, if it is non-nil. */
2924 widget_value
*wv_title
= xmalloc_widget_value ();
2925 widget_value
*wv_sep1
= xmalloc_widget_value ();
2926 widget_value
*wv_sep2
= xmalloc_widget_value ();
2928 wv_sep2
->name
= "--";
2929 wv_sep2
->next
= first_wv
->contents
;
2930 wv_sep2
->help
= Qnil
;
2932 wv_sep1
->name
= "--";
2933 wv_sep1
->next
= wv_sep2
;
2934 wv_sep1
->help
= Qnil
;
2936 #ifndef HAVE_MULTILINGUAL_MENU
2937 if (STRING_MULTIBYTE (title
))
2938 title
= ENCODE_MENU_STRING (title
);
2941 wv_title
->name
= (char *) SDATA (title
);
2942 wv_title
->enabled
= TRUE
;
2943 wv_title
->button_type
= BUTTON_TYPE_NONE
;
2944 wv_title
->next
= wv_sep1
;
2945 wv_title
->help
= Qnil
;
2946 first_wv
->contents
= wv_title
;
2949 /* No selection has been chosen yet. */
2950 menu_item_selection
= 0;
2952 /* Actually create and show the menu until popped down. */
2953 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
);
2955 /* Free the widget_value objects we used to specify the contents. */
2956 free_menubar_widget_value_tree (first_wv
);
2958 /* Find the selected item, and its pane, to return
2959 the proper value. */
2960 if (menu_item_selection
!= 0)
2962 Lisp_Object prefix
, entry
;
2964 prefix
= entry
= Qnil
;
2966 while (i
< menu_items_used
)
2968 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2970 subprefix_stack
[submenu_depth
++] = prefix
;
2974 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2976 prefix
= subprefix_stack
[--submenu_depth
];
2979 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2982 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2983 i
+= MENU_ITEMS_PANE_LENGTH
;
2985 /* Ignore a nil in the item list.
2986 It's meaningful only for dialog boxes. */
2987 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2992 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2993 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2999 entry
= Fcons (entry
, Qnil
);
3001 entry
= Fcons (prefix
, entry
);
3002 for (j
= submenu_depth
- 1; j
>= 0; j
--)
3003 if (!NILP (subprefix_stack
[j
]))
3004 entry
= Fcons (subprefix_stack
[j
], entry
);
3008 i
+= MENU_ITEMS_ITEM_LENGTH
;
3012 else if (!for_click
)
3013 /* Make "Cancel" equivalent to C-g. */
3014 Fsignal (Qquit
, Qnil
);
3021 dialog_selection_callback (widget
, client_data
)
3023 gpointer client_data
;
3025 /* The EMACS_INT cast avoids a warning. There's no problem
3026 as long as pointers have enough bits to hold small integers. */
3027 if ((int) (EMACS_INT
) client_data
!= -1)
3028 menu_item_selection
= (Lisp_Object
*) client_data
;
3030 popup_activated_flag
= 0;
3033 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3035 menu_item_selection will be set to the selection. */
3037 create_and_show_dialog (f
, first_wv
)
3039 widget_value
*first_wv
;
3043 if (! FRAME_X_P (f
))
3046 menu
= xg_create_widget ("dialog", first_wv
->name
, f
, first_wv
,
3047 G_CALLBACK (dialog_selection_callback
),
3048 G_CALLBACK (popup_deactivate_callback
),
3053 int specpdl_count
= SPECPDL_INDEX ();
3054 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
3056 /* Display the menu. */
3057 gtk_widget_show_all (menu
);
3059 /* Process events that apply to the menu. */
3060 popup_widget_loop (1, menu
);
3062 unbind_to (specpdl_count
, Qnil
);
3066 #else /* not USE_GTK */
3068 dialog_selection_callback (widget
, id
, client_data
)
3071 XtPointer client_data
;
3073 /* The EMACS_INT cast avoids a warning. There's no problem
3074 as long as pointers have enough bits to hold small integers. */
3075 if ((int) (EMACS_INT
) client_data
!= -1)
3076 menu_item_selection
= (Lisp_Object
*) client_data
;
3079 lw_destroy_all_widgets (id
);
3081 popup_activated_flag
= 0;
3085 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3087 menu_item_selection will be set to the selection. */
3089 create_and_show_dialog (f
, first_wv
)
3091 widget_value
*first_wv
;
3098 dialog_id
= widget_id_tick
++;
3099 lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
3100 f
->output_data
.x
->widget
, 1, 0,
3101 dialog_selection_callback
, 0, 0);
3102 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
3104 /* Display the dialog box. */
3105 lw_pop_up_all_widgets (dialog_id
);
3106 popup_activated_flag
= 1;
3108 /* Process events that apply to the dialog box.
3109 Also handle timers. */
3111 int count
= SPECPDL_INDEX ();
3112 int fact
= 4 * sizeof (LWLIB_ID
);
3114 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3115 record_unwind_protect (pop_down_menu
,
3116 Fcons (make_number (dialog_id
>> (fact
)),
3117 make_number (dialog_id
& ~(-1 << (fact
)))));
3119 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
),
3122 unbind_to (count
, Qnil
);
3126 #endif /* not USE_GTK */
3128 static char * button_names
[] = {
3129 "button1", "button2", "button3", "button4", "button5",
3130 "button6", "button7", "button8", "button9", "button10" };
3133 xdialog_show (f
, keymaps
, title
, header
, error_name
)
3136 Lisp_Object title
, header
;
3139 int i
, nb_buttons
=0;
3140 char dialog_name
[6];
3142 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
3144 /* Number of elements seen so far, before boundary. */
3146 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3147 int boundary_seen
= 0;
3149 if (! FRAME_X_P (f
))
3154 if (menu_items_n_panes
> 1)
3156 *error_name
= "Multiple panes in dialog box";
3160 /* Create a tree of widget_value objects
3161 representing the text label and buttons. */
3163 Lisp_Object pane_name
, prefix
;
3165 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
3166 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
3167 pane_string
= (NILP (pane_name
)
3168 ? "" : (char *) SDATA (pane_name
));
3169 prev_wv
= xmalloc_widget_value ();
3170 prev_wv
->value
= pane_string
;
3171 if (keymaps
&& !NILP (prefix
))
3173 prev_wv
->enabled
= 1;
3174 prev_wv
->name
= "message";
3175 prev_wv
->help
= Qnil
;
3178 /* Loop over all panes and items, filling in the tree. */
3179 i
= MENU_ITEMS_PANE_LENGTH
;
3180 while (i
< menu_items_used
)
3183 /* Create a new item within current pane. */
3184 Lisp_Object item_name
, enable
, descrip
;
3185 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3186 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3188 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3190 if (NILP (item_name
))
3192 free_menubar_widget_value_tree (first_wv
);
3193 *error_name
= "Submenu in dialog items";
3196 if (EQ (item_name
, Qquote
))
3198 /* This is the boundary between left-side elts
3199 and right-side elts. Stop incrementing right_count. */
3204 if (nb_buttons
>= 9)
3206 free_menubar_widget_value_tree (first_wv
);
3207 *error_name
= "Too many dialog items";
3211 wv
= xmalloc_widget_value ();
3213 wv
->name
= (char *) button_names
[nb_buttons
];
3214 if (!NILP (descrip
))
3215 wv
->key
= (char *) SDATA (descrip
);
3216 wv
->value
= (char *) SDATA (item_name
);
3217 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
3218 wv
->enabled
= !NILP (enable
);
3222 if (! boundary_seen
)
3226 i
+= MENU_ITEMS_ITEM_LENGTH
;
3229 /* If the boundary was not specified,
3230 by default put half on the left and half on the right. */
3231 if (! boundary_seen
)
3232 left_count
= nb_buttons
- nb_buttons
/ 2;
3234 wv
= xmalloc_widget_value ();
3235 wv
->name
= dialog_name
;
3238 /* Frame title: 'Q' = Question, 'I' = Information.
3239 Can also have 'E' = Error if, one day, we want
3240 a popup for errors. */
3242 dialog_name
[0] = 'Q';
3244 dialog_name
[0] = 'I';
3246 /* Dialog boxes use a really stupid name encoding
3247 which specifies how many buttons to use
3248 and how many buttons are on the right. */
3249 dialog_name
[1] = '0' + nb_buttons
;
3250 dialog_name
[2] = 'B';
3251 dialog_name
[3] = 'R';
3252 /* Number of buttons to put on the right. */
3253 dialog_name
[4] = '0' + nb_buttons
- left_count
;
3255 wv
->contents
= first_wv
;
3259 /* No selection has been chosen yet. */
3260 menu_item_selection
= 0;
3262 /* Actually create and show the dialog. */
3263 create_and_show_dialog (f
, first_wv
);
3265 /* Free the widget_value objects we used to specify the contents. */
3266 free_menubar_widget_value_tree (first_wv
);
3268 /* Find the selected item, and its pane, to return
3269 the proper value. */
3270 if (menu_item_selection
!= 0)
3276 while (i
< menu_items_used
)
3280 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3283 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3284 i
+= MENU_ITEMS_PANE_LENGTH
;
3286 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3288 /* This is the boundary between left-side elts and
3295 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3296 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
3300 entry
= Fcons (entry
, Qnil
);
3302 entry
= Fcons (prefix
, entry
);
3306 i
+= MENU_ITEMS_ITEM_LENGTH
;
3311 /* Make "Cancel" equivalent to C-g. */
3312 Fsignal (Qquit
, Qnil
);
3317 #else /* not USE_X_TOOLKIT && not USE_GTK */
3319 /* The frame of the last activated non-toolkit menu bar.
3320 Used to generate menu help events. */
3322 static struct frame
*menu_help_frame
;
3325 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3327 PANE is the pane number, and ITEM is the menu item number in
3328 the menu (currently not used).
3330 This cannot be done with generating a HELP_EVENT because
3331 XMenuActivate contains a loop that doesn't let Emacs process
3335 menu_help_callback (help_string
, pane
, item
)
3339 extern Lisp_Object Qmenu_item
;
3340 Lisp_Object
*first_item
;
3341 Lisp_Object pane_name
;
3342 Lisp_Object menu_object
;
3344 first_item
= XVECTOR (menu_items
)->contents
;
3345 if (EQ (first_item
[0], Qt
))
3346 pane_name
= first_item
[MENU_ITEMS_PANE_NAME
];
3347 else if (EQ (first_item
[0], Qquote
))
3348 /* This shouldn't happen, see xmenu_show. */
3349 pane_name
= empty_string
;
3351 pane_name
= first_item
[MENU_ITEMS_ITEM_NAME
];
3353 /* (menu-item MENU-NAME PANE-NUMBER) */
3354 menu_object
= Fcons (Qmenu_item
,
3356 Fcons (make_number (pane
), Qnil
)));
3357 show_help_echo (help_string
? build_string (help_string
) : Qnil
,
3358 Qnil
, menu_object
, make_number (item
), 1);
3365 struct Lisp_Save_Value
*p1
= XSAVE_VALUE (Fcar (arg
));
3366 struct Lisp_Save_Value
*p2
= XSAVE_VALUE (Fcdr (arg
));
3368 FRAME_PTR f
= p1
->pointer
;
3369 XMenu
*menu
= p2
->pointer
;
3373 XUngrabPointer (FRAME_X_DISPLAY (f
), CurrentTime
);
3374 XUngrabKeyboard (FRAME_X_DISPLAY (f
), CurrentTime
);
3376 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3378 #ifdef HAVE_X_WINDOWS
3379 /* Assume the mouse has moved out of the X window.
3380 If it has actually moved in, we will get an EnterNotify. */
3381 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
3383 /* State that no mouse buttons are now held.
3384 (The oldXMenu code doesn't track this info for us.)
3385 That is not necessarily true, but the fiction leads to reasonable
3386 results, and it is a pain to ask which are actually held now. */
3387 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
3389 #endif /* HAVE_X_WINDOWS */
3398 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
3408 int pane
, selidx
, lpane
, status
;
3409 Lisp_Object entry
, pane_prefix
;
3411 int ulx
, uly
, width
, height
;
3412 int dispwidth
, dispheight
;
3413 int i
, j
, lines
, maxlines
;
3416 unsigned int dummy_uint
;
3417 int specpdl_count
= SPECPDL_INDEX ();
3419 if (! FRAME_X_P (f
))
3423 if (menu_items_n_panes
== 0)
3426 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
3428 *error
= "Empty menu";
3432 /* Figure out which root window F is on. */
3433 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
3434 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
3435 &dummy_uint
, &dummy_uint
);
3437 /* Make the menu on that window. */
3438 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
3441 *error
= "Can't create menu";
3445 /* Don't GC while we prepare and show the menu,
3446 because we give the oldxmenu library pointers to the
3447 contents of strings. */
3448 inhibit_garbage_collection ();
3450 #ifdef HAVE_X_WINDOWS
3451 /* Adjust coordinates to relative to the outer (window manager) window. */
3452 x
+= FRAME_OUTER_TO_INNER_DIFF_X (f
);
3453 y
+= FRAME_OUTER_TO_INNER_DIFF_Y (f
);
3454 #endif /* HAVE_X_WINDOWS */
3456 /* Adjust coordinates to be root-window-relative. */
3460 /* Create all the necessary panes and their items. */
3461 maxlines
= lines
= i
= 0;
3462 while (i
< menu_items_used
)
3464 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3466 /* Create a new pane. */
3467 Lisp_Object pane_name
, prefix
;
3470 maxlines
= max (maxlines
, lines
);
3472 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
3473 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3474 pane_string
= (NILP (pane_name
)
3475 ? "" : (char *) SDATA (pane_name
));
3476 if (keymaps
&& !NILP (prefix
))
3479 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
3480 if (lpane
== XM_FAILURE
)
3482 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3483 *error
= "Can't create pane";
3486 i
+= MENU_ITEMS_PANE_LENGTH
;
3488 /* Find the width of the widest item in this pane. */
3491 while (j
< menu_items_used
)
3494 item
= XVECTOR (menu_items
)->contents
[j
];
3502 width
= SBYTES (item
);
3503 if (width
> maxwidth
)
3506 j
+= MENU_ITEMS_ITEM_LENGTH
;
3509 /* Ignore a nil in the item list.
3510 It's meaningful only for dialog boxes. */
3511 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
3515 /* Create a new item within current pane. */
3516 Lisp_Object item_name
, enable
, descrip
, help
;
3517 unsigned char *item_data
;
3520 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
3521 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
3523 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
3524 help
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_HELP
];
3525 help_string
= STRINGP (help
) ? SDATA (help
) : NULL
;
3527 if (!NILP (descrip
))
3529 int gap
= maxwidth
- SBYTES (item_name
);
3532 spacer
= Fmake_string (make_number (gap
), make_number (' '));
3533 item_name
= concat2 (item_name
, spacer
);
3534 item_name
= concat2 (item_name
, descrip
);
3535 item_data
= SDATA (item_name
);
3537 /* if alloca is fast, use that to make the space,
3538 to reduce gc needs. */
3540 = (unsigned char *) alloca (maxwidth
3541 + SBYTES (descrip
) + 1);
3542 bcopy (SDATA (item_name
), item_data
,
3543 SBYTES (item_name
));
3544 for (j
= SCHARS (item_name
); j
< maxwidth
; j
++)
3546 bcopy (SDATA (descrip
), item_data
+ j
,
3548 item_data
[j
+ SBYTES (descrip
)] = 0;
3552 item_data
= SDATA (item_name
);
3554 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
3555 menu
, lpane
, 0, item_data
,
3556 !NILP (enable
), help_string
)
3559 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
3560 *error
= "Can't add selection to menu";
3563 i
+= MENU_ITEMS_ITEM_LENGTH
;
3568 maxlines
= max (maxlines
, lines
);
3570 /* All set and ready to fly. */
3571 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
3572 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3573 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
3574 x
= min (x
, dispwidth
);
3575 y
= min (y
, dispheight
);
3578 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
3579 &ulx
, &uly
, &width
, &height
);
3580 if (ulx
+width
> dispwidth
)
3582 x
-= (ulx
+ width
) - dispwidth
;
3583 ulx
= dispwidth
- width
;
3585 if (uly
+height
> dispheight
)
3587 y
-= (uly
+ height
) - dispheight
;
3588 uly
= dispheight
- height
;
3590 if (ulx
< 0) x
-= ulx
;
3591 if (uly
< 0) y
-= uly
;
3595 /* If position was not given by a mouse click, adjust so upper left
3596 corner of the menu as a whole ends up at given coordinates. This
3597 is what x-popup-menu says in its documentation. */
3599 y
+= 1.5*height
/(maxlines
+2);
3602 XMenuSetAEQ (menu
, TRUE
);
3603 XMenuSetFreeze (menu
, TRUE
);
3607 XMenuActivateSetWaitFunction (x_menu_wait_for_event
, FRAME_X_DISPLAY (f
));
3610 record_unwind_protect (pop_down_menu
,
3611 Fcons (make_save_value (f
, 0),
3612 make_save_value (menu
, 0)));
3614 /* Help display under X won't work because XMenuActivate contains
3615 a loop that doesn't give Emacs a chance to process it. */
3616 menu_help_frame
= f
;
3617 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
3618 x
, y
, ButtonReleaseMask
, &datap
,
3619 menu_help_callback
);
3625 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
3628 /* Find the item number SELIDX in pane number PANE. */
3630 while (i
< menu_items_used
)
3632 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
3636 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
3638 i
+= MENU_ITEMS_PANE_LENGTH
;
3647 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
3650 entry
= Fcons (entry
, Qnil
);
3651 if (!NILP (pane_prefix
))
3652 entry
= Fcons (pane_prefix
, entry
);
3658 i
+= MENU_ITEMS_ITEM_LENGTH
;
3664 *error
= "Can't activate menu";
3669 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3670 the menu was invoked with a mouse event as POSITION). */
3672 Fsignal (Qquit
, Qnil
);
3677 unbind_to (specpdl_count
, Qnil
);
3682 #endif /* not USE_X_TOOLKIT */
3684 #endif /* HAVE_MENUS */
3689 staticpro (&menu_items
);
3691 menu_items_inuse
= Qnil
;
3693 Qdebug_on_next_call
= intern ("debug-on-next-call");
3694 staticpro (&Qdebug_on_next_call
);
3696 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame
,
3697 doc
: /* Frame for which we are updating a menu.
3698 The enable predicate for a menu command should check this variable. */);
3699 Vmenu_updating_frame
= Qnil
;
3701 #ifdef USE_X_TOOLKIT
3702 widget_id_tick
= (1<<16);
3703 next_menubar_widget_id
= 1;
3706 defsubr (&Sx_popup_menu
);
3708 defsubr (&Sx_popup_dialog
);
3712 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3713 (do not change this comment) */