1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 88, 93, 94, 96, 99, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* X pop-up deck-of-cards menu facility for GNU Emacs.
24 * Written by Jon Arnold and Roman Budzianowski
25 * Mods and rewrite by Robert Krawitz
29 /* Modified by Fred Pierresteguy on December 93
30 to make the popup menus and menubar use the Xt. */
32 /* Rewritten for clarity and GC protection by rms in Feb 94. */
36 /* On 4.3 this loses if it comes after xterm.h. */
42 #include "termhooks.h"
47 #include "blockinput.h"
57 /* This may include sys/types.h, and that somehow loses
58 if this is not done before the other system files. */
62 /* Load sys/types.h if not already loaded.
63 In some systems loading it twice is suicidal. */
65 #include <sys/types.h>
68 #include "dispextern.h"
71 #undef HAVE_MULTILINGUAL_MENU
75 #include <X11/IntrinsicP.h>
76 #include <X11/CoreP.h>
77 #include <X11/StringDefs.h>
78 #include <X11/Shell.h>
80 #include <X11/Xaw/Paned.h>
81 #endif /* USE_LUCID */
82 #include "../lwlib/lwlib.h"
83 #else /* not USE_X_TOOLKIT */
84 #include "../oldXMenu/XMenu.h"
85 #endif /* not USE_X_TOOLKIT */
86 #endif /* HAVE_X_WINDOWS */
93 Lisp_Object Vmenu_updating_frame
;
95 Lisp_Object Qdebug_on_next_call
;
97 extern Lisp_Object Qmenu_bar
;
98 extern Lisp_Object Qmouse_click
, Qevent_kind
;
100 extern Lisp_Object QCtoggle
, QCradio
;
102 extern Lisp_Object Voverriding_local_map
;
103 extern Lisp_Object Voverriding_local_map_menu_flag
;
105 extern Lisp_Object Qoverriding_local_map
, Qoverriding_terminal_local_map
;
107 extern Lisp_Object Qmenu_bar_update_hook
;
110 extern void set_frame_menubar ();
111 extern void process_expose_from_menu ();
112 extern XtAppContext Xt_app_con
;
114 static Lisp_Object
xdialog_show ();
115 void popup_get_selection ();
117 /* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
122 static void push_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
123 Lisp_Object
, Lisp_Object
, Lisp_Object
,
124 Lisp_Object
, Lisp_Object
));
125 static int update_frame_menubar
P_ ((struct frame
*));
126 static Lisp_Object xmenu_show
P_ ((struct frame
*, int, int, int, int,
127 Lisp_Object
, char **));
128 static void keymap_panes
P_ ((Lisp_Object
*, int, int));
129 static void single_keymap_panes
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
131 static void single_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
*,
133 static void list_of_panes
P_ ((Lisp_Object
));
134 static void list_of_items
P_ ((Lisp_Object
));
136 /* This holds a Lisp vector that holds the results of decoding
137 the keymaps or alist-of-alists that specify a menu.
139 It describes the panes and items within the panes.
141 Each pane is described by 3 elements in the vector:
142 t, the pane name, the pane's prefix key.
143 Then follow the pane's items, with 5 elements per item:
144 the item string, the enable flag, the item's value,
145 the definition, and the equivalent keyboard key's description string.
147 In some cases, multiple levels of menus may be described.
148 A single vector slot containing nil indicates the start of a submenu.
149 A single vector slot containing lambda indicates the end of a submenu.
150 The submenu follows a menu item which is the way to reach the submenu.
152 A single vector slot containing quote indicates that the
153 following items should appear on the right of a dialog box.
155 Using a Lisp vector to hold this information while we decode it
156 takes care of protecting all the data from GC. */
158 #define MENU_ITEMS_PANE_NAME 1
159 #define MENU_ITEMS_PANE_PREFIX 2
160 #define MENU_ITEMS_PANE_LENGTH 3
164 MENU_ITEMS_ITEM_NAME
= 0,
165 MENU_ITEMS_ITEM_ENABLE
,
166 MENU_ITEMS_ITEM_VALUE
,
167 MENU_ITEMS_ITEM_EQUIV_KEY
,
168 MENU_ITEMS_ITEM_DEFINITION
,
169 MENU_ITEMS_ITEM_TYPE
,
170 MENU_ITEMS_ITEM_SELECTED
,
171 MENU_ITEMS_ITEM_HELP
,
172 MENU_ITEMS_ITEM_LENGTH
175 static Lisp_Object menu_items
;
177 /* Number of slots currently allocated in menu_items. */
178 static int menu_items_allocated
;
180 /* This is the index in menu_items of the first empty slot. */
181 static int menu_items_used
;
183 /* The number of panes currently recorded in menu_items,
184 excluding those within submenus. */
185 static int menu_items_n_panes
;
187 /* Current depth within submenus. */
188 static int menu_items_submenu_depth
;
190 /* Flag which when set indicates a dialog or menu has been posted by
191 Xt on behalf of one of the widget sets. */
192 int popup_activated_flag
;
194 static int next_menubar_widget_id
;
196 /* This is set nonzero after the user activates the menu bar, and set
197 to zero again after the menu bars are redisplayed by prepare_menu_bar.
198 While it is nonzero, all calls to set_frame_menubar go deep.
200 I don't understand why this is needed, but it does seem to be
201 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
203 int pending_menu_activation
;
207 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
209 static struct frame
*
210 menubar_id_to_frame (id
)
213 Lisp_Object tail
, frame
;
216 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
219 if (!GC_FRAMEP (frame
))
222 if (!FRAME_WINDOW_P (f
))
224 if (f
->output_data
.x
->id
== id
)
232 /* Initialize the menu_items structure if we haven't already done so.
233 Also mark it as currently empty. */
238 if (NILP (menu_items
))
240 menu_items_allocated
= 60;
241 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
245 menu_items_n_panes
= 0;
246 menu_items_submenu_depth
= 0;
249 /* Call at the end of generating the data in menu_items. */
256 /* Call when finished using the data for the current menu
260 discard_menu_items ()
262 /* Free the structure if it is especially large.
263 Otherwise, hold on to it, to save time. */
264 if (menu_items_allocated
> 200)
267 menu_items_allocated
= 0;
271 /* Make the menu_items vector twice as large. */
277 int old_size
= menu_items_allocated
;
280 menu_items_allocated
*= 2;
281 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
282 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
283 old_size
* sizeof (Lisp_Object
));
286 /* Begin a submenu. */
289 push_submenu_start ()
291 if (menu_items_used
+ 1 > menu_items_allocated
)
294 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
295 menu_items_submenu_depth
++;
303 if (menu_items_used
+ 1 > menu_items_allocated
)
306 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
307 menu_items_submenu_depth
--;
310 /* Indicate boundary between left and right. */
313 push_left_right_boundary ()
315 if (menu_items_used
+ 1 > menu_items_allocated
)
318 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
321 /* Start a new menu pane in menu_items.
322 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
325 push_menu_pane (name
, prefix_vec
)
326 Lisp_Object name
, prefix_vec
;
328 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
331 if (menu_items_submenu_depth
== 0)
332 menu_items_n_panes
++;
333 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
334 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
335 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
338 /* Push one menu item into the current pane. NAME is the string to
339 display. ENABLE if non-nil means this item can be selected. KEY
340 is the key generated by choosing this item, or nil if this item
341 doesn't really have a definition. DEF is the definition of this
342 item. EQUIV is the textual description of the keyboard equivalent
343 for this item (or nil if none). TYPE is the type of this menu
344 item, one of nil, `toggle' or `radio'. */
347 push_menu_item (name
, enable
, key
, def
, equiv
, type
, selected
, help
)
348 Lisp_Object name
, enable
, key
, def
, equiv
, type
, selected
, help
;
350 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
353 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
354 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
355 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
356 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
357 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
358 XVECTOR (menu_items
)->contents
[menu_items_used
++] = type
;
359 XVECTOR (menu_items
)->contents
[menu_items_used
++] = selected
;
360 XVECTOR (menu_items
)->contents
[menu_items_used
++] = help
;
363 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
364 and generate menu panes for them in menu_items.
365 If NOTREAL is nonzero,
366 don't bother really computing whether an item is enabled. */
369 keymap_panes (keymaps
, nmaps
, notreal
)
370 Lisp_Object
*keymaps
;
378 /* Loop over the given keymaps, making a pane for each map.
379 But don't make a pane that is empty--ignore that map instead.
380 P is the number of panes we have made so far. */
381 for (mapno
= 0; mapno
< nmaps
; mapno
++)
382 single_keymap_panes (keymaps
[mapno
],
383 Fkeymap_prompt (keymaps
[mapno
]), Qnil
, notreal
, 10);
385 finish_menu_items ();
388 /* This is a recursive subroutine of keymap_panes.
389 It handles one keymap, KEYMAP.
390 The other arguments are passed along
391 or point to local variables of the previous function.
392 If NOTREAL is nonzero, only check for equivalent key bindings, don't
393 evaluate expressions in menu items and don't make any menu.
395 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
398 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
400 Lisp_Object pane_name
;
405 Lisp_Object pending_maps
= Qnil
;
406 Lisp_Object tail
, item
;
407 struct gcpro gcpro1
, gcpro2
;
413 push_menu_pane (pane_name
, prefix
);
416 /* Remember index for first item in this pane so we can go back and
417 add a prefix when (if) we see the first button. After that, notbuttons
418 is set to 0, to mark that we have seen a button and all non button
419 items need a prefix. */
420 notbuttons
= menu_items_used
;
423 for (tail
= keymap
; CONSP (tail
); tail
= XCDR (tail
))
425 GCPRO2 (keymap
, pending_maps
);
426 /* Look at each key binding, and if it is a menu item add it
430 single_menu_item (XCAR (item
), XCDR (item
),
431 &pending_maps
, notreal
, maxdepth
, ¬buttons
);
432 else if (VECTORP (item
))
434 /* Loop over the char values represented in the vector. */
435 int len
= XVECTOR (item
)->size
;
437 for (c
= 0; c
< len
; c
++)
439 Lisp_Object character
;
440 XSETFASTINT (character
, c
);
441 single_menu_item (character
, XVECTOR (item
)->contents
[c
],
442 &pending_maps
, notreal
, maxdepth
, ¬buttons
);
448 /* Process now any submenus which want to be panes at this level. */
449 while (!NILP (pending_maps
))
451 Lisp_Object elt
, eltcdr
, string
;
452 elt
= Fcar (pending_maps
);
454 string
= XCAR (eltcdr
);
455 /* We no longer discard the @ from the beginning of the string here.
456 Instead, we do this in xmenu_show. */
457 single_keymap_panes (Fcar (elt
), string
,
458 XCDR (eltcdr
), notreal
, maxdepth
- 1);
459 pending_maps
= Fcdr (pending_maps
);
463 /* This is a subroutine of single_keymap_panes that handles one
465 KEY is a key in a keymap and ITEM is its binding.
466 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
468 If NOTREAL is nonzero, only check for equivalent key bindings, don't
469 evaluate expressions in menu items and don't make any menu.
470 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
471 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
472 buttons. It points to variable notbuttons in single_keymap_panes,
473 which keeps track of if we have seen a button in this menu or not. */
476 single_menu_item (key
, item
, pending_maps_ptr
, notreal
, maxdepth
,
478 Lisp_Object key
, item
;
479 Lisp_Object
*pending_maps_ptr
;
480 int maxdepth
, notreal
;
483 Lisp_Object map
, item_string
, enabled
;
484 struct gcpro gcpro1
, gcpro2
;
487 /* Parse the menu item and leave the result in item_properties. */
489 res
= parse_menu_item (item
, notreal
, 0);
492 return; /* Not a menu item. */
494 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
498 /* We don't want to make a menu, just traverse the keymaps to
499 precompute equivalent key bindings. */
501 single_keymap_panes (map
, Qnil
, key
, 1, maxdepth
- 1);
505 enabled
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_ENABLE
];
506 item_string
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_NAME
];
508 if (!NILP (map
) && XSTRING (item_string
)->data
[0] == '@')
511 /* An enabled separate pane. Remember this to handle it later. */
512 *pending_maps_ptr
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
518 /* Simulate radio buttons and toggle boxes by putting a prefix in
521 Lisp_Object prefix
= Qnil
;
522 Lisp_Object type
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
];
526 = XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
];
529 /* The first button. Line up previous items in this menu. */
531 int index
= *notbuttons_ptr
; /* Index for first item this menu. */
534 while (index
< menu_items_used
)
537 = XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
];
541 submenu
++; /* Skip sub menu. */
543 else if (EQ (tem
, Qlambda
))
546 submenu
--; /* End sub menu. */
548 else if (EQ (tem
, Qt
))
549 index
+= 3; /* Skip new pane marker. */
550 else if (EQ (tem
, Qquote
))
551 index
++; /* Skip a left, right divider. */
554 if (!submenu
&& XSTRING (tem
)->data
[0] != '\0'
555 && XSTRING (tem
)->data
[0] != '-')
556 XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
]
557 = concat2 (build_string (" "), tem
);
558 index
+= MENU_ITEMS_ITEM_LENGTH
;
564 /* Calculate prefix, if any, for this item. */
565 if (EQ (type
, QCtoggle
))
566 prefix
= build_string (NILP (selected
) ? "[ ] " : "[X] ");
567 else if (EQ (type
, QCradio
))
568 prefix
= build_string (NILP (selected
) ? "( ) " : "(*) ");
570 /* Not a button. If we have earlier buttons, then we need a prefix. */
571 else if (!*notbuttons_ptr
&& XSTRING (item_string
)->data
[0] != '\0'
572 && XSTRING (item_string
)->data
[0] != '-')
573 prefix
= build_string (" ");
576 item_string
= concat2 (prefix
, item_string
);
578 #endif /* not HAVE_BOXES */
580 #ifndef USE_X_TOOLKIT
582 /* Indicate visually that this is a submenu. */
583 item_string
= concat2 (item_string
, build_string (" >"));
586 push_menu_item (item_string
, enabled
, key
,
587 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_DEF
],
588 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_KEYEQ
],
589 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
],
590 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
],
591 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_HELP
]);
594 /* Display a submenu using the toolkit. */
595 if (! (NILP (map
) || NILP (enabled
)))
597 push_submenu_start ();
598 single_keymap_panes (map
, Qnil
, key
, 0, maxdepth
- 1);
604 /* Push all the panes and items of a menu described by the
605 alist-of-alists MENU.
606 This handles old-fashioned calls to x-popup-menu. */
616 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
618 Lisp_Object elt
, pane_name
, pane_data
;
620 pane_name
= Fcar (elt
);
621 CHECK_STRING (pane_name
);
622 push_menu_pane (pane_name
, Qnil
);
623 pane_data
= Fcdr (elt
);
624 CHECK_CONS (pane_data
);
625 list_of_items (pane_data
);
628 finish_menu_items ();
631 /* Push the items in a single pane defined by the alist PANE. */
637 Lisp_Object tail
, item
, item1
;
639 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
643 push_menu_item (item
, Qnil
, Qnil
, Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
644 else if (NILP (item
))
645 push_left_right_boundary ();
650 CHECK_STRING (item1
);
651 push_menu_item (item1
, Qt
, Fcdr (item
), Qt
, Qnil
, Qnil
, Qnil
, Qnil
);
656 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
657 doc
: /* Pop up a deck-of-cards menu and return user's selection.
658 POSITION is a position specification. This is either a mouse button event
659 or a list ((XOFFSET YOFFSET) WINDOW)
660 where XOFFSET and YOFFSET are positions in pixels from the top left
661 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)
662 This controls the position of the center of the first line
663 in the first pane of the menu, not the top left of the menu as a whole.
664 If POSITION is t, it means to use the current mouse position.
666 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
667 The menu items come from key bindings that have a menu string as well as
668 a definition; actually, the "definition" in such a key binding looks like
669 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into
670 the keymap as a top-level element.
672 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
673 Otherwise, REAL-DEFINITION should be a valid key binding definition.
675 You can also use a list of keymaps as MENU.
676 Then each keymap makes a separate pane.
677 When MENU is a keymap or a list of keymaps, the return value
680 Alternatively, you can specify a menu of multiple panes
681 with a list of the form (TITLE PANE1 PANE2...),
682 where each pane is a list of form (TITLE ITEM1 ITEM2...).
683 Each ITEM is normally a cons cell (STRING . VALUE);
684 but a string can appear as an item--that makes a nonselectable line
686 With this form of menu, the return value is VALUE from the chosen item.
688 If POSITION is nil, don't display the menu at all, just precalculate the
689 cached information about equivalent key sequences. */)
691 Lisp_Object position
, menu
;
693 Lisp_Object keymap
, tem
;
694 int xpos
= 0, ypos
= 0;
697 Lisp_Object selection
;
699 Lisp_Object x
, y
, window
;
705 if (! NILP (position
))
709 /* Decode the first argument: find the window and the coordinates. */
710 if (EQ (position
, Qt
)
711 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
712 || EQ (XCAR (position
), Qtool_bar
))))
714 /* Use the mouse's current position. */
715 FRAME_PTR new_f
= SELECTED_FRAME ();
716 Lisp_Object bar_window
;
717 enum scroll_bar_part part
;
720 if (mouse_position_hook
)
721 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
722 &part
, &x
, &y
, &time
);
724 XSETFRAME (window
, new_f
);
727 window
= selected_window
;
734 tem
= Fcar (position
);
737 window
= Fcar (Fcdr (position
));
739 y
= Fcar (Fcdr (tem
));
744 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
745 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
746 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
755 /* Decode where to put the menu. */
763 else if (WINDOWP (window
))
765 CHECK_LIVE_WINDOW (window
);
766 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
768 xpos
= (FONT_WIDTH (FRAME_FONT (f
))
769 * XFASTINT (XWINDOW (window
)->left
));
770 ypos
= (FRAME_LINE_HEIGHT (f
)
771 * XFASTINT (XWINDOW (window
)->top
));
774 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
775 but I don't want to make one now. */
776 CHECK_WINDOW (window
);
781 XSETFRAME (Vmenu_updating_frame
, f
);
783 Vmenu_updating_frame
= Qnil
;
784 #endif /* HAVE_MENUS */
789 /* Decode the menu items from what was specified. */
791 keymap
= get_keymap (menu
, 0, 0);
794 /* We were given a keymap. Extract menu info from the keymap. */
797 /* Extract the detailed info to make one pane. */
798 keymap_panes (&menu
, 1, NILP (position
));
800 /* Search for a string appearing directly as an element of the keymap.
801 That string is the title of the menu. */
802 prompt
= Fkeymap_prompt (keymap
);
803 if (NILP (title
) && !NILP (prompt
))
806 /* Make that be the pane title of the first pane. */
807 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
808 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
812 else if (CONSP (menu
) && KEYMAPP (XCAR (menu
)))
814 /* We were given a list of keymaps. */
815 int nmaps
= XFASTINT (Flength (menu
));
817 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
822 /* The first keymap that has a prompt string
823 supplies the menu title. */
824 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= Fcdr (tem
))
828 maps
[i
++] = keymap
= get_keymap (Fcar (tem
), 1, 0);
830 prompt
= Fkeymap_prompt (keymap
);
831 if (NILP (title
) && !NILP (prompt
))
835 /* Extract the detailed info to make one pane. */
836 keymap_panes (maps
, nmaps
, NILP (position
));
838 /* Make the title be the pane title of the first pane. */
839 if (!NILP (title
) && menu_items_n_panes
>= 0)
840 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
846 /* We were given an old-fashioned menu. */
848 CHECK_STRING (title
);
850 list_of_panes (Fcdr (menu
));
857 discard_menu_items ();
863 /* Display them in a menu. */
866 selection
= xmenu_show (f
, xpos
, ypos
, for_click
,
867 keymaps
, title
, &error_name
);
870 discard_menu_items ();
873 #endif /* HAVE_MENUS */
875 if (error_name
) error (error_name
);
881 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
882 doc
: /* Pop up a dialog box and return user's selection.
883 POSITION specifies which frame to use.
884 This is normally a mouse button event or a window or frame.
885 If POSITION is t, it means to use the frame the mouse is on.
886 The dialog box appears in the middle of the specified frame.
888 CONTENTS specifies the alternatives to display in the dialog box.
889 It is a list of the form (TITLE ITEM1 ITEM2...).
890 Each ITEM is a cons cell (STRING . VALUE).
891 The return value is VALUE from the chosen item.
893 An ITEM may also be just a string--that makes a nonselectable item.
894 An ITEM may also be nil--that means to put all preceding items
895 on the left of the dialog box and all following items on the right.
896 \(By default, approximately half appear on each side.) */)
898 Lisp_Object position
, contents
;
905 /* Decode the first argument: find the window or frame to use. */
906 if (EQ (position
, Qt
)
907 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
908 || EQ (XCAR (position
), Qtool_bar
))))
910 #if 0 /* Using the frame the mouse is on may not be right. */
911 /* Use the mouse's current position. */
912 FRAME_PTR new_f
= SELECTED_FRAME ();
913 Lisp_Object bar_window
;
914 enum scroll_bar_part part
;
918 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
921 XSETFRAME (window
, new_f
);
923 window
= selected_window
;
925 window
= selected_window
;
927 else if (CONSP (position
))
930 tem
= Fcar (position
);
932 window
= Fcar (Fcdr (position
));
935 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
936 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
939 else if (WINDOWP (position
) || FRAMEP (position
))
944 /* Decode where to put the menu. */
948 else if (WINDOWP (window
))
950 CHECK_LIVE_WINDOW (window
);
951 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
954 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
955 but I don't want to make one now. */
956 CHECK_WINDOW (window
);
958 #ifndef USE_X_TOOLKIT
959 /* Display a menu with these alternatives
960 in the middle of frame F. */
962 Lisp_Object x
, y
, frame
, newpos
;
963 XSETFRAME (frame
, f
);
964 XSETINT (x
, x_pixel_width (f
) / 2);
965 XSETINT (y
, x_pixel_height (f
) / 2);
966 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
968 return Fx_popup_menu (newpos
,
969 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
975 Lisp_Object selection
;
977 /* Decode the dialog items from what was specified. */
978 title
= Fcar (contents
);
979 CHECK_STRING (title
);
981 list_of_panes (Fcons (contents
, Qnil
));
983 /* Display them in a dialog box. */
985 selection
= xdialog_show (f
, 0, title
, &error_name
);
988 discard_menu_items ();
990 if (error_name
) error (error_name
);
998 /* Loop in Xt until the menu pulldown or dialog popup has been
999 popped down (deactivated). This is used for x-popup-menu
1000 and x-popup-dialog; it is not used for the menu bar any more.
1002 NOTE: All calls to popup_get_selection should be protected
1003 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1006 popup_get_selection (initial_event
, dpyinfo
, id
)
1007 XEvent
*initial_event
;
1008 struct x_display_info
*dpyinfo
;
1013 /* Define a queue to save up for later unreading
1014 all X events that don't pertain to the menu. */
1018 struct event_queue
*next
;
1021 struct event_queue
*queue
= NULL
;
1022 struct event_queue
*queue_tmp
;
1025 event
= *initial_event
;
1027 XtAppNextEvent (Xt_app_con
, &event
);
1031 /* Handle expose events for editor frames right away. */
1032 if (event
.type
== Expose
)
1033 process_expose_from_menu (event
);
1034 /* Make sure we don't consider buttons grabbed after menu goes.
1035 And make sure to deactivate for any ButtonRelease,
1036 even if XtDispatchEvent doesn't do that. */
1037 else if (event
.type
== ButtonRelease
1038 && dpyinfo
->display
== event
.xbutton
.display
)
1040 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1041 popup_activated_flag
= 0;
1042 #ifdef USE_MOTIF /* Pretending that the event came from a
1043 Btn1Down seems the only way to convince Motif to
1044 activate its callbacks; setting the XmNmenuPost
1045 isn't working. --marcus@sysc.pdx.edu. */
1046 event
.xbutton
.button
= 1;
1049 /* If the user presses a key, deactivate the menu.
1050 The user is likely to do that if we get wedged. */
1051 else if (event
.type
== KeyPress
1052 && dpyinfo
->display
== event
.xbutton
.display
)
1054 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
1055 if (!IsModifierKey (keysym
))
1057 popup_activated_flag
= 0;
1061 /* Button presses outside the menu also pop it down. */
1062 else if (event
.type
== ButtonPress
1063 && event
.xany
.display
== dpyinfo
->display
1064 && x_any_window_to_frame (dpyinfo
, event
.xany
.window
))
1066 popup_activated_flag
= 0;
1070 /* Queue all events not for this popup,
1071 except for Expose, which we've already handled, and ButtonRelease.
1072 Note that the X window is associated with the frame if this
1073 is a menu bar popup, but not if it's a dialog box. So we use
1074 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1075 if (event
.type
!= Expose
1076 && !(event
.type
== ButtonRelease
1077 && dpyinfo
->display
== event
.xbutton
.display
)
1078 && (event
.xany
.display
!= dpyinfo
->display
1079 || x_non_menubar_window_to_frame (dpyinfo
, event
.xany
.window
)))
1081 queue_tmp
= (struct event_queue
*) xmalloc (sizeof *queue_tmp
);
1082 queue_tmp
->event
= event
;
1083 queue_tmp
->next
= queue
;
1087 XtDispatchEvent (&event
);
1089 if (!popup_activated ())
1091 XtAppNextEvent (Xt_app_con
, &event
);
1094 /* Unread any events that we got but did not handle. */
1095 while (queue
!= NULL
)
1098 XPutBackEvent (queue_tmp
->event
.xany
.display
, &queue_tmp
->event
);
1099 queue
= queue_tmp
->next
;
1100 xfree ((char *)queue_tmp
);
1101 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1102 interrupt_input_pending
= 1;
1106 /* Activate the menu bar of frame F.
1107 This is called from keyboard.c when it gets the
1108 menu_bar_activate_event out of the Emacs event queue.
1110 To activate the menu bar, we use the X button-press event
1111 that was saved in saved_menu_event.
1112 That makes the toolkit do its thing.
1114 But first we recompute the menu bar contents (the whole tree).
1116 The reason for saving the button event until here, instead of
1117 passing it to the toolkit right away, is that we can safely
1118 execute Lisp code. */
1121 x_activate_menubar (f
)
1124 if (!f
->output_data
.x
->saved_menu_event
->type
)
1127 set_frame_menubar (f
, 0, 1);
1129 XtDispatchEvent ((XEvent
*) f
->output_data
.x
->saved_menu_event
);
1132 if (f
->output_data
.x
->saved_menu_event
->type
== ButtonRelease
)
1133 pending_menu_activation
= 1;
1136 /* Ignore this if we get it a second time. */
1137 f
->output_data
.x
->saved_menu_event
->type
= 0;
1140 /* Detect if a dialog or menu has been posted. */
1145 return popup_activated_flag
;
1148 /* This callback is invoked when the user selects a menubar cascade
1149 pushbutton, but before the pulldown menu is posted. */
1152 popup_activate_callback (widget
, id
, client_data
)
1155 XtPointer client_data
;
1157 popup_activated_flag
= 1;
1160 /* This callback is invoked when a dialog or menu is finished being
1161 used and has been unposted. */
1164 popup_deactivate_callback (widget
, id
, client_data
)
1167 XtPointer client_data
;
1169 popup_activated_flag
= 0;
1172 /* Lwlib callback called when menu items are highlighted/unhighlighted
1173 while moving the mouse over them. WIDGET is the menu bar or menu
1174 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1175 the widget_value structure for the menu item, or null in case of
1179 menu_highlight_callback (widget
, id
, call_data
)
1184 widget_value
*wv
= (widget_value
*) call_data
;
1186 Lisp_Object frame
, help
;
1188 help
= wv
? wv
->help
: Qnil
;
1190 /* Determine the frame for the help event. */
1191 f
= menubar_id_to_frame (id
);
1194 XSETFRAME (frame
, f
);
1195 kbd_buffer_store_help_event (frame
, help
);
1199 /* WIDGET is the popup menu. It's parent is the frame's
1200 widget. See which frame that is. */
1201 Widget frame_widget
= XtParent (widget
);
1204 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
1206 frame
= XCAR (tail
);
1207 if (GC_FRAMEP (frame
)
1208 && (f
= XFRAME (frame
),
1209 FRAME_X_P (f
) && f
->output_data
.x
->widget
== frame_widget
))
1213 show_help_echo (help
, Qnil
, Qnil
, Qnil
, 1);
1217 /* This callback is called from the menu bar pulldown menu
1218 when the user makes a selection.
1219 Figure out what the user chose
1220 and put the appropriate events into the keyboard buffer. */
1223 menubar_selection_callback (widget
, id
, client_data
)
1226 XtPointer client_data
;
1228 Lisp_Object prefix
, entry
;
1229 FRAME_PTR f
= menubar_id_to_frame (id
);
1231 Lisp_Object
*subprefix_stack
;
1232 int submenu_depth
= 0;
1238 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* sizeof (Lisp_Object
));
1239 vector
= f
->menu_bar_vector
;
1242 while (i
< f
->menu_bar_items_used
)
1244 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1246 subprefix_stack
[submenu_depth
++] = prefix
;
1250 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1252 prefix
= subprefix_stack
[--submenu_depth
];
1255 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1257 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1258 i
+= MENU_ITEMS_PANE_LENGTH
;
1262 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1263 /* The EMACS_INT cast avoids a warning. There's no problem
1264 as long as pointers have enough bits to hold small integers. */
1265 if ((int) (EMACS_INT
) client_data
== i
)
1268 struct input_event buf
;
1271 XSETFRAME (frame
, f
);
1272 buf
.kind
= MENU_BAR_EVENT
;
1273 buf
.frame_or_window
= frame
;
1275 kbd_buffer_store_event (&buf
);
1277 for (j
= 0; j
< submenu_depth
; j
++)
1278 if (!NILP (subprefix_stack
[j
]))
1280 buf
.kind
= MENU_BAR_EVENT
;
1281 buf
.frame_or_window
= frame
;
1282 buf
.arg
= subprefix_stack
[j
];
1283 kbd_buffer_store_event (&buf
);
1288 buf
.kind
= MENU_BAR_EVENT
;
1289 buf
.frame_or_window
= frame
;
1291 kbd_buffer_store_event (&buf
);
1294 buf
.kind
= MENU_BAR_EVENT
;
1295 buf
.frame_or_window
= frame
;
1297 kbd_buffer_store_event (&buf
);
1301 i
+= MENU_ITEMS_ITEM_LENGTH
;
1306 /* Allocate a widget_value, blocking input. */
1309 xmalloc_widget_value ()
1311 widget_value
*value
;
1314 value
= malloc_widget_value ();
1320 /* This recursively calls free_widget_value on the tree of widgets.
1321 It must free all data that was malloc'ed for these widget_values.
1322 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1323 must be left alone. */
1326 free_menubar_widget_value_tree (wv
)
1331 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1333 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1335 free_menubar_widget_value_tree (wv
->contents
);
1336 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1340 free_menubar_widget_value_tree (wv
->next
);
1341 wv
->next
= (widget_value
*) 0xDEADBEEF;
1344 free_widget_value (wv
);
1348 /* Return a tree of widget_value structures for a menu bar item
1349 whose event type is ITEM_KEY (with string ITEM_NAME)
1350 and whose contents come from the list of keymaps MAPS. */
1352 static widget_value
*
1353 single_submenu (item_key
, item_name
, maps
)
1354 Lisp_Object item_key
, item_name
, maps
;
1356 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1358 int submenu_depth
= 0;
1361 Lisp_Object
*mapvec
;
1362 widget_value
**submenu_stack
;
1363 int previous_items
= menu_items_used
;
1364 int top_level_items
= 0;
1366 length
= Flength (maps
);
1367 len
= XINT (length
);
1369 /* Convert the list MAPS into a vector MAPVEC. */
1370 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1371 for (i
= 0; i
< len
; i
++)
1373 mapvec
[i
] = Fcar (maps
);
1377 menu_items_n_panes
= 0;
1379 /* Loop over the given keymaps, making a pane for each map.
1380 But don't make a pane that is empty--ignore that map instead. */
1381 for (i
= 0; i
< len
; i
++)
1383 if (SYMBOLP (mapvec
[i
])
1384 || (CONSP (mapvec
[i
]) && !KEYMAPP (mapvec
[i
])))
1386 /* Here we have a command at top level in the menu bar
1387 as opposed to a submenu. */
1388 top_level_items
= 1;
1389 push_menu_pane (Qnil
, Qnil
);
1390 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
],
1391 Qnil
, Qnil
, Qnil
, Qnil
);
1394 single_keymap_panes (mapvec
[i
], item_name
, item_key
, 0, 10);
1397 /* Create a tree of widget_value objects
1398 representing the panes and their items. */
1401 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1402 wv
= xmalloc_widget_value ();
1406 wv
->button_type
= BUTTON_TYPE_NONE
;
1412 /* Loop over all panes and items made during this call
1413 and construct a tree of widget_value objects.
1414 Ignore the panes and items made by previous calls to
1415 single_submenu, even though those are also in menu_items. */
1417 while (i
< menu_items_used
)
1419 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1421 submenu_stack
[submenu_depth
++] = save_wv
;
1426 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1429 save_wv
= submenu_stack
[--submenu_depth
];
1432 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1433 && submenu_depth
!= 0)
1434 i
+= MENU_ITEMS_PANE_LENGTH
;
1435 /* Ignore a nil in the item list.
1436 It's meaningful only for dialog boxes. */
1437 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1439 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1441 /* Create a new pane. */
1442 Lisp_Object pane_name
, prefix
;
1445 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1446 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1448 #ifndef HAVE_MULTILINGUAL_MENU
1449 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1451 pane_name
= ENCODE_SYSTEM (pane_name
);
1452 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
1455 pane_string
= (NILP (pane_name
)
1456 ? "" : (char *) XSTRING (pane_name
)->data
);
1457 /* If there is just one top-level pane, put all its items directly
1458 under the top-level menu. */
1459 if (menu_items_n_panes
== 1)
1462 /* If the pane has a meaningful name,
1463 make the pane a top-level menu item
1464 with its items as a submenu beneath it. */
1465 if (strcmp (pane_string
, ""))
1467 wv
= xmalloc_widget_value ();
1471 first_wv
->contents
= wv
;
1472 wv
->name
= pane_string
;
1473 /* Ignore the @ that means "separate pane".
1474 This is a kludge, but this isn't worth more time. */
1475 if (!NILP (prefix
) && wv
->name
[0] == '@')
1479 wv
->button_type
= BUTTON_TYPE_NONE
;
1484 i
+= MENU_ITEMS_PANE_LENGTH
;
1488 /* Create a new item within current pane. */
1489 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1492 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1493 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1494 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1495 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1496 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1497 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1498 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1500 #ifndef HAVE_MULTILINGUAL_MENU
1501 if (STRING_MULTIBYTE (item_name
))
1503 item_name
= ENCODE_SYSTEM (item_name
);
1504 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
1507 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1509 descrip
= ENCODE_SYSTEM (descrip
);
1510 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
1512 #endif /* not HAVE_MULTILINGUAL_MENU */
1514 wv
= xmalloc_widget_value ();
1518 save_wv
->contents
= wv
;
1520 wv
->name
= (char *) XSTRING (item_name
)->data
;
1521 if (!NILP (descrip
))
1522 wv
->key
= (char *) XSTRING (descrip
)->data
;
1524 /* The EMACS_INT cast avoids a warning. There's no problem
1525 as long as pointers have enough bits to hold small integers. */
1526 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1527 wv
->enabled
= !NILP (enable
);
1530 wv
->button_type
= BUTTON_TYPE_NONE
;
1531 else if (EQ (type
, QCradio
))
1532 wv
->button_type
= BUTTON_TYPE_RADIO
;
1533 else if (EQ (type
, QCtoggle
))
1534 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1538 wv
->selected
= !NILP (selected
);
1539 if (! STRINGP (help
))
1546 i
+= MENU_ITEMS_ITEM_LENGTH
;
1550 /* If we have just one "menu item"
1551 that was originally a button, return it by itself. */
1552 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
1554 wv
= first_wv
->contents
;
1555 free_widget_value (first_wv
);
1564 /* Recompute all the widgets of frame F, when the menu bar has been
1565 changed. Value is non-zero if widgets were updated. */
1568 update_frame_menubar (f
)
1571 struct x_output
*x
= f
->output_data
.x
;
1574 if (!x
->menubar_widget
|| XtIsManaged (x
->menubar_widget
))
1578 /* Save the size of the frame because the pane widget doesn't accept
1579 to resize itself. So force it. */
1583 /* Do the voodoo which means "I'm changing lots of things, don't try
1584 to refigure sizes until I'm done." */
1585 lw_refigure_widget (x
->column_widget
, False
);
1587 /* The order in which children are managed is the top to bottom
1588 order in which they are displayed in the paned window. First,
1589 remove the text-area widget. */
1590 XtUnmanageChild (x
->edit_widget
);
1592 /* Remove the menubar that is there now, and put up the menubar that
1594 XtManageChild (x
->menubar_widget
);
1595 XtMapWidget (x
->menubar_widget
);
1596 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, NULL
);
1598 /* Re-manage the text-area widget, and then thrash the sizes. */
1599 XtManageChild (x
->edit_widget
);
1600 lw_refigure_widget (x
->column_widget
, True
);
1602 /* Force the pane widget to resize itself with the right values. */
1603 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1608 /* Set the contents of the menubar widgets of frame F.
1609 The argument FIRST_TIME is currently ignored;
1610 it is set the first time this is called, from initialize_frame_menubar. */
1613 set_frame_menubar (f
, first_time
, deep_p
)
1618 Widget menubar_widget
= f
->output_data
.x
->menubar_widget
;
1620 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1625 XSETFRAME (Vmenu_updating_frame
, f
);
1627 if (f
->output_data
.x
->id
== 0)
1628 f
->output_data
.x
->id
= next_menubar_widget_id
++;
1629 id
= f
->output_data
.x
->id
;
1631 if (! menubar_widget
)
1633 else if (pending_menu_activation
&& !deep_p
)
1635 /* Make the first call for any given frame always go deep. */
1636 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
1639 f
->output_data
.x
->saved_menu_event
= (XEvent
*)xmalloc (sizeof (XEvent
));
1640 f
->output_data
.x
->saved_menu_event
->type
= 0;
1643 wv
= xmalloc_widget_value ();
1644 wv
->name
= "menubar";
1647 wv
->button_type
= BUTTON_TYPE_NONE
;
1652 /* Make a widget-value tree representing the entire menu trees. */
1654 struct buffer
*prev
= current_buffer
;
1656 int specpdl_count
= specpdl_ptr
- specpdl
;
1657 int previous_menu_items_used
= f
->menu_bar_items_used
;
1658 Lisp_Object
*previous_items
1659 = (Lisp_Object
*) alloca (previous_menu_items_used
1660 * sizeof (Lisp_Object
));
1662 /* If we are making a new widget, its contents are empty,
1663 do always reinitialize them. */
1664 if (! menubar_widget
)
1665 previous_menu_items_used
= 0;
1667 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
1668 specbind (Qinhibit_quit
, Qt
);
1669 /* Don't let the debugger step into this code
1670 because it is not reentrant. */
1671 specbind (Qdebug_on_next_call
, Qnil
);
1673 record_unwind_protect (Fset_match_data
, Fmatch_data (Qnil
, Qnil
));
1674 if (NILP (Voverriding_local_map_menu_flag
))
1676 specbind (Qoverriding_terminal_local_map
, Qnil
);
1677 specbind (Qoverriding_local_map
, Qnil
);
1680 set_buffer_internal_1 (XBUFFER (buffer
));
1682 /* Run the Lucid hook. */
1683 safe_run_hooks (Qactivate_menubar_hook
);
1685 /* If it has changed current-menubar from previous value,
1686 really recompute the menubar from the value. */
1687 if (! NILP (Vlucid_menu_bar_dirty_flag
))
1688 call0 (Qrecompute_lucid_menubar
);
1689 safe_run_hooks (Qmenu_bar_update_hook
);
1690 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1692 items
= FRAME_MENU_BAR_ITEMS (f
);
1694 inhibit_garbage_collection ();
1696 /* Save the frame's previous menu bar contents data. */
1697 if (previous_menu_items_used
)
1698 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
1699 previous_menu_items_used
* sizeof (Lisp_Object
));
1701 /* Fill in the current menu bar contents. */
1702 menu_items
= f
->menu_bar_vector
;
1703 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
1705 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1707 Lisp_Object key
, string
, maps
;
1709 key
= XVECTOR (items
)->contents
[i
];
1710 string
= XVECTOR (items
)->contents
[i
+ 1];
1711 maps
= XVECTOR (items
)->contents
[i
+ 2];
1715 wv
= single_submenu (key
, string
, maps
);
1719 first_wv
->contents
= wv
;
1720 /* Don't set wv->name here; GC during the loop might relocate it. */
1722 wv
->button_type
= BUTTON_TYPE_NONE
;
1726 finish_menu_items ();
1728 set_buffer_internal_1 (prev
);
1729 unbind_to (specpdl_count
, Qnil
);
1731 /* If there has been no change in the Lisp-level contents
1732 of the menu bar, skip redisplaying it. Just exit. */
1734 for (i
= 0; i
< previous_menu_items_used
; i
++)
1735 if (menu_items_used
== i
1736 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
1738 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
1740 free_menubar_widget_value_tree (first_wv
);
1746 /* Now GC cannot happen during the lifetime of the widget_value,
1747 so it's safe to store data from a Lisp_String. */
1748 wv
= first_wv
->contents
;
1749 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1752 string
= XVECTOR (items
)->contents
[i
+ 1];
1755 wv
->name
= (char *) XSTRING (string
)->data
;
1759 f
->menu_bar_vector
= menu_items
;
1760 f
->menu_bar_items_used
= menu_items_used
;
1765 /* Make a widget-value tree containing
1766 just the top level menu bar strings. */
1768 items
= FRAME_MENU_BAR_ITEMS (f
);
1769 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1773 string
= XVECTOR (items
)->contents
[i
+ 1];
1777 wv
= xmalloc_widget_value ();
1778 wv
->name
= (char *) XSTRING (string
)->data
;
1781 wv
->button_type
= BUTTON_TYPE_NONE
;
1782 /* This prevents lwlib from assuming this
1783 menu item is really supposed to be empty. */
1784 /* The EMACS_INT cast avoids a warning.
1785 This value just has to be different from small integers. */
1786 wv
->call_data
= (void *) (EMACS_INT
) (-1);
1791 first_wv
->contents
= wv
;
1795 /* Forget what we thought we knew about what is in the
1796 detailed contents of the menu bar menus.
1797 Changing the top level always destroys the contents. */
1798 f
->menu_bar_items_used
= 0;
1801 /* Create or update the menu bar widget. */
1807 /* Disable resizing (done for Motif!) */
1808 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
1810 /* The third arg is DEEP_P, which says to consider the entire
1811 menu trees we supply, rather than just the menu bar item names. */
1812 lw_modify_all_widgets (id
, first_wv
, deep_p
);
1814 /* Re-enable the edit widget to resize. */
1815 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
1819 menubar_widget
= lw_create_widget ("menubar", "menubar", id
, first_wv
,
1820 f
->output_data
.x
->column_widget
,
1822 popup_activate_callback
,
1823 menubar_selection_callback
,
1824 popup_deactivate_callback
,
1825 menu_highlight_callback
);
1826 f
->output_data
.x
->menubar_widget
= menubar_widget
;
1831 = (f
->output_data
.x
->menubar_widget
1832 ? (f
->output_data
.x
->menubar_widget
->core
.height
1833 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
1836 #if 0 /* Experimentally, we now get the right results
1837 for -geometry -0-0 without this. 24 Aug 96, rms. */
1839 if (FRAME_EXTERNAL_MENU_BAR (f
))
1842 XtVaGetValues (f
->output_data
.x
->column_widget
,
1843 XtNinternalBorderWidth
, &ibw
, NULL
);
1844 menubar_size
+= ibw
;
1846 #endif /* USE_LUCID */
1849 f
->output_data
.x
->menubar_height
= menubar_size
;
1852 free_menubar_widget_value_tree (first_wv
);
1853 update_frame_menubar (f
);
1858 /* Called from Fx_create_frame to create the initial menubar of a frame
1859 before it is mapped, so that the window is mapped with the menubar already
1860 there instead of us tacking it on later and thrashing the window after it
1864 initialize_frame_menubar (f
)
1867 /* This function is called before the first chance to redisplay
1868 the frame. It has to be, so the frame will have the right size. */
1869 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1870 set_frame_menubar (f
, 1, 1);
1874 /* Get rid of the menu bar of frame F, and free its storage.
1875 This is used when deleting a frame, and when turning off the menu bar. */
1878 free_frame_menubar (f
)
1881 Widget menubar_widget
;
1883 menubar_widget
= f
->output_data
.x
->menubar_widget
;
1885 f
->output_data
.x
->menubar_height
= 0;
1890 /* Removing the menu bar magically changes the shell widget's x
1891 and y position of (0, 0) which, when the menu bar is turned
1892 on again, leads to pull-down menuss appearing in strange
1893 positions near the upper-left corner of the display. This
1894 happens only with some window managers like twm and ctwm,
1895 but not with other like Motif's mwm or kwm, because the
1896 latter generate ConfigureNotify events when the menu bar
1897 is switched off, which fixes the shell position. */
1898 Position x0
, y0
, x1
, y1
;
1904 if (f
->output_data
.x
->widget
)
1905 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x0
, XtNy
, &y0
, NULL
);
1908 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
1909 f
->output_data
.x
->menubar_widget
= NULL
;
1912 if (f
->output_data
.x
->widget
)
1914 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x1
, XtNy
, &y1
, NULL
);
1915 if (x1
== 0 && y1
== 0)
1916 XtVaSetValues (f
->output_data
.x
->widget
, XtNx
, x0
, XtNy
, y0
, NULL
);
1924 #endif /* USE_X_TOOLKIT */
1926 /* xmenu_show actually displays a menu using the panes and items in menu_items
1927 and returns the value selected from it.
1928 There are two versions of xmenu_show, one for Xt and one for Xlib.
1929 Both assume input is blocked by the caller. */
1931 /* F is the frame the menu is for.
1932 X and Y are the frame-relative specified position,
1933 relative to the inside upper left corner of the frame F.
1934 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1935 KEYMAPS is 1 if this menu was specified with keymaps;
1936 in that case, we return a list containing the chosen item's value
1937 and perhaps also the pane's prefix.
1938 TITLE is the specified menu title.
1939 ERROR is a place to store an error message string in case of failure.
1940 (We return nil on failure, but the value doesn't actually matter.) */
1942 #ifdef USE_X_TOOLKIT
1944 /* We need a unique id for each widget handled by the Lucid Widget
1947 For the main windows, and popup menus, we use this counter,
1948 which we increment each time after use. This starts from 1<<16.
1950 For menu bars, we use numbers starting at 0, counted in
1951 next_menubar_widget_id. */
1952 LWLIB_ID widget_id_tick
;
1954 static Lisp_Object
*volatile menu_item_selection
;
1957 popup_selection_callback (widget
, id
, client_data
)
1960 XtPointer client_data
;
1962 menu_item_selection
= (Lisp_Object
*) client_data
;
1966 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
1980 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1981 widget_value
**submenu_stack
1982 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1983 Lisp_Object
*subprefix_stack
1984 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1985 int submenu_depth
= 0;
1986 XButtonPressedEvent dummy
;
1992 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1994 *error
= "Empty menu";
1998 /* Create a tree of widget_value objects
1999 representing the panes and their items. */
2000 wv
= xmalloc_widget_value ();
2004 wv
->button_type
= BUTTON_TYPE_NONE
;
2008 /* Loop over all panes and items, filling in the tree. */
2010 while (i
< menu_items_used
)
2012 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2014 submenu_stack
[submenu_depth
++] = save_wv
;
2020 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2023 save_wv
= submenu_stack
[--submenu_depth
];
2027 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
2028 && submenu_depth
!= 0)
2029 i
+= MENU_ITEMS_PANE_LENGTH
;
2030 /* Ignore a nil in the item list.
2031 It's meaningful only for dialog boxes. */
2032 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2034 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2036 /* Create a new pane. */
2037 Lisp_Object pane_name
, prefix
;
2040 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
2041 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2043 #ifndef HAVE_MULTILINGUAL_MENU
2044 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
2046 pane_name
= ENCODE_SYSTEM (pane_name
);
2047 AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
) = pane_name
;
2050 pane_string
= (NILP (pane_name
)
2051 ? "" : (char *) XSTRING (pane_name
)->data
);
2052 /* If there is just one top-level pane, put all its items directly
2053 under the top-level menu. */
2054 if (menu_items_n_panes
== 1)
2057 /* If the pane has a meaningful name,
2058 make the pane a top-level menu item
2059 with its items as a submenu beneath it. */
2060 if (!keymaps
&& strcmp (pane_string
, ""))
2062 wv
= xmalloc_widget_value ();
2066 first_wv
->contents
= wv
;
2067 wv
->name
= pane_string
;
2068 if (keymaps
&& !NILP (prefix
))
2072 wv
->button_type
= BUTTON_TYPE_NONE
;
2076 else if (first_pane
)
2082 i
+= MENU_ITEMS_PANE_LENGTH
;
2086 /* Create a new item within current pane. */
2087 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
2088 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
2089 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
2090 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
2091 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
2092 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
2093 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
2094 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
2096 #ifndef HAVE_MULTILINGUAL_MENU
2097 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
2099 item_name
= ENCODE_SYSTEM (item_name
);
2100 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
) = item_name
;
2103 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
2105 descrip
= ENCODE_SYSTEM (descrip
);
2106 AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
) = descrip
;
2108 #endif /* not HAVE_MULTILINGUAL_MENU */
2110 wv
= xmalloc_widget_value ();
2114 save_wv
->contents
= wv
;
2115 wv
->name
= (char *) XSTRING (item_name
)->data
;
2116 if (!NILP (descrip
))
2117 wv
->key
= (char *) XSTRING (descrip
)->data
;
2119 /* If this item has a null value,
2120 make the call_data null so that it won't display a box
2121 when the mouse is on it. */
2123 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
2124 wv
->enabled
= !NILP (enable
);
2127 wv
->button_type
= BUTTON_TYPE_NONE
;
2128 else if (EQ (type
, QCtoggle
))
2129 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
2130 else if (EQ (type
, QCradio
))
2131 wv
->button_type
= BUTTON_TYPE_RADIO
;
2135 wv
->selected
= !NILP (selected
);
2137 if (! STRINGP (help
))
2144 i
+= MENU_ITEMS_ITEM_LENGTH
;
2148 /* Deal with the title, if it is non-nil. */
2151 widget_value
*wv_title
= xmalloc_widget_value ();
2152 widget_value
*wv_sep1
= xmalloc_widget_value ();
2153 widget_value
*wv_sep2
= xmalloc_widget_value ();
2155 wv_sep2
->name
= "--";
2156 wv_sep2
->next
= first_wv
->contents
;
2158 wv_sep1
->name
= "--";
2159 wv_sep1
->next
= wv_sep2
;
2161 #ifndef HAVE_MULTILINGUAL_MENU
2162 if (STRING_MULTIBYTE (title
))
2163 title
= ENCODE_SYSTEM (title
);
2166 wv_title
->name
= (char *) XSTRING (title
)->data
;
2167 wv_title
->enabled
= TRUE
;
2168 wv_title
->button_type
= BUTTON_TYPE_NONE
;
2169 wv_title
->next
= wv_sep1
;
2170 first_wv
->contents
= wv_title
;
2173 /* Actually create the menu. */
2174 menu_id
= widget_id_tick
++;
2175 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
2176 f
->output_data
.x
->widget
, 1, 0,
2177 popup_selection_callback
,
2178 popup_deactivate_callback
,
2179 menu_highlight_callback
);
2181 /* Adjust coordinates to relative to the outer (window manager) window. */
2184 int win_x
= 0, win_y
= 0;
2186 /* Find the position of the outside upper-left corner of
2187 the inner window, with respect to the outer window. */
2188 if (f
->output_data
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2191 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2193 /* From-window, to-window. */
2194 f
->output_data
.x
->window_desc
,
2195 f
->output_data
.x
->parent_desc
,
2197 /* From-position, to-position. */
2198 0, 0, &win_x
, &win_y
,
2200 /* Child of window. */
2208 /* Adjust coordinates to be root-window-relative. */
2209 x
+= f
->output_data
.x
->left_pos
;
2210 y
+= f
->output_data
.x
->top_pos
;
2212 dummy
.type
= ButtonPress
;
2214 dummy
.send_event
= 0;
2215 dummy
.display
= FRAME_X_DISPLAY (f
);
2216 dummy
.time
= CurrentTime
;
2217 dummy
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
2218 dummy
.window
= dummy
.root
;
2219 dummy
.subwindow
= dummy
.root
;
2224 dummy
.state
= (FRAME_X_DISPLAY_INFO (f
)->grabbed
>> 1) * Button1Mask
;
2226 for (i
= 0; i
< 5; i
++)
2227 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2230 /* Don't allow any geometry request from the user. */
2231 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
2232 XtSetValues (menu
, av
, ac
);
2234 /* Free the widget_value objects we used to specify the contents. */
2235 free_menubar_widget_value_tree (first_wv
);
2237 /* No selection has been chosen yet. */
2238 menu_item_selection
= 0;
2240 /* Display the menu. */
2241 lw_popup_menu (menu
, (XEvent
*) &dummy
);
2242 popup_activated_flag
= 1;
2244 /* Process events that apply to the menu. */
2245 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
);
2247 /* fp turned off the following statement and wrote a comment
2248 that it is unnecessary--that the menu has already disappeared.
2249 Nowadays the menu disappears ok, all right, but
2250 we need to delete the widgets or multiple ones will pile up. */
2251 lw_destroy_all_widgets (menu_id
);
2253 /* Find the selected item, and its pane, to return
2254 the proper value. */
2255 if (menu_item_selection
!= 0)
2257 Lisp_Object prefix
, entry
;
2259 prefix
= entry
= Qnil
;
2261 while (i
< menu_items_used
)
2263 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2265 subprefix_stack
[submenu_depth
++] = prefix
;
2269 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2271 prefix
= subprefix_stack
[--submenu_depth
];
2274 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2277 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2278 i
+= MENU_ITEMS_PANE_LENGTH
;
2280 /* Ignore a nil in the item list.
2281 It's meaningful only for dialog boxes. */
2282 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2287 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2288 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2294 entry
= Fcons (entry
, Qnil
);
2296 entry
= Fcons (prefix
, entry
);
2297 for (j
= submenu_depth
- 1; j
>= 0; j
--)
2298 if (!NILP (subprefix_stack
[j
]))
2299 entry
= Fcons (subprefix_stack
[j
], entry
);
2303 i
+= MENU_ITEMS_ITEM_LENGTH
;
2312 dialog_selection_callback (widget
, id
, client_data
)
2315 XtPointer client_data
;
2317 /* The EMACS_INT cast avoids a warning. There's no problem
2318 as long as pointers have enough bits to hold small integers. */
2319 if ((int) (EMACS_INT
) client_data
!= -1)
2320 menu_item_selection
= (Lisp_Object
*) client_data
;
2322 lw_destroy_all_widgets (id
);
2324 popup_activated_flag
= 0;
2327 static char * button_names
[] = {
2328 "button1", "button2", "button3", "button4", "button5",
2329 "button6", "button7", "button8", "button9", "button10" };
2332 xdialog_show (f
, keymaps
, title
, error
)
2338 int i
, nb_buttons
=0;
2341 char dialog_name
[6];
2343 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
2345 /* Number of elements seen so far, before boundary. */
2347 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2348 int boundary_seen
= 0;
2352 if (menu_items_n_panes
> 1)
2354 *error
= "Multiple panes in dialog box";
2358 /* Create a tree of widget_value objects
2359 representing the text label and buttons. */
2361 Lisp_Object pane_name
, prefix
;
2363 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
2364 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
2365 pane_string
= (NILP (pane_name
)
2366 ? "" : (char *) XSTRING (pane_name
)->data
);
2367 prev_wv
= xmalloc_widget_value ();
2368 prev_wv
->value
= pane_string
;
2369 if (keymaps
&& !NILP (prefix
))
2371 prev_wv
->enabled
= 1;
2372 prev_wv
->name
= "message";
2375 /* Loop over all panes and items, filling in the tree. */
2376 i
= MENU_ITEMS_PANE_LENGTH
;
2377 while (i
< menu_items_used
)
2380 /* Create a new item within current pane. */
2381 Lisp_Object item_name
, enable
, descrip
;
2382 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2383 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2385 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2387 if (NILP (item_name
))
2389 free_menubar_widget_value_tree (first_wv
);
2390 *error
= "Submenu in dialog items";
2393 if (EQ (item_name
, Qquote
))
2395 /* This is the boundary between left-side elts
2396 and right-side elts. Stop incrementing right_count. */
2401 if (nb_buttons
>= 9)
2403 free_menubar_widget_value_tree (first_wv
);
2404 *error
= "Too many dialog items";
2408 wv
= xmalloc_widget_value ();
2410 wv
->name
= (char *) button_names
[nb_buttons
];
2411 if (!NILP (descrip
))
2412 wv
->key
= (char *) XSTRING (descrip
)->data
;
2413 wv
->value
= (char *) XSTRING (item_name
)->data
;
2414 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
2415 wv
->enabled
= !NILP (enable
);
2418 if (! boundary_seen
)
2422 i
+= MENU_ITEMS_ITEM_LENGTH
;
2425 /* If the boundary was not specified,
2426 by default put half on the left and half on the right. */
2427 if (! boundary_seen
)
2428 left_count
= nb_buttons
- nb_buttons
/ 2;
2430 wv
= xmalloc_widget_value ();
2431 wv
->name
= dialog_name
;
2433 /* Dialog boxes use a really stupid name encoding
2434 which specifies how many buttons to use
2435 and how many buttons are on the right.
2436 The Q means something also. */
2437 dialog_name
[0] = 'Q';
2438 dialog_name
[1] = '0' + nb_buttons
;
2439 dialog_name
[2] = 'B';
2440 dialog_name
[3] = 'R';
2441 /* Number of buttons to put on the right. */
2442 dialog_name
[4] = '0' + nb_buttons
- left_count
;
2444 wv
->contents
= first_wv
;
2448 /* Actually create the dialog. */
2449 dialog_id
= widget_id_tick
++;
2450 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
2451 f
->output_data
.x
->widget
, 1, 0,
2452 dialog_selection_callback
, 0, 0);
2453 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
2454 /* Free the widget_value objects we used to specify the contents. */
2455 free_menubar_widget_value_tree (first_wv
);
2457 /* No selection has been chosen yet. */
2458 menu_item_selection
= 0;
2460 /* Display the menu. */
2461 lw_pop_up_all_widgets (dialog_id
);
2462 popup_activated_flag
= 1;
2464 /* Process events that apply to the menu. */
2465 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), dialog_id
);
2467 lw_destroy_all_widgets (dialog_id
);
2469 /* Find the selected item, and its pane, to return
2470 the proper value. */
2471 if (menu_item_selection
!= 0)
2477 while (i
< menu_items_used
)
2481 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2484 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2485 i
+= MENU_ITEMS_PANE_LENGTH
;
2487 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2489 /* This is the boundary between left-side elts and
2496 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2497 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2501 entry
= Fcons (entry
, Qnil
);
2503 entry
= Fcons (prefix
, entry
);
2507 i
+= MENU_ITEMS_ITEM_LENGTH
;
2515 #else /* not USE_X_TOOLKIT */
2517 /* The frame of the last activated non-toolkit menu bar.
2518 Used to generate menu help events. */
2520 static struct frame
*menu_help_frame
;
2523 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2525 PANE is the pane number, and ITEM is the menu item number in
2526 the menu (currently not used).
2528 This cannot be done with generating a HELP_EVENT because
2529 XMenuActivate contains a loop that doesn't let Emacs process
2533 menu_help_callback (help_string
, pane
, item
)
2537 extern Lisp_Object Qmenu_item
;
2538 Lisp_Object
*first_item
;
2539 Lisp_Object pane_name
;
2540 Lisp_Object menu_object
;
2542 first_item
= XVECTOR (menu_items
)->contents
;
2543 if (EQ (first_item
[0], Qt
))
2544 pane_name
= first_item
[MENU_ITEMS_PANE_NAME
];
2545 else if (EQ (first_item
[0], Qquote
))
2546 /* This shouldn't happen, see xmenu_show. */
2547 pane_name
= empty_string
;
2549 pane_name
= first_item
[MENU_ITEMS_ITEM_NAME
];
2551 /* (menu-item MENU-NAME PANE-NUMBER) */
2552 menu_object
= Fcons (Qmenu_item
,
2554 Fcons (make_number (pane
), Qnil
)));
2555 show_help_echo (help_string
? build_string (help_string
) : Qnil
,
2556 Qnil
, menu_object
, make_number (item
), 1);
2561 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
2571 int pane
, selidx
, lpane
, status
;
2572 Lisp_Object entry
, pane_prefix
;
2574 int ulx
, uly
, width
, height
;
2575 int dispwidth
, dispheight
;
2579 unsigned int dummy_uint
;
2582 if (menu_items_n_panes
== 0)
2585 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2587 *error
= "Empty menu";
2591 /* Figure out which root window F is on. */
2592 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2593 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2594 &dummy_uint
, &dummy_uint
);
2596 /* Make the menu on that window. */
2597 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2600 *error
= "Can't create menu";
2604 #ifdef HAVE_X_WINDOWS
2605 /* Adjust coordinates to relative to the outer (window manager) window. */
2608 int win_x
= 0, win_y
= 0;
2610 /* Find the position of the outside upper-left corner of
2611 the inner window, with respect to the outer window. */
2612 if (f
->output_data
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2615 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2617 /* From-window, to-window. */
2618 f
->output_data
.x
->window_desc
,
2619 f
->output_data
.x
->parent_desc
,
2621 /* From-position, to-position. */
2622 0, 0, &win_x
, &win_y
,
2624 /* Child of window. */
2631 #endif /* HAVE_X_WINDOWS */
2633 /* Adjust coordinates to be root-window-relative. */
2634 x
+= f
->output_data
.x
->left_pos
;
2635 y
+= f
->output_data
.x
->top_pos
;
2637 /* Create all the necessary panes and their items. */
2639 while (i
< menu_items_used
)
2641 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2643 /* Create a new pane. */
2644 Lisp_Object pane_name
, prefix
;
2647 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2648 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2649 pane_string
= (NILP (pane_name
)
2650 ? "" : (char *) XSTRING (pane_name
)->data
);
2651 if (keymaps
&& !NILP (prefix
))
2654 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
2655 if (lpane
== XM_FAILURE
)
2657 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2658 *error
= "Can't create pane";
2661 i
+= MENU_ITEMS_PANE_LENGTH
;
2663 /* Find the width of the widest item in this pane. */
2666 while (j
< menu_items_used
)
2669 item
= XVECTOR (menu_items
)->contents
[j
];
2677 width
= STRING_BYTES (XSTRING (item
));
2678 if (width
> maxwidth
)
2681 j
+= MENU_ITEMS_ITEM_LENGTH
;
2684 /* Ignore a nil in the item list.
2685 It's meaningful only for dialog boxes. */
2686 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2690 /* Create a new item within current pane. */
2691 Lisp_Object item_name
, enable
, descrip
, help
;
2692 unsigned char *item_data
;
2695 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2696 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2698 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2699 help
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_HELP
];
2700 help_string
= STRINGP (help
) ? XSTRING (help
)->data
: NULL
;
2702 if (!NILP (descrip
))
2704 int gap
= maxwidth
- STRING_BYTES (XSTRING (item_name
));
2707 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2708 item_name
= concat2 (item_name
, spacer
);
2709 item_name
= concat2 (item_name
, descrip
);
2710 item_data
= XSTRING (item_name
)->data
;
2712 /* if alloca is fast, use that to make the space,
2713 to reduce gc needs. */
2715 = (unsigned char *) alloca (maxwidth
2716 + STRING_BYTES (XSTRING (descrip
)) + 1);
2717 bcopy (XSTRING (item_name
)->data
, item_data
,
2718 STRING_BYTES (XSTRING (item_name
)));
2719 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2721 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2722 STRING_BYTES (XSTRING (descrip
)));
2723 item_data
[j
+ STRING_BYTES (XSTRING (descrip
))] = 0;
2727 item_data
= XSTRING (item_name
)->data
;
2729 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2730 menu
, lpane
, 0, item_data
,
2731 !NILP (enable
), help_string
)
2734 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2735 *error
= "Can't add selection to menu";
2738 i
+= MENU_ITEMS_ITEM_LENGTH
;
2742 /* All set and ready to fly. */
2743 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2744 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
2745 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
2746 x
= min (x
, dispwidth
);
2747 y
= min (y
, dispheight
);
2750 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2751 &ulx
, &uly
, &width
, &height
);
2752 if (ulx
+width
> dispwidth
)
2754 x
-= (ulx
+ width
) - dispwidth
;
2755 ulx
= dispwidth
- width
;
2757 if (uly
+height
> dispheight
)
2759 y
-= (uly
+ height
) - dispheight
;
2760 uly
= dispheight
- height
;
2762 if (ulx
< 0) x
-= ulx
;
2763 if (uly
< 0) y
-= uly
;
2765 XMenuSetAEQ (menu
, TRUE
);
2766 XMenuSetFreeze (menu
, TRUE
);
2769 /* Help display under X won't work because XMenuActivate contains
2770 a loop that doesn't give Emacs a chance to process it. */
2771 menu_help_frame
= f
;
2772 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2773 x
, y
, ButtonReleaseMask
, &datap
,
2774 menu_help_callback
);
2777 #ifdef HAVE_X_WINDOWS
2778 /* Assume the mouse has moved out of the X window.
2779 If it has actually moved in, we will get an EnterNotify. */
2780 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
2787 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2790 /* Find the item number SELIDX in pane number PANE. */
2792 while (i
< menu_items_used
)
2794 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2798 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2800 i
+= MENU_ITEMS_PANE_LENGTH
;
2809 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2812 entry
= Fcons (entry
, Qnil
);
2813 if (!NILP (pane_prefix
))
2814 entry
= Fcons (pane_prefix
, entry
);
2820 i
+= MENU_ITEMS_ITEM_LENGTH
;
2826 *error
= "Can't activate menu";
2832 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2834 #ifdef HAVE_X_WINDOWS
2835 /* State that no mouse buttons are now held.
2836 (The oldXMenu code doesn't track this info for us.)
2837 That is not necessarily true, but the fiction leads to reasonable
2838 results, and it is a pain to ask which are actually held now. */
2839 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2845 #endif /* not USE_X_TOOLKIT */
2847 #endif /* HAVE_MENUS */
2852 staticpro (&menu_items
);
2855 Qdebug_on_next_call
= intern ("debug-on-next-call");
2856 staticpro (&Qdebug_on_next_call
);
2858 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame
,
2859 doc
: /* Frame for which we are updating a menu.
2860 The enable predicate for a menu command should check this variable. */);
2861 Vmenu_updating_frame
= Qnil
;
2863 #ifdef USE_X_TOOLKIT
2864 widget_id_tick
= (1<<16);
2865 next_menubar_widget_id
= 1;
2868 defsubr (&Sx_popup_menu
);
2870 defsubr (&Sx_popup_dialog
);