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
;
1422 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1424 *error
= "Empty menu";
1427 this_menu_bar_item_beg
= -1;
1428 this_menu_bar_item_end
= -1;
1429 last_menu_bar_item_end
= -1;
1431 /* Figure out which menu bar item, if any, this menu is for. */
1436 widget_value
*mb_item
= 0;
1438 for (mb_item
= menubar
->menu
.old_stack
[0]->contents
;
1440 mb_item
= mb_item
->next
)
1443 xend
+= (string_width (menubar
, mb_item
->name
)
1444 + 2 * (menubar
->menu
.horizontal_spacing
1445 + menubar
->menu
.shadow_thickness
));
1446 if (x
>= xbeg
&& x
< xend
)
1450 menubar_item
= mb_item
;
1451 /* Arrange to show a different menu if we move in the menu bar
1452 to a different item. */
1453 this_menu_bar_item_beg
= xbeg
;
1454 this_menu_bar_item_end
= xend
;
1457 last_menu_bar_item_end
= xend
;
1459 if (menubar_item
== 0)
1462 /* Offset the coordinates to root-relative. */
1463 if (f
->display
.x
->menubar_widget
!= 0)
1464 y
+= f
->display
.x
->menubar_widget
->core
.height
;
1465 XtTranslateCoords (f
->display
.x
->widget
,
1466 x
, y
, &root_x
, &root_y
);
1470 /* Create a tree of widget_value objects
1471 representing the panes and their items. */
1472 wv
= malloc_widget_value ();
1479 /* Loop over all panes and items, filling in the tree. */
1481 while (i
< menu_items_used
)
1483 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1485 submenu_stack
[submenu_depth
++] = save_wv
;
1491 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1494 save_wv
= submenu_stack
[--submenu_depth
];
1498 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1499 && submenu_depth
!= 0)
1500 i
+= MENU_ITEMS_PANE_LENGTH
;
1501 /* Ignore a nil in the item list.
1502 It's meaningful only for dialog boxes. */
1503 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1505 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1507 /* Create a new pane. */
1508 Lisp_Object pane_name
, prefix
;
1510 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1511 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1512 pane_string
= (NILP (pane_name
)
1513 ? "" : (char *) XSTRING (pane_name
)->data
);
1514 /* If there is just one top-level pane, put all its items directly
1515 under the top-level menu. */
1516 if (menu_items_n_panes
== 1)
1519 /* If the pane has a meaningful name,
1520 make the pane a top-level menu item
1521 with its items as a submenu beneath it. */
1522 if (!keymaps
&& strcmp (pane_string
, ""))
1524 wv
= malloc_widget_value ();
1528 first_wv
->contents
= wv
;
1529 wv
->name
= pane_string
;
1530 if (keymaps
&& !NILP (prefix
))
1537 else if (first_pane
)
1543 i
+= MENU_ITEMS_PANE_LENGTH
;
1547 /* Create a new item within current pane. */
1548 Lisp_Object item_name
, enable
, descrip
;
1549 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1550 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1552 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1554 wv
= malloc_widget_value ();
1558 save_wv
->contents
= wv
;
1559 wv
->name
= (char *) XSTRING (item_name
)->data
;
1560 if (!NILP (descrip
))
1561 wv
->key
= (char *) XSTRING (descrip
)->data
;
1563 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1564 wv
->enabled
= !NILP (enable
);
1567 i
+= MENU_ITEMS_ITEM_LENGTH
;
1571 /* Actually create the menu. */
1572 menu_id
= ++popup_id_tick
;
1573 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1574 f
->display
.x
->widget
, 1, 0,
1575 popup_selection_callback
, popup_down_callback
);
1576 /* Free the widget_value objects we used to specify the contents. */
1577 free_menubar_widget_value_tree (first_wv
);
1579 /* No selection has been chosen yet. */
1580 menu_item_selection
= 0;
1582 /* If the mouse moves out of the menu before we show the menu,
1583 don't show it at all. */
1584 if (check_mouse_other_menu_bar (f
))
1586 lw_destroy_all_widgets (menu_id
);
1591 /* Highlight the menu bar item (if any) that led to this menu. */
1594 menubar_item
->call_data
= (XtPointer
) 1;
1595 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
1598 /* Display the menu. */
1600 XButtonPressedEvent dummy
;
1603 mw
= (XlwMenuWidget
) ((CompositeWidget
)menu
)->composite
.children
[0];
1605 dummy
.type
= ButtonPress
;
1607 dummy
.send_event
= 0;
1608 dummy
.display
= XtDisplay (menu
);
1609 dummy
.window
= XtWindow (XtParent (menu
));
1610 dummy
.time
= CurrentTime
;
1615 /* We activate directly the lucid implementation. */
1616 pop_up_menu (mw
, &dummy
);
1619 /* No need to check a second time since this is done in the XEvent loop.
1620 This slows done the execution. */
1622 /* Check again whether the mouse has moved to another menu bar item. */
1623 if (check_mouse_other_menu_bar (f
))
1625 /* The mouse moved into a different menu bar item.
1626 We should bring up that item's menu instead.
1627 First pop down this menu. */
1628 XtUngrabPointer ((Widget
)
1630 ((CompositeWidget
)menu
)->composite
.children
[0]),
1632 lw_destroy_all_widgets (menu_id
);
1637 /* Process events that apply to the menu. */
1641 int queue_and_exit
= 0;
1643 XtAppNextEvent (Xt_app_con
, &event
);
1644 if (event
.type
== ButtonRelease
)
1646 XtDispatchEvent (&event
);
1649 /* Do the work of construct_mouse_click since it can't
1650 be called. Initially, the popup menu has been called
1651 from a ButtonPress in the edit_widget. Then the mouse
1652 has been set to grabbed. Reset it now. */
1653 x_mouse_grabbed
&= ~(1 << event
.xbutton
.button
);
1654 if (!x_mouse_grabbed
)
1655 Vmouse_depressed
= Qnil
;
1657 if (! (menu_item_selection
== 0
1658 && (((XButtonEvent
*) (&event
))->time
- last_event_timestamp
1659 < XINT (Vdouble_click_time
))))
1662 else if (event
.type
== ButtonPress
)
1664 /* Any mouse button activity that doesn't select in the menu
1665 should unpost the menu. */
1666 if (menu_item_selection
== 0)
1669 else if (event
.type
== KeyPress
)
1671 /* Exit the loop, but first queue this event for reuse. */
1674 else if (event
.type
== Expose
)
1675 process_expose_from_menu (event
);
1676 /* If the mouse moves to a different menu bar item, switch to
1677 that item's menu. But only if the button is still held down. */
1678 else if (event
.type
== MotionNotify
1681 int event_x
= (event
.xmotion
.x_root
1682 - (f
->display
.x
->widget
->core
.x
1683 + f
->display
.x
->widget
->core
.border_width
));
1684 int event_y
= (event
.xmotion
.y_root
1685 - (f
->display
.x
->widget
->core
.y
1686 + f
->display
.x
->widget
->core
.border_width
));
1688 if (other_menu_bar_item_p (f
, event_x
, event_y
))
1690 /* The mouse moved into a different menu bar item.
1691 We should bring up that item's menu instead.
1692 First pop down this menu. */
1693 XtUngrabPointer ((Widget
)
1695 ((CompositeWidget
)menu
)->composite
.children
[0]),
1696 event
.xbutton
.time
);
1697 lw_destroy_all_widgets (menu_id
);
1699 /* Put back an event that will bring up the other item's menu. */
1700 unread_menu_bar_button (f
, event_x
);
1701 /* Don't let us select anything in this case. */
1702 menu_item_selection
= 0;
1707 XtDispatchEvent (&event
);
1709 || XtWindowToWidget (XDISPLAY event
.xany
.window
) != menu
)
1712 = (struct event_queue
*) malloc (sizeof (struct event_queue
));
1714 if (queue_tmp
!= NULL
)
1716 queue_tmp
->event
= event
;
1717 queue_tmp
->next
= queue
;
1726 /* Unhighlight the menu bar item (if any) that led to this menu. */
1729 menubar_item
->call_data
= (XtPointer
) 0;
1730 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
1733 /* fp turned off the following statement and wrote a comment
1734 that it is unnecessary--that the menu has already disappeared.
1735 I observer that is not so. -- rms. */
1736 /* Make sure the menu disappears. */
1737 lw_destroy_all_widgets (menu_id
);
1739 /* Unread any events that we got but did not handle. */
1740 while (queue
!= NULL
)
1743 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
1744 queue
= queue_tmp
->next
;
1745 free ((char *)queue_tmp
);
1746 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1747 interrupt_input_pending
= 1;
1750 /* Find the selected item, and its pane, to return
1751 the proper value. */
1752 if (menu_item_selection
!= 0)
1758 while (i
< menu_items_used
)
1762 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1764 subprefix_stack
[submenu_depth
++] = prefix
;
1768 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1770 prefix
= subprefix_stack
[--submenu_depth
];
1773 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1776 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1777 i
+= MENU_ITEMS_PANE_LENGTH
;
1782 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1783 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1789 entry
= Fcons (entry
, Qnil
);
1791 entry
= Fcons (prefix
, entry
);
1792 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1793 if (!NILP (subprefix_stack
[j
]))
1794 entry
= Fcons (subprefix_stack
[j
], entry
);
1798 i
+= MENU_ITEMS_ITEM_LENGTH
;
1806 static char * button_names
[] = {
1807 "button1", "button2", "button3", "button4", "button5",
1808 "button6", "button7", "button8", "button9", "button10" };
1811 xdialog_show (f
, menubarp
, keymaps
, title
, error
)
1818 int i
, nb_buttons
=0;
1821 XlwMenuWidget menubar
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1822 char dialog_name
[6];
1824 /* This is the menu bar item (if any) that led to this menu. */
1825 widget_value
*menubar_item
= 0;
1827 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1829 /* Define a queue to save up for later unreading
1830 all X events that don't pertain to the menu. */
1834 struct event_queue
*next
;
1837 struct event_queue
*queue
= NULL
;
1838 struct event_queue
*queue_tmp
;
1840 /* Number of elements seen so far, before boundary. */
1842 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1843 int boundary_seen
= 0;
1847 if (menu_items_n_panes
> 1)
1849 *error
= "Multiple panes in dialog box";
1853 /* Create a tree of widget_value objects
1854 representing the text label and buttons. */
1856 Lisp_Object pane_name
, prefix
;
1858 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
1859 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
1860 pane_string
= (NILP (pane_name
)
1861 ? "" : (char *) XSTRING (pane_name
)->data
);
1862 prev_wv
= malloc_widget_value ();
1863 prev_wv
->value
= pane_string
;
1864 if (keymaps
&& !NILP (prefix
))
1866 prev_wv
->enabled
= 1;
1867 prev_wv
->name
= "message";
1870 /* Loop over all panes and items, filling in the tree. */
1871 i
= MENU_ITEMS_PANE_LENGTH
;
1872 while (i
< menu_items_used
)
1875 /* Create a new item within current pane. */
1876 Lisp_Object item_name
, enable
, descrip
;
1877 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1878 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1880 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1882 if (NILP (item_name
))
1884 free_menubar_widget_value_tree (first_wv
);
1885 *error
= "Submenu in dialog items";
1888 if (EQ (item_name
, Qquote
))
1890 /* This is the boundary between left-side elts
1891 and right-side elts. Stop incrementing right_count. */
1896 if (nb_buttons
>= 10)
1898 free_menubar_widget_value_tree (first_wv
);
1899 *error
= "Too many dialog items";
1903 wv
= malloc_widget_value ();
1905 wv
->name
= (char *) button_names
[nb_buttons
];
1906 if (!NILP (descrip
))
1907 wv
->key
= (char *) XSTRING (descrip
)->data
;
1908 wv
->value
= (char *) XSTRING (item_name
)->data
;
1909 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1910 wv
->enabled
= !NILP (enable
);
1913 if (! boundary_seen
)
1917 i
+= MENU_ITEMS_ITEM_LENGTH
;
1920 /* If the boundary was not specified,
1921 by default put half on the left and half on the right. */
1922 if (! boundary_seen
)
1923 left_count
= nb_buttons
- nb_buttons
/ 2;
1925 wv
= malloc_widget_value ();
1926 wv
->name
= dialog_name
;
1928 /* Dialog boxes use a really stupid name encoding
1929 which specifies how many buttons to use
1930 and how many buttons are on the right.
1931 The Q means something also. */
1932 dialog_name
[0] = 'Q';
1933 dialog_name
[1] = '0' + nb_buttons
;
1934 dialog_name
[2] = 'B';
1935 dialog_name
[3] = 'R';
1936 /* Number of buttons to put on the right. */
1937 dialog_name
[4] = '0' + nb_buttons
- left_count
;
1939 wv
->contents
= first_wv
;
1943 /* Actually create the dialog. */
1944 dialog_id
= ++popup_id_tick
;
1945 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1946 f
->display
.x
->widget
, 1, 0,
1947 dialog_selection_callback
, 0);
1948 #if 0 /* This causes crashes, and seems to be redundant -- rms. */
1949 lw_modify_all_widgets (dialog_id
, first_wv
, True
);
1951 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1952 /* Free the widget_value objects we used to specify the contents. */
1953 free_menubar_widget_value_tree (first_wv
);
1955 /* No selection has been chosen yet. */
1956 menu_item_selection
= 0;
1958 /* Display the menu. */
1959 lw_pop_up_all_widgets (dialog_id
);
1961 /* Process events that apply to the menu. */
1966 XtAppNextEvent (Xt_app_con
, &event
);
1967 if (event
.type
== ButtonRelease
)
1969 XtDispatchEvent (&event
);
1972 else if (event
.type
== Expose
)
1973 process_expose_from_menu (event
);
1974 XtDispatchEvent (&event
);
1975 if (XtWindowToWidget(XDISPLAY event
.xany
.window
) != menu
)
1977 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
1979 if (queue_tmp
!= NULL
)
1981 queue_tmp
->event
= event
;
1982 queue_tmp
->next
= queue
;
1989 /* State that no mouse buttons are now held.
1990 That is not necessarily true, but the fiction leads to reasonable
1991 results, and it is a pain to ask which are actually held now
1992 or track this in the loop above. */
1993 x_mouse_grabbed
= 0;
1995 /* Unread any events that we got but did not handle. */
1996 while (queue
!= NULL
)
1999 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
2000 queue
= queue_tmp
->next
;
2001 free ((char *)queue_tmp
);
2002 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
2003 interrupt_input_pending
= 1;
2006 /* Find the selected item, and its pane, to return
2007 the proper value. */
2008 if (menu_item_selection
!= 0)
2014 while (i
< menu_items_used
)
2018 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2021 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2022 i
+= MENU_ITEMS_PANE_LENGTH
;
2027 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2028 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2032 entry
= Fcons (entry
, Qnil
);
2034 entry
= Fcons (prefix
, entry
);
2038 i
+= MENU_ITEMS_ITEM_LENGTH
;
2045 #else /* not USE_X_TOOLKIT */
2048 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
2058 int pane
, selidx
, lpane
, status
;
2059 Lisp_Object entry
, pane_prefix
;
2061 int ulx
, uly
, width
, height
;
2062 int dispwidth
, dispheight
;
2066 unsigned int dummy_uint
;
2069 if (menu_items_n_panes
== 0)
2072 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2074 *error
= "Empty menu";
2078 /* Figure out which root window F is on. */
2079 XGetGeometry (x_current_display
, FRAME_X_WINDOW (f
), &root
,
2080 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2081 &dummy_uint
, &dummy_uint
);
2083 /* Make the menu on that window. */
2084 menu
= XMenuCreate (XDISPLAY root
, "emacs");
2087 *error
= "Can't create menu";
2091 /* Adjust coordinates to relative to the outer (window manager) window. */
2095 int win_x
= 0, win_y
= 0;
2097 /* Find the position of the outside upper-left corner of
2098 the inner window, with respect to the outer window. */
2099 if (f
->display
.x
->parent_desc
!= ROOT_WINDOW
)
2102 XTranslateCoordinates (x_current_display
,
2104 /* From-window, to-window. */
2105 f
->display
.x
->window_desc
,
2106 f
->display
.x
->parent_desc
,
2108 /* From-position, to-position. */
2109 0, 0, &win_x
, &win_y
,
2111 /* Child of window. */
2118 #endif /* HAVE_X11 */
2120 /* Adjust coordinates to be root-window-relative. */
2121 x
+= f
->display
.x
->left_pos
;
2122 y
+= f
->display
.x
->top_pos
;
2124 /* Create all the necessary panes and their items. */
2126 while (i
< menu_items_used
)
2128 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2130 /* Create a new pane. */
2131 Lisp_Object pane_name
, prefix
;
2134 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2135 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2136 pane_string
= (NILP (pane_name
)
2137 ? "" : (char *) XSTRING (pane_name
)->data
);
2138 if (keymaps
&& !NILP (prefix
))
2141 lpane
= XMenuAddPane (XDISPLAY menu
, pane_string
, TRUE
);
2142 if (lpane
== XM_FAILURE
)
2144 XMenuDestroy (XDISPLAY menu
);
2145 *error
= "Can't create pane";
2148 i
+= MENU_ITEMS_PANE_LENGTH
;
2150 /* Find the width of the widest item in this pane. */
2153 while (j
< menu_items_used
)
2156 item
= XVECTOR (menu_items
)->contents
[j
];
2164 width
= XSTRING (item
)->size
;
2165 if (width
> maxwidth
)
2168 j
+= MENU_ITEMS_ITEM_LENGTH
;
2171 /* Ignore a nil in the item list.
2172 It's meaningful only for dialog boxes. */
2173 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2177 /* Create a new item within current pane. */
2178 Lisp_Object item_name
, enable
, descrip
;
2179 unsigned char *item_data
;
2181 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2182 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2184 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2185 if (!NILP (descrip
))
2187 int gap
= maxwidth
- XSTRING (item_name
)->size
;
2190 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2191 item_name
= concat2 (item_name
, spacer
);
2192 item_name
= concat2 (item_name
, descrip
);
2193 item_data
= XSTRING (item_name
)->data
;
2195 /* if alloca is fast, use that to make the space,
2196 to reduce gc needs. */
2198 = (unsigned char *) alloca (maxwidth
2199 + XSTRING (descrip
)->size
+ 1);
2200 bcopy (XSTRING (item_name
)->data
, item_data
,
2201 XSTRING (item_name
)->size
);
2202 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2204 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2205 XSTRING (descrip
)->size
);
2206 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2210 item_data
= XSTRING (item_name
)->data
;
2212 if (XMenuAddSelection (XDISPLAY menu
, lpane
, 0, item_data
,
2216 XMenuDestroy (XDISPLAY menu
);
2217 *error
= "Can't add selection to menu";
2220 i
+= MENU_ITEMS_ITEM_LENGTH
;
2224 /* All set and ready to fly. */
2225 XMenuRecompute (XDISPLAY menu
);
2226 dispwidth
= DisplayWidth (x_current_display
, XDefaultScreen (x_current_display
));
2227 dispheight
= DisplayHeight (x_current_display
, XDefaultScreen (x_current_display
));
2228 x
= min (x
, dispwidth
);
2229 y
= min (y
, dispheight
);
2232 XMenuLocate (XDISPLAY menu
, 0, 0, x
, y
,
2233 &ulx
, &uly
, &width
, &height
);
2234 if (ulx
+width
> dispwidth
)
2236 x
-= (ulx
+ width
) - dispwidth
;
2237 ulx
= dispwidth
- width
;
2239 if (uly
+height
> dispheight
)
2241 y
-= (uly
+ height
) - dispheight
;
2242 uly
= dispheight
- height
;
2244 if (ulx
< 0) x
-= ulx
;
2245 if (uly
< 0) y
-= uly
;
2247 XMenuSetAEQ (menu
, TRUE
);
2248 XMenuSetFreeze (menu
, TRUE
);
2251 status
= XMenuActivate (XDISPLAY menu
, &pane
, &selidx
,
2252 x
, y
, ButtonReleaseMask
, &datap
);
2257 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2260 /* Find the item number SELIDX in pane number PANE. */
2262 while (i
< menu_items_used
)
2264 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2268 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2270 i
+= MENU_ITEMS_PANE_LENGTH
;
2279 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2282 entry
= Fcons (entry
, Qnil
);
2283 if (!NILP (pane_prefix
))
2284 entry
= Fcons (pane_prefix
, entry
);
2290 i
+= MENU_ITEMS_ITEM_LENGTH
;
2296 *error
= "Can't activate menu";
2302 XMenuDestroy (XDISPLAY menu
);
2304 /* State that no mouse buttons are now held.
2305 (The oldXMenu code doesn't track this info for us.)
2306 That is not necessarily true, but the fiction leads to reasonable
2307 results, and it is a pain to ask which are actually held now. */
2308 x_mouse_grabbed
= 0;
2312 #endif /* not USE_X_TOOLKIT */
2316 staticpro (&menu_items
);
2319 popup_id_tick
= (1<<16);
2320 defsubr (&Sx_popup_menu
);
2321 defsubr (&Sx_popup_dialog
);