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"
50 /* This may include sys/types.h, and that somehow loses
51 if this is not done before the other system files. */
55 /* Load sys/types.h if not already loaded.
56 In some systems loading it twice is suicidal. */
58 #include <sys/types.h>
61 #include "dispextern.h"
66 #include <X11/IntrinsicP.h>
67 #include <X11/CoreP.h>
68 #include <X11/StringDefs.h>
69 #include <X11/Shell.h>
70 #include "../lwlib/lwlib.h"
71 #else /* not USE_X_TOOLKIT */
72 #include "../oldXMenu/XMenu.h"
73 #endif /* not USE_X_TOOLKIT */
74 #endif /* HAVE_X_WINDOWS */
76 #define min(x,y) (((x) < (y)) ? (x) : (y))
77 #define max(x,y) (((x) > (y)) ? (x) : (y))
84 extern Lisp_Object Qmenu_enable
;
85 extern Lisp_Object Qmenu_bar
;
86 extern Lisp_Object Qmouse_click
, Qevent_kind
;
89 extern void process_expose_from_menu ();
90 extern XtAppContext Xt_app_con
;
92 static Lisp_Object
xdialog_show ();
93 void popup_get_selection ();
96 static Lisp_Object
xmenu_show ();
97 static void keymap_panes ();
98 static void single_keymap_panes ();
99 static void list_of_panes ();
100 static void list_of_items ();
102 /* This holds a Lisp vector that holds the results of decoding
103 the keymaps or alist-of-alists that specify a menu.
105 It describes the panes and items within the panes.
107 Each pane is described by 3 elements in the vector:
108 t, the pane name, the pane's prefix key.
109 Then follow the pane's items, with 5 elements per item:
110 the item string, the enable flag, the item's value,
111 the definition, and the equivalent keyboard key's description string.
113 In some cases, multiple levels of menus may be described.
114 A single vector slot containing nil indicates the start of a submenu.
115 A single vector slot containing lambda indicates the end of a submenu.
116 The submenu follows a menu item which is the way to reach the submenu.
118 A single vector slot containing quote indicates that the
119 following items should appear on the right of a dialog box.
121 Using a Lisp vector to hold this information while we decode it
122 takes care of protecting all the data from GC. */
124 #define MENU_ITEMS_PANE_NAME 1
125 #define MENU_ITEMS_PANE_PREFIX 2
126 #define MENU_ITEMS_PANE_LENGTH 3
128 #define MENU_ITEMS_ITEM_NAME 0
129 #define MENU_ITEMS_ITEM_ENABLE 1
130 #define MENU_ITEMS_ITEM_VALUE 2
131 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
132 #define MENU_ITEMS_ITEM_DEFINITION 4
133 #define MENU_ITEMS_ITEM_LENGTH 5
135 static Lisp_Object menu_items
;
137 /* Number of slots currently allocated in menu_items. */
138 static int menu_items_allocated
;
140 /* This is the index in menu_items of the first empty slot. */
141 static int menu_items_used
;
143 /* The number of panes currently recorded in menu_items,
144 excluding those within submenus. */
145 static int menu_items_n_panes
;
147 /* Current depth within submenus. */
148 static int menu_items_submenu_depth
;
150 /* Flag which when set indicates a dialog or menu has been posted by
151 Xt on behalf of one of the widget sets. */
152 static int popup_activated_flag
;
155 /* Initialize the menu_items structure if we haven't already done so.
156 Also mark it as currently empty. */
161 if (NILP (menu_items
))
163 menu_items_allocated
= 60;
164 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
168 menu_items_n_panes
= 0;
169 menu_items_submenu_depth
= 0;
172 /* Call at the end of generating the data in menu_items.
173 This fills in the number of items in the last pane. */
180 /* Call when finished using the data for the current menu
184 discard_menu_items ()
186 /* Free the structure if it is especially large.
187 Otherwise, hold on to it, to save time. */
188 if (menu_items_allocated
> 200)
191 menu_items_allocated
= 0;
195 /* Make the menu_items vector twice as large. */
201 int old_size
= menu_items_allocated
;
204 menu_items_allocated
*= 2;
205 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
206 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
207 old_size
* sizeof (Lisp_Object
));
210 /* Begin a submenu. */
213 push_submenu_start ()
215 if (menu_items_used
+ 1 > menu_items_allocated
)
218 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
219 menu_items_submenu_depth
++;
227 if (menu_items_used
+ 1 > menu_items_allocated
)
230 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
231 menu_items_submenu_depth
--;
234 /* Indicate boundary between left and right. */
237 push_left_right_boundary ()
239 if (menu_items_used
+ 1 > menu_items_allocated
)
242 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
245 /* Start a new menu pane in menu_items..
246 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
249 push_menu_pane (name
, prefix_vec
)
250 Lisp_Object name
, prefix_vec
;
252 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
255 if (menu_items_submenu_depth
== 0)
256 menu_items_n_panes
++;
257 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
258 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
259 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
262 /* Push one menu item into the current pane.
263 NAME is the string to display. ENABLE if non-nil means
264 this item can be selected. KEY is the key generated by
265 choosing this item, or nil if this item doesn't really have a definition.
266 DEF is the definition of this item.
267 EQUIV is the textual description of the keyboard equivalent for
268 this item (or nil if none). */
271 push_menu_item (name
, enable
, key
, def
, equiv
)
272 Lisp_Object name
, enable
, key
, def
, equiv
;
274 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
277 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
278 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
279 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
280 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
281 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
284 /* Figure out the current keyboard equivalent of a menu item ITEM1.
285 The item string for menu display should be ITEM_STRING.
286 Store the equivalent keyboard key sequence's
287 textual description into *DESCRIP_PTR.
288 Also cache them in the item itself.
289 Return the real definition to execute. */
292 menu_item_equiv_key (item_string
, item1
, descrip_ptr
)
293 Lisp_Object item_string
;
295 Lisp_Object
*descrip_ptr
;
297 /* This is the real definition--the function to run. */
299 /* This is the sublist that records cached equiv key data
300 so we can save time. */
301 Lisp_Object cachelist
;
302 /* These are the saved equivalent keyboard key sequence
303 and its key-description. */
304 Lisp_Object savedkey
, descrip
;
307 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
309 /* If a help string follows the item string, skip it. */
310 if (CONSP (XCONS (item1
)->cdr
)
311 && STRINGP (XCONS (XCONS (item1
)->cdr
)->car
))
312 item1
= XCONS (item1
)->cdr
;
316 /* Get out the saved equivalent-keyboard-key info. */
317 cachelist
= savedkey
= descrip
= Qnil
;
318 if (CONSP (def
) && CONSP (XCONS (def
)->car
)
319 && (NILP (XCONS (XCONS (def
)->car
)->car
)
320 || VECTORP (XCONS (XCONS (def
)->car
)->car
)))
322 cachelist
= XCONS (def
)->car
;
323 def
= XCONS (def
)->cdr
;
324 savedkey
= XCONS (cachelist
)->car
;
325 descrip
= XCONS (cachelist
)->cdr
;
328 GCPRO4 (def
, def1
, savedkey
, descrip
);
330 /* Is it still valid? */
332 if (!NILP (savedkey
))
333 def1
= Fkey_binding (savedkey
, Qnil
);
334 /* If not, update it. */
336 /* If the command is an alias for another
337 (such as easymenu.el and lmenu.el set it up),
338 check if the original command matches the cached command. */
339 && !(SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
)
340 && EQ (def1
, XSYMBOL (def
)->function
))
341 /* If something had no key binding before, don't recheck it--
342 doing that takes too much time and makes menus too slow. */
343 && !(!NILP (cachelist
) && NILP (savedkey
)))
347 savedkey
= Fwhere_is_internal (def
, Qnil
, Qt
, Qnil
);
348 /* If the command is an alias for another
349 (such as easymenu.el and lmenu.el set it up),
350 see if the original command name has equivalent keys. */
351 if (SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
))
352 savedkey
= Fwhere_is_internal (XSYMBOL (def
)->function
,
355 if (VECTORP (savedkey
)
356 && EQ (XVECTOR (savedkey
)->contents
[0], Qmenu_bar
))
358 /* Reject two-key sequences that start with a mouse click.
359 These are probably menu items. */
360 if (VECTORP (savedkey
)
361 && XVECTOR (savedkey
)->size
> 1
362 && SYMBOLP (XVECTOR (savedkey
)->contents
[0]))
366 tem
= Fget (XVECTOR (savedkey
)->contents
[0], Qevent_kind
);
367 if (EQ (tem
, Qmouse_click
))
370 if (!NILP (savedkey
))
372 descrip
= Fkey_description (savedkey
);
373 descrip
= concat2 (make_string (" (", 3), descrip
);
374 descrip
= concat2 (descrip
, make_string (")", 1));
378 /* Cache the data we just got in a sublist of the menu binding. */
379 if (NILP (cachelist
))
381 CHECK_IMPURE (item1
);
382 XCONS (item1
)->cdr
= Fcons (Fcons (savedkey
, descrip
), def
);
386 XCONS (cachelist
)->car
= savedkey
;
387 XCONS (cachelist
)->cdr
= descrip
;
391 *descrip_ptr
= descrip
;
395 /* This is used as the handler when calling internal_condition_case_1. */
398 menu_item_enabled_p_1 (arg
)
404 /* Return non-nil if the command DEF is enabled when used as a menu item.
405 This is based on looking for a menu-enable property.
406 If NOTREAL is set, don't bother really computing this. */
409 menu_item_enabled_p (def
, notreal
)
413 Lisp_Object enabled
, tem
;
420 /* No property, or nil, means enable.
421 Otherwise, enable if value is not nil. */
422 tem
= Fget (def
, Qmenu_enable
);
424 /* (condition-case nil (eval tem)
426 enabled
= internal_condition_case_1 (Feval
, tem
, Qerror
,
427 menu_item_enabled_p_1
);
432 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
433 and generate menu panes for them in menu_items.
434 If NOTREAL is nonzero,
435 don't bother really computing whether an item is enabled. */
438 keymap_panes (keymaps
, nmaps
, notreal
)
439 Lisp_Object
*keymaps
;
447 /* Loop over the given keymaps, making a pane for each map.
448 But don't make a pane that is empty--ignore that map instead.
449 P is the number of panes we have made so far. */
450 for (mapno
= 0; mapno
< nmaps
; mapno
++)
451 single_keymap_panes (keymaps
[mapno
], Qnil
, Qnil
, notreal
);
453 finish_menu_items ();
456 /* This is a recursive subroutine of keymap_panes.
457 It handles one keymap, KEYMAP.
458 The other arguments are passed along
459 or point to local variables of the previous function.
460 If NOTREAL is nonzero,
461 don't bother really computing whether an item is enabled. */
464 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
)
466 Lisp_Object pane_name
;
470 Lisp_Object pending_maps
;
471 Lisp_Object tail
, item
, item1
, item_string
, table
;
472 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
476 push_menu_pane (pane_name
, prefix
);
478 for (tail
= keymap
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
480 /* Look at each key binding, and if it has a menu string,
481 make a menu item from it. */
482 item
= XCONS (tail
)->car
;
485 item1
= XCONS (item
)->cdr
;
488 item_string
= XCONS (item1
)->car
;
489 if (STRINGP (item_string
))
491 /* This is the real definition--the function to run. */
493 /* These are the saved equivalent keyboard key sequence
494 and its key-description. */
496 Lisp_Object tem
, enabled
;
498 /* GCPRO because ...enabled_p will call eval
499 and ..._equiv_key may autoload something.
500 Protecting KEYMAP preserves everything we use;
501 aside from that, must protect whatever might be
502 a string. Since there's no GCPRO5, we refetch
503 item_string instead of protecting it. */
504 descrip
= def
= Qnil
;
505 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
507 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
508 enabled
= menu_item_enabled_p (def
, notreal
);
512 item_string
= XCONS (item1
)->car
;
514 tem
= Fkeymapp (def
);
515 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
516 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, XCONS (item
)->car
)),
521 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
522 submap
= get_keymap_1 (def
, 0, 1);
524 #ifndef USE_X_TOOLKIT
525 /* Indicate visually that this is a submenu. */
527 item_string
= concat2 (item_string
,
528 build_string (" >"));
530 /* If definition is nil, pass nil as the key. */
531 push_menu_item (item_string
, enabled
,
532 XCONS (item
)->car
, def
,
535 /* Display a submenu using the toolkit. */
538 push_submenu_start ();
539 single_keymap_panes (submap
, Qnil
,
540 XCONS (item
)->car
, notreal
);
548 else if (VECTORP (item
))
550 /* Loop over the char values represented in the vector. */
551 int len
= XVECTOR (item
)->size
;
553 for (c
= 0; c
< len
; c
++)
555 Lisp_Object character
;
556 XSETFASTINT (character
, c
);
557 item1
= XVECTOR (item
)->contents
[c
];
560 item_string
= XCONS (item1
)->car
;
561 if (STRINGP (item_string
))
565 /* These are the saved equivalent keyboard key sequence
566 and its key-description. */
568 Lisp_Object tem
, enabled
;
570 /* GCPRO because ...enabled_p will call eval
571 and ..._equiv_key may autoload something.
572 Protecting KEYMAP preserves everything we use;
573 aside from that, must protect whatever might be
574 a string. Since there's no GCPRO5, we refetch
575 item_string instead of protecting it. */
576 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
577 descrip
= def
= Qnil
;
579 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
580 enabled
= menu_item_enabled_p (def
, notreal
);
584 item_string
= XCONS (item1
)->car
;
586 tem
= Fkeymapp (def
);
587 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
588 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, character
)),
593 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
594 submap
= get_keymap_1 (def
, 0, 1);
596 #ifndef USE_X_TOOLKIT
598 item_string
= concat2 (item_string
,
599 build_string (" >"));
601 /* If definition is nil, pass nil as the key. */
602 push_menu_item (item_string
, enabled
, character
,
607 push_submenu_start ();
608 single_keymap_panes (submap
, Qnil
,
620 /* Process now any submenus which want to be panes at this level. */
621 while (!NILP (pending_maps
))
623 Lisp_Object elt
, eltcdr
, string
;
624 elt
= Fcar (pending_maps
);
625 eltcdr
= XCONS (elt
)->cdr
;
626 string
= XCONS (eltcdr
)->car
;
627 /* We no longer discard the @ from the beginning of the string here.
628 Instead, we do this in xmenu_show. */
629 single_keymap_panes (Fcar (elt
), string
,
630 XCONS (eltcdr
)->cdr
, notreal
);
631 pending_maps
= Fcdr (pending_maps
);
635 /* Push all the panes and items of a menu decsribed by the
636 alist-of-alists MENU.
637 This handles old-fashioned calls to x-popup-menu. */
647 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
649 Lisp_Object elt
, pane_name
, pane_data
;
651 pane_name
= Fcar (elt
);
652 CHECK_STRING (pane_name
, 0);
653 push_menu_pane (pane_name
, Qnil
);
654 pane_data
= Fcdr (elt
);
655 CHECK_CONS (pane_data
, 0);
656 list_of_items (pane_data
);
659 finish_menu_items ();
662 /* Push the items in a single pane defined by the alist PANE. */
668 Lisp_Object tail
, item
, item1
;
670 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
674 push_menu_item (item
, Qnil
, Qnil
, Qt
, Qnil
);
675 else if (NILP (item
))
676 push_left_right_boundary ();
679 CHECK_CONS (item
, 0);
681 CHECK_STRING (item1
, 1);
682 push_menu_item (item1
, Qt
, Fcdr (item
), Qt
, Qnil
);
687 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
688 "Pop up a deck-of-cards menu and return user's selection.\n\
689 POSITION is a position specification. This is either a mouse button event\n\
690 or a list ((XOFFSET YOFFSET) WINDOW)\n\
691 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
692 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
693 This controls the position of the center of the first line\n\
694 in the first pane of the menu, not the top left of the menu as a whole.\n\
695 If POSITION is t, it means to use the current mouse position.\n\
697 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
698 The menu items come from key bindings that have a menu string as well as\n\
699 a definition; actually, the \"definition\" in such a key binding looks like\n\
700 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
701 the keymap as a top-level element.\n\n\
702 You can also use a list of keymaps as MENU.\n\
703 Then each keymap makes a separate pane.\n\
704 When MENU is a keymap or a list of keymaps, the return value\n\
705 is a list of events.\n\n\
706 Alternatively, you can specify a menu of multiple panes\n\
707 with a list of the form (TITLE PANE1 PANE2...),\n\
708 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
709 Each ITEM is normally a cons cell (STRING . VALUE);\n\
710 but a string can appear as an item--that makes a nonselectable line\n\
712 With this form of menu, the return value is VALUE from the chosen item.\n\
714 If POSITION is nil, don't display the menu at all, just precalculate the\n\
715 cached information about equivalent key sequences.")
717 Lisp_Object position
, menu
;
719 int number_of_panes
, panes
;
720 Lisp_Object keymap
, tem
;
724 Lisp_Object selection
;
727 Lisp_Object x
, y
, window
;
732 if (! NILP (position
))
736 /* Decode the first argument: find the window and the coordinates. */
737 if (EQ (position
, Qt
))
739 /* Use the mouse's current position. */
741 Lisp_Object bar_window
;
745 if (mouse_position_hook
)
746 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
748 XSETFRAME (window
, new_f
);
751 window
= selected_window
;
758 tem
= Fcar (position
);
761 window
= Fcar (Fcdr (position
));
763 y
= Fcar (Fcdr (tem
));
767 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
768 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
769 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
773 /* Determine whether this menu is handling a menu bar click. */
774 tem
= Fcar (Fcdr (Fcar (Fcdr (position
))));
775 if (CONSP (tem
) && EQ (Fcar (tem
), Qmenu_bar
))
783 /* Decode where to put the menu. */
791 else if (WINDOWP (window
))
793 CHECK_LIVE_WINDOW (window
, 0);
794 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
796 xpos
= (FONT_WIDTH (f
->display
.x
->font
) * XWINDOW (window
)->left
);
797 ypos
= (f
->display
.x
->line_height
* XWINDOW (window
)->top
);
800 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
801 but I don't want to make one now. */
802 CHECK_WINDOW (window
, 0);
811 /* Decode the menu items from what was specified. */
813 keymap
= Fkeymapp (menu
);
816 tem
= Fkeymapp (Fcar (menu
));
819 /* We were given a keymap. Extract menu info from the keymap. */
821 keymap
= get_keymap (menu
);
823 /* Extract the detailed info to make one pane. */
824 keymap_panes (&menu
, 1, NILP (position
));
826 /* Search for a string appearing directly as an element of the keymap.
827 That string is the title of the menu. */
828 prompt
= map_prompt (keymap
);
830 /* Make that be the pane title of the first pane. */
831 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
832 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
836 else if (!NILP (tem
))
838 /* We were given a list of keymaps. */
839 int nmaps
= XFASTINT (Flength (menu
));
841 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
846 /* The first keymap that has a prompt string
847 supplies the menu title. */
848 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= Fcdr (tem
))
852 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
854 prompt
= map_prompt (keymap
);
855 if (NILP (title
) && !NILP (prompt
))
859 /* Extract the detailed info to make one pane. */
860 keymap_panes (maps
, nmaps
, NILP (position
));
862 /* Make the title be the pane title of the first pane. */
863 if (!NILP (title
) && menu_items_n_panes
>= 0)
864 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
870 /* We were given an old-fashioned menu. */
872 CHECK_STRING (title
, 1);
874 list_of_panes (Fcdr (menu
));
881 discard_menu_items ();
886 /* Display them in a menu. */
889 selection
= xmenu_show (f
, xpos
, ypos
, menubarp
,
890 keymaps
, title
, &error_name
);
893 discard_menu_items ();
897 if (error_name
) error (error_name
);
901 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
902 "Pop up a dialog box and return user's selection.\n\
903 POSITION specifies which frame to use.\n\
904 This is normally a mouse button event or a window or frame.\n\
905 If POSITION is t, it means to use the frame the mouse is on.\n\
906 The dialog box appears in the middle of the specified frame.\n\
908 CONTENTS specifies the alternatives to display in the dialog box.\n\
909 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
910 Each ITEM is a cons cell (STRING . VALUE).\n\
911 The return value is VALUE from the chosen item.\n\n\
912 An ITEM may also be just a string--that makes a nonselectable item.\n\
913 An ITEM may also be nil--that means to put all preceding items\n\
914 on the left of the dialog box and all following items on the right.\n\
915 \(By default, approximately half appear on each side.)")
917 Lisp_Object position
, contents
;
924 /* Decode the first argument: find the window or frame to use. */
925 if (EQ (position
, Qt
))
927 #if 0 /* Using the frame the mouse is on may not be right. */
928 /* Use the mouse's current position. */
930 Lisp_Object bar_window
;
935 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
938 XSETFRAME (window
, new_f
);
940 window
= selected_window
;
942 /* Decode the first argument: find the window and the coordinates. */
943 if (EQ (position
, Qt
))
944 window
= selected_window
;
946 else if (CONSP (position
))
949 tem
= Fcar (position
);
951 window
= Fcar (Fcdr (position
));
954 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
955 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
958 else if (WINDOWP (position
) || FRAMEP (position
))
961 /* Decode where to put the menu. */
965 else if (WINDOWP (window
))
967 CHECK_LIVE_WINDOW (window
, 0);
968 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
971 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
972 but I don't want to make one now. */
973 CHECK_WINDOW (window
, 0);
975 #ifndef USE_X_TOOLKIT
976 /* Display a menu with these alternatives
977 in the middle of frame F. */
979 Lisp_Object x
, y
, frame
, newpos
;
980 XSETFRAME (frame
, f
);
981 XSETINT (x
, x_pixel_width (f
) / 2);
982 XSETINT (y
, x_pixel_height (f
) / 2);
983 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
985 return Fx_popup_menu (newpos
,
986 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
992 Lisp_Object selection
;
994 /* Decode the dialog items from what was specified. */
995 title
= Fcar (contents
);
996 CHECK_STRING (title
, 1);
998 list_of_panes (Fcons (contents
, Qnil
));
1000 /* Display them in a dialog box. */
1002 selection
= xdialog_show (f
, 0, 0, title
, &error_name
);
1005 discard_menu_items ();
1007 if (error_name
) error (error_name
);
1013 #ifdef USE_X_TOOLKIT
1015 /* Loop in Xt until the menu pulldown or dialog popup has been
1016 popped down (deactivated).
1018 NOTE: All calls to popup_get_selection() should be protected
1019 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1021 popup_get_selection (initial_event
)
1022 XEvent
*initial_event
;
1027 event
= *initial_event
;
1029 XtAppNextEvent (Xt_app_con
, &event
);
1033 XtDispatchEvent (&event
);
1034 if (!popup_activated())
1036 XtAppNextEvent (Xt_app_con
, &event
);
1040 /* Detect if a dialog or menu has been posted. */
1044 return popup_activated_flag
;
1048 /* This callback is invoked when the user selects a menubar cascade
1049 pushbutton, but before the pulldown menu is posted. */
1052 popup_activate_callback (widget
, id
, client_data
)
1055 XtPointer client_data
;
1057 popup_activated_flag
= 1;
1060 /* This callback is called from the menu bar pulldown menu
1061 when the user makes a selection.
1062 Figure out what the user chose
1063 and put the appropriate events into the keyboard buffer. */
1066 menubar_selection_callback (widget
, id
, client_data
)
1069 XtPointer client_data
;
1072 FRAME_PTR f
= (FRAME_PTR
) id
;
1074 Lisp_Object
*subprefix_stack
;
1075 int submenu_depth
= 0;
1080 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* sizeof (Lisp_Object
));
1081 vector
= f
->menu_bar_vector
;
1084 while (i
< f
->menu_bar_items_used
)
1088 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1090 subprefix_stack
[submenu_depth
++] = prefix
;
1094 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1096 prefix
= subprefix_stack
[--submenu_depth
];
1099 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1102 = XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1103 i
+= MENU_ITEMS_PANE_LENGTH
;
1108 = XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1109 if ((int) client_data
== i
)
1112 struct input_event buf
;
1114 buf
.kind
= menu_bar_event
;
1115 buf
.frame_or_window
= Qmenu_bar
;
1116 kbd_buffer_store_event (&buf
);
1118 for (j
= 0; j
< submenu_depth
; j
++)
1119 if (!NILP (subprefix_stack
[j
]))
1121 buf
.kind
= menu_bar_event
;
1122 buf
.frame_or_window
= subprefix_stack
[j
];
1123 kbd_buffer_store_event (&buf
);
1128 buf
.kind
= menu_bar_event
;
1129 buf
.frame_or_window
= prefix
;
1130 kbd_buffer_store_event (&buf
);
1133 buf
.kind
= menu_bar_event
;
1134 buf
.frame_or_window
= entry
;
1135 kbd_buffer_store_event (&buf
);
1139 i
+= MENU_ITEMS_ITEM_LENGTH
;
1144 /* This callback is invoked when a dialog or menu is finished being
1145 used and has been unposted. */
1148 popup_deactivate_callback (widget
, id
, client_data
)
1151 XtPointer client_data
;
1153 popup_activated_flag
= 0;
1157 /* This recursively calls free_widget_value on the tree of widgets.
1158 It must free all data that was malloc'ed for these widget_values.
1159 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1160 must be left alone. */
1163 free_menubar_widget_value_tree (wv
)
1168 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1170 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1172 free_menubar_widget_value_tree (wv
->contents
);
1173 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1177 free_menubar_widget_value_tree (wv
->next
);
1178 wv
->next
= (widget_value
*) 0xDEADBEEF;
1181 free_widget_value (wv
);
1185 /* Return a tree of widget_value structures for a menu bar item
1186 whose event type is ITEM_KEY (with string ITEM_NAME)
1187 and whose contents come from the list of keymaps MAPS. */
1189 static widget_value
*
1190 single_submenu (item_key
, item_name
, maps
)
1191 Lisp_Object item_key
, item_name
, maps
;
1193 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1195 int submenu_depth
= 0;
1198 Lisp_Object
*mapvec
;
1199 widget_value
**submenu_stack
;
1201 int previous_items
= menu_items_used
;
1203 length
= Flength (maps
);
1204 len
= XINT (length
);
1206 /* Convert the list MAPS into a vector MAPVEC. */
1207 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1208 for (i
= 0; i
< len
; i
++)
1210 mapvec
[i
] = Fcar (maps
);
1214 menu_items_n_panes
= 0;
1216 /* Loop over the given keymaps, making a pane for each map.
1217 But don't make a pane that is empty--ignore that map instead. */
1218 for (i
= 0; i
< len
; i
++)
1219 single_keymap_panes (mapvec
[i
], item_name
, item_key
, 0);
1221 /* Create a tree of widget_value objects
1222 representing the panes and their items. */
1225 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1226 wv
= malloc_widget_value ();
1233 /* Loop over all panes and items made during this call
1234 and construct a tree of widget_value objects.
1235 Ignore the panes and items made by previous calls to
1236 single_submenu, even though those are also in menu_items. */
1238 while (i
< menu_items_used
)
1240 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1242 submenu_stack
[submenu_depth
++] = save_wv
;
1247 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1250 save_wv
= submenu_stack
[--submenu_depth
];
1253 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1254 && submenu_depth
!= 0)
1255 i
+= MENU_ITEMS_PANE_LENGTH
;
1256 /* Ignore a nil in the item list.
1257 It's meaningful only for dialog boxes. */
1258 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1260 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1262 /* Create a new pane. */
1263 Lisp_Object pane_name
, prefix
;
1265 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1266 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1267 pane_string
= (NILP (pane_name
)
1268 ? "" : (char *) XSTRING (pane_name
)->data
);
1269 /* If there is just one top-level pane, put all its items directly
1270 under the top-level menu. */
1271 if (menu_items_n_panes
== 1)
1274 /* If the pane has a meaningful name,
1275 make the pane a top-level menu item
1276 with its items as a submenu beneath it. */
1277 if (strcmp (pane_string
, ""))
1279 wv
= malloc_widget_value ();
1283 first_wv
->contents
= wv
;
1284 wv
->name
= pane_string
;
1292 i
+= MENU_ITEMS_PANE_LENGTH
;
1296 /* Create a new item within current pane. */
1297 Lisp_Object item_name
, enable
, descrip
, def
;
1298 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1299 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1301 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1302 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
1304 wv
= malloc_widget_value ();
1308 save_wv
->contents
= wv
;
1309 wv
->name
= (char *) XSTRING (item_name
)->data
;
1310 if (!NILP (descrip
))
1311 wv
->key
= (char *) XSTRING (descrip
)->data
;
1313 wv
->call_data
= (!NILP (def
) ? (void *) i
: 0);
1314 wv
->enabled
= !NILP (enable
);
1317 i
+= MENU_ITEMS_ITEM_LENGTH
;
1324 extern void EmacsFrameSetCharSize ();
1326 /* Recompute the menu bar of frame F. */
1329 update_frame_menubar (f
)
1332 struct x_display
*x
= f
->display
.x
;
1334 int menubar_changed
;
1336 Dimension shell_height
;
1338 /* We assume the menubar contents has changed if the global flag is set,
1339 or if the current buffer has changed, or if the menubar has never
1340 been updated before.
1342 menubar_changed
= (x
->menubar_widget
1343 && !XtIsManaged (x
->menubar_widget
));
1345 if (! (menubar_changed
))
1349 /* Save the size of the frame because the pane widget doesn't accept to
1350 resize itself. So force it. */
1354 /* Do the voodoo which means "I'm changing lots of things, don't try to
1355 refigure sizes until I'm done." */
1356 lw_refigure_widget (x
->column_widget
, False
);
1358 /* the order in which children are managed is the top to
1359 bottom order in which they are displayed in the paned window.
1360 First, remove the text-area widget.
1362 XtUnmanageChild (x
->edit_widget
);
1364 /* remove the menubar that is there now, and put up the menubar that
1367 if (menubar_changed
)
1369 XtManageChild (x
->menubar_widget
);
1370 XtMapWidget (x
->menubar_widget
);
1371 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1374 /* Re-manage the text-area widget, and then thrash the sizes. */
1375 XtManageChild (x
->edit_widget
);
1376 lw_refigure_widget (x
->column_widget
, True
);
1378 /* Force the pane widget to resize itself with the right values. */
1379 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1385 set_frame_menubar (f
, first_time
)
1389 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
1391 Lisp_Object tail
, items
;
1392 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1397 wv
= malloc_widget_value ();
1398 wv
->name
= "menubar";
1402 items
= FRAME_MENU_BAR_ITEMS (f
);
1403 menu_items
= f
->menu_bar_vector
;
1404 menu_items_allocated
= XVECTOR (menu_items
)->size
;
1407 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1409 Lisp_Object key
, string
, maps
;
1411 key
= XVECTOR (items
)->contents
[i
];
1412 string
= XVECTOR (items
)->contents
[i
+ 1];
1413 maps
= XVECTOR (items
)->contents
[i
+ 2];
1417 wv
= single_submenu (key
, string
, maps
);
1421 first_wv
->contents
= wv
;
1422 /* Don't set wv->name here; GC during the loop might relocate it. */
1427 /* Now GC cannot happen during the lifetime of the widget_value,
1428 so it's safe to store data from a Lisp_String. */
1429 wv
= first_wv
->contents
;
1430 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1433 string
= XVECTOR (items
)->contents
[i
+ 1];
1436 wv
->name
= (char *) XSTRING (string
)->data
;
1440 finish_menu_items ();
1442 f
->menu_bar_vector
= menu_items
;
1443 f
->menu_bar_items_used
= menu_items_used
;
1448 /* Disable resizing (done for Motif!) */
1449 lw_allow_resizing (f
->display
.x
->widget
, False
);
1451 /* The third arg is DEEP_P, which says to consider the entire
1452 menu trees we supply, rather than just the menu bar item names. */
1453 lw_modify_all_widgets (id
, first_wv
, 1);
1455 /* Re-enable the edit widget to resize. */
1456 lw_allow_resizing (f
->display
.x
->widget
, True
);
1460 menubar_widget
= lw_create_widget ("menubar", "menubar",
1462 f
->display
.x
->column_widget
,
1464 popup_activate_callback
,
1465 menubar_selection_callback
,
1466 popup_deactivate_callback
);
1467 f
->display
.x
->menubar_widget
= menubar_widget
;
1470 free_menubar_widget_value_tree (first_wv
);
1472 /* Don't update the menubar the first time it is created via x_window. */
1474 update_frame_menubar (f
);
1479 /* Called from Fx_create_frame to create the inital menubar of a frame
1480 before it is mapped, so that the window is mapped with the menubar already
1481 there instead of us tacking it on later and thrashing the window after it
1485 initialize_frame_menubar (f
)
1488 /* This function is called before the first chance to redisplay
1489 the frame. It has to be, so the frame will have the right size. */
1490 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1491 set_frame_menubar (f
, 1);
1494 /* Get rid of the menu bar of frame F, and free its storage.
1495 This is used when deleting a frame, and when turning off the menu bar. */
1498 free_frame_menubar (f
)
1501 Widget menubar_widget
;
1504 menubar_widget
= f
->display
.x
->menubar_widget
;
1510 lw_destroy_all_widgets (id
);
1515 #endif /* USE_X_TOOLKIT */
1517 /* xmenu_show actually displays a menu using the panes and items in menu_items
1518 and returns the value selected from it.
1519 There are two versions of xmenu_show, one for Xt and one for Xlib.
1520 Both assume input is blocked by the caller. */
1522 /* F is the frame the menu is for.
1523 X and Y are the frame-relative specified position,
1524 relative to the inside upper left corner of the frame F.
1525 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1526 KEYMAPS is 1 if this menu was specified with keymaps;
1527 in that case, we return a list containing the chosen item's value
1528 and perhaps also the pane's prefix.
1529 TITLE is the specified menu title.
1530 ERROR is a place to store an error message string in case of failure.
1531 (We return nil on failure, but the value doesn't actually matter.) */
1533 #ifdef USE_X_TOOLKIT
1535 /* We need a unique id for each widget handled by the Lucid Widget
1536 library. This includes the frame main windows, popup menu and
1538 LWLIB_ID widget_id_tick
;
1541 static Lisp_Object
*volatile menu_item_selection
;
1543 static Lisp_Object
*menu_item_selection
;
1547 popup_selection_callback (widget
, id
, client_data
)
1550 XtPointer client_data
;
1552 menu_item_selection
= (Lisp_Object
*) client_data
;
1556 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
1560 int menubarp
; /* This arg is unused in Xt version. */
1570 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1571 widget_value
**submenu_stack
1572 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1573 Lisp_Object
*subprefix_stack
1574 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1575 int submenu_depth
= 0;
1577 /* Define a queue to save up for later unreading
1578 all X events that don't pertain to the menu. */
1582 struct event_queue
*next
;
1585 struct event_queue
*queue
= NULL
;
1586 struct event_queue
*queue_tmp
;
1588 Position root_x
, root_y
;
1591 int next_release_must_exit
= 0;
1595 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1597 *error
= "Empty menu";
1601 /* Create a tree of widget_value objects
1602 representing the panes and their items. */
1603 wv
= malloc_widget_value ();
1610 /* Loop over all panes and items, filling in the tree. */
1612 while (i
< menu_items_used
)
1614 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1616 submenu_stack
[submenu_depth
++] = save_wv
;
1622 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1625 save_wv
= submenu_stack
[--submenu_depth
];
1629 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1630 && submenu_depth
!= 0)
1631 i
+= MENU_ITEMS_PANE_LENGTH
;
1632 /* Ignore a nil in the item list.
1633 It's meaningful only for dialog boxes. */
1634 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1636 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1638 /* Create a new pane. */
1639 Lisp_Object pane_name
, prefix
;
1641 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1642 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1643 pane_string
= (NILP (pane_name
)
1644 ? "" : (char *) XSTRING (pane_name
)->data
);
1645 /* If there is just one top-level pane, put all its items directly
1646 under the top-level menu. */
1647 if (menu_items_n_panes
== 1)
1650 /* If the pane has a meaningful name,
1651 make the pane a top-level menu item
1652 with its items as a submenu beneath it. */
1653 if (!keymaps
&& strcmp (pane_string
, ""))
1655 wv
= malloc_widget_value ();
1659 first_wv
->contents
= wv
;
1660 wv
->name
= pane_string
;
1661 if (keymaps
&& !NILP (prefix
))
1668 else if (first_pane
)
1674 i
+= MENU_ITEMS_PANE_LENGTH
;
1678 /* Create a new item within current pane. */
1679 Lisp_Object item_name
, enable
, descrip
, def
;
1680 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1681 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1683 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1684 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
1686 wv
= malloc_widget_value ();
1690 save_wv
->contents
= wv
;
1691 wv
->name
= (char *) XSTRING (item_name
)->data
;
1692 if (!NILP (descrip
))
1693 wv
->key
= (char *) XSTRING (descrip
)->data
;
1695 /* If this item has a null value,
1696 make the call_data null so that it won't display a box
1697 when the mouse is on it. */
1699 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
1700 wv
->enabled
= !NILP (enable
);
1703 i
+= MENU_ITEMS_ITEM_LENGTH
;
1707 /* Deal with the title, if it is non-nil. */
1710 widget_value
*wv_title
= malloc_widget_value ();
1711 widget_value
*wv_sep1
= malloc_widget_value ();
1712 widget_value
*wv_sep2
= malloc_widget_value ();
1714 wv_sep2
->name
= "--";
1715 wv_sep2
->next
= first_wv
->contents
;
1717 wv_sep1
->name
= "--";
1718 wv_sep1
->next
= wv_sep2
;
1720 wv_title
->name
= (char *) XSTRING (title
)->data
;
1721 wv_title
->enabled
= True
;
1722 wv_title
->next
= wv_sep1
;
1723 first_wv
->contents
= wv_title
;
1726 /* Actually create the menu. */
1727 menu_id
= ++widget_id_tick
;
1728 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1729 f
->display
.x
->widget
, 1, 0,
1730 popup_selection_callback
,
1731 popup_deactivate_callback
);
1733 /* Don't allow any geometry request from the user. */
1734 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1735 XtSetValues (menu
, av
, ac
);
1737 /* Free the widget_value objects we used to specify the contents. */
1738 free_menubar_widget_value_tree (first_wv
);
1740 /* No selection has been chosen yet. */
1741 menu_item_selection
= 0;
1743 /* Display the menu. */
1744 lw_popup_menu (menu
);
1745 popup_activated_flag
= 1;
1747 /* Process events that apply to the menu. */
1748 popup_get_selection ((XEvent
*) 0);
1751 /* fp turned off the following statement and wrote a comment
1752 that it is unnecessary--that the menu has already disappeared.
1753 I observer that is not so. -- rms. */
1754 /* Make sure the menu disappears. */
1755 lw_destroy_all_widgets (menu_id
);
1757 /* Unread any events that we got but did not handle. */
1758 while (queue
!= NULL
)
1761 XPutBackEvent (FRAME_X_DISPLAY (f
), &queue_tmp
->event
);
1762 queue
= queue_tmp
->next
;
1763 free ((char *)queue_tmp
);
1764 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1765 interrupt_input_pending
= 1;
1768 /* Find the selected item, and its pane, to return
1769 the proper value. */
1770 if (menu_item_selection
!= 0)
1776 while (i
< menu_items_used
)
1780 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1782 subprefix_stack
[submenu_depth
++] = prefix
;
1786 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1788 prefix
= subprefix_stack
[--submenu_depth
];
1791 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1794 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1795 i
+= MENU_ITEMS_PANE_LENGTH
;
1800 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1801 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1807 entry
= Fcons (entry
, Qnil
);
1809 entry
= Fcons (prefix
, entry
);
1810 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1811 if (!NILP (subprefix_stack
[j
]))
1812 entry
= Fcons (subprefix_stack
[j
], entry
);
1816 i
+= MENU_ITEMS_ITEM_LENGTH
;
1825 dialog_selection_callback (widget
, id
, client_data
)
1828 XtPointer client_data
;
1830 if ((int)client_data
!= -1)
1831 menu_item_selection
= (Lisp_Object
*) client_data
;
1833 lw_destroy_all_widgets (id
);
1837 static char * button_names
[] = {
1838 "button1", "button2", "button3", "button4", "button5",
1839 "button6", "button7", "button8", "button9", "button10" };
1842 xdialog_show (f
, menubarp
, keymaps
, title
, error
)
1849 int i
, nb_buttons
=0;
1852 char dialog_name
[6];
1854 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1856 /* Define a queue to save up for later unreading
1857 all X events that don't pertain to the menu. */
1861 struct event_queue
*next
;
1864 struct event_queue
*queue
= NULL
;
1865 struct event_queue
*queue_tmp
;
1867 /* Number of elements seen so far, before boundary. */
1869 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1870 int boundary_seen
= 0;
1874 if (menu_items_n_panes
> 1)
1876 *error
= "Multiple panes in dialog box";
1880 /* Create a tree of widget_value objects
1881 representing the text label and buttons. */
1883 Lisp_Object pane_name
, prefix
;
1885 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
1886 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
1887 pane_string
= (NILP (pane_name
)
1888 ? "" : (char *) XSTRING (pane_name
)->data
);
1889 prev_wv
= malloc_widget_value ();
1890 prev_wv
->value
= pane_string
;
1891 if (keymaps
&& !NILP (prefix
))
1893 prev_wv
->enabled
= 1;
1894 prev_wv
->name
= "message";
1897 /* Loop over all panes and items, filling in the tree. */
1898 i
= MENU_ITEMS_PANE_LENGTH
;
1899 while (i
< menu_items_used
)
1902 /* Create a new item within current pane. */
1903 Lisp_Object item_name
, enable
, descrip
;
1904 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1905 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1907 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1909 if (NILP (item_name
))
1911 free_menubar_widget_value_tree (first_wv
);
1912 *error
= "Submenu in dialog items";
1915 if (EQ (item_name
, Qquote
))
1917 /* This is the boundary between left-side elts
1918 and right-side elts. Stop incrementing right_count. */
1923 if (nb_buttons
>= 10)
1925 free_menubar_widget_value_tree (first_wv
);
1926 *error
= "Too many dialog items";
1930 wv
= malloc_widget_value ();
1932 wv
->name
= (char *) button_names
[nb_buttons
];
1933 if (!NILP (descrip
))
1934 wv
->key
= (char *) XSTRING (descrip
)->data
;
1935 wv
->value
= (char *) XSTRING (item_name
)->data
;
1936 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1937 wv
->enabled
= !NILP (enable
);
1940 if (! boundary_seen
)
1944 i
+= MENU_ITEMS_ITEM_LENGTH
;
1947 /* If the boundary was not specified,
1948 by default put half on the left and half on the right. */
1949 if (! boundary_seen
)
1950 left_count
= nb_buttons
- nb_buttons
/ 2;
1952 wv
= malloc_widget_value ();
1953 wv
->name
= dialog_name
;
1955 /* Dialog boxes use a really stupid name encoding
1956 which specifies how many buttons to use
1957 and how many buttons are on the right.
1958 The Q means something also. */
1959 dialog_name
[0] = 'Q';
1960 dialog_name
[1] = '0' + nb_buttons
;
1961 dialog_name
[2] = 'B';
1962 dialog_name
[3] = 'R';
1963 /* Number of buttons to put on the right. */
1964 dialog_name
[4] = '0' + nb_buttons
- left_count
;
1966 wv
->contents
= first_wv
;
1970 /* Actually create the dialog. */
1971 dialog_id
= ++widget_id_tick
;
1972 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1973 f
->display
.x
->widget
, 1, 0,
1974 dialog_selection_callback
, 0);
1975 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1976 /* Free the widget_value objects we used to specify the contents. */
1977 free_menubar_widget_value_tree (first_wv
);
1979 /* No selection has been chosen yet. */
1980 menu_item_selection
= 0;
1982 /* Display the menu. */
1983 lw_pop_up_all_widgets (dialog_id
);
1985 /* Process events that apply to the menu. */
1990 XtAppNextEvent (Xt_app_con
, &event
);
1991 if (event
.type
== ButtonRelease
)
1993 XtDispatchEvent (&event
);
1996 else if (event
.type
== Expose
)
1997 process_expose_from_menu (event
);
1998 XtDispatchEvent (&event
);
1999 if (XtWindowToWidget (FRAME_X_DISPLAY (f
), event
.xany
.window
) != menu
)
2001 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
2003 if (queue_tmp
!= NULL
)
2005 queue_tmp
->event
= event
;
2006 queue_tmp
->next
= queue
;
2013 #ifdef HAVE_X_WINDOWS
2014 /* State that no mouse buttons are now held.
2015 That is not necessarily true, but the fiction leads to reasonable
2016 results, and it is a pain to ask which are actually held now
2017 or track this in the loop above. */
2018 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2021 /* Unread any events that we got but did not handle. */
2022 while (queue
!= NULL
)
2025 XPutBackEvent (FRAME_X_DISPLAY (f
), &queue_tmp
->event
);
2026 queue
= queue_tmp
->next
;
2027 free ((char *)queue_tmp
);
2028 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
2029 interrupt_input_pending
= 1;
2032 /* Find the selected item, and its pane, to return
2033 the proper value. */
2034 if (menu_item_selection
!= 0)
2040 while (i
< menu_items_used
)
2044 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2047 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2048 i
+= MENU_ITEMS_PANE_LENGTH
;
2053 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2054 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2058 entry
= Fcons (entry
, Qnil
);
2060 entry
= Fcons (prefix
, entry
);
2064 i
+= MENU_ITEMS_ITEM_LENGTH
;
2071 #else /* not USE_X_TOOLKIT */
2074 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
2084 int pane
, selidx
, lpane
, status
;
2085 Lisp_Object entry
, pane_prefix
;
2087 int ulx
, uly
, width
, height
;
2088 int dispwidth
, dispheight
;
2092 unsigned int dummy_uint
;
2095 if (menu_items_n_panes
== 0)
2098 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2100 *error
= "Empty menu";
2104 /* Figure out which root window F is on. */
2105 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2106 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2107 &dummy_uint
, &dummy_uint
);
2109 /* Make the menu on that window. */
2110 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2113 *error
= "Can't create menu";
2117 #ifdef HAVE_X_WINDOWS
2118 /* Adjust coordinates to relative to the outer (window manager) window. */
2121 int win_x
= 0, win_y
= 0;
2123 /* Find the position of the outside upper-left corner of
2124 the inner window, with respect to the outer window. */
2125 if (f
->display
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2128 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2130 /* From-window, to-window. */
2131 f
->display
.x
->window_desc
,
2132 f
->display
.x
->parent_desc
,
2134 /* From-position, to-position. */
2135 0, 0, &win_x
, &win_y
,
2137 /* Child of window. */
2144 #endif /* HAVE_X_WINDOWS */
2146 /* Adjust coordinates to be root-window-relative. */
2147 x
+= f
->display
.x
->left_pos
;
2148 y
+= f
->display
.x
->top_pos
;
2150 /* Create all the necessary panes and their items. */
2152 while (i
< menu_items_used
)
2154 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2156 /* Create a new pane. */
2157 Lisp_Object pane_name
, prefix
;
2160 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2161 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2162 pane_string
= (NILP (pane_name
)
2163 ? "" : (char *) XSTRING (pane_name
)->data
);
2164 if (keymaps
&& !NILP (prefix
))
2167 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
2168 if (lpane
== XM_FAILURE
)
2170 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2171 *error
= "Can't create pane";
2174 i
+= MENU_ITEMS_PANE_LENGTH
;
2176 /* Find the width of the widest item in this pane. */
2179 while (j
< menu_items_used
)
2182 item
= XVECTOR (menu_items
)->contents
[j
];
2190 width
= XSTRING (item
)->size
;
2191 if (width
> maxwidth
)
2194 j
+= MENU_ITEMS_ITEM_LENGTH
;
2197 /* Ignore a nil in the item list.
2198 It's meaningful only for dialog boxes. */
2199 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2203 /* Create a new item within current pane. */
2204 Lisp_Object item_name
, enable
, descrip
;
2205 unsigned char *item_data
;
2207 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2208 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2210 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2211 if (!NILP (descrip
))
2213 int gap
= maxwidth
- XSTRING (item_name
)->size
;
2216 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2217 item_name
= concat2 (item_name
, spacer
);
2218 item_name
= concat2 (item_name
, descrip
);
2219 item_data
= XSTRING (item_name
)->data
;
2221 /* if alloca is fast, use that to make the space,
2222 to reduce gc needs. */
2224 = (unsigned char *) alloca (maxwidth
2225 + XSTRING (descrip
)->size
+ 1);
2226 bcopy (XSTRING (item_name
)->data
, item_data
,
2227 XSTRING (item_name
)->size
);
2228 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2230 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2231 XSTRING (descrip
)->size
);
2232 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2236 item_data
= XSTRING (item_name
)->data
;
2238 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2239 menu
, lpane
, 0, item_data
,
2243 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2244 *error
= "Can't add selection to menu";
2247 i
+= MENU_ITEMS_ITEM_LENGTH
;
2251 /* All set and ready to fly. */
2252 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2253 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
),
2254 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2255 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
),
2256 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2257 x
= min (x
, dispwidth
);
2258 y
= min (y
, dispheight
);
2261 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2262 &ulx
, &uly
, &width
, &height
);
2263 if (ulx
+width
> dispwidth
)
2265 x
-= (ulx
+ width
) - dispwidth
;
2266 ulx
= dispwidth
- width
;
2268 if (uly
+height
> dispheight
)
2270 y
-= (uly
+ height
) - dispheight
;
2271 uly
= dispheight
- height
;
2273 if (ulx
< 0) x
-= ulx
;
2274 if (uly
< 0) y
-= uly
;
2276 XMenuSetAEQ (menu
, TRUE
);
2277 XMenuSetFreeze (menu
, TRUE
);
2280 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2281 x
, y
, ButtonReleaseMask
, &datap
);
2284 /* Assume the mouse has moved out of the X window.
2285 If it has actually moved in, we will get an EnterNotify. */
2292 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2295 /* Find the item number SELIDX in pane number PANE. */
2297 while (i
< menu_items_used
)
2299 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2303 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2305 i
+= MENU_ITEMS_PANE_LENGTH
;
2314 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2317 entry
= Fcons (entry
, Qnil
);
2318 if (!NILP (pane_prefix
))
2319 entry
= Fcons (pane_prefix
, entry
);
2325 i
+= MENU_ITEMS_ITEM_LENGTH
;
2331 *error
= "Can't activate menu";
2337 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2339 #ifdef HAVE_X_WINDOWS
2340 /* State that no mouse buttons are now held.
2341 (The oldXMenu code doesn't track this info for us.)
2342 That is not necessarily true, but the fiction leads to reasonable
2343 results, and it is a pain to ask which are actually held now. */
2344 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2350 #endif /* not USE_X_TOOLKIT */
2354 staticpro (&menu_items
);
2357 #ifdef USE_X_TOOLKIT
2358 widget_id_tick
= (1<<16);
2361 defsubr (&Sx_popup_menu
);
2362 defsubr (&Sx_popup_dialog
);