1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
32 /* On 4.3 this loses if it comes after xterm.h. */
38 #include "termhooks.h"
42 #include "blockinput.h"
46 /* This may include sys/types.h, and that somehow loses
47 if this is not done before the other system files. */
51 /* Load sys/types.h if not already loaded.
52 In some systems loading it twice is suicidal. */
54 #include <sys/types.h>
57 #include "dispextern.h"
60 #include "../oldXMenu/XMenu.h"
65 #include <X11/IntrinsicP.h>
66 #include <X11/CoreP.h>
67 #include <X11/StringDefs.h>
68 #include <X11/Shell.h>
69 #include "../lwlib/lwlib.h"
70 #endif /* USE_X_TOOLKIT */
72 #define min(x,y) (((x) < (y)) ? (x) : (y))
73 #define max(x,y) (((x) > (y)) ? (x) : (y))
80 extern Lisp_Object Qmenu_enable
;
81 extern Lisp_Object Qmenu_bar
;
82 extern Lisp_Object Qmouse_click
, Qevent_kind
;
85 extern void process_expose_from_menu ();
86 extern XtAppContext Xt_app_con
;
88 static Lisp_Object
xdialog_show ();
89 void popup_get_selection ();
92 static Lisp_Object
xmenu_show ();
93 static void keymap_panes ();
94 static void single_keymap_panes ();
95 static void list_of_panes ();
96 static void list_of_items ();
98 /* This holds a Lisp vector that holds the results of decoding
99 the keymaps or alist-of-alists that specify a menu.
101 It describes the panes and items within the panes.
103 Each pane is described by 3 elements in the vector:
104 t, the pane name, the pane's prefix key.
105 Then follow the pane's items, with 4 elements per item:
106 the item string, the enable flag, the item's value,
107 and the equivalent keyboard key's description string.
109 In some cases, multiple levels of menus may be described.
110 A single vector slot containing nil indicates the start of a submenu.
111 A single vector slot containing lambda indicates the end of a submenu.
112 The submenu follows a menu item which is the way to reach the submenu.
114 A single vector slot containing quote indicates that the
115 following items should appear on the right of a dialog box.
117 Using a Lisp vector to hold this information while we decode it
118 takes care of protecting all the data from GC. */
120 #define MENU_ITEMS_PANE_NAME 1
121 #define MENU_ITEMS_PANE_PREFIX 2
122 #define MENU_ITEMS_PANE_LENGTH 3
124 #define MENU_ITEMS_ITEM_NAME 0
125 #define MENU_ITEMS_ITEM_ENABLE 1
126 #define MENU_ITEMS_ITEM_VALUE 2
127 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
128 #define MENU_ITEMS_ITEM_LENGTH 4
130 static Lisp_Object menu_items
;
132 /* Number of slots currently allocated in menu_items. */
133 static int menu_items_allocated
;
135 /* This is the index in menu_items of the first empty slot. */
136 static int menu_items_used
;
138 /* The number of panes currently recorded in menu_items,
139 excluding those within submenus. */
140 static int menu_items_n_panes
;
142 /* Current depth within submenus. */
143 static int menu_items_submenu_depth
;
145 /* Flag which when set indicates a dialog or menu has been posted by
146 Xt on behalf of one of the widget sets. */
147 static int popup_activated_flag
;
150 /* Initialize the menu_items structure if we haven't already done so.
151 Also mark it as currently empty. */
156 if (NILP (menu_items
))
158 menu_items_allocated
= 60;
159 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
163 menu_items_n_panes
= 0;
164 menu_items_submenu_depth
= 0;
167 /* Call at the end of generating the data in menu_items.
168 This fills in the number of items in the last pane. */
175 /* Call when finished using the data for the current menu
179 discard_menu_items ()
181 /* Free the structure if it is especially large.
182 Otherwise, hold on to it, to save time. */
183 if (menu_items_allocated
> 200)
186 menu_items_allocated
= 0;
190 /* Make the menu_items vector twice as large. */
196 int old_size
= menu_items_allocated
;
199 menu_items_allocated
*= 2;
200 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
201 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
202 old_size
* sizeof (Lisp_Object
));
205 /* Begin a submenu. */
208 push_submenu_start ()
210 if (menu_items_used
+ 1 > menu_items_allocated
)
213 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
214 menu_items_submenu_depth
++;
222 if (menu_items_used
+ 1 > menu_items_allocated
)
225 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
226 menu_items_submenu_depth
--;
229 /* Indicate boundary between left and right. */
232 push_left_right_boundary ()
234 if (menu_items_used
+ 1 > menu_items_allocated
)
237 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
240 /* Start a new menu pane in menu_items..
241 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
244 push_menu_pane (name
, prefix_vec
)
245 Lisp_Object name
, prefix_vec
;
247 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
250 if (menu_items_submenu_depth
== 0)
251 menu_items_n_panes
++;
252 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
253 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
254 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
257 /* Push one menu item into the current pane.
258 NAME is the string to display. ENABLE if non-nil means
259 this item can be selected. KEY is the key generated by
260 choosing this item. EQUIV is the textual description
261 of the keyboard equivalent for this item (or nil if none). */
264 push_menu_item (name
, enable
, key
, equiv
)
265 Lisp_Object name
, enable
, key
, equiv
;
267 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
270 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
271 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
272 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
273 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
276 /* Figure out the current keyboard equivalent of a menu item ITEM1.
277 The item string for menu display should be ITEM_STRING.
278 Store the equivalent keyboard key sequence's
279 textual description into *DESCRIP_PTR.
280 Also cache them in the item itself.
281 Return the real definition to execute. */
284 menu_item_equiv_key (item_string
, item1
, descrip_ptr
)
285 Lisp_Object item_string
;
287 Lisp_Object
*descrip_ptr
;
289 /* This is the real definition--the function to run. */
291 /* This is the sublist that records cached equiv key data
292 so we can save time. */
293 Lisp_Object cachelist
;
294 /* These are the saved equivalent keyboard key sequence
295 and its key-description. */
296 Lisp_Object savedkey
, descrip
;
299 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
301 /* If a help string follows the item string, skip it. */
302 if (CONSP (XCONS (item1
)->cdr
)
303 && STRINGP (XCONS (XCONS (item1
)->cdr
)->car
))
304 item1
= XCONS (item1
)->cdr
;
308 /* Get out the saved equivalent-keyboard-key info. */
309 cachelist
= savedkey
= descrip
= Qnil
;
310 if (CONSP (def
) && CONSP (XCONS (def
)->car
)
311 && (NILP (XCONS (XCONS (def
)->car
)->car
)
312 || VECTORP (XCONS (XCONS (def
)->car
)->car
)))
314 cachelist
= XCONS (def
)->car
;
315 def
= XCONS (def
)->cdr
;
316 savedkey
= XCONS (cachelist
)->car
;
317 descrip
= XCONS (cachelist
)->cdr
;
320 GCPRO4 (def
, def1
, savedkey
, descrip
);
322 /* Is it still valid? */
324 if (!NILP (savedkey
))
325 def1
= Fkey_binding (savedkey
, Qnil
);
326 /* If not, update it. */
328 /* If the command is an alias for another
329 (such as easymenu.el and lmenu.el set it up),
330 check if the original command matches the cached command. */
331 && !(SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
)
332 && EQ (def1
, XSYMBOL (def
)->function
))
333 /* If something had no key binding before, don't recheck it--
334 doing that takes too much time and makes menus too slow. */
335 && !(!NILP (cachelist
) && NILP (savedkey
)))
339 savedkey
= Fwhere_is_internal (def
, Qnil
, Qt
, Qnil
);
340 /* If the command is an alias for another
341 (such as easymenu.el and lmenu.el set it up),
342 see if the original command name has equivalent keys. */
343 if (SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
))
344 savedkey
= Fwhere_is_internal (XSYMBOL (def
)->function
,
347 if (VECTORP (savedkey
)
348 && EQ (XVECTOR (savedkey
)->contents
[0], Qmenu_bar
))
350 /* Reject two-key sequences that start with a mouse click.
351 These are probably menu items. */
352 if (VECTORP (savedkey
)
353 && XVECTOR (savedkey
)->size
> 1
354 && SYMBOLP (XVECTOR (savedkey
)->contents
[0]))
358 tem
= Fget (XVECTOR (savedkey
)->contents
[0], Qevent_kind
);
359 if (EQ (tem
, Qmouse_click
))
362 if (!NILP (savedkey
))
364 descrip
= Fkey_description (savedkey
);
365 descrip
= concat2 (make_string (" (", 3), descrip
);
366 descrip
= concat2 (descrip
, make_string (")", 1));
370 /* Cache the data we just got in a sublist of the menu binding. */
371 if (NILP (cachelist
))
373 CHECK_IMPURE (item1
);
374 XCONS (item1
)->cdr
= Fcons (Fcons (savedkey
, descrip
), def
);
378 XCONS (cachelist
)->car
= savedkey
;
379 XCONS (cachelist
)->cdr
= descrip
;
383 *descrip_ptr
= descrip
;
387 /* This is used as the handler when calling internal_condition_case_1. */
390 menu_item_enabled_p_1 (arg
)
396 /* Return non-nil if the command DEF is enabled when used as a menu item.
397 This is based on looking for a menu-enable property.
398 If NOTREAL is set, don't bother really computing this. */
401 menu_item_enabled_p (def
, notreal
)
405 Lisp_Object enabled
, tem
;
412 /* No property, or nil, means enable.
413 Otherwise, enable if value is not nil. */
414 tem
= Fget (def
, Qmenu_enable
);
416 /* (condition-case nil (eval tem)
418 enabled
= internal_condition_case_1 (Feval
, tem
, Qerror
,
419 menu_item_enabled_p_1
);
424 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
425 and generate menu panes for them in menu_items.
426 If NOTREAL is nonzero,
427 don't bother really computing whether an item is enabled. */
430 keymap_panes (keymaps
, nmaps
, notreal
)
431 Lisp_Object
*keymaps
;
439 /* Loop over the given keymaps, making a pane for each map.
440 But don't make a pane that is empty--ignore that map instead.
441 P is the number of panes we have made so far. */
442 for (mapno
= 0; mapno
< nmaps
; mapno
++)
443 single_keymap_panes (keymaps
[mapno
], Qnil
, Qnil
, notreal
);
445 finish_menu_items ();
448 /* This is a recursive subroutine of keymap_panes.
449 It handles one keymap, KEYMAP.
450 The other arguments are passed along
451 or point to local variables of the previous function.
452 If NOTREAL is nonzero,
453 don't bother really computing whether an item is enabled. */
456 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
)
458 Lisp_Object pane_name
;
462 Lisp_Object pending_maps
;
463 Lisp_Object tail
, item
, item1
, item_string
, table
;
464 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
468 push_menu_pane (pane_name
, prefix
);
470 for (tail
= keymap
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
472 /* Look at each key binding, and if it has a menu string,
473 make a menu item from it. */
474 item
= XCONS (tail
)->car
;
477 item1
= XCONS (item
)->cdr
;
480 item_string
= XCONS (item1
)->car
;
481 if (STRINGP (item_string
))
483 /* This is the real definition--the function to run. */
485 /* These are the saved equivalent keyboard key sequence
486 and its key-description. */
488 Lisp_Object tem
, enabled
;
490 /* GCPRO because ...enabled_p will call eval
491 and ..._equiv_key may autoload something.
492 Protecting KEYMAP preserves everything we use;
493 aside from that, must protect whatever might be
494 a string. Since there's no GCPRO5, we refetch
495 item_string instead of protecting it. */
496 descrip
= def
= Qnil
;
497 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
499 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
500 enabled
= menu_item_enabled_p (def
, notreal
);
504 item_string
= XCONS (item1
)->car
;
506 tem
= Fkeymapp (def
);
507 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
508 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, XCONS (item
)->car
)),
513 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
514 submap
= get_keymap_1 (def
, 0, 1);
516 #ifndef USE_X_TOOLKIT
517 /* Indicate visually that this is a submenu. */
519 item_string
= concat2 (item_string
,
520 build_string (" >"));
522 push_menu_item (item_string
, enabled
, XCONS (item
)->car
,
525 /* Display a submenu using the toolkit. */
528 push_submenu_start ();
529 single_keymap_panes (submap
, Qnil
,
530 XCONS (item
)->car
, notreal
);
538 else if (VECTORP (item
))
540 /* Loop over the char values represented in the vector. */
541 int len
= XVECTOR (item
)->size
;
543 for (c
= 0; c
< len
; c
++)
545 Lisp_Object character
;
546 XSETFASTINT (character
, c
);
547 item1
= XVECTOR (item
)->contents
[c
];
550 item_string
= XCONS (item1
)->car
;
551 if (STRINGP (item_string
))
555 /* These are the saved equivalent keyboard key sequence
556 and its key-description. */
558 Lisp_Object tem
, enabled
;
560 /* GCPRO because ...enabled_p will call eval
561 and ..._equiv_key may autoload something.
562 Protecting KEYMAP preserves everything we use;
563 aside from that, must protect whatever might be
564 a string. Since there's no GCPRO5, we refetch
565 item_string instead of protecting it. */
566 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
567 descrip
= def
= Qnil
;
569 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
570 enabled
= menu_item_enabled_p (def
, notreal
);
574 item_string
= XCONS (item1
)->car
;
576 tem
= Fkeymapp (def
);
577 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
578 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, character
)),
583 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
584 submap
= get_keymap_1 (def
, 0, 1);
586 #ifndef USE_X_TOOLKIT
588 item_string
= concat2 (item_string
,
589 build_string (" >"));
591 push_menu_item (item_string
, enabled
, character
,
596 push_submenu_start ();
597 single_keymap_panes (submap
, Qnil
,
609 /* Process now any submenus which want to be panes at this level. */
610 while (!NILP (pending_maps
))
612 Lisp_Object elt
, eltcdr
, string
;
613 elt
= Fcar (pending_maps
);
614 eltcdr
= XCONS (elt
)->cdr
;
615 string
= XCONS (eltcdr
)->car
;
616 /* We no longer discard the @ from the beginning of the string here.
617 Instead, we do this in xmenu_show. */
618 single_keymap_panes (Fcar (elt
), string
,
619 XCONS (eltcdr
)->cdr
, notreal
);
620 pending_maps
= Fcdr (pending_maps
);
624 /* Push all the panes and items of a menu decsribed by the
625 alist-of-alists MENU.
626 This handles old-fashioned calls to x-popup-menu. */
636 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
638 Lisp_Object elt
, pane_name
, pane_data
;
640 pane_name
= Fcar (elt
);
641 CHECK_STRING (pane_name
, 0);
642 push_menu_pane (pane_name
, Qnil
);
643 pane_data
= Fcdr (elt
);
644 CHECK_CONS (pane_data
, 0);
645 list_of_items (pane_data
);
648 finish_menu_items ();
651 /* Push the items in a single pane defined by the alist PANE. */
657 Lisp_Object tail
, item
, item1
;
659 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
663 push_menu_item (item
, Qnil
, Qnil
, Qnil
);
664 else if (NILP (item
))
665 push_left_right_boundary ();
668 CHECK_CONS (item
, 0);
670 CHECK_STRING (item1
, 1);
671 push_menu_item (item1
, Qt
, Fcdr (item
), Qnil
);
676 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
677 "Pop up a deck-of-cards menu and return user's selection.\n\
678 POSITION is a position specification. This is either a mouse button event\n\
679 or a list ((XOFFSET YOFFSET) WINDOW)\n\
680 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
681 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
682 This controls the position of the center of the first line\n\
683 in the first pane of the menu, not the top left of the menu as a whole.\n\
684 If POSITION is t, it means to use the current mouse position.\n\
686 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
687 The menu items come from key bindings that have a menu string as well as\n\
688 a definition; actually, the \"definition\" in such a key binding looks like\n\
689 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
690 the keymap as a top-level element.\n\n\
691 You can also use a list of keymaps as MENU.\n\
692 Then each keymap makes a separate pane.\n\
693 When MENU is a keymap or a list of keymaps, the return value\n\
694 is a list of events.\n\n\
695 Alternatively, you can specify a menu of multiple panes\n\
696 with a list of the form (TITLE PANE1 PANE2...),\n\
697 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
698 Each ITEM is normally a cons cell (STRING . VALUE);\n\
699 but a string can appear as an item--that makes a nonselectable line\n\
701 With this form of menu, the return value is VALUE from the chosen item.\n\
703 If POSITION is nil, don't display the menu at all, just precalculate the\n\
704 cached information about equivalent key sequences.")
706 Lisp_Object position
, menu
;
708 int number_of_panes
, panes
;
709 Lisp_Object keymap
, tem
;
713 Lisp_Object selection
;
716 Lisp_Object x
, y
, window
;
721 if (! NILP (position
))
725 /* Decode the first argument: find the window and the coordinates. */
726 if (EQ (position
, Qt
))
728 /* Use the mouse's current position. */
730 Lisp_Object bar_window
;
734 if (mouse_position_hook
)
735 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
737 XSETFRAME (window
, new_f
);
740 window
= selected_window
;
747 tem
= Fcar (position
);
750 window
= Fcar (Fcdr (position
));
752 y
= Fcar (Fcdr (tem
));
756 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
757 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
758 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
762 /* Determine whether this menu is handling a menu bar click. */
763 tem
= Fcar (Fcdr (Fcar (Fcdr (position
))));
764 if (CONSP (tem
) && EQ (Fcar (tem
), Qmenu_bar
))
772 /* Decode where to put the menu. */
780 else if (WINDOWP (window
))
782 CHECK_LIVE_WINDOW (window
, 0);
783 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
785 xpos
= (FONT_WIDTH (f
->display
.x
->font
) * XWINDOW (window
)->left
);
786 ypos
= (f
->display
.x
->line_height
* XWINDOW (window
)->top
);
789 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
790 but I don't want to make one now. */
791 CHECK_WINDOW (window
, 0);
800 /* Decode the menu items from what was specified. */
802 keymap
= Fkeymapp (menu
);
805 tem
= Fkeymapp (Fcar (menu
));
808 /* We were given a keymap. Extract menu info from the keymap. */
810 keymap
= get_keymap (menu
);
812 /* Extract the detailed info to make one pane. */
813 keymap_panes (&menu
, 1, NILP (position
));
815 /* Search for a string appearing directly as an element of the keymap.
816 That string is the title of the menu. */
817 prompt
= map_prompt (keymap
);
819 /* Make that be the pane title of the first pane. */
820 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
821 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
825 else if (!NILP (tem
))
827 /* We were given a list of keymaps. */
828 int nmaps
= XFASTINT (Flength (menu
));
830 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
835 /* The first keymap that has a prompt string
836 supplies the menu title. */
837 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= Fcdr (tem
))
841 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
843 prompt
= map_prompt (keymap
);
844 if (NILP (title
) && !NILP (prompt
))
848 /* Extract the detailed info to make one pane. */
849 keymap_panes (maps
, nmaps
, NILP (position
));
851 /* Make the title be the pane title of the first pane. */
852 if (!NILP (title
) && menu_items_n_panes
>= 0)
853 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
859 /* We were given an old-fashioned menu. */
861 CHECK_STRING (title
, 1);
863 list_of_panes (Fcdr (menu
));
870 discard_menu_items ();
875 /* Display them in a menu. */
878 selection
= xmenu_show (f
, xpos
, ypos
, menubarp
,
879 keymaps
, title
, &error_name
);
882 discard_menu_items ();
886 if (error_name
) error (error_name
);
890 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
891 "Pop up a dialog box and return user's selection.\n\
892 POSITION specifies which frame to use.\n\
893 This is normally a mouse button event or a window or frame.\n\
894 If POSITION is t, it means to use the frame the mouse is on.\n\
895 The dialog box appears in the middle of the specified frame.\n\
897 CONTENTS specifies the alternatives to display in the dialog box.\n\
898 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
899 Each ITEM is a cons cell (STRING . VALUE).\n\
900 The return value is VALUE from the chosen item.\n\n\
901 An ITEM may also be just a string--that makes a nonselectable item.\n\
902 An ITEM may also be nil--that means to put all preceding items\n\
903 on the left of the dialog box and all following items on the right.\n\
904 \(By default, approximately half appear on each side.)")
906 Lisp_Object position
, contents
;
913 /* Decode the first argument: find the window or frame to use. */
914 if (EQ (position
, Qt
))
916 #if 0 /* Using the frame the mouse is on may not be right. */
917 /* Use the mouse's current position. */
919 Lisp_Object bar_window
;
924 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
927 XSETFRAME (window
, new_f
);
929 window
= selected_window
;
931 /* Decode the first argument: find the window and the coordinates. */
932 if (EQ (position
, Qt
))
933 window
= selected_window
;
935 else if (CONSP (position
))
938 tem
= Fcar (position
);
940 window
= Fcar (Fcdr (position
));
943 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
944 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
947 else if (WINDOWP (position
) || FRAMEP (position
))
950 /* Decode where to put the menu. */
954 else if (WINDOWP (window
))
956 CHECK_LIVE_WINDOW (window
, 0);
957 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
960 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
961 but I don't want to make one now. */
962 CHECK_WINDOW (window
, 0);
964 #ifndef USE_X_TOOLKIT
965 /* Display a menu with these alternatives
966 in the middle of frame F. */
968 Lisp_Object x
, y
, frame
, newpos
;
969 XSETFRAME (frame
, f
);
970 XSETINT (x
, x_pixel_width (f
) / 2);
971 XSETINT (y
, x_pixel_height (f
) / 2);
972 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
974 return Fx_popup_menu (newpos
,
975 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
981 Lisp_Object selection
;
983 /* Decode the dialog items from what was specified. */
984 title
= Fcar (contents
);
985 CHECK_STRING (title
, 1);
987 list_of_panes (Fcons (contents
, Qnil
));
989 /* Display them in a dialog box. */
991 selection
= xdialog_show (f
, 0, 0, title
, &error_name
);
994 discard_menu_items ();
996 if (error_name
) error (error_name
);
1002 #ifdef USE_X_TOOLKIT
1004 /* Loop in Xt until the menu pulldown or dialog popup has been
1005 popped down (deactivated).
1007 NOTE: All calls to popup_get_selection() should be protected
1008 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1010 popup_get_selection (initial_event
)
1011 XEvent
*initial_event
;
1016 event
= *initial_event
;
1018 XtAppNextEvent (Xt_app_con
, &event
);
1022 XtDispatchEvent (&event
);
1023 if (!popup_activated())
1025 XtAppNextEvent (Xt_app_con
, &event
);
1029 /* Detect if a dialog or menu has been posted. */
1033 return popup_activated_flag
;
1037 /* This callback is invoked when the user selects a menubar cascade
1038 pushbutton, but before the pulldown menu is posted. */
1041 popup_activate_callback (widget
, id
, client_data
)
1044 XtPointer client_data
;
1046 popup_activated_flag
= 1;
1049 /* This callback is called from the menu bar pulldown menu
1050 when the user makes a selection.
1051 Figure out what the user chose
1052 and put the appropriate events into the keyboard buffer. */
1055 menubar_selection_callback (widget
, id
, client_data
)
1058 XtPointer client_data
;
1061 FRAME_PTR f
= (FRAME_PTR
) id
;
1063 Lisp_Object
*subprefix_stack
;
1064 int submenu_depth
= 0;
1069 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* sizeof (Lisp_Object
));
1070 vector
= f
->menu_bar_vector
;
1073 while (i
< f
->menu_bar_items_used
)
1077 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1079 subprefix_stack
[submenu_depth
++] = prefix
;
1083 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1085 prefix
= subprefix_stack
[--submenu_depth
];
1088 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1091 = XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1092 i
+= MENU_ITEMS_PANE_LENGTH
;
1097 = XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1098 if ((int) client_data
== i
)
1101 struct input_event buf
;
1103 buf
.kind
= menu_bar_event
;
1104 buf
.frame_or_window
= Qmenu_bar
;
1105 kbd_buffer_store_event (&buf
);
1107 for (j
= 0; j
< submenu_depth
; j
++)
1108 if (!NILP (subprefix_stack
[j
]))
1110 buf
.kind
= menu_bar_event
;
1111 buf
.frame_or_window
= subprefix_stack
[j
];
1112 kbd_buffer_store_event (&buf
);
1117 buf
.kind
= menu_bar_event
;
1118 buf
.frame_or_window
= prefix
;
1119 kbd_buffer_store_event (&buf
);
1122 buf
.kind
= menu_bar_event
;
1123 buf
.frame_or_window
= entry
;
1124 kbd_buffer_store_event (&buf
);
1128 i
+= MENU_ITEMS_ITEM_LENGTH
;
1133 /* This callback is invoked when a dialog or menu is finished being
1134 used and has been unposted. */
1137 popup_deactivate_callback (widget
, id
, client_data
)
1140 XtPointer client_data
;
1142 popup_activated_flag
= 0;
1146 /* This recursively calls free_widget_value on the tree of widgets.
1147 It must free all data that was malloc'ed for these widget_values.
1148 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1149 must be left alone. */
1152 free_menubar_widget_value_tree (wv
)
1157 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1159 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1161 free_menubar_widget_value_tree (wv
->contents
);
1162 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1166 free_menubar_widget_value_tree (wv
->next
);
1167 wv
->next
= (widget_value
*) 0xDEADBEEF;
1170 free_widget_value (wv
);
1174 /* Return a tree of widget_value structures for a menu bar item
1175 whose event type is ITEM_KEY (with string ITEM_NAME)
1176 and whose contents come from the list of keymaps MAPS. */
1178 static widget_value
*
1179 single_submenu (item_key
, item_name
, maps
)
1180 Lisp_Object item_key
, item_name
, maps
;
1182 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1184 int submenu_depth
= 0;
1187 Lisp_Object
*mapvec
;
1188 widget_value
**submenu_stack
;
1190 int previous_items
= menu_items_used
;
1192 length
= Flength (maps
);
1193 len
= XINT (length
);
1195 /* Convert the list MAPS into a vector MAPVEC. */
1196 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1197 for (i
= 0; i
< len
; i
++)
1199 mapvec
[i
] = Fcar (maps
);
1203 menu_items_n_panes
= 0;
1205 /* Loop over the given keymaps, making a pane for each map.
1206 But don't make a pane that is empty--ignore that map instead. */
1207 for (i
= 0; i
< len
; i
++)
1208 single_keymap_panes (mapvec
[i
], item_name
, item_key
, 0);
1210 /* Create a tree of widget_value objects
1211 representing the panes and their items. */
1214 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1215 wv
= malloc_widget_value ();
1222 /* Loop over all panes and items made during this call
1223 and construct a tree of widget_value objects.
1224 Ignore the panes and items made by previous calls to
1225 single_submenu, even though those are also in menu_items. */
1227 while (i
< menu_items_used
)
1229 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1231 submenu_stack
[submenu_depth
++] = save_wv
;
1236 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1239 save_wv
= submenu_stack
[--submenu_depth
];
1242 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1243 && submenu_depth
!= 0)
1244 i
+= MENU_ITEMS_PANE_LENGTH
;
1245 /* Ignore a nil in the item list.
1246 It's meaningful only for dialog boxes. */
1247 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1249 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1251 /* Create a new pane. */
1252 Lisp_Object pane_name
, prefix
;
1254 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1255 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1256 pane_string
= (NILP (pane_name
)
1257 ? "" : (char *) XSTRING (pane_name
)->data
);
1258 /* If there is just one top-level pane, put all its items directly
1259 under the top-level menu. */
1260 if (menu_items_n_panes
== 1)
1263 /* If the pane has a meaningful name,
1264 make the pane a top-level menu item
1265 with its items as a submenu beneath it. */
1266 if (strcmp (pane_string
, ""))
1268 wv
= malloc_widget_value ();
1272 first_wv
->contents
= wv
;
1273 wv
->name
= pane_string
;
1281 i
+= MENU_ITEMS_PANE_LENGTH
;
1285 /* Create a new item within current pane. */
1286 Lisp_Object item_name
, enable
, descrip
;
1287 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1288 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1290 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1292 wv
= malloc_widget_value ();
1296 save_wv
->contents
= wv
;
1297 wv
->name
= (char *) XSTRING (item_name
)->data
;
1298 if (!NILP (descrip
))
1299 wv
->key
= (char *) XSTRING (descrip
)->data
;
1301 wv
->call_data
= (void *) i
;
1302 wv
->enabled
= !NILP (enable
);
1305 i
+= MENU_ITEMS_ITEM_LENGTH
;
1312 extern void EmacsFrameSetCharSize ();
1314 /* Recompute the menu bar of frame F. */
1317 update_frame_menubar (f
)
1320 struct x_display
*x
= f
->display
.x
;
1322 int menubar_changed
;
1324 Dimension shell_height
;
1326 /* We assume the menubar contents has changed if the global flag is set,
1327 or if the current buffer has changed, or if the menubar has never
1328 been updated before.
1330 menubar_changed
= (x
->menubar_widget
1331 && !XtIsManaged (x
->menubar_widget
));
1333 if (! (menubar_changed
))
1337 /* Save the size of the frame because the pane widget doesn't accept to
1338 resize itself. So force it. */
1342 /* Do the voodoo which means "I'm changing lots of things, don't try to
1343 refigure sizes until I'm done." */
1344 lw_refigure_widget (x
->column_widget
, False
);
1346 /* the order in which children are managed is the top to
1347 bottom order in which they are displayed in the paned window.
1348 First, remove the text-area widget.
1350 XtUnmanageChild (x
->edit_widget
);
1352 /* remove the menubar that is there now, and put up the menubar that
1355 if (menubar_changed
)
1357 XtManageChild (x
->menubar_widget
);
1358 XtMapWidget (x
->menubar_widget
);
1359 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1362 /* Re-manage the text-area widget, and then thrash the sizes. */
1363 XtManageChild (x
->edit_widget
);
1364 lw_refigure_widget (x
->column_widget
, True
);
1366 /* Force the pane widget to resize itself with the right values. */
1367 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1373 set_frame_menubar (f
, first_time
)
1377 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
1379 Lisp_Object tail
, items
;
1380 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1385 wv
= malloc_widget_value ();
1386 wv
->name
= "menubar";
1390 items
= FRAME_MENU_BAR_ITEMS (f
);
1391 menu_items
= f
->menu_bar_vector
;
1392 menu_items_allocated
= XVECTOR (menu_items
)->size
;
1395 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1397 Lisp_Object key
, string
, maps
;
1399 key
= XVECTOR (items
)->contents
[i
];
1400 string
= XVECTOR (items
)->contents
[i
+ 1];
1401 maps
= XVECTOR (items
)->contents
[i
+ 2];
1405 wv
= single_submenu (key
, string
, maps
);
1409 first_wv
->contents
= wv
;
1410 /* Don't set wv->name here; GC during the loop might relocate it. */
1415 /* Now GC cannot happen during the lifetime of the widget_value,
1416 so it's safe to store data from a Lisp_String. */
1417 wv
= first_wv
->contents
;
1418 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1421 string
= XVECTOR (items
)->contents
[i
+ 1];
1424 wv
->name
= (char *) XSTRING (string
)->data
;
1428 finish_menu_items ();
1430 f
->menu_bar_vector
= menu_items
;
1431 f
->menu_bar_items_used
= menu_items_used
;
1436 /* Disable resizing (done for Motif!) */
1437 lw_allow_resizing (f
->display
.x
->widget
, False
);
1439 /* The third arg is DEEP_P, which says to consider the entire
1440 menu trees we supply, rather than just the menu bar item names. */
1441 lw_modify_all_widgets (id
, first_wv
, 1);
1443 /* Re-enable the edit widget to resize. */
1444 lw_allow_resizing (f
->display
.x
->widget
, True
);
1448 menubar_widget
= lw_create_widget ("menubar", "menubar",
1450 f
->display
.x
->column_widget
,
1452 popup_activate_callback
,
1453 menubar_selection_callback
,
1454 popup_deactivate_callback
);
1455 f
->display
.x
->menubar_widget
= menubar_widget
;
1458 free_menubar_widget_value_tree (first_wv
);
1460 /* Don't update the menubar the first time it is created via x_window. */
1462 update_frame_menubar (f
);
1467 /* Called from Fx_create_frame to create the inital menubar of a frame
1468 before it is mapped, so that the window is mapped with the menubar already
1469 there instead of us tacking it on later and thrashing the window after it
1473 initialize_frame_menubar (f
)
1476 /* This function is called before the first chance to redisplay
1477 the frame. It has to be, so the frame will have the right size. */
1478 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1479 set_frame_menubar (f
, 1);
1482 /* Get rid of the menu bar of frame F, and free its storage.
1483 This is used when deleting a frame, and when turning off the menu bar. */
1486 free_frame_menubar (f
)
1489 Widget menubar_widget
;
1492 menubar_widget
= f
->display
.x
->menubar_widget
;
1498 lw_destroy_all_widgets (id
);
1503 #endif /* USE_X_TOOLKIT */
1505 /* xmenu_show actually displays a menu using the panes and items in menu_items
1506 and returns the value selected from it.
1507 There are two versions of xmenu_show, one for Xt and one for Xlib.
1508 Both assume input is blocked by the caller. */
1510 /* F is the frame the menu is for.
1511 X and Y are the frame-relative specified position,
1512 relative to the inside upper left corner of the frame F.
1513 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1514 KEYMAPS is 1 if this menu was specified with keymaps;
1515 in that case, we return a list containing the chosen item's value
1516 and perhaps also the pane's prefix.
1517 TITLE is the specified menu title.
1518 ERROR is a place to store an error message string in case of failure.
1519 (We return nil on failure, but the value doesn't actually matter.) */
1521 #ifdef USE_X_TOOLKIT
1523 /* We need a unique id for each widget handled by the Lucid Widget
1524 library. This includes the frame main windows, popup menu and
1526 LWLIB_ID widget_id_tick
;
1529 static Lisp_Object
*volatile menu_item_selection
;
1531 static Lisp_Object
*menu_item_selection
;
1535 popup_selection_callback (widget
, id
, client_data
)
1538 XtPointer client_data
;
1540 menu_item_selection
= (Lisp_Object
*) client_data
;
1544 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
1548 int menubarp
; /* Dummy parameter for Xt version of
1559 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1560 widget_value
**submenu_stack
1561 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1562 Lisp_Object
*subprefix_stack
1563 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1564 int submenu_depth
= 0;
1566 /* Define a queue to save up for later unreading
1567 all X events that don't pertain to the menu. */
1571 struct event_queue
*next
;
1574 struct event_queue
*queue
= NULL
;
1575 struct event_queue
*queue_tmp
;
1577 Position root_x
, root_y
;
1580 int next_release_must_exit
= 0;
1584 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1586 *error
= "Empty menu";
1590 /* Create a tree of widget_value objects
1591 representing the panes and their items. */
1592 wv
= malloc_widget_value ();
1599 /* Loop over all panes and items, filling in the tree. */
1601 while (i
< menu_items_used
)
1603 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1605 submenu_stack
[submenu_depth
++] = save_wv
;
1611 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1614 save_wv
= submenu_stack
[--submenu_depth
];
1618 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1619 && submenu_depth
!= 0)
1620 i
+= MENU_ITEMS_PANE_LENGTH
;
1621 /* Ignore a nil in the item list.
1622 It's meaningful only for dialog boxes. */
1623 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1625 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1627 /* Create a new pane. */
1628 Lisp_Object pane_name
, prefix
;
1630 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1631 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1632 pane_string
= (NILP (pane_name
)
1633 ? "" : (char *) XSTRING (pane_name
)->data
);
1634 /* If there is just one top-level pane, put all its items directly
1635 under the top-level menu. */
1636 if (menu_items_n_panes
== 1)
1639 /* If the pane has a meaningful name,
1640 make the pane a top-level menu item
1641 with its items as a submenu beneath it. */
1642 if (!keymaps
&& strcmp (pane_string
, ""))
1644 wv
= malloc_widget_value ();
1648 first_wv
->contents
= wv
;
1649 wv
->name
= pane_string
;
1650 if (keymaps
&& !NILP (prefix
))
1657 else if (first_pane
)
1663 i
+= MENU_ITEMS_PANE_LENGTH
;
1667 /* Create a new item within current pane. */
1668 Lisp_Object item_name
, enable
, descrip
;
1669 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1670 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1672 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1674 wv
= malloc_widget_value ();
1678 save_wv
->contents
= wv
;
1679 wv
->name
= (char *) XSTRING (item_name
)->data
;
1680 if (!NILP (descrip
))
1681 wv
->key
= (char *) XSTRING (descrip
)->data
;
1683 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1684 wv
->enabled
= !NILP (enable
);
1687 i
+= MENU_ITEMS_ITEM_LENGTH
;
1691 /* Deal with the title, if it is non-nil. */
1694 widget_value
*wv_title
= malloc_widget_value ();
1695 widget_value
*wv_sep1
= malloc_widget_value ();
1696 widget_value
*wv_sep2
= malloc_widget_value ();
1698 wv_sep2
->name
= "--";
1699 wv_sep2
->next
= first_wv
->contents
;
1701 wv_sep1
->name
= "--";
1702 wv_sep1
->next
= wv_sep2
;
1704 wv_title
->name
= (char *) XSTRING (title
)->data
;
1705 wv_title
->enabled
= True
;
1706 wv_title
->next
= wv_sep1
;
1707 first_wv
->contents
= wv_title
;
1710 /* Actually create the menu. */
1711 menu_id
= ++widget_id_tick
;
1712 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1713 f
->display
.x
->widget
, 1, 0,
1714 popup_selection_callback
,
1715 popup_deactivate_callback
);
1717 /* Don't allow any geometry request from the user. */
1718 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1719 XtSetValues (menu
, av
, ac
);
1721 /* Free the widget_value objects we used to specify the contents. */
1722 free_menubar_widget_value_tree (first_wv
);
1724 /* No selection has been chosen yet. */
1725 menu_item_selection
= 0;
1727 /* Display the menu. */
1728 lw_popup_menu (menu
);
1729 popup_activated_flag
= 1;
1731 /* Process events that apply to the menu. */
1732 popup_get_selection ((XEvent
*) 0);
1735 /* fp turned off the following statement and wrote a comment
1736 that it is unnecessary--that the menu has already disappeared.
1737 I observer that is not so. -- rms. */
1738 /* Make sure the menu disappears. */
1739 lw_destroy_all_widgets (menu_id
);
1741 /* Unread any events that we got but did not handle. */
1742 while (queue
!= NULL
)
1745 XPutBackEvent (FRAME_X_DISPLAY (f
), &queue_tmp
->event
);
1746 queue
= queue_tmp
->next
;
1747 free ((char *)queue_tmp
);
1748 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1749 interrupt_input_pending
= 1;
1752 /* Find the selected item, and its pane, to return
1753 the proper value. */
1754 if (menu_item_selection
!= 0)
1760 while (i
< menu_items_used
)
1764 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1766 subprefix_stack
[submenu_depth
++] = prefix
;
1770 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1772 prefix
= subprefix_stack
[--submenu_depth
];
1775 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1778 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1779 i
+= MENU_ITEMS_PANE_LENGTH
;
1784 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1785 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1791 entry
= Fcons (entry
, Qnil
);
1793 entry
= Fcons (prefix
, entry
);
1794 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1795 if (!NILP (subprefix_stack
[j
]))
1796 entry
= Fcons (subprefix_stack
[j
], entry
);
1800 i
+= MENU_ITEMS_ITEM_LENGTH
;
1809 dialog_selection_callback (widget
, id
, client_data
)
1812 XtPointer client_data
;
1814 if ((int)client_data
!= -1)
1815 menu_item_selection
= (Lisp_Object
*) client_data
;
1817 lw_destroy_all_widgets (id
);
1821 static char * button_names
[] = {
1822 "button1", "button2", "button3", "button4", "button5",
1823 "button6", "button7", "button8", "button9", "button10" };
1826 xdialog_show (f
, menubarp
, keymaps
, title
, error
)
1833 int i
, nb_buttons
=0;
1836 char dialog_name
[6];
1838 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1840 /* Define a queue to save up for later unreading
1841 all X events that don't pertain to the menu. */
1845 struct event_queue
*next
;
1848 struct event_queue
*queue
= NULL
;
1849 struct event_queue
*queue_tmp
;
1851 /* Number of elements seen so far, before boundary. */
1853 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1854 int boundary_seen
= 0;
1858 if (menu_items_n_panes
> 1)
1860 *error
= "Multiple panes in dialog box";
1864 /* Create a tree of widget_value objects
1865 representing the text label and buttons. */
1867 Lisp_Object pane_name
, prefix
;
1869 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
1870 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
1871 pane_string
= (NILP (pane_name
)
1872 ? "" : (char *) XSTRING (pane_name
)->data
);
1873 prev_wv
= malloc_widget_value ();
1874 prev_wv
->value
= pane_string
;
1875 if (keymaps
&& !NILP (prefix
))
1877 prev_wv
->enabled
= 1;
1878 prev_wv
->name
= "message";
1881 /* Loop over all panes and items, filling in the tree. */
1882 i
= MENU_ITEMS_PANE_LENGTH
;
1883 while (i
< menu_items_used
)
1886 /* Create a new item within current pane. */
1887 Lisp_Object item_name
, enable
, descrip
;
1888 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1889 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1891 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1893 if (NILP (item_name
))
1895 free_menubar_widget_value_tree (first_wv
);
1896 *error
= "Submenu in dialog items";
1899 if (EQ (item_name
, Qquote
))
1901 /* This is the boundary between left-side elts
1902 and right-side elts. Stop incrementing right_count. */
1907 if (nb_buttons
>= 10)
1909 free_menubar_widget_value_tree (first_wv
);
1910 *error
= "Too many dialog items";
1914 wv
= malloc_widget_value ();
1916 wv
->name
= (char *) button_names
[nb_buttons
];
1917 if (!NILP (descrip
))
1918 wv
->key
= (char *) XSTRING (descrip
)->data
;
1919 wv
->value
= (char *) XSTRING (item_name
)->data
;
1920 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1921 wv
->enabled
= !NILP (enable
);
1924 if (! boundary_seen
)
1928 i
+= MENU_ITEMS_ITEM_LENGTH
;
1931 /* If the boundary was not specified,
1932 by default put half on the left and half on the right. */
1933 if (! boundary_seen
)
1934 left_count
= nb_buttons
- nb_buttons
/ 2;
1936 wv
= malloc_widget_value ();
1937 wv
->name
= dialog_name
;
1939 /* Dialog boxes use a really stupid name encoding
1940 which specifies how many buttons to use
1941 and how many buttons are on the right.
1942 The Q means something also. */
1943 dialog_name
[0] = 'Q';
1944 dialog_name
[1] = '0' + nb_buttons
;
1945 dialog_name
[2] = 'B';
1946 dialog_name
[3] = 'R';
1947 /* Number of buttons to put on the right. */
1948 dialog_name
[4] = '0' + nb_buttons
- left_count
;
1950 wv
->contents
= first_wv
;
1954 /* Actually create the dialog. */
1955 dialog_id
= ++widget_id_tick
;
1956 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1957 f
->display
.x
->widget
, 1, 0,
1958 dialog_selection_callback
, 0);
1959 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1960 /* Free the widget_value objects we used to specify the contents. */
1961 free_menubar_widget_value_tree (first_wv
);
1963 /* No selection has been chosen yet. */
1964 menu_item_selection
= 0;
1966 /* Display the menu. */
1967 lw_pop_up_all_widgets (dialog_id
);
1969 /* Process events that apply to the menu. */
1974 XtAppNextEvent (Xt_app_con
, &event
);
1975 if (event
.type
== ButtonRelease
)
1977 XtDispatchEvent (&event
);
1980 else if (event
.type
== Expose
)
1981 process_expose_from_menu (event
);
1982 XtDispatchEvent (&event
);
1983 if (XtWindowToWidget (FRAME_X_DISPLAY (f
), event
.xany
.window
) != menu
)
1985 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
1987 if (queue_tmp
!= NULL
)
1989 queue_tmp
->event
= event
;
1990 queue_tmp
->next
= queue
;
1997 #ifdef HAVE_X_WINDOWS
1998 /* State that no mouse buttons are now held.
1999 That is not necessarily true, but the fiction leads to reasonable
2000 results, and it is a pain to ask which are actually held now
2001 or track this in the loop above. */
2002 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2005 /* Unread any events that we got but did not handle. */
2006 while (queue
!= NULL
)
2009 XPutBackEvent (FRAME_X_DISPLAY (f
), &queue_tmp
->event
);
2010 queue
= queue_tmp
->next
;
2011 free ((char *)queue_tmp
);
2012 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
2013 interrupt_input_pending
= 1;
2016 /* Find the selected item, and its pane, to return
2017 the proper value. */
2018 if (menu_item_selection
!= 0)
2024 while (i
< menu_items_used
)
2028 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2031 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2032 i
+= MENU_ITEMS_PANE_LENGTH
;
2037 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2038 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2042 entry
= Fcons (entry
, Qnil
);
2044 entry
= Fcons (prefix
, entry
);
2048 i
+= MENU_ITEMS_ITEM_LENGTH
;
2055 #else /* not USE_X_TOOLKIT */
2058 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
2068 int pane
, selidx
, lpane
, status
;
2069 Lisp_Object entry
, pane_prefix
;
2071 int ulx
, uly
, width
, height
;
2072 int dispwidth
, dispheight
;
2076 unsigned int dummy_uint
;
2079 if (menu_items_n_panes
== 0)
2082 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2084 *error
= "Empty menu";
2088 /* Figure out which root window F is on. */
2089 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2090 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2091 &dummy_uint
, &dummy_uint
);
2093 /* Make the menu on that window. */
2094 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2097 *error
= "Can't create menu";
2101 #ifdef HAVE_X_WINDOWS
2102 /* Adjust coordinates to relative to the outer (window manager) window. */
2105 int win_x
= 0, win_y
= 0;
2107 /* Find the position of the outside upper-left corner of
2108 the inner window, with respect to the outer window. */
2109 if (f
->display
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2112 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2114 /* From-window, to-window. */
2115 f
->display
.x
->window_desc
,
2116 f
->display
.x
->parent_desc
,
2118 /* From-position, to-position. */
2119 0, 0, &win_x
, &win_y
,
2121 /* Child of window. */
2128 #endif /* HAVE_X_WINDOWS */
2130 /* Adjust coordinates to be root-window-relative. */
2131 x
+= f
->display
.x
->left_pos
;
2132 y
+= f
->display
.x
->top_pos
;
2134 /* Create all the necessary panes and their items. */
2136 while (i
< menu_items_used
)
2138 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2140 /* Create a new pane. */
2141 Lisp_Object pane_name
, prefix
;
2144 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2145 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2146 pane_string
= (NILP (pane_name
)
2147 ? "" : (char *) XSTRING (pane_name
)->data
);
2148 if (keymaps
&& !NILP (prefix
))
2151 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
2152 if (lpane
== XM_FAILURE
)
2154 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2155 *error
= "Can't create pane";
2158 i
+= MENU_ITEMS_PANE_LENGTH
;
2160 /* Find the width of the widest item in this pane. */
2163 while (j
< menu_items_used
)
2166 item
= XVECTOR (menu_items
)->contents
[j
];
2174 width
= XSTRING (item
)->size
;
2175 if (width
> maxwidth
)
2178 j
+= MENU_ITEMS_ITEM_LENGTH
;
2181 /* Ignore a nil in the item list.
2182 It's meaningful only for dialog boxes. */
2183 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2187 /* Create a new item within current pane. */
2188 Lisp_Object item_name
, enable
, descrip
;
2189 unsigned char *item_data
;
2191 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2192 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2194 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2195 if (!NILP (descrip
))
2197 int gap
= maxwidth
- XSTRING (item_name
)->size
;
2200 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2201 item_name
= concat2 (item_name
, spacer
);
2202 item_name
= concat2 (item_name
, descrip
);
2203 item_data
= XSTRING (item_name
)->data
;
2205 /* if alloca is fast, use that to make the space,
2206 to reduce gc needs. */
2208 = (unsigned char *) alloca (maxwidth
2209 + XSTRING (descrip
)->size
+ 1);
2210 bcopy (XSTRING (item_name
)->data
, item_data
,
2211 XSTRING (item_name
)->size
);
2212 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2214 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2215 XSTRING (descrip
)->size
);
2216 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2220 item_data
= XSTRING (item_name
)->data
;
2222 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2223 menu
, lpane
, 0, item_data
,
2227 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2228 *error
= "Can't add selection to menu";
2231 i
+= MENU_ITEMS_ITEM_LENGTH
;
2235 /* All set and ready to fly. */
2236 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2237 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
),
2238 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2239 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
),
2240 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2241 x
= min (x
, dispwidth
);
2242 y
= min (y
, dispheight
);
2245 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2246 &ulx
, &uly
, &width
, &height
);
2247 if (ulx
+width
> dispwidth
)
2249 x
-= (ulx
+ width
) - dispwidth
;
2250 ulx
= dispwidth
- width
;
2252 if (uly
+height
> dispheight
)
2254 y
-= (uly
+ height
) - dispheight
;
2255 uly
= dispheight
- height
;
2257 if (ulx
< 0) x
-= ulx
;
2258 if (uly
< 0) y
-= uly
;
2260 XMenuSetAEQ (menu
, TRUE
);
2261 XMenuSetFreeze (menu
, TRUE
);
2264 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2265 x
, y
, ButtonReleaseMask
, &datap
);
2270 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2273 /* Find the item number SELIDX in pane number PANE. */
2275 while (i
< menu_items_used
)
2277 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2281 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2283 i
+= MENU_ITEMS_PANE_LENGTH
;
2292 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2295 entry
= Fcons (entry
, Qnil
);
2296 if (!NILP (pane_prefix
))
2297 entry
= Fcons (pane_prefix
, entry
);
2303 i
+= MENU_ITEMS_ITEM_LENGTH
;
2309 *error
= "Can't activate menu";
2315 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2317 #ifdef HAVE_X_WINDOWS
2318 /* State that no mouse buttons are now held.
2319 (The oldXMenu code doesn't track this info for us.)
2320 That is not necessarily true, but the fiction leads to reasonable
2321 results, and it is a pain to ask which are actually held now. */
2322 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2328 #endif /* not USE_X_TOOLKIT */
2332 staticpro (&menu_items
);
2335 #ifdef USE_X_TOOLKIT
2336 widget_id_tick
= (1<<16);
2339 defsubr (&Sx_popup_menu
);
2340 defsubr (&Sx_popup_dialog
);