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"
44 /* This may include sys/types.h, and that somehow loses
45 if this is not done before the other system files. */
48 /* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
51 #include <sys/types.h>
54 #include "dispextern.h"
57 #include "../oldXMenu/XMenu.h"
64 #include <X11/IntrinsicP.h>
65 #include <X11/CoreP.h>
66 #include <X11/StringDefs.h>
67 #include <X11/Xaw/Paned.h>
68 #include "../lwlib/lwlib.h"
69 #include "../lwlib/xlwmenuP.h"
70 #endif /* USE_X_TOOLKIT */
72 #define min(x,y) (((x) < (y)) ? (x) : (y))
73 #define max(x,y) (((x) > (y)) ? (x) : (y))
81 extern Display
*x_current_display
;
83 #define ButtonReleaseMask ButtonReleased
84 #endif /* not HAVE_X11 */
86 /* We need a unique id for each popup menu and dialog box. */
87 static unsigned int popup_id_tick
;
89 extern Lisp_Object Qmenu_enable
;
90 extern Lisp_Object Qmenu_bar
;
93 extern void process_expose_from_menu ();
94 extern XtAppContext Xt_app_con
;
96 static Lisp_Object
xdialog_show ();
99 static Lisp_Object
xmenu_show ();
100 static void keymap_panes ();
101 static void single_keymap_panes ();
102 static void list_of_panes ();
103 static void list_of_items ();
105 /* This holds a Lisp vector that holds the results of decoding
106 the keymaps or alist-of-alists that specify a menu.
108 It describes the panes and items within the panes.
110 Each pane is described by 3 elements in the vector:
111 t, the pane name, the pane's prefix key.
112 Then follow the pane's items, with 4 elements per item:
113 the item string, the enable flag, the item's value,
114 and the equivalent keyboard key's description string.
116 In some cases, multiple levels of menus may be described.
117 A single vector slot containing nil indicates the start of a submenu.
118 A single vector slot containing lambda indicates the end of a submenu.
119 The submenu follows a menu item which is the way to reach the submenu.
121 A single vector slot containing quote indicates that the
122 following items should appear on the right of a dialog box.
124 Using a Lisp vector to hold this information while we decode it
125 takes care of protecting all the data from GC. */
127 #define MENU_ITEMS_PANE_NAME 1
128 #define MENU_ITEMS_PANE_PREFIX 2
129 #define MENU_ITEMS_PANE_LENGTH 3
131 #define MENU_ITEMS_ITEM_NAME 0
132 #define MENU_ITEMS_ITEM_ENABLE 1
133 #define MENU_ITEMS_ITEM_VALUE 2
134 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
135 #define MENU_ITEMS_ITEM_LENGTH 4
137 static Lisp_Object menu_items
;
139 /* Number of slots currently allocated in menu_items. */
140 static int menu_items_allocated
;
142 /* This is the index in menu_items of the first empty slot. */
143 static int menu_items_used
;
145 /* The number of panes currently recorded in menu_items,
146 excluding those within submenus. */
147 static int menu_items_n_panes
;
149 /* Current depth within submenus. */
150 static int menu_items_submenu_depth
;
152 /* Initialize the menu_items structure if we haven't already done so.
153 Also mark it as currently empty. */
158 if (NILP (menu_items
))
160 menu_items_allocated
= 60;
161 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
165 menu_items_n_panes
= 0;
166 menu_items_submenu_depth
= 0;
169 /* Call at the end of generating the data in menu_items.
170 This fills in the number of items in the last pane. */
177 /* Call when finished using the data for the current menu
181 discard_menu_items ()
183 /* Free the structure if it is especially large.
184 Otherwise, hold on to it, to save time. */
185 if (menu_items_allocated
> 200)
188 menu_items_allocated
= 0;
192 /* Make the menu_items vector twice as large. */
198 int old_size
= menu_items_allocated
;
201 menu_items_allocated
*= 2;
202 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
203 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
204 old_size
* sizeof (Lisp_Object
));
207 /* Begin a submenu. */
210 push_submenu_start ()
212 if (menu_items_used
+ 1 > menu_items_allocated
)
215 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
216 menu_items_submenu_depth
++;
224 if (menu_items_used
+ 1 > menu_items_allocated
)
227 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
228 menu_items_submenu_depth
--;
231 /* Indicate boundary between left and right. */
234 push_left_right_boundary ()
236 if (menu_items_used
+ 1 > menu_items_allocated
)
239 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
242 /* Start a new menu pane in menu_items..
243 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
246 push_menu_pane (name
, prefix_vec
)
247 Lisp_Object name
, prefix_vec
;
249 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
252 if (menu_items_submenu_depth
== 0)
253 menu_items_n_panes
++;
254 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
255 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
256 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
259 /* Push one menu item into the current pane.
260 NAME is the string to display. ENABLE if non-nil means
261 this item can be selected. KEY is the key generated by
262 choosing this item. EQUIV is the textual description
263 of the keyboard equivalent for this item (or nil if none). */
266 push_menu_item (name
, enable
, key
, equiv
)
267 Lisp_Object name
, enable
, key
, equiv
;
269 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
272 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
273 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
274 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
275 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
278 /* Figure out the current keyboard equivalent of a menu item ITEM1.
279 The item string for menu display should be ITEM_STRING.
280 Store the equivalent keyboard key sequence's
281 textual description into *DESCRIP_PTR.
282 Also cache them in the item itself.
283 Return the real definition to execute. */
286 menu_item_equiv_key (item_string
, item1
, descrip_ptr
)
287 Lisp_Object item_string
;
289 Lisp_Object
*descrip_ptr
;
291 /* This is the real definition--the function to run. */
293 /* This is the sublist that records cached equiv key data
294 so we can save time. */
295 Lisp_Object cachelist
;
296 /* These are the saved equivalent keyboard key sequence
297 and its key-description. */
298 Lisp_Object savedkey
, descrip
;
301 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
303 /* If a help string follows the item string, skip it. */
304 if (CONSP (XCONS (item1
)->cdr
)
305 && STRINGP (XCONS (XCONS (item1
)->cdr
)->car
))
306 item1
= XCONS (item1
)->cdr
;
310 /* Get out the saved equivalent-keyboard-key info. */
311 cachelist
= savedkey
= descrip
= Qnil
;
312 if (CONSP (def
) && CONSP (XCONS (def
)->car
)
313 && (NILP (XCONS (XCONS (def
)->car
)->car
)
314 || VECTORP (XCONS (XCONS (def
)->car
)->car
)))
316 cachelist
= XCONS (def
)->car
;
317 def
= XCONS (def
)->cdr
;
318 savedkey
= XCONS (cachelist
)->car
;
319 descrip
= XCONS (cachelist
)->cdr
;
322 GCPRO4 (def
, def1
, savedkey
, descrip
);
324 /* Is it still valid? */
326 if (!NILP (savedkey
))
327 def1
= Fkey_binding (savedkey
, Qnil
);
328 /* If not, update it. */
330 /* If the command is an alias for another
331 (such as easymenu.el and lmenu.el set it up),
332 check if the original command matches the cached command. */
333 && !(SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
)
334 && EQ (def1
, XSYMBOL (def
)->function
))
335 /* If something had no key binding before, don't recheck it--
336 doing that takes too much time and makes menus too slow. */
337 && !(!NILP (cachelist
) && NILP (savedkey
)))
341 savedkey
= Fwhere_is_internal (def
, Qnil
, Qt
, Qnil
);
342 /* If the command is an alias for another
343 (such as easymenu.el and lmenu.el set it up),
344 see if the original command name has equivalent keys. */
345 if (SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
))
346 savedkey
= Fwhere_is_internal (XSYMBOL (def
)->function
,
349 if (VECTORP (savedkey
)
350 && EQ (XVECTOR (savedkey
)->contents
[0], Qmenu_bar
))
352 if (!NILP (savedkey
))
354 descrip
= Fkey_description (savedkey
);
355 descrip
= concat2 (make_string (" (", 3), descrip
);
356 descrip
= concat2 (descrip
, make_string (")", 1));
360 /* Cache the data we just got in a sublist of the menu binding. */
361 if (NILP (cachelist
))
362 XCONS (item1
)->cdr
= Fcons (Fcons (savedkey
, descrip
), def
);
365 XCONS (cachelist
)->car
= savedkey
;
366 XCONS (cachelist
)->cdr
= descrip
;
370 *descrip_ptr
= descrip
;
374 /* This is used as the handler when calling internal_condition_case_1. */
377 menu_item_enabled_p_1 (arg
)
383 /* Return non-nil if the command DEF is enabled when used as a menu item.
384 This is based on looking for a menu-enable property.
385 If NOTREAL is set, don't bother really computing this. */
388 menu_item_enabled_p (def
, notreal
)
391 Lisp_Object enabled
, tem
;
396 if (XTYPE (def
) == Lisp_Symbol
)
398 /* No property, or nil, means enable.
399 Otherwise, enable if value is not nil. */
400 tem
= Fget (def
, Qmenu_enable
);
402 /* (condition-case nil (eval tem)
404 enabled
= internal_condition_case_1 (Feval
, tem
, Qerror
,
405 menu_item_enabled_p_1
);
410 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
411 and generate menu panes for them in menu_items.
412 If NOTREAL is nonzero,
413 don't bother really computing whether an item is enabled. */
416 keymap_panes (keymaps
, nmaps
, notreal
)
417 Lisp_Object
*keymaps
;
425 /* Loop over the given keymaps, making a pane for each map.
426 But don't make a pane that is empty--ignore that map instead.
427 P is the number of panes we have made so far. */
428 for (mapno
= 0; mapno
< nmaps
; mapno
++)
429 single_keymap_panes (keymaps
[mapno
], Qnil
, Qnil
, notreal
);
431 finish_menu_items ();
434 /* This is a recursive subroutine of keymap_panes.
435 It handles one keymap, KEYMAP.
436 The other arguments are passed along
437 or point to local variables of the previous function.
438 If NOTREAL is nonzero,
439 don't bother really computing whether an item is enabled. */
442 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
)
444 Lisp_Object pane_name
;
448 Lisp_Object pending_maps
;
449 Lisp_Object tail
, item
, item1
, item_string
, table
;
450 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
454 push_menu_pane (pane_name
, prefix
);
456 for (tail
= keymap
; XTYPE (tail
) == Lisp_Cons
; tail
= XCONS (tail
)->cdr
)
458 /* Look at each key binding, and if it has a menu string,
459 make a menu item from it. */
460 item
= XCONS (tail
)->car
;
461 if (XTYPE (item
) == Lisp_Cons
)
463 item1
= XCONS (item
)->cdr
;
464 if (XTYPE (item1
) == Lisp_Cons
)
466 item_string
= XCONS (item1
)->car
;
467 if (XTYPE (item_string
) == Lisp_String
)
469 /* This is the real definition--the function to run. */
471 /* These are the saved equivalent keyboard key sequence
472 and its key-description. */
474 Lisp_Object tem
, enabled
;
476 /* GCPRO because ...enabled_p will call eval
477 and ..._equiv_key may autoload something.
478 Protecting KEYMAP preserves everything we use;
479 aside from that, must protect whatever might be
480 a string. Since there's no GCPRO5, we refetch
481 item_string instead of protecting it. */
482 descrip
= def
= Qnil
;
483 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
485 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
486 enabled
= menu_item_enabled_p (def
, notreal
);
490 item_string
= XCONS (item1
)->car
;
492 tem
= Fkeymapp (def
);
493 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
494 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, XCONS (item
)->car
)),
499 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
500 submap
= get_keymap_1 (def
, 0, 1);
502 #ifndef USE_X_TOOLKIT
503 /* Indicate visually that this is a submenu. */
505 item_string
= concat2 (item_string
,
506 build_string (" >"));
508 push_menu_item (item_string
, enabled
, XCONS (item
)->car
,
511 /* Display a submenu using the toolkit. */
514 push_submenu_start ();
515 single_keymap_panes (submap
, Qnil
,
516 XCONS (item
)->car
, notreal
);
524 else if (XTYPE (item
) == Lisp_Vector
)
526 /* Loop over the char values represented in the vector. */
527 int len
= XVECTOR (item
)->size
;
529 for (c
= 0; c
< len
; c
++)
531 Lisp_Object character
;
532 XFASTINT (character
) = c
;
533 item1
= XVECTOR (item
)->contents
[c
];
534 if (XTYPE (item1
) == Lisp_Cons
)
536 item_string
= XCONS (item1
)->car
;
537 if (XTYPE (item_string
) == Lisp_String
)
541 /* These are the saved equivalent keyboard key sequence
542 and its key-description. */
544 Lisp_Object tem
, enabled
;
546 /* GCPRO because ...enabled_p will call eval
547 and ..._equiv_key may autoload something.
548 Protecting KEYMAP preserves everything we use;
549 aside from that, must protect whatever might be
550 a string. Since there's no GCPRO5, we refetch
551 item_string instead of protecting it. */
552 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
553 descrip
= def
= Qnil
;
555 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
556 enabled
= menu_item_enabled_p (def
, notreal
);
560 item_string
= XCONS (item1
)->car
;
562 tem
= Fkeymapp (def
);
563 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
564 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, character
)),
569 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
570 submap
= get_keymap_1 (def
, 0, 1);
572 #ifndef USE_X_TOOLKIT
574 item_string
= concat2 (item_string
,
575 build_string (" >"));
577 push_menu_item (item_string
, enabled
, character
,
582 push_submenu_start ();
583 single_keymap_panes (submap
, Qnil
,
595 /* Process now any submenus which want to be panes at this level. */
596 while (!NILP (pending_maps
))
598 Lisp_Object elt
, eltcdr
, string
;
599 elt
= Fcar (pending_maps
);
600 eltcdr
= XCONS (elt
)->cdr
;
601 string
= XCONS (eltcdr
)->car
;
602 /* We no longer discard the @ from the beginning of the string here.
603 Instead, we do this in xmenu_show. */
604 single_keymap_panes (Fcar (elt
), string
,
605 XCONS (eltcdr
)->cdr
, notreal
);
606 pending_maps
= Fcdr (pending_maps
);
610 /* Push all the panes and items of a menu decsribed by the
611 alist-of-alists MENU.
612 This handles old-fashioned calls to x-popup-menu. */
622 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
624 Lisp_Object elt
, pane_name
, pane_data
;
626 pane_name
= Fcar (elt
);
627 CHECK_STRING (pane_name
, 0);
628 push_menu_pane (pane_name
, Qnil
);
629 pane_data
= Fcdr (elt
);
630 CHECK_CONS (pane_data
, 0);
631 list_of_items (pane_data
);
634 finish_menu_items ();
637 /* Push the items in a single pane defined by the alist PANE. */
643 Lisp_Object tail
, item
, item1
;
645 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
649 push_menu_item (item
, Qnil
, Qnil
, Qnil
);
650 else if (NILP (item
))
651 push_left_right_boundary ();
654 CHECK_CONS (item
, 0);
656 CHECK_STRING (item1
, 1);
657 push_menu_item (item1
, Qt
, Fcdr (item
), Qnil
);
662 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
663 "Pop up a deck-of-cards menu and return user's selection.\n\
664 POSITION is a position specification. This is either a mouse button event\n\
665 or a list ((XOFFSET YOFFSET) WINDOW)\n\
666 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
667 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
668 This controls the position of the center of the first line\n\
669 in the first pane of the menu, not the top left of the menu as a whole.\n\
670 If POSITION is t, it means to use the current mouse position.\n\
672 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
673 The menu items come from key bindings that have a menu string as well as\n\
674 a definition; actually, the \"definition\" in such a key binding looks like\n\
675 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
676 the keymap as a top-level element.\n\n\
677 You can also use a list of keymaps as MENU.\n\
678 Then each keymap makes a separate pane.\n\
679 When MENU is a keymap or a list of keymaps, the return value\n\
680 is a list of events.\n\n\
681 Alternatively, you can specify a menu of multiple panes\n\
682 with a list of the form (TITLE PANE1 PANE2...),\n\
683 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
684 Each ITEM is normally a cons cell (STRING . VALUE);\n\
685 but a string can appear as an item--that makes a nonselectable line\n\
687 With this form of menu, the return value is VALUE from the chosen item.\n\
689 If POSITION is nil, don't display the menu at all, just precalculate the\n\
690 cached information about equivalent key sequences.")
692 Lisp_Object position
, menu
;
694 int number_of_panes
, panes
;
695 Lisp_Object keymap
, tem
;
699 Lisp_Object selection
;
702 Lisp_Object x
, y
, window
;
707 if (! NILP (position
))
711 /* Decode the first argument: find the window and the coordinates. */
712 if (EQ (position
, Qt
))
714 /* Use the mouse's current position. */
716 Lisp_Object bar_window
;
720 if (mouse_position_hook
)
721 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
723 XSET (window
, Lisp_Frame
, new_f
);
726 window
= selected_window
;
733 tem
= Fcar (position
);
734 if (XTYPE (tem
) == Lisp_Cons
)
736 window
= Fcar (Fcdr (position
));
738 y
= Fcar (Fcdr (tem
));
742 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
743 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
744 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
748 /* Determine whether this menu is handling a menu bar click. */
749 tem
= Fcar (Fcdr (Fcar (Fcdr (position
))));
750 if (CONSP (tem
) && EQ (Fcar (tem
), Qmenu_bar
))
758 /* Decode where to put the menu. */
760 if (XTYPE (window
) == Lisp_Frame
)
767 else if (XTYPE (window
) == Lisp_Window
)
769 CHECK_LIVE_WINDOW (window
, 0);
770 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
772 xpos
= (FONT_WIDTH (f
->display
.x
->font
) * XWINDOW (window
)->left
);
773 ypos
= (f
->display
.x
->line_height
* XWINDOW (window
)->top
);
776 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
777 but I don't want to make one now. */
778 CHECK_WINDOW (window
, 0);
787 /* Decode the menu items from what was specified. */
789 keymap
= Fkeymapp (menu
);
791 if (XTYPE (menu
) == Lisp_Cons
)
792 tem
= Fkeymapp (Fcar (menu
));
795 /* We were given a keymap. Extract menu info from the keymap. */
797 keymap
= get_keymap (menu
);
799 /* Extract the detailed info to make one pane. */
800 keymap_panes (&menu
, 1, NILP (position
));
802 /* Search for a string appearing directly as an element of the keymap.
803 That string is the title of the menu. */
804 prompt
= map_prompt (keymap
);
806 /* Make that be the pane title of the first pane. */
807 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
808 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
812 else if (!NILP (tem
))
814 /* We were given a list of keymaps. */
815 int nmaps
= XFASTINT (Flength (menu
));
817 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
822 /* The first keymap that has a prompt string
823 supplies the menu title. */
824 for (tem
= menu
, i
= 0; XTYPE (tem
) == Lisp_Cons
; tem
= Fcdr (tem
))
828 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
830 prompt
= map_prompt (keymap
);
831 if (NILP (title
) && !NILP (prompt
))
835 /* Extract the detailed info to make one pane. */
836 keymap_panes (maps
, nmaps
, NILP (position
));
838 /* Make the title be the pane title of the first pane. */
839 if (!NILP (title
) && menu_items_n_panes
>= 0)
840 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
846 /* We were given an old-fashioned menu. */
848 CHECK_STRING (title
, 1);
850 list_of_panes (Fcdr (menu
));
857 discard_menu_items ();
862 /* Display them in a menu. */
865 selection
= xmenu_show (f
, xpos
, ypos
, menubarp
,
866 keymaps
, title
, &error_name
);
869 discard_menu_items ();
873 if (error_name
) error (error_name
);
877 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
878 "Pop up a dialog box and return user's selection.\n\
879 POSITION specifies which frame to use.\n\
880 This is normally a mouse button event or a window or frame.\n\
881 If POSITION is t, it means to use the frame the mouse is on.\n\
882 The dialog box appears in the middle of the specified frame.\n\
884 CONTENTS specifies the alternatives to display in the dialog box.\n\
885 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
886 Each ITEM is a cons cell (STRING . VALUE).\n\
887 The return value is VALUE from the chosen item.\n\n\
888 An ITEM may also be just a string--that makes a nonselectable item.\n\
889 An ITEM may also be nil--that means to put all preceding items\n\
890 on the left of the dialog box and all following items on the right.\n\
891 \(By default, approximately half appear on each side.)")
893 Lisp_Object position
, contents
;
900 /* Decode the first argument: find the window or frame to use. */
901 if (EQ (position
, Qt
))
903 #if 0 /* Using the frame the mouse is on may not be right. */
904 /* Use the mouse's current position. */
906 Lisp_Object bar_window
;
911 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
914 XSET (window
, Lisp_Frame
, new_f
);
916 window
= selected_window
;
918 /* Decode the first argument: find the window and the coordinates. */
919 if (EQ (position
, Qt
))
920 window
= selected_window
;
922 else if (CONSP (position
))
925 tem
= Fcar (position
);
926 if (XTYPE (tem
) == Lisp_Cons
)
927 window
= Fcar (Fcdr (position
));
930 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
931 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
934 else if (WINDOWP (position
) || FRAMEP (position
))
937 /* Decode where to put the menu. */
939 if (XTYPE (window
) == Lisp_Frame
)
941 else if (XTYPE (window
) == Lisp_Window
)
943 CHECK_LIVE_WINDOW (window
, 0);
944 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
947 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
948 but I don't want to make one now. */
949 CHECK_WINDOW (window
, 0);
951 #ifndef USE_X_TOOLKIT
952 /* Display a menu with these alternatives
953 in the middle of frame F. */
955 Lisp_Object x
, y
, frame
, newpos
;
956 XSET (frame
, Lisp_Frame
, f
);
957 XSET (x
, Lisp_Int
, x_pixel_width (f
) / 2);
958 XSET (y
, Lisp_Int
, x_pixel_height (f
) / 2);
959 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
961 return Fx_popup_menu (newpos
,
962 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
968 Lisp_Object selection
;
970 /* Decode the dialog items from what was specified. */
971 title
= Fcar (contents
);
972 CHECK_STRING (title
, 1);
974 list_of_panes (Fcons (contents
, Qnil
));
976 /* Display them in a dialog box. */
978 selection
= xdialog_show (f
, 0, 0, title
, &error_name
);
981 discard_menu_items ();
983 if (error_name
) error (error_name
);
992 dispatch_dummy_expose (w
, x
, y
)
1000 dummy
.window
= XtWindow (w
);
1003 dummy
.send_event
= 0;
1004 dummy
.display
= XtDisplay (w
);
1008 XtDispatchEvent ((XEvent
*) &dummy
);
1012 event_is_in_menu_item (mw
, event
, name
, string_w
)
1014 struct input_event
*event
;
1018 *string_w
+= (string_width (mw
, name
)
1019 + 2 * (mw
->menu
.horizontal_spacing
1020 + mw
->menu
.shadow_thickness
));
1021 return XINT (event
->x
) < *string_w
;
1025 /* Return the menu bar key which corresponds to event EVENT in frame F. */
1028 map_event_to_object (event
, f
)
1029 struct input_event
*event
;
1034 XlwMenuWidget mw
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1039 /* Find the window */
1040 for (val
= mw
->menu
.old_stack
[0]->contents
; val
; val
= val
->next
)
1042 ws
= &mw
->menu
.windows
[0];
1043 if (ws
&& event_is_in_menu_item (mw
, event
, val
->name
, &string_w
))
1048 items
= FRAME_MENU_BAR_ITEMS (f
);
1050 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1052 Lisp_Object pos
, string
, item
;
1053 item
= XVECTOR (items
)->contents
[i
];
1054 string
= XVECTOR (items
)->contents
[i
+ 1];
1055 pos
= XVECTOR (items
)->contents
[i
+ 2];
1059 if (!strcmp (val
->name
, XSTRING (string
)->data
))
1067 static Lisp_Object
*menu_item_selection
;
1070 popup_selection_callback (widget
, id
, client_data
)
1073 XtPointer client_data
;
1075 menu_item_selection
= (Lisp_Object
*) client_data
;
1079 popup_down_callback (widget
, id
, client_data
)
1082 XtPointer client_data
;
1085 lw_destroy_all_widgets (id
);
1090 dialog_selection_callback (widget
, id
, client_data
)
1093 XtPointer client_data
;
1095 if ((int)client_data
!= -1)
1096 menu_item_selection
= (Lisp_Object
*) client_data
;
1098 lw_destroy_all_widgets (id
);
1102 /* This recursively calls free_widget_value() on the tree of widgets.
1103 It must free all data that was malloc'ed for these widget_values.
1104 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1105 must be left alone. */
1108 free_menubar_widget_value_tree (wv
)
1113 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1115 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1117 free_menubar_widget_value_tree (wv
->contents
);
1118 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1122 free_menubar_widget_value_tree (wv
->next
);
1123 wv
->next
= (widget_value
*) 0xDEADBEEF;
1126 free_widget_value (wv
);
1130 extern void EmacsFrameSetCharSize ();
1133 update_frame_menubar (f
)
1136 struct x_display
*x
= f
->display
.x
;
1138 int menubar_changed
;
1140 menubar_changed
= (x
->menubar_widget
1141 && !XtIsManaged (x
->menubar_widget
));
1143 if (! (menubar_changed
))
1147 /* Save the size of the frame because the pane widget doesn't accept to
1148 resize itself. So force it. */
1153 XawPanedSetRefigureMode (x
->column_widget
, 0);
1155 /* the order in which children are managed is the top to
1156 bottom order in which they are displayed in the paned window.
1157 First, remove the text-area widget.
1159 XtUnmanageChild (x
->edit_widget
);
1161 /* remove the menubar that is there now, and put up the menubar that
1164 if (menubar_changed
)
1166 XtManageChild (x
->menubar_widget
);
1167 XtMapWidget (x
->menubar_widget
);
1168 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1172 /* Re-manage the text-area widget */
1173 XtManageChild (x
->edit_widget
);
1175 /* and now thrash the sizes */
1176 XawPanedSetRefigureMode (x
->column_widget
, 1);
1178 /* Force the pane widget to resize itself with the right values. */
1179 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1185 set_frame_menubar (f
, first_time
)
1189 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
1191 Lisp_Object tail
, items
;
1192 widget_value
*wv
, *save_wv
, *first_wv
, *prev_wv
= 0;
1197 wv
= malloc_widget_value ();
1198 wv
->name
= "menubar";
1201 save_wv
= first_wv
= wv
;
1203 if (NILP (items
= FRAME_MENU_BAR_ITEMS (f
)))
1204 items
= FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1206 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1210 string
= XVECTOR (items
)->contents
[i
+ 1];
1214 wv
= malloc_widget_value ();
1218 save_wv
->contents
= wv
;
1219 wv
->name
= (char *) XSTRING (string
)->data
;
1226 lw_modify_all_widgets (id
, first_wv
, False
);
1229 menubar_widget
= lw_create_widget ("menubar", "menubar",
1231 f
->display
.x
->column_widget
,
1234 f
->display
.x
->menubar_widget
= menubar_widget
;
1235 XtVaSetValues (menubar_widget
,
1237 XtNresizeToPreferred
, 1,
1242 free_menubar_widget_value_tree (first_wv
);
1244 /* Don't update the menubar the first time it is created via x_window. */
1246 update_frame_menubar (f
);
1252 free_frame_menubar (f
)
1255 Widget menubar_widget
;
1258 menubar_widget
= f
->display
.x
->menubar_widget
;
1264 lw_destroy_all_widgets (id
);
1268 /* Called from Fx_create_frame to create the inital menubar of a frame
1269 before it is mapped, so that the window is mapped with the menubar already
1270 there instead of us tacking it on later and thrashing the window after it
1273 initialize_frame_menubar (f
)
1276 set_frame_menubar (f
, 1);
1279 /* Horizontal bounds of the current menu bar item. */
1281 static int this_menu_bar_item_beg
;
1282 static int this_menu_bar_item_end
;
1284 /* Horizontal position of the end of the last menu bar item. */
1286 static int last_menu_bar_item_end
;
1288 /* Nonzero if position X, Y is in the menu bar and in some menu bar item
1289 but not in the current menu bar item. */
1292 other_menu_bar_item_p (f
, x
, y
)
1297 && f
->display
.x
->menubar_widget
!= 0
1298 && y
< f
->display
.x
->menubar_widget
->core
.height
1300 && x
< last_menu_bar_item_end
1301 && (x
>= this_menu_bar_item_end
1302 || x
< this_menu_bar_item_beg
));
1305 /* Unread a button-press event in the menu bar of frame F
1306 at x position XPOS relative to the inside of the frame. */
1309 unread_menu_bar_button (f
, xpos
)
1315 event
.type
= ButtonPress
;
1316 event
.xbutton
.display
= x_current_display
;
1317 event
.xbutton
.serial
= 0;
1318 event
.xbutton
.send_event
= 0;
1319 event
.xbutton
.time
= CurrentTime
;
1320 event
.xbutton
.button
= Button1
;
1321 event
.xbutton
.window
= XtWindow (f
->display
.x
->menubar_widget
);
1322 event
.xbutton
.x
= xpos
;
1323 XPutBackEvent (XDISPLAY
&event
);
1326 /* If the mouse has moved to another menu bar item,
1327 return 1 and unread a button press event for that item.
1328 Otherwise return 0. */
1331 check_mouse_other_menu_bar (f
)
1335 Lisp_Object bar_window
;
1340 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
1342 if (f
== new_f
&& other_menu_bar_item_p (f
, x
, y
))
1344 unread_menu_bar_button (f
, x
);
1350 #endif /* USE_X_TOOLKIT */
1352 /* xmenu_show actually displays a menu using the panes and items in menu_items
1353 and returns the value selected from it.
1354 There are two versions of xmenu_show, one for Xt and one for Xlib.
1355 Both assume input is blocked by the caller. */
1357 /* F is the frame the menu is for.
1358 X and Y are the frame-relative specified position,
1359 relative to the inside upper left corner of the frame F.
1360 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1361 KEYMAPS is 1 if this menu was specified with keymaps;
1362 in that case, we return a list containing the chosen item's value
1363 and perhaps also the pane's prefix.
1364 TITLE is the specified menu title.
1365 ERROR is a place to store an error message string in case of failure.
1366 (We return nil on failure, but the value doesn't actually matter.) */
1368 #ifdef USE_X_TOOLKIT
1370 extern unsigned int x_mouse_grabbed
;
1371 extern Lisp_Object Vmouse_depressed
;
1374 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
1386 XlwMenuWidget menubar
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1388 /* This is the menu bar item (if any) that led to this menu. */
1389 widget_value
*menubar_item
= 0;
1391 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1392 widget_value
**submenu_stack
1393 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1394 Lisp_Object
*subprefix_stack
1395 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1396 int submenu_depth
= 0;
1398 /* Define a queue to save up for later unreading
1399 all X events that don't pertain to the menu. */
1403 struct event_queue
*next
;
1406 struct event_queue
*queue
= NULL
;
1407 struct event_queue
*queue_tmp
;
1409 Position root_x
, root_y
;
1415 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1417 *error
= "Empty menu";
1420 this_menu_bar_item_beg
= -1;
1421 this_menu_bar_item_end
= -1;
1422 last_menu_bar_item_end
= -1;
1424 /* Figure out which menu bar item, if any, this menu is for. */
1429 widget_value
*mb_item
= 0;
1431 for (mb_item
= menubar
->menu
.old_stack
[0]->contents
;
1433 mb_item
= mb_item
->next
)
1436 xend
+= (string_width (menubar
, mb_item
->name
)
1437 + 2 * (menubar
->menu
.horizontal_spacing
1438 + menubar
->menu
.shadow_thickness
));
1439 if (x
>= xbeg
&& x
< xend
)
1443 menubar_item
= mb_item
;
1444 /* Arrange to show a different menu if we move in the menu bar
1445 to a different item. */
1446 this_menu_bar_item_beg
= xbeg
;
1447 this_menu_bar_item_end
= xend
;
1450 last_menu_bar_item_end
= xend
;
1452 if (menubar_item
== 0)
1455 /* Offset the coordinates to root-relative. */
1456 if (f
->display
.x
->menubar_widget
!= 0)
1457 y
+= f
->display
.x
->menubar_widget
->core
.height
;
1458 XtTranslateCoords (f
->display
.x
->widget
,
1459 x
, y
, &root_x
, &root_y
);
1463 /* Create a tree of widget_value objects
1464 representing the panes and their items. */
1465 wv
= malloc_widget_value ();
1472 /* Loop over all panes and items, filling in the tree. */
1474 while (i
< menu_items_used
)
1476 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1478 submenu_stack
[submenu_depth
++] = save_wv
;
1484 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1487 save_wv
= submenu_stack
[--submenu_depth
];
1491 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1492 && submenu_depth
!= 0)
1493 i
+= MENU_ITEMS_PANE_LENGTH
;
1494 /* Ignore a nil in the item list.
1495 It's meaningful only for dialog boxes. */
1496 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1498 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1500 /* Create a new pane. */
1501 Lisp_Object pane_name
, prefix
;
1503 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1504 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1505 pane_string
= (NILP (pane_name
)
1506 ? "" : (char *) XSTRING (pane_name
)->data
);
1507 /* If there is just one top-level pane, put all its items directly
1508 under the top-level menu. */
1509 if (menu_items_n_panes
== 1)
1512 /* If the pane has a meaningful name,
1513 make the pane a top-level menu item
1514 with its items as a submenu beneath it. */
1515 if (!keymaps
&& strcmp (pane_string
, ""))
1517 wv
= malloc_widget_value ();
1521 first_wv
->contents
= wv
;
1522 wv
->name
= pane_string
;
1523 if (keymaps
&& !NILP (prefix
))
1530 else if (first_pane
)
1536 i
+= MENU_ITEMS_PANE_LENGTH
;
1540 /* Create a new item within current pane. */
1541 Lisp_Object item_name
, enable
, descrip
;
1542 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1543 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1545 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1547 wv
= malloc_widget_value ();
1551 save_wv
->contents
= wv
;
1552 wv
->name
= (char *) XSTRING (item_name
)->data
;
1553 if (!NILP (descrip
))
1554 wv
->key
= (char *) XSTRING (descrip
)->data
;
1556 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1557 wv
->enabled
= !NILP (enable
);
1560 i
+= MENU_ITEMS_ITEM_LENGTH
;
1564 /* Actually create the menu. */
1565 menu_id
= ++popup_id_tick
;
1566 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1567 f
->display
.x
->widget
, 1, 0,
1568 popup_selection_callback
, popup_down_callback
);
1569 /* Free the widget_value objects we used to specify the contents. */
1570 free_menubar_widget_value_tree (first_wv
);
1572 /* No selection has been chosen yet. */
1573 menu_item_selection
= 0;
1575 /* If the mouse moves out of the menu before we show the menu,
1576 don't show it at all. */
1577 if (check_mouse_other_menu_bar (f
))
1579 lw_destroy_all_widgets (menu_id
);
1584 /* Highlight the menu bar item (if any) that led to this menu. */
1587 menubar_item
->call_data
= (XtPointer
) 1;
1588 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
1591 /* Display the menu. */
1593 XButtonPressedEvent dummy
;
1596 mw
= (XlwMenuWidget
) ((CompositeWidget
)menu
)->composite
.children
[0];
1598 dummy
.type
= ButtonPress
;
1600 dummy
.send_event
= 0;
1601 dummy
.display
= XtDisplay (menu
);
1602 dummy
.window
= XtWindow (XtParent (menu
));
1603 dummy
.time
= CurrentTime
;
1608 /* We activate directly the lucid implementation. */
1609 pop_up_menu (mw
, &dummy
);
1612 /* No need to check a second time since this is done in the XEvent loop.
1613 This slows done the execution. */
1615 /* Check again whether the mouse has moved to another menu bar item. */
1616 if (check_mouse_other_menu_bar (f
))
1618 /* The mouse moved into a different menu bar item.
1619 We should bring up that item's menu instead.
1620 First pop down this menu. */
1621 XtUngrabPointer ((Widget
)
1623 ((CompositeWidget
)menu
)->composite
.children
[0]),
1625 lw_destroy_all_widgets (menu_id
);
1630 /* Process events that apply to the menu. */
1635 XtAppNextEvent (Xt_app_con
, &event
);
1636 if (event
.type
== ButtonRelease
)
1638 XtDispatchEvent (&event
);
1641 /* Do the work of construct_mouse_click since it can't
1642 be called. Initially, the popup menu has been called
1643 from a ButtonPress in the edit_widget. Then the mouse
1644 has been set to grabbed. Reset it now. */
1645 x_mouse_grabbed
&= ~(1 << event
.xbutton
.button
);
1646 if (!x_mouse_grabbed
)
1647 Vmouse_depressed
= Qnil
;
1651 else if (event
.type
== Expose
)
1652 process_expose_from_menu (event
);
1653 else if (event
.type
== MotionNotify
)
1655 int event_x
= (event
.xmotion
.x_root
1656 - (f
->display
.x
->widget
->core
.x
1657 + f
->display
.x
->widget
->core
.border_width
));
1658 int event_y
= (event
.xmotion
.y_root
1659 - (f
->display
.x
->widget
->core
.y
1660 + f
->display
.x
->widget
->core
.border_width
));
1662 if (other_menu_bar_item_p (f
, event_x
, event_y
))
1664 /* The mouse moved into a different menu bar item.
1665 We should bring up that item's menu instead.
1666 First pop down this menu. */
1667 XtUngrabPointer ((Widget
)
1669 ((CompositeWidget
)menu
)->composite
.children
[0]),
1670 event
.xbutton
.time
);
1671 lw_destroy_all_widgets (menu_id
);
1673 /* Put back an event that will bring up the other item's menu. */
1674 unread_menu_bar_button (f
, event_x
);
1675 /* Don't let us select anything in this case. */
1676 menu_item_selection
= 0;
1681 XtDispatchEvent (&event
);
1682 if (XtWindowToWidget(XDISPLAY event
.xany
.window
) != menu
)
1685 = (struct event_queue
*) malloc (sizeof (struct event_queue
));
1687 if (queue_tmp
!= NULL
)
1689 queue_tmp
->event
= event
;
1690 queue_tmp
->next
= queue
;
1697 /* Unhighlight the menu bar item (if any) that led to this menu. */
1700 menubar_item
->call_data
= (XtPointer
) 0;
1701 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
1704 /* fp turned off the following statement and wrote a comment
1705 that it is unnecessary--that the menu has already disappeared.
1706 I observer that is not so. -- rms. */
1707 /* Make sure the menu disappears. */
1708 lw_destroy_all_widgets (menu_id
);
1710 /* Unread any events that we got but did not handle. */
1711 while (queue
!= NULL
)
1714 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
1715 queue
= queue_tmp
->next
;
1716 free ((char *)queue_tmp
);
1717 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1718 interrupt_input_pending
= 1;
1721 /* Find the selected item, and its pane, to return
1722 the proper value. */
1723 if (menu_item_selection
!= 0)
1729 while (i
< menu_items_used
)
1733 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1735 subprefix_stack
[submenu_depth
++] = prefix
;
1739 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1741 prefix
= subprefix_stack
[--submenu_depth
];
1744 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1747 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1748 i
+= MENU_ITEMS_PANE_LENGTH
;
1753 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1754 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1760 entry
= Fcons (entry
, Qnil
);
1762 entry
= Fcons (prefix
, entry
);
1763 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1764 if (!NILP (subprefix_stack
[j
]))
1765 entry
= Fcons (subprefix_stack
[j
], entry
);
1769 i
+= MENU_ITEMS_ITEM_LENGTH
;
1777 static char * button_names
[] = {
1778 "button1", "button2", "button3", "button4", "button5",
1779 "button6", "button7", "button8", "button9", "button10" };
1782 xdialog_show (f
, menubarp
, keymaps
, title
, error
)
1789 int i
, nb_buttons
=0;
1792 XlwMenuWidget menubar
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1793 char dialog_name
[6];
1795 /* This is the menu bar item (if any) that led to this menu. */
1796 widget_value
*menubar_item
= 0;
1798 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1800 /* Define a queue to save up for later unreading
1801 all X events that don't pertain to the menu. */
1805 struct event_queue
*next
;
1808 struct event_queue
*queue
= NULL
;
1809 struct event_queue
*queue_tmp
;
1811 /* Number of elements seen so far, before boundary. */
1813 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1814 int boundary_seen
= 0;
1818 if (menu_items_n_panes
> 1)
1820 *error
= "Multiple panes in dialog box";
1824 /* Create a tree of widget_value objects
1825 representing the text label and buttons. */
1827 Lisp_Object pane_name
, prefix
;
1829 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
1830 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
1831 pane_string
= (NILP (pane_name
)
1832 ? "" : (char *) XSTRING (pane_name
)->data
);
1833 prev_wv
= malloc_widget_value ();
1834 prev_wv
->value
= pane_string
;
1835 if (keymaps
&& !NILP (prefix
))
1837 prev_wv
->enabled
= 1;
1838 prev_wv
->name
= "message";
1841 /* Loop over all panes and items, filling in the tree. */
1842 i
= MENU_ITEMS_PANE_LENGTH
;
1843 while (i
< menu_items_used
)
1846 /* Create a new item within current pane. */
1847 Lisp_Object item_name
, enable
, descrip
;
1848 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1849 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1851 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1853 if (NILP (item_name
))
1855 free_menubar_widget_value_tree (first_wv
);
1856 *error
= "Submenu in dialog items";
1859 if (EQ (item_name
, Qquote
))
1861 /* This is the boundary between left-side elts
1862 and right-side elts. Stop incrementing right_count. */
1867 if (nb_buttons
>= 10)
1869 free_menubar_widget_value_tree (first_wv
);
1870 *error
= "Too many dialog items";
1874 wv
= malloc_widget_value ();
1876 wv
->name
= (char *) button_names
[nb_buttons
];
1877 if (!NILP (descrip
))
1878 wv
->key
= (char *) XSTRING (descrip
)->data
;
1879 wv
->value
= (char *) XSTRING (item_name
)->data
;
1880 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1881 wv
->enabled
= !NILP (enable
);
1884 if (! boundary_seen
)
1888 i
+= MENU_ITEMS_ITEM_LENGTH
;
1891 /* If the boundary was not specified,
1892 by default put half on the left and half on the right. */
1893 if (! boundary_seen
)
1894 left_count
= nb_buttons
- nb_buttons
/ 2;
1896 wv
= malloc_widget_value ();
1897 wv
->name
= dialog_name
;
1899 /* Dialog boxes use a really stupid name encoding
1900 which specifies how many buttons to use
1901 and how many buttons are on the right.
1902 The Q means something also. */
1903 dialog_name
[0] = 'Q';
1904 dialog_name
[1] = '0' + nb_buttons
;
1905 dialog_name
[2] = 'B';
1906 dialog_name
[3] = 'R';
1907 /* Number of buttons to put on the right. */
1908 dialog_name
[4] = '0' + nb_buttons
- left_count
;
1910 wv
->contents
= first_wv
;
1914 /* Actually create the dialog. */
1915 dialog_id
= ++popup_id_tick
;
1916 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1917 f
->display
.x
->widget
, 1, 0,
1918 dialog_selection_callback
, 0);
1919 #if 0 /* This causes crashes, and seems to be redundant -- rms. */
1920 lw_modify_all_widgets (dialog_id
, first_wv
, True
);
1922 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1923 /* Free the widget_value objects we used to specify the contents. */
1924 free_menubar_widget_value_tree (first_wv
);
1926 /* No selection has been chosen yet. */
1927 menu_item_selection
= 0;
1929 /* Display the menu. */
1930 lw_pop_up_all_widgets (dialog_id
);
1932 /* Process events that apply to the menu. */
1937 XtAppNextEvent (Xt_app_con
, &event
);
1938 if (event
.type
== ButtonRelease
)
1940 XtDispatchEvent (&event
);
1943 else if (event
.type
== Expose
)
1944 process_expose_from_menu (event
);
1945 XtDispatchEvent (&event
);
1946 if (XtWindowToWidget(XDISPLAY event
.xany
.window
) != menu
)
1948 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
1950 if (queue_tmp
!= NULL
)
1952 queue_tmp
->event
= event
;
1953 queue_tmp
->next
= queue
;
1960 /* State that no mouse buttons are now held.
1961 That is not necessarily true, but the fiction leads to reasonable
1962 results, and it is a pain to ask which are actually held now
1963 or track this in the loop above. */
1964 x_mouse_grabbed
= 0;
1966 /* Unread any events that we got but did not handle. */
1967 while (queue
!= NULL
)
1970 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
1971 queue
= queue_tmp
->next
;
1972 free ((char *)queue_tmp
);
1973 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1974 interrupt_input_pending
= 1;
1977 /* Find the selected item, and its pane, to return
1978 the proper value. */
1979 if (menu_item_selection
!= 0)
1985 while (i
< menu_items_used
)
1989 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1992 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1993 i
+= MENU_ITEMS_PANE_LENGTH
;
1998 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1999 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2003 entry
= Fcons (entry
, Qnil
);
2005 entry
= Fcons (prefix
, entry
);
2009 i
+= MENU_ITEMS_ITEM_LENGTH
;
2016 #else /* not USE_X_TOOLKIT */
2019 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
2029 int pane
, selidx
, lpane
, status
;
2030 Lisp_Object entry
, pane_prefix
;
2032 int ulx
, uly
, width
, height
;
2033 int dispwidth
, dispheight
;
2037 unsigned int dummy_uint
;
2040 if (menu_items_n_panes
== 0)
2043 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2045 *error
= "Empty menu";
2049 /* Figure out which root window F is on. */
2050 XGetGeometry (x_current_display
, FRAME_X_WINDOW (f
), &root
,
2051 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2052 &dummy_uint
, &dummy_uint
);
2054 /* Make the menu on that window. */
2055 menu
= XMenuCreate (XDISPLAY root
, "emacs");
2058 *error
= "Can't create menu";
2062 /* Adjust coordinates to relative to the outer (window manager) window. */
2066 int win_x
= 0, win_y
= 0;
2068 /* Find the position of the outside upper-left corner of
2069 the inner window, with respect to the outer window. */
2070 if (f
->display
.x
->parent_desc
!= ROOT_WINDOW
)
2073 XTranslateCoordinates (x_current_display
,
2075 /* From-window, to-window. */
2076 f
->display
.x
->window_desc
,
2077 f
->display
.x
->parent_desc
,
2079 /* From-position, to-position. */
2080 0, 0, &win_x
, &win_y
,
2082 /* Child of window. */
2089 #endif /* HAVE_X11 */
2091 /* Adjust coordinates to be root-window-relative. */
2092 x
+= f
->display
.x
->left_pos
;
2093 y
+= f
->display
.x
->top_pos
;
2095 /* Create all the necessary panes and their items. */
2097 while (i
< menu_items_used
)
2099 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2101 /* Create a new pane. */
2102 Lisp_Object pane_name
, prefix
;
2105 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2106 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2107 pane_string
= (NILP (pane_name
)
2108 ? "" : (char *) XSTRING (pane_name
)->data
);
2109 if (keymaps
&& !NILP (prefix
))
2112 lpane
= XMenuAddPane (XDISPLAY menu
, pane_string
, TRUE
);
2113 if (lpane
== XM_FAILURE
)
2115 XMenuDestroy (XDISPLAY menu
);
2116 *error
= "Can't create pane";
2119 i
+= MENU_ITEMS_PANE_LENGTH
;
2121 /* Find the width of the widest item in this pane. */
2124 while (j
< menu_items_used
)
2127 item
= XVECTOR (menu_items
)->contents
[j
];
2135 width
= XSTRING (item
)->size
;
2136 if (width
> maxwidth
)
2139 j
+= MENU_ITEMS_ITEM_LENGTH
;
2142 /* Ignore a nil in the item list.
2143 It's meaningful only for dialog boxes. */
2144 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2148 /* Create a new item within current pane. */
2149 Lisp_Object item_name
, enable
, descrip
;
2150 unsigned char *item_data
;
2152 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2153 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2155 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2156 if (!NILP (descrip
))
2158 int gap
= maxwidth
- XSTRING (item_name
)->size
;
2161 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2162 item_name
= concat2 (item_name
, spacer
);
2163 item_name
= concat2 (item_name
, descrip
);
2164 item_data
= XSTRING (item_name
)->data
;
2166 /* if alloca is fast, use that to make the space,
2167 to reduce gc needs. */
2169 = (unsigned char *) alloca (maxwidth
2170 + XSTRING (descrip
)->size
+ 1);
2171 bcopy (XSTRING (item_name
)->data
, item_data
,
2172 XSTRING (item_name
)->size
);
2173 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2175 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2176 XSTRING (descrip
)->size
);
2177 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2181 item_data
= XSTRING (item_name
)->data
;
2183 if (XMenuAddSelection (XDISPLAY menu
, lpane
, 0, item_data
,
2187 XMenuDestroy (XDISPLAY menu
);
2188 *error
= "Can't add selection to menu";
2191 i
+= MENU_ITEMS_ITEM_LENGTH
;
2195 /* All set and ready to fly. */
2196 XMenuRecompute (XDISPLAY menu
);
2197 dispwidth
= DisplayWidth (x_current_display
, XDefaultScreen (x_current_display
));
2198 dispheight
= DisplayHeight (x_current_display
, XDefaultScreen (x_current_display
));
2199 x
= min (x
, dispwidth
);
2200 y
= min (y
, dispheight
);
2203 XMenuLocate (XDISPLAY menu
, 0, 0, x
, y
,
2204 &ulx
, &uly
, &width
, &height
);
2205 if (ulx
+width
> dispwidth
)
2207 x
-= (ulx
+ width
) - dispwidth
;
2208 ulx
= dispwidth
- width
;
2210 if (uly
+height
> dispheight
)
2212 y
-= (uly
+ height
) - dispheight
;
2213 uly
= dispheight
- height
;
2215 if (ulx
< 0) x
-= ulx
;
2216 if (uly
< 0) y
-= uly
;
2218 XMenuSetAEQ (menu
, TRUE
);
2219 XMenuSetFreeze (menu
, TRUE
);
2222 status
= XMenuActivate (XDISPLAY menu
, &pane
, &selidx
,
2223 x
, y
, ButtonReleaseMask
, &datap
);
2228 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2231 /* Find the item number SELIDX in pane number PANE. */
2233 while (i
< menu_items_used
)
2235 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2239 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2241 i
+= MENU_ITEMS_PANE_LENGTH
;
2250 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2253 entry
= Fcons (entry
, Qnil
);
2254 if (!NILP (pane_prefix
))
2255 entry
= Fcons (pane_prefix
, entry
);
2261 i
+= MENU_ITEMS_ITEM_LENGTH
;
2267 *error
= "Can't activate menu";
2273 XMenuDestroy (XDISPLAY menu
);
2275 /* State that no mouse buttons are now held.
2276 (The oldXMenu code doesn't track this info for us.)
2277 That is not necessarily true, but the fiction leads to reasonable
2278 results, and it is a pain to ask which are actually held now. */
2279 x_mouse_grabbed
= 0;
2283 #endif /* not USE_X_TOOLKIT */
2287 staticpro (&menu_items
);
2290 popup_id_tick
= (1<<16);
2291 defsubr (&Sx_popup_menu
);
2292 defsubr (&Sx_popup_dialog
);