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
))
1068 static Lisp_Object
*volatile menu_item_selection
;
1070 static Lisp_Object
*menu_item_selection
;
1074 popup_selection_callback (widget
, id
, client_data
)
1077 XtPointer client_data
;
1079 menu_item_selection
= (Lisp_Object
*) client_data
;
1083 popup_down_callback (widget
, id
, client_data
)
1086 XtPointer client_data
;
1089 lw_destroy_all_widgets (id
);
1094 dialog_selection_callback (widget
, id
, client_data
)
1097 XtPointer client_data
;
1099 if ((int)client_data
!= -1)
1100 menu_item_selection
= (Lisp_Object
*) client_data
;
1102 lw_destroy_all_widgets (id
);
1106 /* This recursively calls free_widget_value() on the tree of widgets.
1107 It must free all data that was malloc'ed for these widget_values.
1108 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1109 must be left alone. */
1112 free_menubar_widget_value_tree (wv
)
1117 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1119 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1121 free_menubar_widget_value_tree (wv
->contents
);
1122 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1126 free_menubar_widget_value_tree (wv
->next
);
1127 wv
->next
= (widget_value
*) 0xDEADBEEF;
1130 free_widget_value (wv
);
1134 extern void EmacsFrameSetCharSize ();
1137 update_frame_menubar (f
)
1140 struct x_display
*x
= f
->display
.x
;
1142 int menubar_changed
;
1144 menubar_changed
= (x
->menubar_widget
1145 && !XtIsManaged (x
->menubar_widget
));
1147 if (! (menubar_changed
))
1151 /* Save the size of the frame because the pane widget doesn't accept to
1152 resize itself. So force it. */
1157 XawPanedSetRefigureMode (x
->column_widget
, 0);
1159 /* the order in which children are managed is the top to
1160 bottom order in which they are displayed in the paned window.
1161 First, remove the text-area widget.
1163 XtUnmanageChild (x
->edit_widget
);
1165 /* remove the menubar that is there now, and put up the menubar that
1168 if (menubar_changed
)
1170 XtManageChild (x
->menubar_widget
);
1171 XtMapWidget (x
->menubar_widget
);
1172 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1176 /* Re-manage the text-area widget */
1177 XtManageChild (x
->edit_widget
);
1179 /* and now thrash the sizes */
1180 XawPanedSetRefigureMode (x
->column_widget
, 1);
1182 /* Force the pane widget to resize itself with the right values. */
1183 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1189 set_frame_menubar (f
, first_time
)
1193 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
1195 Lisp_Object tail
, items
;
1196 widget_value
*wv
, *save_wv
, *first_wv
, *prev_wv
= 0;
1201 wv
= malloc_widget_value ();
1202 wv
->name
= "menubar";
1205 save_wv
= first_wv
= wv
;
1207 if (NILP (items
= FRAME_MENU_BAR_ITEMS (f
)))
1208 items
= FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1210 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1214 string
= XVECTOR (items
)->contents
[i
+ 1];
1218 wv
= malloc_widget_value ();
1222 save_wv
->contents
= wv
;
1223 wv
->name
= (char *) XSTRING (string
)->data
;
1230 lw_modify_all_widgets (id
, first_wv
, False
);
1233 menubar_widget
= lw_create_widget ("menubar", "menubar",
1235 f
->display
.x
->column_widget
,
1238 f
->display
.x
->menubar_widget
= menubar_widget
;
1239 XtVaSetValues (menubar_widget
,
1241 XtNresizeToPreferred
, 1,
1246 free_menubar_widget_value_tree (first_wv
);
1248 /* Don't update the menubar the first time it is created via x_window. */
1250 update_frame_menubar (f
);
1256 free_frame_menubar (f
)
1259 Widget menubar_widget
;
1262 menubar_widget
= f
->display
.x
->menubar_widget
;
1268 lw_destroy_all_widgets (id
);
1272 /* Called from Fx_create_frame to create the inital menubar of a frame
1273 before it is mapped, so that the window is mapped with the menubar already
1274 there instead of us tacking it on later and thrashing the window after it
1277 initialize_frame_menubar (f
)
1280 set_frame_menubar (f
, 1);
1283 /* Horizontal bounds of the current menu bar item. */
1285 static int this_menu_bar_item_beg
;
1286 static int this_menu_bar_item_end
;
1288 /* Horizontal position of the end of the last menu bar item. */
1290 static int last_menu_bar_item_end
;
1292 /* Nonzero if position X, Y is in the menu bar and in some menu bar item
1293 but not in the current menu bar item. */
1296 other_menu_bar_item_p (f
, x
, y
)
1301 && f
->display
.x
->menubar_widget
!= 0
1302 && y
< f
->display
.x
->menubar_widget
->core
.height
1304 && x
< last_menu_bar_item_end
1305 && (x
>= this_menu_bar_item_end
1306 || x
< this_menu_bar_item_beg
));
1309 /* Unread a button-press event in the menu bar of frame F
1310 at x position XPOS relative to the inside of the frame. */
1313 unread_menu_bar_button (f
, xpos
)
1319 event
.type
= ButtonPress
;
1320 event
.xbutton
.display
= x_current_display
;
1321 event
.xbutton
.serial
= 0;
1322 event
.xbutton
.send_event
= 0;
1323 event
.xbutton
.time
= CurrentTime
;
1324 event
.xbutton
.button
= Button1
;
1325 event
.xbutton
.window
= XtWindow (f
->display
.x
->menubar_widget
);
1326 event
.xbutton
.x
= xpos
;
1327 XPutBackEvent (XDISPLAY
&event
);
1330 /* If the mouse has moved to another menu bar item,
1331 return 1 and unread a button press event for that item.
1332 Otherwise return 0. */
1335 check_mouse_other_menu_bar (f
)
1339 Lisp_Object bar_window
;
1344 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
1346 if (f
== new_f
&& other_menu_bar_item_p (f
, x
, y
))
1348 unread_menu_bar_button (f
, x
);
1354 #endif /* USE_X_TOOLKIT */
1356 /* xmenu_show actually displays a menu using the panes and items in menu_items
1357 and returns the value selected from it.
1358 There are two versions of xmenu_show, one for Xt and one for Xlib.
1359 Both assume input is blocked by the caller. */
1361 /* F is the frame the menu is for.
1362 X and Y are the frame-relative specified position,
1363 relative to the inside upper left corner of the frame F.
1364 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1365 KEYMAPS is 1 if this menu was specified with keymaps;
1366 in that case, we return a list containing the chosen item's value
1367 and perhaps also the pane's prefix.
1368 TITLE is the specified menu title.
1369 ERROR is a place to store an error message string in case of failure.
1370 (We return nil on failure, but the value doesn't actually matter.) */
1372 #ifdef USE_X_TOOLKIT
1374 extern unsigned last_event_timestamp
;
1375 extern Lisp_Object Vdouble_click_time
;
1377 extern unsigned int x_mouse_grabbed
;
1378 extern Lisp_Object Vmouse_depressed
;
1381 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
1393 XlwMenuWidget menubar
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1395 /* This is the menu bar item (if any) that led to this menu. */
1396 widget_value
*menubar_item
= 0;
1398 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1399 widget_value
**submenu_stack
1400 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1401 Lisp_Object
*subprefix_stack
1402 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1403 int submenu_depth
= 0;
1405 /* Define a queue to save up for later unreading
1406 all X events that don't pertain to the menu. */
1410 struct event_queue
*next
;
1413 struct event_queue
*queue
= NULL
;
1414 struct event_queue
*queue_tmp
;
1416 Position root_x
, root_y
;
1419 int next_release_must_exit
= 0;
1423 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1425 *error
= "Empty menu";
1428 this_menu_bar_item_beg
= -1;
1429 this_menu_bar_item_end
= -1;
1430 last_menu_bar_item_end
= -1;
1432 /* Figure out which menu bar item, if any, this menu is for. */
1437 widget_value
*mb_item
= 0;
1439 for (mb_item
= menubar
->menu
.old_stack
[0]->contents
;
1441 mb_item
= mb_item
->next
)
1444 xend
+= (string_width (menubar
, mb_item
->name
)
1445 + 2 * (menubar
->menu
.horizontal_spacing
1446 + menubar
->menu
.shadow_thickness
));
1447 if (x
>= xbeg
&& x
< xend
)
1451 menubar_item
= mb_item
;
1452 /* Arrange to show a different menu if we move in the menu bar
1453 to a different item. */
1454 this_menu_bar_item_beg
= xbeg
;
1455 this_menu_bar_item_end
= xend
;
1458 last_menu_bar_item_end
= xend
;
1460 if (menubar_item
== 0)
1463 /* Offset the coordinates to root-relative. */
1464 if (f
->display
.x
->menubar_widget
!= 0)
1465 y
+= f
->display
.x
->menubar_widget
->core
.height
;
1466 XtTranslateCoords (f
->display
.x
->widget
,
1467 x
, y
, &root_x
, &root_y
);
1471 /* Create a tree of widget_value objects
1472 representing the panes and their items. */
1473 wv
= malloc_widget_value ();
1480 /* Loop over all panes and items, filling in the tree. */
1482 while (i
< menu_items_used
)
1484 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1486 submenu_stack
[submenu_depth
++] = save_wv
;
1492 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1495 save_wv
= submenu_stack
[--submenu_depth
];
1499 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1500 && submenu_depth
!= 0)
1501 i
+= MENU_ITEMS_PANE_LENGTH
;
1502 /* Ignore a nil in the item list.
1503 It's meaningful only for dialog boxes. */
1504 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1506 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1508 /* Create a new pane. */
1509 Lisp_Object pane_name
, prefix
;
1511 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1512 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1513 pane_string
= (NILP (pane_name
)
1514 ? "" : (char *) XSTRING (pane_name
)->data
);
1515 /* If there is just one top-level pane, put all its items directly
1516 under the top-level menu. */
1517 if (menu_items_n_panes
== 1)
1520 /* If the pane has a meaningful name,
1521 make the pane a top-level menu item
1522 with its items as a submenu beneath it. */
1523 if (!keymaps
&& strcmp (pane_string
, ""))
1525 wv
= malloc_widget_value ();
1529 first_wv
->contents
= wv
;
1530 wv
->name
= pane_string
;
1531 if (keymaps
&& !NILP (prefix
))
1538 else if (first_pane
)
1544 i
+= MENU_ITEMS_PANE_LENGTH
;
1548 /* Create a new item within current pane. */
1549 Lisp_Object item_name
, enable
, descrip
;
1550 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1551 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1553 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1555 wv
= malloc_widget_value ();
1559 save_wv
->contents
= wv
;
1560 wv
->name
= (char *) XSTRING (item_name
)->data
;
1561 if (!NILP (descrip
))
1562 wv
->key
= (char *) XSTRING (descrip
)->data
;
1564 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1565 wv
->enabled
= !NILP (enable
);
1568 i
+= MENU_ITEMS_ITEM_LENGTH
;
1572 /* Actually create the menu. */
1573 menu_id
= ++popup_id_tick
;
1574 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1575 f
->display
.x
->widget
, 1, 0,
1576 popup_selection_callback
, popup_down_callback
);
1577 /* Free the widget_value objects we used to specify the contents. */
1578 free_menubar_widget_value_tree (first_wv
);
1580 /* No selection has been chosen yet. */
1581 menu_item_selection
= 0;
1583 /* If the mouse moves out of the menu before we show the menu,
1584 don't show it at all. */
1585 if (check_mouse_other_menu_bar (f
))
1587 lw_destroy_all_widgets (menu_id
);
1592 /* Highlight the menu bar item (if any) that led to this menu. */
1595 menubar_item
->call_data
= (XtPointer
) 1;
1596 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
1599 /* Display the menu. */
1601 XButtonPressedEvent dummy
;
1604 mw
= (XlwMenuWidget
) ((CompositeWidget
)menu
)->composite
.children
[0];
1606 dummy
.type
= ButtonPress
;
1608 dummy
.send_event
= 0;
1609 dummy
.display
= XtDisplay (menu
);
1610 dummy
.window
= XtWindow (XtParent (menu
));
1611 dummy
.time
= CurrentTime
;
1616 /* We activate directly the lucid implementation. */
1617 pop_up_menu (mw
, &dummy
);
1620 /* No need to check a second time since this is done in the XEvent loop.
1621 This slows done the execution. */
1623 /* Check again whether the mouse has moved to another menu bar item. */
1624 if (check_mouse_other_menu_bar (f
))
1626 /* The mouse moved into a different menu bar item.
1627 We should bring up that item's menu instead.
1628 First pop down this menu. */
1629 #if 0 /* xlwmenu.c now does this. */
1630 XtUngrabPointer ((Widget
)
1632 ((CompositeWidget
)menu
)->composite
.children
[0]),
1635 lw_destroy_all_widgets (menu_id
);
1640 /* Process events that apply to the menu. */
1644 int queue_and_exit
= 0;
1646 XtAppNextEvent (Xt_app_con
, &event
);
1647 if (event
.type
== ButtonRelease
)
1649 XtDispatchEvent (&event
);
1652 /* Do the work of construct_mouse_click since it can't
1653 be called. Initially, the popup menu has been called
1654 from a ButtonPress in the edit_widget. Then the mouse
1655 has been set to grabbed. Reset it now. */
1656 x_mouse_grabbed
&= ~(1 << event
.xbutton
.button
);
1657 if (!x_mouse_grabbed
)
1658 Vmouse_depressed
= Qnil
;
1660 if (! (menu_item_selection
== 0
1661 && !next_release_must_exit
1662 && (((XButtonEvent
*) (&event
))->time
- last_event_timestamp
1663 < XINT (Vdouble_click_time
))))
1665 /* Don't call XtDispatchEvent again for the same event! */
1668 else if (event
.type
== ButtonPress
)
1670 next_release_must_exit
= 1;
1672 XtDispatchEvent (&event
);
1673 /* Any mouse button activity that doesn't select in the menu
1674 should unpost the menu. */
1675 if (menu_item_selection
== 0)
1678 /* Don't call XtDispatchEvent for the down event.
1679 Doing so seems to give strange results
1680 when you click on the menu bar while a menu is posted. */
1683 else if (event
.type
== KeyPress
)
1685 /* Exit the loop, but first queue this event for reuse. */
1688 else if (event
.type
== Expose
)
1689 process_expose_from_menu (event
);
1690 /* If the mouse moves to a different menu bar item, switch to
1691 that item's menu. But only if the button is still held down. */
1692 else if (event
.type
== MotionNotify
1695 int event_x
= (event
.xmotion
.x_root
1696 - (f
->display
.x
->widget
->core
.x
1697 + f
->display
.x
->widget
->core
.border_width
));
1698 int event_y
= (event
.xmotion
.y_root
1699 - (f
->display
.x
->widget
->core
.y
1700 + f
->display
.x
->widget
->core
.border_width
));
1702 if (other_menu_bar_item_p (f
, event_x
, event_y
))
1704 /* The mouse moved into a different menu bar item.
1705 We should bring up that item's menu instead.
1706 First pop down this menu. */
1707 #if 0 /* xlwmenu.c now does this. */
1708 XtUngrabPointer ((Widget
)
1710 ((CompositeWidget
)menu
)->composite
.children
[0]),
1711 event
.xbutton
.time
);
1713 lw_destroy_all_widgets (menu_id
);
1715 /* Put back an event that will bring up the other item's menu. */
1716 unread_menu_bar_button (f
, event_x
);
1717 /* Don't let us select anything in this case. */
1718 menu_item_selection
= 0;
1722 else if (event
.type
== UnmapNotify
)
1724 /* If the menu disappears, there is no need to stay in the
1726 if (event
.xunmap
.window
== menu
->core
.window
)
1730 XtDispatchEvent (&event
);
1732 || XtWindowToWidget (XDISPLAY event
.xany
.window
) != menu
)
1735 = (struct event_queue
*) malloc (sizeof (struct event_queue
));
1737 if (queue_tmp
!= NULL
)
1739 queue_tmp
->event
= event
;
1740 queue_tmp
->next
= queue
;
1749 /* Unhighlight the menu bar item (if any) that led to this menu. */
1752 menubar_item
->call_data
= (XtPointer
) 0;
1753 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
1756 /* fp turned off the following statement and wrote a comment
1757 that it is unnecessary--that the menu has already disappeared.
1758 I observer that is not so. -- rms. */
1759 /* Make sure the menu disappears. */
1760 lw_destroy_all_widgets (menu_id
);
1762 /* Unread any events that we got but did not handle. */
1763 while (queue
!= NULL
)
1766 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
1767 queue
= queue_tmp
->next
;
1768 free ((char *)queue_tmp
);
1769 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1770 interrupt_input_pending
= 1;
1773 /* Find the selected item, and its pane, to return
1774 the proper value. */
1775 if (menu_item_selection
!= 0)
1781 while (i
< menu_items_used
)
1785 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1787 subprefix_stack
[submenu_depth
++] = prefix
;
1791 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1793 prefix
= subprefix_stack
[--submenu_depth
];
1796 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1799 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1800 i
+= MENU_ITEMS_PANE_LENGTH
;
1805 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1806 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1812 entry
= Fcons (entry
, Qnil
);
1814 entry
= Fcons (prefix
, entry
);
1815 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1816 if (!NILP (subprefix_stack
[j
]))
1817 entry
= Fcons (subprefix_stack
[j
], entry
);
1821 i
+= MENU_ITEMS_ITEM_LENGTH
;
1829 static char * button_names
[] = {
1830 "button1", "button2", "button3", "button4", "button5",
1831 "button6", "button7", "button8", "button9", "button10" };
1834 xdialog_show (f
, menubarp
, keymaps
, title
, error
)
1841 int i
, nb_buttons
=0;
1844 XlwMenuWidget menubar
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1845 char dialog_name
[6];
1847 /* This is the menu bar item (if any) that led to this menu. */
1848 widget_value
*menubar_item
= 0;
1850 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1852 /* Define a queue to save up for later unreading
1853 all X events that don't pertain to the menu. */
1857 struct event_queue
*next
;
1860 struct event_queue
*queue
= NULL
;
1861 struct event_queue
*queue_tmp
;
1863 /* Number of elements seen so far, before boundary. */
1865 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1866 int boundary_seen
= 0;
1870 if (menu_items_n_panes
> 1)
1872 *error
= "Multiple panes in dialog box";
1876 /* Create a tree of widget_value objects
1877 representing the text label and buttons. */
1879 Lisp_Object pane_name
, prefix
;
1881 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
1882 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
1883 pane_string
= (NILP (pane_name
)
1884 ? "" : (char *) XSTRING (pane_name
)->data
);
1885 prev_wv
= malloc_widget_value ();
1886 prev_wv
->value
= pane_string
;
1887 if (keymaps
&& !NILP (prefix
))
1889 prev_wv
->enabled
= 1;
1890 prev_wv
->name
= "message";
1893 /* Loop over all panes and items, filling in the tree. */
1894 i
= MENU_ITEMS_PANE_LENGTH
;
1895 while (i
< menu_items_used
)
1898 /* Create a new item within current pane. */
1899 Lisp_Object item_name
, enable
, descrip
;
1900 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1901 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1903 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1905 if (NILP (item_name
))
1907 free_menubar_widget_value_tree (first_wv
);
1908 *error
= "Submenu in dialog items";
1911 if (EQ (item_name
, Qquote
))
1913 /* This is the boundary between left-side elts
1914 and right-side elts. Stop incrementing right_count. */
1919 if (nb_buttons
>= 10)
1921 free_menubar_widget_value_tree (first_wv
);
1922 *error
= "Too many dialog items";
1926 wv
= malloc_widget_value ();
1928 wv
->name
= (char *) button_names
[nb_buttons
];
1929 if (!NILP (descrip
))
1930 wv
->key
= (char *) XSTRING (descrip
)->data
;
1931 wv
->value
= (char *) XSTRING (item_name
)->data
;
1932 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1933 wv
->enabled
= !NILP (enable
);
1936 if (! boundary_seen
)
1940 i
+= MENU_ITEMS_ITEM_LENGTH
;
1943 /* If the boundary was not specified,
1944 by default put half on the left and half on the right. */
1945 if (! boundary_seen
)
1946 left_count
= nb_buttons
- nb_buttons
/ 2;
1948 wv
= malloc_widget_value ();
1949 wv
->name
= dialog_name
;
1951 /* Dialog boxes use a really stupid name encoding
1952 which specifies how many buttons to use
1953 and how many buttons are on the right.
1954 The Q means something also. */
1955 dialog_name
[0] = 'Q';
1956 dialog_name
[1] = '0' + nb_buttons
;
1957 dialog_name
[2] = 'B';
1958 dialog_name
[3] = 'R';
1959 /* Number of buttons to put on the right. */
1960 dialog_name
[4] = '0' + nb_buttons
- left_count
;
1962 wv
->contents
= first_wv
;
1966 /* Actually create the dialog. */
1967 dialog_id
= ++popup_id_tick
;
1968 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1969 f
->display
.x
->widget
, 1, 0,
1970 dialog_selection_callback
, 0);
1971 #if 0 /* This causes crashes, and seems to be redundant -- rms. */
1972 lw_modify_all_widgets (dialog_id
, first_wv
, True
);
1974 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1975 /* Free the widget_value objects we used to specify the contents. */
1976 free_menubar_widget_value_tree (first_wv
);
1978 /* No selection has been chosen yet. */
1979 menu_item_selection
= 0;
1981 /* Display the menu. */
1982 lw_pop_up_all_widgets (dialog_id
);
1984 /* Process events that apply to the menu. */
1989 XtAppNextEvent (Xt_app_con
, &event
);
1990 if (event
.type
== ButtonRelease
)
1992 XtDispatchEvent (&event
);
1995 else if (event
.type
== Expose
)
1996 process_expose_from_menu (event
);
1997 XtDispatchEvent (&event
);
1998 if (XtWindowToWidget(XDISPLAY event
.xany
.window
) != menu
)
2000 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
2002 if (queue_tmp
!= NULL
)
2004 queue_tmp
->event
= event
;
2005 queue_tmp
->next
= queue
;
2012 /* State that no mouse buttons are now held.
2013 That is not necessarily true, but the fiction leads to reasonable
2014 results, and it is a pain to ask which are actually held now
2015 or track this in the loop above. */
2016 x_mouse_grabbed
= 0;
2018 /* Unread any events that we got but did not handle. */
2019 while (queue
!= NULL
)
2022 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
2023 queue
= queue_tmp
->next
;
2024 free ((char *)queue_tmp
);
2025 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
2026 interrupt_input_pending
= 1;
2029 /* Find the selected item, and its pane, to return
2030 the proper value. */
2031 if (menu_item_selection
!= 0)
2037 while (i
< menu_items_used
)
2041 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2044 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2045 i
+= MENU_ITEMS_PANE_LENGTH
;
2050 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2051 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2055 entry
= Fcons (entry
, Qnil
);
2057 entry
= Fcons (prefix
, entry
);
2061 i
+= MENU_ITEMS_ITEM_LENGTH
;
2068 #else /* not USE_X_TOOLKIT */
2071 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
2081 int pane
, selidx
, lpane
, status
;
2082 Lisp_Object entry
, pane_prefix
;
2084 int ulx
, uly
, width
, height
;
2085 int dispwidth
, dispheight
;
2089 unsigned int dummy_uint
;
2092 if (menu_items_n_panes
== 0)
2095 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2097 *error
= "Empty menu";
2101 /* Figure out which root window F is on. */
2102 XGetGeometry (x_current_display
, FRAME_X_WINDOW (f
), &root
,
2103 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2104 &dummy_uint
, &dummy_uint
);
2106 /* Make the menu on that window. */
2107 menu
= XMenuCreate (XDISPLAY root
, "emacs");
2110 *error
= "Can't create menu";
2114 /* Adjust coordinates to relative to the outer (window manager) window. */
2118 int win_x
= 0, win_y
= 0;
2120 /* Find the position of the outside upper-left corner of
2121 the inner window, with respect to the outer window. */
2122 if (f
->display
.x
->parent_desc
!= ROOT_WINDOW
)
2125 XTranslateCoordinates (x_current_display
,
2127 /* From-window, to-window. */
2128 f
->display
.x
->window_desc
,
2129 f
->display
.x
->parent_desc
,
2131 /* From-position, to-position. */
2132 0, 0, &win_x
, &win_y
,
2134 /* Child of window. */
2141 #endif /* HAVE_X11 */
2143 /* Adjust coordinates to be root-window-relative. */
2144 x
+= f
->display
.x
->left_pos
;
2145 y
+= f
->display
.x
->top_pos
;
2147 /* Create all the necessary panes and their items. */
2149 while (i
< menu_items_used
)
2151 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2153 /* Create a new pane. */
2154 Lisp_Object pane_name
, prefix
;
2157 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2158 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2159 pane_string
= (NILP (pane_name
)
2160 ? "" : (char *) XSTRING (pane_name
)->data
);
2161 if (keymaps
&& !NILP (prefix
))
2164 lpane
= XMenuAddPane (XDISPLAY menu
, pane_string
, TRUE
);
2165 if (lpane
== XM_FAILURE
)
2167 XMenuDestroy (XDISPLAY menu
);
2168 *error
= "Can't create pane";
2171 i
+= MENU_ITEMS_PANE_LENGTH
;
2173 /* Find the width of the widest item in this pane. */
2176 while (j
< menu_items_used
)
2179 item
= XVECTOR (menu_items
)->contents
[j
];
2187 width
= XSTRING (item
)->size
;
2188 if (width
> maxwidth
)
2191 j
+= MENU_ITEMS_ITEM_LENGTH
;
2194 /* Ignore a nil in the item list.
2195 It's meaningful only for dialog boxes. */
2196 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2200 /* Create a new item within current pane. */
2201 Lisp_Object item_name
, enable
, descrip
;
2202 unsigned char *item_data
;
2204 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2205 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2207 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2208 if (!NILP (descrip
))
2210 int gap
= maxwidth
- XSTRING (item_name
)->size
;
2213 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2214 item_name
= concat2 (item_name
, spacer
);
2215 item_name
= concat2 (item_name
, descrip
);
2216 item_data
= XSTRING (item_name
)->data
;
2218 /* if alloca is fast, use that to make the space,
2219 to reduce gc needs. */
2221 = (unsigned char *) alloca (maxwidth
2222 + XSTRING (descrip
)->size
+ 1);
2223 bcopy (XSTRING (item_name
)->data
, item_data
,
2224 XSTRING (item_name
)->size
);
2225 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2227 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2228 XSTRING (descrip
)->size
);
2229 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2233 item_data
= XSTRING (item_name
)->data
;
2235 if (XMenuAddSelection (XDISPLAY menu
, lpane
, 0, item_data
,
2239 XMenuDestroy (XDISPLAY menu
);
2240 *error
= "Can't add selection to menu";
2243 i
+= MENU_ITEMS_ITEM_LENGTH
;
2247 /* All set and ready to fly. */
2248 XMenuRecompute (XDISPLAY menu
);
2249 dispwidth
= DisplayWidth (x_current_display
, XDefaultScreen (x_current_display
));
2250 dispheight
= DisplayHeight (x_current_display
, XDefaultScreen (x_current_display
));
2251 x
= min (x
, dispwidth
);
2252 y
= min (y
, dispheight
);
2255 XMenuLocate (XDISPLAY menu
, 0, 0, x
, y
,
2256 &ulx
, &uly
, &width
, &height
);
2257 if (ulx
+width
> dispwidth
)
2259 x
-= (ulx
+ width
) - dispwidth
;
2260 ulx
= dispwidth
- width
;
2262 if (uly
+height
> dispheight
)
2264 y
-= (uly
+ height
) - dispheight
;
2265 uly
= dispheight
- height
;
2267 if (ulx
< 0) x
-= ulx
;
2268 if (uly
< 0) y
-= uly
;
2270 XMenuSetAEQ (menu
, TRUE
);
2271 XMenuSetFreeze (menu
, TRUE
);
2274 status
= XMenuActivate (XDISPLAY menu
, &pane
, &selidx
,
2275 x
, y
, ButtonReleaseMask
, &datap
);
2280 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2283 /* Find the item number SELIDX in pane number PANE. */
2285 while (i
< menu_items_used
)
2287 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2291 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2293 i
+= MENU_ITEMS_PANE_LENGTH
;
2302 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2305 entry
= Fcons (entry
, Qnil
);
2306 if (!NILP (pane_prefix
))
2307 entry
= Fcons (pane_prefix
, entry
);
2313 i
+= MENU_ITEMS_ITEM_LENGTH
;
2319 *error
= "Can't activate menu";
2325 XMenuDestroy (XDISPLAY menu
);
2327 /* State that no mouse buttons are now held.
2328 (The oldXMenu code doesn't track this info for us.)
2329 That is not necessarily true, but the fiction leads to reasonable
2330 results, and it is a pain to ask which are actually held now. */
2331 x_mouse_grabbed
= 0;
2335 #endif /* not USE_X_TOOLKIT */
2339 staticpro (&menu_items
);
2342 popup_id_tick
= (1<<16);
2343 defsubr (&Sx_popup_menu
);
2344 defsubr (&Sx_popup_dialog
);