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 Display
*x_current_display
;
82 extern Lisp_Object Qmenu_enable
;
83 extern Lisp_Object Qmenu_bar
;
86 extern void process_expose_from_menu ();
87 extern XtAppContext Xt_app_con
;
89 static Lisp_Object
xdialog_show ();
90 void popup_get_selection ();
93 static Lisp_Object
xmenu_show ();
94 static void keymap_panes ();
95 static void single_keymap_panes ();
96 static void list_of_panes ();
97 static void list_of_items ();
99 /* This holds a Lisp vector that holds the results of decoding
100 the keymaps or alist-of-alists that specify a menu.
102 It describes the panes and items within the panes.
104 Each pane is described by 3 elements in the vector:
105 t, the pane name, the pane's prefix key.
106 Then follow the pane's items, with 4 elements per item:
107 the item string, the enable flag, the item's value,
108 and the equivalent keyboard key's description string.
110 In some cases, multiple levels of menus may be described.
111 A single vector slot containing nil indicates the start of a submenu.
112 A single vector slot containing lambda indicates the end of a submenu.
113 The submenu follows a menu item which is the way to reach the submenu.
115 A single vector slot containing quote indicates that the
116 following items should appear on the right of a dialog box.
118 Using a Lisp vector to hold this information while we decode it
119 takes care of protecting all the data from GC. */
121 #define MENU_ITEMS_PANE_NAME 1
122 #define MENU_ITEMS_PANE_PREFIX 2
123 #define MENU_ITEMS_PANE_LENGTH 3
125 #define MENU_ITEMS_ITEM_NAME 0
126 #define MENU_ITEMS_ITEM_ENABLE 1
127 #define MENU_ITEMS_ITEM_VALUE 2
128 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
129 #define MENU_ITEMS_ITEM_LENGTH 4
131 static Lisp_Object menu_items
;
133 /* Number of slots currently allocated in menu_items. */
134 static int menu_items_allocated
;
136 /* This is the index in menu_items of the first empty slot. */
137 static int menu_items_used
;
139 /* The number of panes currently recorded in menu_items,
140 excluding those within submenus. */
141 static int menu_items_n_panes
;
143 /* Current depth within submenus. */
144 static int menu_items_submenu_depth
;
146 /* Flag which when set indicates a dialog or menu has been posted by
147 Xt on behalf of one of the widget sets. */
148 static int popup_activated_flag
;
151 /* Initialize the menu_items structure if we haven't already done so.
152 Also mark it as currently empty. */
157 if (NILP (menu_items
))
159 menu_items_allocated
= 60;
160 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
164 menu_items_n_panes
= 0;
165 menu_items_submenu_depth
= 0;
168 /* Call at the end of generating the data in menu_items.
169 This fills in the number of items in the last pane. */
176 /* Call when finished using the data for the current menu
180 discard_menu_items ()
182 /* Free the structure if it is especially large.
183 Otherwise, hold on to it, to save time. */
184 if (menu_items_allocated
> 200)
187 menu_items_allocated
= 0;
191 /* Make the menu_items vector twice as large. */
197 int old_size
= menu_items_allocated
;
200 menu_items_allocated
*= 2;
201 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
202 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
203 old_size
* sizeof (Lisp_Object
));
206 /* Begin a submenu. */
209 push_submenu_start ()
211 if (menu_items_used
+ 1 > menu_items_allocated
)
214 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
215 menu_items_submenu_depth
++;
223 if (menu_items_used
+ 1 > menu_items_allocated
)
226 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
227 menu_items_submenu_depth
--;
230 /* Indicate boundary between left and right. */
233 push_left_right_boundary ()
235 if (menu_items_used
+ 1 > menu_items_allocated
)
238 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
241 /* Start a new menu pane in menu_items..
242 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
245 push_menu_pane (name
, prefix_vec
)
246 Lisp_Object name
, prefix_vec
;
248 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
251 if (menu_items_submenu_depth
== 0)
252 menu_items_n_panes
++;
253 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
254 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
255 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
258 /* Push one menu item into the current pane.
259 NAME is the string to display. ENABLE if non-nil means
260 this item can be selected. KEY is the key generated by
261 choosing this item. EQUIV is the textual description
262 of the keyboard equivalent for this item (or nil if none). */
265 push_menu_item (name
, enable
, key
, equiv
)
266 Lisp_Object name
, enable
, key
, equiv
;
268 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
271 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
272 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
273 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
274 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
277 /* Figure out the current keyboard equivalent of a menu item ITEM1.
278 The item string for menu display should be ITEM_STRING.
279 Store the equivalent keyboard key sequence's
280 textual description into *DESCRIP_PTR.
281 Also cache them in the item itself.
282 Return the real definition to execute. */
285 menu_item_equiv_key (item_string
, item1
, descrip_ptr
)
286 Lisp_Object item_string
;
288 Lisp_Object
*descrip_ptr
;
290 /* This is the real definition--the function to run. */
292 /* This is the sublist that records cached equiv key data
293 so we can save time. */
294 Lisp_Object cachelist
;
295 /* These are the saved equivalent keyboard key sequence
296 and its key-description. */
297 Lisp_Object savedkey
, descrip
;
300 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
302 /* If a help string follows the item string, skip it. */
303 if (CONSP (XCONS (item1
)->cdr
)
304 && STRINGP (XCONS (XCONS (item1
)->cdr
)->car
))
305 item1
= XCONS (item1
)->cdr
;
309 /* Get out the saved equivalent-keyboard-key info. */
310 cachelist
= savedkey
= descrip
= Qnil
;
311 if (CONSP (def
) && CONSP (XCONS (def
)->car
)
312 && (NILP (XCONS (XCONS (def
)->car
)->car
)
313 || VECTORP (XCONS (XCONS (def
)->car
)->car
)))
315 cachelist
= XCONS (def
)->car
;
316 def
= XCONS (def
)->cdr
;
317 savedkey
= XCONS (cachelist
)->car
;
318 descrip
= XCONS (cachelist
)->cdr
;
321 GCPRO4 (def
, def1
, savedkey
, descrip
);
323 /* Is it still valid? */
325 if (!NILP (savedkey
))
326 def1
= Fkey_binding (savedkey
, Qnil
);
327 /* If not, update it. */
329 /* If the command is an alias for another
330 (such as easymenu.el and lmenu.el set it up),
331 check if the original command matches the cached command. */
332 && !(SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
)
333 && EQ (def1
, XSYMBOL (def
)->function
))
334 /* If something had no key binding before, don't recheck it--
335 doing that takes too much time and makes menus too slow. */
336 && !(!NILP (cachelist
) && NILP (savedkey
)))
340 savedkey
= Fwhere_is_internal (def
, Qnil
, Qt
, Qnil
);
341 /* If the command is an alias for another
342 (such as easymenu.el and lmenu.el set it up),
343 see if the original command name has equivalent keys. */
344 if (SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
))
345 savedkey
= Fwhere_is_internal (XSYMBOL (def
)->function
,
348 if (VECTORP (savedkey
)
349 && EQ (XVECTOR (savedkey
)->contents
[0], Qmenu_bar
))
351 if (!NILP (savedkey
))
353 descrip
= Fkey_description (savedkey
);
354 descrip
= concat2 (make_string (" (", 3), descrip
);
355 descrip
= concat2 (descrip
, make_string (")", 1));
359 /* Cache the data we just got in a sublist of the menu binding. */
360 if (NILP (cachelist
))
362 CHECK_IMPURE (item1
);
363 XCONS (item1
)->cdr
= Fcons (Fcons (savedkey
, descrip
), def
);
367 XCONS (cachelist
)->car
= savedkey
;
368 XCONS (cachelist
)->cdr
= descrip
;
372 *descrip_ptr
= descrip
;
376 /* This is used as the handler when calling internal_condition_case_1. */
379 menu_item_enabled_p_1 (arg
)
385 /* Return non-nil if the command DEF is enabled when used as a menu item.
386 This is based on looking for a menu-enable property.
387 If NOTREAL is set, don't bother really computing this. */
390 menu_item_enabled_p (def
, notreal
)
394 Lisp_Object enabled
, tem
;
401 /* No property, or nil, means enable.
402 Otherwise, enable if value is not nil. */
403 tem
= Fget (def
, Qmenu_enable
);
405 /* (condition-case nil (eval tem)
407 enabled
= internal_condition_case_1 (Feval
, tem
, Qerror
,
408 menu_item_enabled_p_1
);
413 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
414 and generate menu panes for them in menu_items.
415 If NOTREAL is nonzero,
416 don't bother really computing whether an item is enabled. */
419 keymap_panes (keymaps
, nmaps
, notreal
)
420 Lisp_Object
*keymaps
;
428 /* Loop over the given keymaps, making a pane for each map.
429 But don't make a pane that is empty--ignore that map instead.
430 P is the number of panes we have made so far. */
431 for (mapno
= 0; mapno
< nmaps
; mapno
++)
432 single_keymap_panes (keymaps
[mapno
], Qnil
, Qnil
, notreal
);
434 finish_menu_items ();
437 /* This is a recursive subroutine of keymap_panes.
438 It handles one keymap, KEYMAP.
439 The other arguments are passed along
440 or point to local variables of the previous function.
441 If NOTREAL is nonzero,
442 don't bother really computing whether an item is enabled. */
445 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
)
447 Lisp_Object pane_name
;
451 Lisp_Object pending_maps
;
452 Lisp_Object tail
, item
, item1
, item_string
, table
;
453 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
457 push_menu_pane (pane_name
, prefix
);
459 for (tail
= keymap
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
461 /* Look at each key binding, and if it has a menu string,
462 make a menu item from it. */
463 item
= XCONS (tail
)->car
;
466 item1
= XCONS (item
)->cdr
;
469 item_string
= XCONS (item1
)->car
;
470 if (STRINGP (item_string
))
472 /* This is the real definition--the function to run. */
474 /* These are the saved equivalent keyboard key sequence
475 and its key-description. */
477 Lisp_Object tem
, enabled
;
479 /* GCPRO because ...enabled_p will call eval
480 and ..._equiv_key may autoload something.
481 Protecting KEYMAP preserves everything we use;
482 aside from that, must protect whatever might be
483 a string. Since there's no GCPRO5, we refetch
484 item_string instead of protecting it. */
485 descrip
= def
= Qnil
;
486 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
488 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
489 enabled
= menu_item_enabled_p (def
, notreal
);
493 item_string
= XCONS (item1
)->car
;
495 tem
= Fkeymapp (def
);
496 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
497 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, XCONS (item
)->car
)),
502 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
503 submap
= get_keymap_1 (def
, 0, 1);
505 #ifndef USE_X_TOOLKIT
506 /* Indicate visually that this is a submenu. */
508 item_string
= concat2 (item_string
,
509 build_string (" >"));
511 push_menu_item (item_string
, enabled
, XCONS (item
)->car
,
514 /* Display a submenu using the toolkit. */
517 push_submenu_start ();
518 single_keymap_panes (submap
, Qnil
,
519 XCONS (item
)->car
, notreal
);
527 else if (VECTORP (item
))
529 /* Loop over the char values represented in the vector. */
530 int len
= XVECTOR (item
)->size
;
532 for (c
= 0; c
< len
; c
++)
534 Lisp_Object character
;
535 XSETFASTINT (character
, c
);
536 item1
= XVECTOR (item
)->contents
[c
];
539 item_string
= XCONS (item1
)->car
;
540 if (STRINGP (item_string
))
544 /* These are the saved equivalent keyboard key sequence
545 and its key-description. */
547 Lisp_Object tem
, enabled
;
549 /* GCPRO because ...enabled_p will call eval
550 and ..._equiv_key may autoload something.
551 Protecting KEYMAP preserves everything we use;
552 aside from that, must protect whatever might be
553 a string. Since there's no GCPRO5, we refetch
554 item_string instead of protecting it. */
555 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
556 descrip
= def
= Qnil
;
558 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
559 enabled
= menu_item_enabled_p (def
, notreal
);
563 item_string
= XCONS (item1
)->car
;
565 tem
= Fkeymapp (def
);
566 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
567 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, character
)),
572 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
573 submap
= get_keymap_1 (def
, 0, 1);
575 #ifndef USE_X_TOOLKIT
577 item_string
= concat2 (item_string
,
578 build_string (" >"));
580 push_menu_item (item_string
, enabled
, character
,
585 push_submenu_start ();
586 single_keymap_panes (submap
, Qnil
,
598 /* Process now any submenus which want to be panes at this level. */
599 while (!NILP (pending_maps
))
601 Lisp_Object elt
, eltcdr
, string
;
602 elt
= Fcar (pending_maps
);
603 eltcdr
= XCONS (elt
)->cdr
;
604 string
= XCONS (eltcdr
)->car
;
605 /* We no longer discard the @ from the beginning of the string here.
606 Instead, we do this in xmenu_show. */
607 single_keymap_panes (Fcar (elt
), string
,
608 XCONS (eltcdr
)->cdr
, notreal
);
609 pending_maps
= Fcdr (pending_maps
);
613 /* Push all the panes and items of a menu decsribed by the
614 alist-of-alists MENU.
615 This handles old-fashioned calls to x-popup-menu. */
625 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
627 Lisp_Object elt
, pane_name
, pane_data
;
629 pane_name
= Fcar (elt
);
630 CHECK_STRING (pane_name
, 0);
631 push_menu_pane (pane_name
, Qnil
);
632 pane_data
= Fcdr (elt
);
633 CHECK_CONS (pane_data
, 0);
634 list_of_items (pane_data
);
637 finish_menu_items ();
640 /* Push the items in a single pane defined by the alist PANE. */
646 Lisp_Object tail
, item
, item1
;
648 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
652 push_menu_item (item
, Qnil
, Qnil
, Qnil
);
653 else if (NILP (item
))
654 push_left_right_boundary ();
657 CHECK_CONS (item
, 0);
659 CHECK_STRING (item1
, 1);
660 push_menu_item (item1
, Qt
, Fcdr (item
), Qnil
);
665 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
666 "Pop up a deck-of-cards menu and return user's selection.\n\
667 POSITION is a position specification. This is either a mouse button event\n\
668 or a list ((XOFFSET YOFFSET) WINDOW)\n\
669 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
670 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
671 This controls the position of the center of the first line\n\
672 in the first pane of the menu, not the top left of the menu as a whole.\n\
673 If POSITION is t, it means to use the current mouse position.\n\
675 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
676 The menu items come from key bindings that have a menu string as well as\n\
677 a definition; actually, the \"definition\" in such a key binding looks like\n\
678 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
679 the keymap as a top-level element.\n\n\
680 You can also use a list of keymaps as MENU.\n\
681 Then each keymap makes a separate pane.\n\
682 When MENU is a keymap or a list of keymaps, the return value\n\
683 is a list of events.\n\n\
684 Alternatively, you can specify a menu of multiple panes\n\
685 with a list of the form (TITLE PANE1 PANE2...),\n\
686 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
687 Each ITEM is normally a cons cell (STRING . VALUE);\n\
688 but a string can appear as an item--that makes a nonselectable line\n\
690 With this form of menu, the return value is VALUE from the chosen item.\n\
692 If POSITION is nil, don't display the menu at all, just precalculate the\n\
693 cached information about equivalent key sequences.")
695 Lisp_Object position
, menu
;
697 int number_of_panes
, panes
;
698 Lisp_Object keymap
, tem
;
702 Lisp_Object selection
;
705 Lisp_Object x
, y
, window
;
710 if (! NILP (position
))
714 /* Decode the first argument: find the window and the coordinates. */
715 if (EQ (position
, Qt
))
717 /* Use the mouse's current position. */
719 Lisp_Object bar_window
;
723 if (mouse_position_hook
)
724 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
726 XSETFRAME (window
, new_f
);
729 window
= selected_window
;
736 tem
= Fcar (position
);
739 window
= Fcar (Fcdr (position
));
741 y
= Fcar (Fcdr (tem
));
745 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
746 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
747 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
751 /* Determine whether this menu is handling a menu bar click. */
752 tem
= Fcar (Fcdr (Fcar (Fcdr (position
))));
753 if (CONSP (tem
) && EQ (Fcar (tem
), Qmenu_bar
))
761 /* Decode where to put the menu. */
769 else if (WINDOWP (window
))
771 CHECK_LIVE_WINDOW (window
, 0);
772 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
774 xpos
= (FONT_WIDTH (f
->display
.x
->font
) * XWINDOW (window
)->left
);
775 ypos
= (f
->display
.x
->line_height
* XWINDOW (window
)->top
);
778 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
779 but I don't want to make one now. */
780 CHECK_WINDOW (window
, 0);
789 /* Decode the menu items from what was specified. */
791 keymap
= Fkeymapp (menu
);
794 tem
= Fkeymapp (Fcar (menu
));
797 /* We were given a keymap. Extract menu info from the keymap. */
799 keymap
= get_keymap (menu
);
801 /* Extract the detailed info to make one pane. */
802 keymap_panes (&menu
, 1, NILP (position
));
804 /* Search for a string appearing directly as an element of the keymap.
805 That string is the title of the menu. */
806 prompt
= map_prompt (keymap
);
808 /* Make that be the pane title of the first pane. */
809 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
810 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
814 else if (!NILP (tem
))
816 /* We were given a list of keymaps. */
817 int nmaps
= XFASTINT (Flength (menu
));
819 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
824 /* The first keymap that has a prompt string
825 supplies the menu title. */
826 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= Fcdr (tem
))
830 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
832 prompt
= map_prompt (keymap
);
833 if (NILP (title
) && !NILP (prompt
))
837 /* Extract the detailed info to make one pane. */
838 keymap_panes (maps
, nmaps
, NILP (position
));
840 /* Make the title be the pane title of the first pane. */
841 if (!NILP (title
) && menu_items_n_panes
>= 0)
842 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
848 /* We were given an old-fashioned menu. */
850 CHECK_STRING (title
, 1);
852 list_of_panes (Fcdr (menu
));
859 discard_menu_items ();
864 /* Display them in a menu. */
867 selection
= xmenu_show (f
, xpos
, ypos
, menubarp
,
868 keymaps
, title
, &error_name
);
871 discard_menu_items ();
875 if (error_name
) error (error_name
);
879 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
880 "Pop up a dialog box and return user's selection.\n\
881 POSITION specifies which frame to use.\n\
882 This is normally a mouse button event or a window or frame.\n\
883 If POSITION is t, it means to use the frame the mouse is on.\n\
884 The dialog box appears in the middle of the specified frame.\n\
886 CONTENTS specifies the alternatives to display in the dialog box.\n\
887 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
888 Each ITEM is a cons cell (STRING . VALUE).\n\
889 The return value is VALUE from the chosen item.\n\n\
890 An ITEM may also be just a string--that makes a nonselectable item.\n\
891 An ITEM may also be nil--that means to put all preceding items\n\
892 on the left of the dialog box and all following items on the right.\n\
893 \(By default, approximately half appear on each side.)")
895 Lisp_Object position
, contents
;
902 /* Decode the first argument: find the window or frame to use. */
903 if (EQ (position
, Qt
))
905 #if 0 /* Using the frame the mouse is on may not be right. */
906 /* Use the mouse's current position. */
908 Lisp_Object bar_window
;
913 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
916 XSETFRAME (window
, new_f
);
918 window
= selected_window
;
920 /* Decode the first argument: find the window and the coordinates. */
921 if (EQ (position
, Qt
))
922 window
= selected_window
;
924 else if (CONSP (position
))
927 tem
= Fcar (position
);
929 window
= Fcar (Fcdr (position
));
932 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
933 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
936 else if (WINDOWP (position
) || FRAMEP (position
))
939 /* Decode where to put the menu. */
943 else if (WINDOWP (window
))
945 CHECK_LIVE_WINDOW (window
, 0);
946 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
949 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
950 but I don't want to make one now. */
951 CHECK_WINDOW (window
, 0);
953 #ifndef USE_X_TOOLKIT
954 /* Display a menu with these alternatives
955 in the middle of frame F. */
957 Lisp_Object x
, y
, frame
, newpos
;
958 XSETFRAME (frame
, f
);
959 XSETINT (x
, x_pixel_width (f
) / 2);
960 XSETINT (y
, x_pixel_height (f
) / 2);
961 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
963 return Fx_popup_menu (newpos
,
964 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
970 Lisp_Object selection
;
972 /* Decode the dialog items from what was specified. */
973 title
= Fcar (contents
);
974 CHECK_STRING (title
, 1);
976 list_of_panes (Fcons (contents
, Qnil
));
978 /* Display them in a dialog box. */
980 selection
= xdialog_show (f
, 0, 0, title
, &error_name
);
983 discard_menu_items ();
985 if (error_name
) error (error_name
);
993 /* Loop in Xt until the menu pulldown or dialog popup has been
994 popped down (deactivated).
996 NOTE: All calls to popup_get_selection() should be protected
997 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
999 popup_get_selection (initial_event
)
1000 XEvent
*initial_event
;
1005 event
= *initial_event
;
1007 XtAppNextEvent (Xt_app_con
, &event
);
1011 XtDispatchEvent (&event
);
1012 if (!popup_activated())
1014 XtAppNextEvent (Xt_app_con
, &event
);
1018 /* Detect if a dialog or menu has been posted. */
1022 return popup_activated_flag
;
1026 /* This callback is invoked when the user selects a menubar cascade
1027 pushbutton, but before the pulldown menu is posted. */
1030 popup_activate_callback (widget
, id
, client_data
)
1033 XtPointer client_data
;
1035 popup_activated_flag
= 1;
1038 /* This callback is called from the menu bar pulldown menu
1039 when the user makes a selection.
1040 Figure out what the user chose
1041 and put the appropriate events into the keyboard buffer. */
1044 menubar_selection_callback (widget
, id
, client_data
)
1047 XtPointer client_data
;
1050 FRAME_PTR f
= (FRAME_PTR
) id
;
1052 Lisp_Object
*subprefix_stack
;
1053 int submenu_depth
= 0;
1058 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* sizeof (Lisp_Object
));
1059 vector
= f
->menu_bar_vector
;
1062 while (i
< f
->menu_bar_items_used
)
1066 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1068 subprefix_stack
[submenu_depth
++] = prefix
;
1072 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1074 prefix
= subprefix_stack
[--submenu_depth
];
1077 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1080 = XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1081 i
+= MENU_ITEMS_PANE_LENGTH
;
1086 = XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1087 if ((int) client_data
== i
)
1090 struct input_event buf
;
1092 buf
.kind
= menu_bar_event
;
1093 buf
.frame_or_window
= Qmenu_bar
;
1094 kbd_buffer_store_event (&buf
);
1096 for (j
= 0; j
< submenu_depth
; j
++)
1097 if (!NILP (subprefix_stack
[j
]))
1099 buf
.kind
= menu_bar_event
;
1100 buf
.frame_or_window
= subprefix_stack
[j
];
1101 kbd_buffer_store_event (&buf
);
1106 buf
.kind
= menu_bar_event
;
1107 buf
.frame_or_window
= prefix
;
1108 kbd_buffer_store_event (&buf
);
1111 buf
.kind
= menu_bar_event
;
1112 buf
.frame_or_window
= entry
;
1113 kbd_buffer_store_event (&buf
);
1117 i
+= MENU_ITEMS_ITEM_LENGTH
;
1122 /* This callback is invoked when a dialog or menu is finished being
1123 used and has been unposted. */
1126 popup_deactivate_callback (widget
, id
, client_data
)
1129 XtPointer client_data
;
1131 popup_activated_flag
= 0;
1135 /* This recursively calls free_widget_value on the tree of widgets.
1136 It must free all data that was malloc'ed for these widget_values.
1137 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1138 must be left alone. */
1141 free_menubar_widget_value_tree (wv
)
1146 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1148 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1150 free_menubar_widget_value_tree (wv
->contents
);
1151 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1155 free_menubar_widget_value_tree (wv
->next
);
1156 wv
->next
= (widget_value
*) 0xDEADBEEF;
1159 free_widget_value (wv
);
1163 /* Return a tree of widget_value structures for a menu bar item
1164 whose event type is ITEM_KEY (with string ITEM_NAME)
1165 and whose contents come from the list of keymaps MAPS. */
1167 static widget_value
*
1168 single_submenu (item_key
, item_name
, maps
)
1169 Lisp_Object item_key
, item_name
, maps
;
1171 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1173 int submenu_depth
= 0;
1176 Lisp_Object
*mapvec
;
1177 widget_value
**submenu_stack
;
1179 int previous_items
= menu_items_used
;
1181 length
= Flength (maps
);
1182 len
= XINT (length
);
1184 /* Convert the list MAPS into a vector MAPVEC. */
1185 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1186 for (i
= 0; i
< len
; i
++)
1188 mapvec
[i
] = Fcar (maps
);
1192 menu_items_n_panes
= 0;
1194 /* Loop over the given keymaps, making a pane for each map.
1195 But don't make a pane that is empty--ignore that map instead. */
1196 for (i
= 0; i
< len
; i
++)
1197 single_keymap_panes (mapvec
[i
], item_name
, item_key
, 0);
1199 /* Create a tree of widget_value objects
1200 representing the panes and their items. */
1203 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1204 wv
= malloc_widget_value ();
1211 /* Loop over all panes and items made during this call
1212 and construct a tree of widget_value objects.
1213 Ignore the panes and items made by previous calls to
1214 single_submenu, even though those are also in menu_items. */
1216 while (i
< menu_items_used
)
1218 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1220 submenu_stack
[submenu_depth
++] = save_wv
;
1225 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1228 save_wv
= submenu_stack
[--submenu_depth
];
1231 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1232 && submenu_depth
!= 0)
1233 i
+= MENU_ITEMS_PANE_LENGTH
;
1234 /* Ignore a nil in the item list.
1235 It's meaningful only for dialog boxes. */
1236 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1238 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1240 /* Create a new pane. */
1241 Lisp_Object pane_name
, prefix
;
1243 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1244 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1245 pane_string
= (NILP (pane_name
)
1246 ? "" : (char *) XSTRING (pane_name
)->data
);
1247 /* If there is just one top-level pane, put all its items directly
1248 under the top-level menu. */
1249 if (menu_items_n_panes
== 1)
1252 /* If the pane has a meaningful name,
1253 make the pane a top-level menu item
1254 with its items as a submenu beneath it. */
1255 if (strcmp (pane_string
, ""))
1257 wv
= malloc_widget_value ();
1261 first_wv
->contents
= wv
;
1262 wv
->name
= pane_string
;
1270 i
+= MENU_ITEMS_PANE_LENGTH
;
1274 /* Create a new item within current pane. */
1275 Lisp_Object item_name
, enable
, descrip
;
1276 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1277 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1279 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1281 wv
= malloc_widget_value ();
1285 save_wv
->contents
= wv
;
1286 wv
->name
= (char *) XSTRING (item_name
)->data
;
1287 if (!NILP (descrip
))
1288 wv
->key
= (char *) XSTRING (descrip
)->data
;
1290 wv
->call_data
= (void *) i
;
1291 wv
->enabled
= !NILP (enable
);
1294 i
+= MENU_ITEMS_ITEM_LENGTH
;
1301 extern void EmacsFrameSetCharSize ();
1303 /* Recompute the menu bar of frame F. */
1306 update_frame_menubar (f
)
1309 struct x_display
*x
= f
->display
.x
;
1311 int menubar_changed
;
1313 Dimension shell_height
;
1315 /* We assume the menubar contents has changed if the global flag is set,
1316 or if the current buffer has changed, or if the menubar has never
1317 been updated before.
1319 menubar_changed
= (x
->menubar_widget
1320 && !XtIsManaged (x
->menubar_widget
));
1322 if (! (menubar_changed
))
1326 /* Save the size of the frame because the pane widget doesn't accept to
1327 resize itself. So force it. */
1331 /* Do the voodoo which means "I'm changing lots of things, don't try to
1332 refigure sizes until I'm done." */
1333 lw_refigure_widget (x
->column_widget
, False
);
1335 /* the order in which children are managed is the top to
1336 bottom order in which they are displayed in the paned window.
1337 First, remove the text-area widget.
1339 XtUnmanageChild (x
->edit_widget
);
1341 /* remove the menubar that is there now, and put up the menubar that
1344 if (menubar_changed
)
1346 XtManageChild (x
->menubar_widget
);
1347 XtMapWidget (x
->menubar_widget
);
1348 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1351 /* Re-manage the text-area widget, and then thrash the sizes. */
1352 XtManageChild (x
->edit_widget
);
1353 lw_refigure_widget (x
->column_widget
, True
);
1355 /* Force the pane widget to resize itself with the right values. */
1356 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1362 set_frame_menubar (f
, first_time
)
1366 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
1368 Lisp_Object tail
, items
;
1369 widget_value
*wv
, *save_wv
, *first_wv
, *prev_wv
= 0;
1374 wv
= malloc_widget_value ();
1375 wv
->name
= "menubar";
1378 save_wv
= first_wv
= wv
;
1379 items
= FRAME_MENU_BAR_ITEMS (f
);
1380 menu_items
= f
->menu_bar_vector
;
1381 menu_items_allocated
= XVECTOR (menu_items
)->size
;
1384 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1386 Lisp_Object key
, string
, maps
;
1388 key
= XVECTOR (items
)->contents
[i
];
1389 string
= XVECTOR (items
)->contents
[i
+ 1];
1390 maps
= XVECTOR (items
)->contents
[i
+ 2];
1394 wv
= single_submenu (key
, string
, maps
);
1398 save_wv
->contents
= wv
;
1399 wv
->name
= (char *) XSTRING (string
)->data
;
1404 finish_menu_items ();
1406 f
->menu_bar_vector
= menu_items
;
1407 f
->menu_bar_items_used
= menu_items_used
;
1412 /* Disable resizing (done for Motif!) */
1413 lw_allow_resizing (f
->display
.x
->widget
, False
);
1415 /* The third arg is DEEP_P, which says to consider the entire
1416 menu trees we supply, rather than just the menu bar item names. */
1417 lw_modify_all_widgets (id
, first_wv
, 1);
1419 /* Re-enable the edit widget to resize. */
1420 lw_allow_resizing (f
->display
.x
->widget
, True
);
1424 menubar_widget
= lw_create_widget ("menubar", "menubar",
1426 f
->display
.x
->column_widget
,
1428 popup_activate_callback
,
1429 menubar_selection_callback
,
1430 popup_deactivate_callback
);
1431 f
->display
.x
->menubar_widget
= menubar_widget
;
1434 free_menubar_widget_value_tree (first_wv
);
1436 /* Don't update the menubar the first time it is created via x_window. */
1438 update_frame_menubar (f
);
1443 /* Called from Fx_create_frame to create the inital menubar of a frame
1444 before it is mapped, so that the window is mapped with the menubar already
1445 there instead of us tacking it on later and thrashing the window after it
1449 initialize_frame_menubar (f
)
1452 /* This function is called before the first chance to redisplay
1453 the frame. It has to be, so the frame will have the right size. */
1454 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1455 set_frame_menubar (f
, 1);
1458 /* Get rid of the menu bar of frame F, and free its storage.
1459 This is used when deleting a frame, and when turning off the menu bar. */
1462 free_frame_menubar (f
)
1465 Widget menubar_widget
;
1468 menubar_widget
= f
->display
.x
->menubar_widget
;
1474 lw_destroy_all_widgets (id
);
1479 #endif /* USE_X_TOOLKIT */
1481 /* xmenu_show actually displays a menu using the panes and items in menu_items
1482 and returns the value selected from it.
1483 There are two versions of xmenu_show, one for Xt and one for Xlib.
1484 Both assume input is blocked by the caller. */
1486 /* F is the frame the menu is for.
1487 X and Y are the frame-relative specified position,
1488 relative to the inside upper left corner of the frame F.
1489 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1490 KEYMAPS is 1 if this menu was specified with keymaps;
1491 in that case, we return a list containing the chosen item's value
1492 and perhaps also the pane's prefix.
1493 TITLE is the specified menu title.
1494 ERROR is a place to store an error message string in case of failure.
1495 (We return nil on failure, but the value doesn't actually matter.) */
1497 #ifdef USE_X_TOOLKIT
1499 extern unsigned int x_mouse_grabbed
;
1501 /* We need a unique id for each widget handled by the Lucid Widget
1502 library. This includes the frame main windows, popup menu and
1504 LWLIB_ID widget_id_tick
;
1507 static Lisp_Object
*volatile menu_item_selection
;
1509 static Lisp_Object
*menu_item_selection
;
1513 popup_selection_callback (widget
, id
, client_data
)
1516 XtPointer client_data
;
1518 menu_item_selection
= (Lisp_Object
*) client_data
;
1522 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
1526 int menubarp
; /* Dummy parameter for Xt version of
1537 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1538 widget_value
**submenu_stack
1539 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1540 Lisp_Object
*subprefix_stack
1541 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1542 int submenu_depth
= 0;
1544 /* Define a queue to save up for later unreading
1545 all X events that don't pertain to the menu. */
1549 struct event_queue
*next
;
1552 struct event_queue
*queue
= NULL
;
1553 struct event_queue
*queue_tmp
;
1555 Position root_x
, root_y
;
1558 int next_release_must_exit
= 0;
1562 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1564 *error
= "Empty menu";
1568 /* Create a tree of widget_value objects
1569 representing the panes and their items. */
1570 wv
= malloc_widget_value ();
1577 /* Loop over all panes and items, filling in the tree. */
1579 while (i
< menu_items_used
)
1581 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1583 submenu_stack
[submenu_depth
++] = save_wv
;
1589 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1592 save_wv
= submenu_stack
[--submenu_depth
];
1596 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1597 && submenu_depth
!= 0)
1598 i
+= MENU_ITEMS_PANE_LENGTH
;
1599 /* Ignore a nil in the item list.
1600 It's meaningful only for dialog boxes. */
1601 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1603 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1605 /* Create a new pane. */
1606 Lisp_Object pane_name
, prefix
;
1608 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1609 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1610 pane_string
= (NILP (pane_name
)
1611 ? "" : (char *) XSTRING (pane_name
)->data
);
1612 /* If there is just one top-level pane, put all its items directly
1613 under the top-level menu. */
1614 if (menu_items_n_panes
== 1)
1617 /* If the pane has a meaningful name,
1618 make the pane a top-level menu item
1619 with its items as a submenu beneath it. */
1620 if (!keymaps
&& strcmp (pane_string
, ""))
1622 wv
= malloc_widget_value ();
1626 first_wv
->contents
= wv
;
1627 wv
->name
= pane_string
;
1628 if (keymaps
&& !NILP (prefix
))
1635 else if (first_pane
)
1641 i
+= MENU_ITEMS_PANE_LENGTH
;
1645 /* Create a new item within current pane. */
1646 Lisp_Object item_name
, enable
, descrip
;
1647 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1648 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1650 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1652 wv
= malloc_widget_value ();
1656 save_wv
->contents
= wv
;
1657 wv
->name
= (char *) XSTRING (item_name
)->data
;
1658 if (!NILP (descrip
))
1659 wv
->key
= (char *) XSTRING (descrip
)->data
;
1661 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1662 wv
->enabled
= !NILP (enable
);
1665 i
+= MENU_ITEMS_ITEM_LENGTH
;
1669 /* Deal with the title, if it is non-nil. */
1672 widget_value
*wv_title
= malloc_widget_value ();
1673 widget_value
*wv_sep1
= malloc_widget_value ();
1674 widget_value
*wv_sep2
= malloc_widget_value ();
1676 wv_sep2
->name
= "--";
1677 wv_sep2
->next
= first_wv
->contents
;
1679 wv_sep1
->name
= "--";
1680 wv_sep1
->next
= wv_sep2
;
1682 wv_title
->name
= (char *) XSTRING (title
)->data
;
1683 wv_title
->enabled
= True
;
1684 wv_title
->next
= wv_sep1
;
1685 first_wv
->contents
= wv_title
;
1688 /* Actually create the menu. */
1689 menu_id
= ++widget_id_tick
;
1690 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1691 f
->display
.x
->widget
, 1, 0,
1692 popup_selection_callback
,
1693 popup_deactivate_callback
);
1695 /* Don't allow any geometry request from the user. */
1696 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1697 XtSetValues (menu
, av
, ac
);
1699 /* Free the widget_value objects we used to specify the contents. */
1700 free_menubar_widget_value_tree (first_wv
);
1702 /* No selection has been chosen yet. */
1703 menu_item_selection
= 0;
1705 /* Display the menu. */
1706 lw_popup_menu (menu
);
1707 popup_activated_flag
= 1;
1709 /* Process events that apply to the menu. */
1710 popup_get_selection ((XEvent
*) 0);
1713 /* fp turned off the following statement and wrote a comment
1714 that it is unnecessary--that the menu has already disappeared.
1715 I observer that is not so. -- rms. */
1716 /* Make sure the menu disappears. */
1717 lw_destroy_all_widgets (menu_id
);
1719 /* Unread any events that we got but did not handle. */
1720 while (queue
!= NULL
)
1723 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
1724 queue
= queue_tmp
->next
;
1725 free ((char *)queue_tmp
);
1726 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1727 interrupt_input_pending
= 1;
1730 /* Find the selected item, and its pane, to return
1731 the proper value. */
1732 if (menu_item_selection
!= 0)
1738 while (i
< menu_items_used
)
1742 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1744 subprefix_stack
[submenu_depth
++] = prefix
;
1748 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1750 prefix
= subprefix_stack
[--submenu_depth
];
1753 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1756 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1757 i
+= MENU_ITEMS_PANE_LENGTH
;
1762 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1763 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1769 entry
= Fcons (entry
, Qnil
);
1771 entry
= Fcons (prefix
, entry
);
1772 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1773 if (!NILP (subprefix_stack
[j
]))
1774 entry
= Fcons (subprefix_stack
[j
], entry
);
1778 i
+= MENU_ITEMS_ITEM_LENGTH
;
1787 dialog_selection_callback (widget
, id
, client_data
)
1790 XtPointer client_data
;
1792 if ((int)client_data
!= -1)
1793 menu_item_selection
= (Lisp_Object
*) client_data
;
1795 lw_destroy_all_widgets (id
);
1799 static char * button_names
[] = {
1800 "button1", "button2", "button3", "button4", "button5",
1801 "button6", "button7", "button8", "button9", "button10" };
1804 xdialog_show (f
, menubarp
, keymaps
, title
, error
)
1811 int i
, nb_buttons
=0;
1814 char dialog_name
[6];
1816 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1818 /* Define a queue to save up for later unreading
1819 all X events that don't pertain to the menu. */
1823 struct event_queue
*next
;
1826 struct event_queue
*queue
= NULL
;
1827 struct event_queue
*queue_tmp
;
1829 /* Number of elements seen so far, before boundary. */
1831 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1832 int boundary_seen
= 0;
1836 if (menu_items_n_panes
> 1)
1838 *error
= "Multiple panes in dialog box";
1842 /* Create a tree of widget_value objects
1843 representing the text label and buttons. */
1845 Lisp_Object pane_name
, prefix
;
1847 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
1848 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
1849 pane_string
= (NILP (pane_name
)
1850 ? "" : (char *) XSTRING (pane_name
)->data
);
1851 prev_wv
= malloc_widget_value ();
1852 prev_wv
->value
= pane_string
;
1853 if (keymaps
&& !NILP (prefix
))
1855 prev_wv
->enabled
= 1;
1856 prev_wv
->name
= "message";
1859 /* Loop over all panes and items, filling in the tree. */
1860 i
= MENU_ITEMS_PANE_LENGTH
;
1861 while (i
< menu_items_used
)
1864 /* Create a new item within current pane. */
1865 Lisp_Object item_name
, enable
, descrip
;
1866 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1867 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1869 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1871 if (NILP (item_name
))
1873 free_menubar_widget_value_tree (first_wv
);
1874 *error
= "Submenu in dialog items";
1877 if (EQ (item_name
, Qquote
))
1879 /* This is the boundary between left-side elts
1880 and right-side elts. Stop incrementing right_count. */
1885 if (nb_buttons
>= 10)
1887 free_menubar_widget_value_tree (first_wv
);
1888 *error
= "Too many dialog items";
1892 wv
= malloc_widget_value ();
1894 wv
->name
= (char *) button_names
[nb_buttons
];
1895 if (!NILP (descrip
))
1896 wv
->key
= (char *) XSTRING (descrip
)->data
;
1897 wv
->value
= (char *) XSTRING (item_name
)->data
;
1898 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1899 wv
->enabled
= !NILP (enable
);
1902 if (! boundary_seen
)
1906 i
+= MENU_ITEMS_ITEM_LENGTH
;
1909 /* If the boundary was not specified,
1910 by default put half on the left and half on the right. */
1911 if (! boundary_seen
)
1912 left_count
= nb_buttons
- nb_buttons
/ 2;
1914 wv
= malloc_widget_value ();
1915 wv
->name
= dialog_name
;
1917 /* Dialog boxes use a really stupid name encoding
1918 which specifies how many buttons to use
1919 and how many buttons are on the right.
1920 The Q means something also. */
1921 dialog_name
[0] = 'Q';
1922 dialog_name
[1] = '0' + nb_buttons
;
1923 dialog_name
[2] = 'B';
1924 dialog_name
[3] = 'R';
1925 /* Number of buttons to put on the right. */
1926 dialog_name
[4] = '0' + nb_buttons
- left_count
;
1928 wv
->contents
= first_wv
;
1932 /* Actually create the dialog. */
1933 dialog_id
= ++widget_id_tick
;
1934 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1935 f
->display
.x
->widget
, 1, 0,
1936 dialog_selection_callback
, 0);
1937 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1938 /* Free the widget_value objects we used to specify the contents. */
1939 free_menubar_widget_value_tree (first_wv
);
1941 /* No selection has been chosen yet. */
1942 menu_item_selection
= 0;
1944 /* Display the menu. */
1945 lw_pop_up_all_widgets (dialog_id
);
1947 /* Process events that apply to the menu. */
1952 XtAppNextEvent (Xt_app_con
, &event
);
1953 if (event
.type
== ButtonRelease
)
1955 XtDispatchEvent (&event
);
1958 else if (event
.type
== Expose
)
1959 process_expose_from_menu (event
);
1960 XtDispatchEvent (&event
);
1961 if (XtWindowToWidget(XDISPLAY event
.xany
.window
) != menu
)
1963 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
1965 if (queue_tmp
!= NULL
)
1967 queue_tmp
->event
= event
;
1968 queue_tmp
->next
= queue
;
1975 #ifdef HAVE_X_WINDOWS
1976 /* State that no mouse buttons are now held.
1977 That is not necessarily true, but the fiction leads to reasonable
1978 results, and it is a pain to ask which are actually held now
1979 or track this in the loop above. */
1980 x_mouse_grabbed
= 0;
1983 /* Unread any events that we got but did not handle. */
1984 while (queue
!= NULL
)
1987 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
1988 queue
= queue_tmp
->next
;
1989 free ((char *)queue_tmp
);
1990 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1991 interrupt_input_pending
= 1;
1994 /* Find the selected item, and its pane, to return
1995 the proper value. */
1996 if (menu_item_selection
!= 0)
2002 while (i
< menu_items_used
)
2006 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2009 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2010 i
+= MENU_ITEMS_PANE_LENGTH
;
2015 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2016 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2020 entry
= Fcons (entry
, Qnil
);
2022 entry
= Fcons (prefix
, entry
);
2026 i
+= MENU_ITEMS_ITEM_LENGTH
;
2033 #else /* not USE_X_TOOLKIT */
2036 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
2046 int pane
, selidx
, lpane
, status
;
2047 Lisp_Object entry
, pane_prefix
;
2049 int ulx
, uly
, width
, height
;
2050 int dispwidth
, dispheight
;
2054 unsigned int dummy_uint
;
2057 if (menu_items_n_panes
== 0)
2060 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2062 *error
= "Empty menu";
2066 /* Figure out which root window F is on. */
2067 XGetGeometry (x_current_display
, FRAME_X_WINDOW (f
), &root
,
2068 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2069 &dummy_uint
, &dummy_uint
);
2071 /* Make the menu on that window. */
2072 menu
= XMenuCreate (XDISPLAY root
, "emacs");
2075 *error
= "Can't create menu";
2079 #ifdef HAVE_X_WINDOWS
2080 /* Adjust coordinates to relative to the outer (window manager) window. */
2083 int win_x
= 0, win_y
= 0;
2085 /* Find the position of the outside upper-left corner of
2086 the inner window, with respect to the outer window. */
2087 if (f
->display
.x
->parent_desc
!= ROOT_WINDOW
)
2090 XTranslateCoordinates (x_current_display
,
2092 /* From-window, to-window. */
2093 f
->display
.x
->window_desc
,
2094 f
->display
.x
->parent_desc
,
2096 /* From-position, to-position. */
2097 0, 0, &win_x
, &win_y
,
2099 /* Child of window. */
2106 #endif /* HAVE_X_WINDOWS */
2108 /* Adjust coordinates to be root-window-relative. */
2109 x
+= f
->display
.x
->left_pos
;
2110 y
+= f
->display
.x
->top_pos
;
2112 /* Create all the necessary panes and their items. */
2114 while (i
< menu_items_used
)
2116 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2118 /* Create a new pane. */
2119 Lisp_Object pane_name
, prefix
;
2122 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2123 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2124 pane_string
= (NILP (pane_name
)
2125 ? "" : (char *) XSTRING (pane_name
)->data
);
2126 if (keymaps
&& !NILP (prefix
))
2129 lpane
= XMenuAddPane (XDISPLAY menu
, pane_string
, TRUE
);
2130 if (lpane
== XM_FAILURE
)
2132 XMenuDestroy (XDISPLAY menu
);
2133 *error
= "Can't create pane";
2136 i
+= MENU_ITEMS_PANE_LENGTH
;
2138 /* Find the width of the widest item in this pane. */
2141 while (j
< menu_items_used
)
2144 item
= XVECTOR (menu_items
)->contents
[j
];
2152 width
= XSTRING (item
)->size
;
2153 if (width
> maxwidth
)
2156 j
+= MENU_ITEMS_ITEM_LENGTH
;
2159 /* Ignore a nil in the item list.
2160 It's meaningful only for dialog boxes. */
2161 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2165 /* Create a new item within current pane. */
2166 Lisp_Object item_name
, enable
, descrip
;
2167 unsigned char *item_data
;
2169 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2170 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2172 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2173 if (!NILP (descrip
))
2175 int gap
= maxwidth
- XSTRING (item_name
)->size
;
2178 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2179 item_name
= concat2 (item_name
, spacer
);
2180 item_name
= concat2 (item_name
, descrip
);
2181 item_data
= XSTRING (item_name
)->data
;
2183 /* if alloca is fast, use that to make the space,
2184 to reduce gc needs. */
2186 = (unsigned char *) alloca (maxwidth
2187 + XSTRING (descrip
)->size
+ 1);
2188 bcopy (XSTRING (item_name
)->data
, item_data
,
2189 XSTRING (item_name
)->size
);
2190 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2192 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2193 XSTRING (descrip
)->size
);
2194 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2198 item_data
= XSTRING (item_name
)->data
;
2200 if (XMenuAddSelection (XDISPLAY menu
, lpane
, 0, item_data
,
2204 XMenuDestroy (XDISPLAY menu
);
2205 *error
= "Can't add selection to menu";
2208 i
+= MENU_ITEMS_ITEM_LENGTH
;
2212 /* All set and ready to fly. */
2213 XMenuRecompute (XDISPLAY menu
);
2214 dispwidth
= DisplayWidth (x_current_display
, XDefaultScreen (x_current_display
));
2215 dispheight
= DisplayHeight (x_current_display
, XDefaultScreen (x_current_display
));
2216 x
= min (x
, dispwidth
);
2217 y
= min (y
, dispheight
);
2220 XMenuLocate (XDISPLAY menu
, 0, 0, x
, y
,
2221 &ulx
, &uly
, &width
, &height
);
2222 if (ulx
+width
> dispwidth
)
2224 x
-= (ulx
+ width
) - dispwidth
;
2225 ulx
= dispwidth
- width
;
2227 if (uly
+height
> dispheight
)
2229 y
-= (uly
+ height
) - dispheight
;
2230 uly
= dispheight
- height
;
2232 if (ulx
< 0) x
-= ulx
;
2233 if (uly
< 0) y
-= uly
;
2235 XMenuSetAEQ (menu
, TRUE
);
2236 XMenuSetFreeze (menu
, TRUE
);
2239 status
= XMenuActivate (XDISPLAY menu
, &pane
, &selidx
,
2240 x
, y
, ButtonReleaseMask
, &datap
);
2245 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2248 /* Find the item number SELIDX in pane number PANE. */
2250 while (i
< menu_items_used
)
2252 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2256 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2258 i
+= MENU_ITEMS_PANE_LENGTH
;
2267 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2270 entry
= Fcons (entry
, Qnil
);
2271 if (!NILP (pane_prefix
))
2272 entry
= Fcons (pane_prefix
, entry
);
2278 i
+= MENU_ITEMS_ITEM_LENGTH
;
2284 *error
= "Can't activate menu";
2290 XMenuDestroy (XDISPLAY menu
);
2292 #ifdef HAVE_X_WINDOWS
2293 /* State that no mouse buttons are now held.
2294 (The oldXMenu code doesn't track this info for us.)
2295 That is not necessarily true, but the fiction leads to reasonable
2296 results, and it is a pain to ask which are actually held now. */
2297 x_mouse_grabbed
= 0;
2303 #endif /* not USE_X_TOOLKIT */
2307 staticpro (&menu_items
);
2310 #ifdef USE_X_TOOLKIT
2311 widget_id_tick
= (1<<16);
2314 defsubr (&Sx_popup_menu
);
2315 defsubr (&Sx_popup_dialog
);