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/Shell.h>
68 #include <X11/Xaw/Paned.h>
69 #include "../lwlib/lwlib.h"
70 #include "../lwlib/xlwmenuP.h"
71 #endif /* USE_X_TOOLKIT */
73 #define min(x,y) (((x) < (y)) ? (x) : (y))
74 #define max(x,y) (((x) > (y)) ? (x) : (y))
82 extern Display
*x_current_display
;
84 #define ButtonReleaseMask ButtonReleased
85 #endif /* not HAVE_X11 */
87 /* We need a unique id for each popup menu and dialog box. */
88 static unsigned int popup_id_tick
;
90 extern Lisp_Object Qmenu_enable
;
91 extern Lisp_Object Qmenu_bar
;
94 extern void process_expose_from_menu ();
95 extern XtAppContext Xt_app_con
;
97 static Lisp_Object
xdialog_show ();
100 static Lisp_Object
xmenu_show ();
101 static void keymap_panes ();
102 static void single_keymap_panes ();
103 static void list_of_panes ();
104 static void list_of_items ();
106 /* This holds a Lisp vector that holds the results of decoding
107 the keymaps or alist-of-alists that specify a menu.
109 It describes the panes and items within the panes.
111 Each pane is described by 3 elements in the vector:
112 t, the pane name, the pane's prefix key.
113 Then follow the pane's items, with 4 elements per item:
114 the item string, the enable flag, the item's value,
115 and the equivalent keyboard key's description string.
117 In some cases, multiple levels of menus may be described.
118 A single vector slot containing nil indicates the start of a submenu.
119 A single vector slot containing lambda indicates the end of a submenu.
120 The submenu follows a menu item which is the way to reach the submenu.
122 A single vector slot containing quote indicates that the
123 following items should appear on the right of a dialog box.
125 Using a Lisp vector to hold this information while we decode it
126 takes care of protecting all the data from GC. */
128 #define MENU_ITEMS_PANE_NAME 1
129 #define MENU_ITEMS_PANE_PREFIX 2
130 #define MENU_ITEMS_PANE_LENGTH 3
132 #define MENU_ITEMS_ITEM_NAME 0
133 #define MENU_ITEMS_ITEM_ENABLE 1
134 #define MENU_ITEMS_ITEM_VALUE 2
135 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
136 #define MENU_ITEMS_ITEM_LENGTH 4
138 static Lisp_Object menu_items
;
140 /* Number of slots currently allocated in menu_items. */
141 static int menu_items_allocated
;
143 /* This is the index in menu_items of the first empty slot. */
144 static int menu_items_used
;
146 /* The number of panes currently recorded in menu_items,
147 excluding those within submenus. */
148 static int menu_items_n_panes
;
150 /* Current depth within submenus. */
151 static int menu_items_submenu_depth
;
153 /* Initialize the menu_items structure if we haven't already done so.
154 Also mark it as currently empty. */
159 if (NILP (menu_items
))
161 menu_items_allocated
= 60;
162 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
166 menu_items_n_panes
= 0;
167 menu_items_submenu_depth
= 0;
170 /* Call at the end of generating the data in menu_items.
171 This fills in the number of items in the last pane. */
178 /* Call when finished using the data for the current menu
182 discard_menu_items ()
184 /* Free the structure if it is especially large.
185 Otherwise, hold on to it, to save time. */
186 if (menu_items_allocated
> 200)
189 menu_items_allocated
= 0;
193 /* Make the menu_items vector twice as large. */
199 int old_size
= menu_items_allocated
;
202 menu_items_allocated
*= 2;
203 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
204 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
205 old_size
* sizeof (Lisp_Object
));
208 /* Begin a submenu. */
211 push_submenu_start ()
213 if (menu_items_used
+ 1 > menu_items_allocated
)
216 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
217 menu_items_submenu_depth
++;
225 if (menu_items_used
+ 1 > menu_items_allocated
)
228 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
229 menu_items_submenu_depth
--;
232 /* Indicate boundary between left and right. */
235 push_left_right_boundary ()
237 if (menu_items_used
+ 1 > menu_items_allocated
)
240 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
243 /* Start a new menu pane in menu_items..
244 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
247 push_menu_pane (name
, prefix_vec
)
248 Lisp_Object name
, prefix_vec
;
250 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
253 if (menu_items_submenu_depth
== 0)
254 menu_items_n_panes
++;
255 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
256 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
257 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
260 /* Push one menu item into the current pane.
261 NAME is the string to display. ENABLE if non-nil means
262 this item can be selected. KEY is the key generated by
263 choosing this item. EQUIV is the textual description
264 of the keyboard equivalent for this item (or nil if none). */
267 push_menu_item (name
, enable
, key
, equiv
)
268 Lisp_Object name
, enable
, key
, equiv
;
270 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
273 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
274 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
275 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
276 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
279 /* Figure out the current keyboard equivalent of a menu item ITEM1.
280 The item string for menu display should be ITEM_STRING.
281 Store the equivalent keyboard key sequence's
282 textual description into *DESCRIP_PTR.
283 Also cache them in the item itself.
284 Return the real definition to execute. */
287 menu_item_equiv_key (item_string
, item1
, descrip_ptr
)
288 Lisp_Object item_string
;
290 Lisp_Object
*descrip_ptr
;
292 /* This is the real definition--the function to run. */
294 /* This is the sublist that records cached equiv key data
295 so we can save time. */
296 Lisp_Object cachelist
;
297 /* These are the saved equivalent keyboard key sequence
298 and its key-description. */
299 Lisp_Object savedkey
, descrip
;
302 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
304 /* If a help string follows the item string, skip it. */
305 if (CONSP (XCONS (item1
)->cdr
)
306 && STRINGP (XCONS (XCONS (item1
)->cdr
)->car
))
307 item1
= XCONS (item1
)->cdr
;
311 /* Get out the saved equivalent-keyboard-key info. */
312 cachelist
= savedkey
= descrip
= Qnil
;
313 if (CONSP (def
) && CONSP (XCONS (def
)->car
)
314 && (NILP (XCONS (XCONS (def
)->car
)->car
)
315 || VECTORP (XCONS (XCONS (def
)->car
)->car
)))
317 cachelist
= XCONS (def
)->car
;
318 def
= XCONS (def
)->cdr
;
319 savedkey
= XCONS (cachelist
)->car
;
320 descrip
= XCONS (cachelist
)->cdr
;
323 GCPRO4 (def
, def1
, savedkey
, descrip
);
325 /* Is it still valid? */
327 if (!NILP (savedkey
))
328 def1
= Fkey_binding (savedkey
, Qnil
);
329 /* If not, update it. */
331 /* If the command is an alias for another
332 (such as easymenu.el and lmenu.el set it up),
333 check if the original command matches the cached command. */
334 && !(SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
)
335 && EQ (def1
, XSYMBOL (def
)->function
))
336 /* If something had no key binding before, don't recheck it--
337 doing that takes too much time and makes menus too slow. */
338 && !(!NILP (cachelist
) && NILP (savedkey
)))
342 savedkey
= Fwhere_is_internal (def
, Qnil
, Qt
, Qnil
);
343 /* If the command is an alias for another
344 (such as easymenu.el and lmenu.el set it up),
345 see if the original command name has equivalent keys. */
346 if (SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
))
347 savedkey
= Fwhere_is_internal (XSYMBOL (def
)->function
,
350 if (VECTORP (savedkey
)
351 && EQ (XVECTOR (savedkey
)->contents
[0], Qmenu_bar
))
353 if (!NILP (savedkey
))
355 descrip
= Fkey_description (savedkey
);
356 descrip
= concat2 (make_string (" (", 3), descrip
);
357 descrip
= concat2 (descrip
, make_string (")", 1));
361 /* Cache the data we just got in a sublist of the menu binding. */
362 if (NILP (cachelist
))
363 XCONS (item1
)->cdr
= Fcons (Fcons (savedkey
, descrip
), def
);
366 XCONS (cachelist
)->car
= savedkey
;
367 XCONS (cachelist
)->cdr
= descrip
;
371 *descrip_ptr
= descrip
;
375 /* This is used as the handler when calling internal_condition_case_1. */
378 menu_item_enabled_p_1 (arg
)
384 /* Return non-nil if the command DEF is enabled when used as a menu item.
385 This is based on looking for a menu-enable property.
386 If NOTREAL is set, don't bother really computing this. */
389 menu_item_enabled_p (def
, notreal
)
392 Lisp_Object enabled
, tem
;
397 if (XTYPE (def
) == Lisp_Symbol
)
399 /* No property, or nil, means enable.
400 Otherwise, enable if value is not nil. */
401 tem
= Fget (def
, Qmenu_enable
);
403 /* (condition-case nil (eval tem)
405 enabled
= internal_condition_case_1 (Feval
, tem
, Qerror
,
406 menu_item_enabled_p_1
);
411 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
412 and generate menu panes for them in menu_items.
413 If NOTREAL is nonzero,
414 don't bother really computing whether an item is enabled. */
417 keymap_panes (keymaps
, nmaps
, notreal
)
418 Lisp_Object
*keymaps
;
426 /* Loop over the given keymaps, making a pane for each map.
427 But don't make a pane that is empty--ignore that map instead.
428 P is the number of panes we have made so far. */
429 for (mapno
= 0; mapno
< nmaps
; mapno
++)
430 single_keymap_panes (keymaps
[mapno
], Qnil
, Qnil
, notreal
);
432 finish_menu_items ();
435 /* This is a recursive subroutine of keymap_panes.
436 It handles one keymap, KEYMAP.
437 The other arguments are passed along
438 or point to local variables of the previous function.
439 If NOTREAL is nonzero,
440 don't bother really computing whether an item is enabled. */
443 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
)
445 Lisp_Object pane_name
;
449 Lisp_Object pending_maps
;
450 Lisp_Object tail
, item
, item1
, item_string
, table
;
451 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
455 push_menu_pane (pane_name
, prefix
);
457 for (tail
= keymap
; XTYPE (tail
) == Lisp_Cons
; tail
= XCONS (tail
)->cdr
)
459 /* Look at each key binding, and if it has a menu string,
460 make a menu item from it. */
461 item
= XCONS (tail
)->car
;
462 if (XTYPE (item
) == Lisp_Cons
)
464 item1
= XCONS (item
)->cdr
;
465 if (XTYPE (item1
) == Lisp_Cons
)
467 item_string
= XCONS (item1
)->car
;
468 if (XTYPE (item_string
) == Lisp_String
)
470 /* This is the real definition--the function to run. */
472 /* These are the saved equivalent keyboard key sequence
473 and its key-description. */
475 Lisp_Object tem
, enabled
;
477 /* GCPRO because ...enabled_p will call eval
478 and ..._equiv_key may autoload something.
479 Protecting KEYMAP preserves everything we use;
480 aside from that, must protect whatever might be
481 a string. Since there's no GCPRO5, we refetch
482 item_string instead of protecting it. */
483 descrip
= def
= Qnil
;
484 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
486 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
487 enabled
= menu_item_enabled_p (def
, notreal
);
491 item_string
= XCONS (item1
)->car
;
493 tem
= Fkeymapp (def
);
494 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
495 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, XCONS (item
)->car
)),
500 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
501 submap
= get_keymap_1 (def
, 0, 1);
503 #ifndef USE_X_TOOLKIT
504 /* Indicate visually that this is a submenu. */
506 item_string
= concat2 (item_string
,
507 build_string (" >"));
509 push_menu_item (item_string
, enabled
, XCONS (item
)->car
,
512 /* Display a submenu using the toolkit. */
515 push_submenu_start ();
516 single_keymap_panes (submap
, Qnil
,
517 XCONS (item
)->car
, notreal
);
525 else if (XTYPE (item
) == Lisp_Vector
)
527 /* Loop over the char values represented in the vector. */
528 int len
= XVECTOR (item
)->size
;
530 for (c
= 0; c
< len
; c
++)
532 Lisp_Object character
;
533 XFASTINT (character
) = c
;
534 item1
= XVECTOR (item
)->contents
[c
];
535 if (XTYPE (item1
) == Lisp_Cons
)
537 item_string
= XCONS (item1
)->car
;
538 if (XTYPE (item_string
) == Lisp_String
)
542 /* These are the saved equivalent keyboard key sequence
543 and its key-description. */
545 Lisp_Object tem
, enabled
;
547 /* GCPRO because ...enabled_p will call eval
548 and ..._equiv_key may autoload something.
549 Protecting KEYMAP preserves everything we use;
550 aside from that, must protect whatever might be
551 a string. Since there's no GCPRO5, we refetch
552 item_string instead of protecting it. */
553 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
554 descrip
= def
= Qnil
;
556 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
557 enabled
= menu_item_enabled_p (def
, notreal
);
561 item_string
= XCONS (item1
)->car
;
563 tem
= Fkeymapp (def
);
564 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
565 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, character
)),
570 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
571 submap
= get_keymap_1 (def
, 0, 1);
573 #ifndef USE_X_TOOLKIT
575 item_string
= concat2 (item_string
,
576 build_string (" >"));
578 push_menu_item (item_string
, enabled
, character
,
583 push_submenu_start ();
584 single_keymap_panes (submap
, Qnil
,
596 /* Process now any submenus which want to be panes at this level. */
597 while (!NILP (pending_maps
))
599 Lisp_Object elt
, eltcdr
, string
;
600 elt
= Fcar (pending_maps
);
601 eltcdr
= XCONS (elt
)->cdr
;
602 string
= XCONS (eltcdr
)->car
;
603 /* We no longer discard the @ from the beginning of the string here.
604 Instead, we do this in xmenu_show. */
605 single_keymap_panes (Fcar (elt
), string
,
606 XCONS (eltcdr
)->cdr
, notreal
);
607 pending_maps
= Fcdr (pending_maps
);
611 /* Push all the panes and items of a menu decsribed by the
612 alist-of-alists MENU.
613 This handles old-fashioned calls to x-popup-menu. */
623 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
625 Lisp_Object elt
, pane_name
, pane_data
;
627 pane_name
= Fcar (elt
);
628 CHECK_STRING (pane_name
, 0);
629 push_menu_pane (pane_name
, Qnil
);
630 pane_data
= Fcdr (elt
);
631 CHECK_CONS (pane_data
, 0);
632 list_of_items (pane_data
);
635 finish_menu_items ();
638 /* Push the items in a single pane defined by the alist PANE. */
644 Lisp_Object tail
, item
, item1
;
646 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
650 push_menu_item (item
, Qnil
, Qnil
, Qnil
);
651 else if (NILP (item
))
652 push_left_right_boundary ();
655 CHECK_CONS (item
, 0);
657 CHECK_STRING (item1
, 1);
658 push_menu_item (item1
, Qt
, Fcdr (item
), Qnil
);
663 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
664 "Pop up a deck-of-cards menu and return user's selection.\n\
665 POSITION is a position specification. This is either a mouse button event\n\
666 or a list ((XOFFSET YOFFSET) WINDOW)\n\
667 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
668 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
669 This controls the position of the center of the first line\n\
670 in the first pane of the menu, not the top left of the menu as a whole.\n\
671 If POSITION is t, it means to use the current mouse position.\n\
673 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
674 The menu items come from key bindings that have a menu string as well as\n\
675 a definition; actually, the \"definition\" in such a key binding looks like\n\
676 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
677 the keymap as a top-level element.\n\n\
678 You can also use a list of keymaps as MENU.\n\
679 Then each keymap makes a separate pane.\n\
680 When MENU is a keymap or a list of keymaps, the return value\n\
681 is a list of events.\n\n\
682 Alternatively, you can specify a menu of multiple panes\n\
683 with a list of the form (TITLE PANE1 PANE2...),\n\
684 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
685 Each ITEM is normally a cons cell (STRING . VALUE);\n\
686 but a string can appear as an item--that makes a nonselectable line\n\
688 With this form of menu, the return value is VALUE from the chosen item.\n\
690 If POSITION is nil, don't display the menu at all, just precalculate the\n\
691 cached information about equivalent key sequences.")
693 Lisp_Object position
, menu
;
695 int number_of_panes
, panes
;
696 Lisp_Object keymap
, tem
;
700 Lisp_Object selection
;
703 Lisp_Object x
, y
, window
;
708 if (! NILP (position
))
712 /* Decode the first argument: find the window and the coordinates. */
713 if (EQ (position
, Qt
))
715 /* Use the mouse's current position. */
717 Lisp_Object bar_window
;
721 if (mouse_position_hook
)
722 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
724 XSET (window
, Lisp_Frame
, new_f
);
727 window
= selected_window
;
734 tem
= Fcar (position
);
735 if (XTYPE (tem
) == Lisp_Cons
)
737 window
= Fcar (Fcdr (position
));
739 y
= Fcar (Fcdr (tem
));
743 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
744 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
745 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
749 /* Determine whether this menu is handling a menu bar click. */
750 tem
= Fcar (Fcdr (Fcar (Fcdr (position
))));
751 if (CONSP (tem
) && EQ (Fcar (tem
), Qmenu_bar
))
759 /* Decode where to put the menu. */
761 if (XTYPE (window
) == Lisp_Frame
)
768 else if (XTYPE (window
) == Lisp_Window
)
770 CHECK_LIVE_WINDOW (window
, 0);
771 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
773 xpos
= (FONT_WIDTH (f
->display
.x
->font
) * XWINDOW (window
)->left
);
774 ypos
= (f
->display
.x
->line_height
* XWINDOW (window
)->top
);
777 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
778 but I don't want to make one now. */
779 CHECK_WINDOW (window
, 0);
788 /* Decode the menu items from what was specified. */
790 keymap
= Fkeymapp (menu
);
792 if (XTYPE (menu
) == Lisp_Cons
)
793 tem
= Fkeymapp (Fcar (menu
));
796 /* We were given a keymap. Extract menu info from the keymap. */
798 keymap
= get_keymap (menu
);
800 /* Extract the detailed info to make one pane. */
801 keymap_panes (&menu
, 1, NILP (position
));
803 /* Search for a string appearing directly as an element of the keymap.
804 That string is the title of the menu. */
805 prompt
= map_prompt (keymap
);
807 /* Make that be the pane title of the first pane. */
808 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
809 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
813 else if (!NILP (tem
))
815 /* We were given a list of keymaps. */
816 int nmaps
= XFASTINT (Flength (menu
));
818 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
823 /* The first keymap that has a prompt string
824 supplies the menu title. */
825 for (tem
= menu
, i
= 0; XTYPE (tem
) == Lisp_Cons
; tem
= Fcdr (tem
))
829 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
831 prompt
= map_prompt (keymap
);
832 if (NILP (title
) && !NILP (prompt
))
836 /* Extract the detailed info to make one pane. */
837 keymap_panes (maps
, nmaps
, NILP (position
));
839 /* Make the title be the pane title of the first pane. */
840 if (!NILP (title
) && menu_items_n_panes
>= 0)
841 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
847 /* We were given an old-fashioned menu. */
849 CHECK_STRING (title
, 1);
851 list_of_panes (Fcdr (menu
));
858 discard_menu_items ();
863 /* Display them in a menu. */
866 selection
= xmenu_show (f
, xpos
, ypos
, menubarp
,
867 keymaps
, title
, &error_name
);
870 discard_menu_items ();
874 if (error_name
) error (error_name
);
878 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
879 "Pop up a dialog box and return user's selection.\n\
880 POSITION specifies which frame to use.\n\
881 This is normally a mouse button event or a window or frame.\n\
882 If POSITION is t, it means to use the frame the mouse is on.\n\
883 The dialog box appears in the middle of the specified frame.\n\
885 CONTENTS specifies the alternatives to display in the dialog box.\n\
886 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
887 Each ITEM is a cons cell (STRING . VALUE).\n\
888 The return value is VALUE from the chosen item.\n\n\
889 An ITEM may also be just a string--that makes a nonselectable item.\n\
890 An ITEM may also be nil--that means to put all preceding items\n\
891 on the left of the dialog box and all following items on the right.\n\
892 \(By default, approximately half appear on each side.)")
894 Lisp_Object position
, contents
;
901 /* Decode the first argument: find the window or frame to use. */
902 if (EQ (position
, Qt
))
904 #if 0 /* Using the frame the mouse is on may not be right. */
905 /* Use the mouse's current position. */
907 Lisp_Object bar_window
;
912 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
915 XSET (window
, Lisp_Frame
, new_f
);
917 window
= selected_window
;
919 /* Decode the first argument: find the window and the coordinates. */
920 if (EQ (position
, Qt
))
921 window
= selected_window
;
923 else if (CONSP (position
))
926 tem
= Fcar (position
);
927 if (XTYPE (tem
) == Lisp_Cons
)
928 window
= Fcar (Fcdr (position
));
931 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
932 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
935 else if (WINDOWP (position
) || FRAMEP (position
))
938 /* Decode where to put the menu. */
940 if (XTYPE (window
) == Lisp_Frame
)
942 else if (XTYPE (window
) == Lisp_Window
)
944 CHECK_LIVE_WINDOW (window
, 0);
945 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
948 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
949 but I don't want to make one now. */
950 CHECK_WINDOW (window
, 0);
952 #ifndef USE_X_TOOLKIT
953 /* Display a menu with these alternatives
954 in the middle of frame F. */
956 Lisp_Object x
, y
, frame
, newpos
;
957 XSET (frame
, Lisp_Frame
, f
);
958 XSET (x
, Lisp_Int
, x_pixel_width (f
) / 2);
959 XSET (y
, Lisp_Int
, x_pixel_height (f
) / 2);
960 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
962 return Fx_popup_menu (newpos
,
963 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
969 Lisp_Object selection
;
971 /* Decode the dialog items from what was specified. */
972 title
= Fcar (contents
);
973 CHECK_STRING (title
, 1);
975 list_of_panes (Fcons (contents
, Qnil
));
977 /* Display them in a dialog box. */
979 selection
= xdialog_show (f
, 0, 0, title
, &error_name
);
982 discard_menu_items ();
984 if (error_name
) error (error_name
);
993 dispatch_dummy_expose (w
, x
, y
)
1000 dummy
.type
= Expose
;
1001 dummy
.window
= XtWindow (w
);
1004 dummy
.send_event
= 0;
1005 dummy
.display
= XtDisplay (w
);
1009 XtDispatchEvent ((XEvent
*) &dummy
);
1013 event_is_in_menu_item (mw
, event
, name
, string_w
)
1015 struct input_event
*event
;
1019 *string_w
+= (string_width (mw
, name
)
1020 + 2 * (mw
->menu
.horizontal_spacing
1021 + mw
->menu
.shadow_thickness
));
1022 return XINT (event
->x
) < *string_w
;
1026 /* Return the menu bar key which corresponds to event EVENT in frame F. */
1029 map_event_to_object (event
, f
)
1030 struct input_event
*event
;
1035 XlwMenuWidget mw
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1040 /* Find the window */
1041 for (val
= mw
->menu
.old_stack
[0]->contents
; val
; val
= val
->next
)
1043 ws
= &mw
->menu
.windows
[0];
1044 if (ws
&& event_is_in_menu_item (mw
, event
, val
->name
, &string_w
))
1049 items
= FRAME_MENU_BAR_ITEMS (f
);
1051 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1053 Lisp_Object pos
, string
, item
;
1054 item
= XVECTOR (items
)->contents
[i
];
1055 string
= XVECTOR (items
)->contents
[i
+ 1];
1056 pos
= XVECTOR (items
)->contents
[i
+ 2];
1060 if (!strcmp (val
->name
, XSTRING (string
)->data
))
1069 static Lisp_Object
*volatile menu_item_selection
;
1071 static Lisp_Object
*menu_item_selection
;
1075 popup_selection_callback (widget
, id
, client_data
)
1078 XtPointer client_data
;
1080 menu_item_selection
= (Lisp_Object
*) client_data
;
1084 popup_down_callback (widget
, id
, client_data
)
1087 XtPointer client_data
;
1090 lw_destroy_all_widgets (id
);
1095 dialog_selection_callback (widget
, id
, client_data
)
1098 XtPointer client_data
;
1100 if ((int)client_data
!= -1)
1101 menu_item_selection
= (Lisp_Object
*) client_data
;
1103 lw_destroy_all_widgets (id
);
1107 /* This recursively calls free_widget_value() on the tree of widgets.
1108 It must free all data that was malloc'ed for these widget_values.
1109 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1110 must be left alone. */
1113 free_menubar_widget_value_tree (wv
)
1118 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1120 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1122 free_menubar_widget_value_tree (wv
->contents
);
1123 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1127 free_menubar_widget_value_tree (wv
->next
);
1128 wv
->next
= (widget_value
*) 0xDEADBEEF;
1131 free_widget_value (wv
);
1135 extern void EmacsFrameSetCharSize ();
1138 update_frame_menubar (f
)
1141 struct x_display
*x
= f
->display
.x
;
1143 int menubar_changed
;
1145 menubar_changed
= (x
->menubar_widget
1146 && !XtIsManaged (x
->menubar_widget
));
1148 if (! (menubar_changed
))
1152 /* Save the size of the frame because the pane widget doesn't accept to
1153 resize itself. So force it. */
1158 XawPanedSetRefigureMode (x
->column_widget
, 0);
1160 /* the order in which children are managed is the top to
1161 bottom order in which they are displayed in the paned window.
1162 First, remove the text-area widget.
1164 XtUnmanageChild (x
->edit_widget
);
1166 /* remove the menubar that is there now, and put up the menubar that
1169 if (menubar_changed
)
1171 XtManageChild (x
->menubar_widget
);
1172 XtMapWidget (x
->menubar_widget
);
1173 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1177 /* Re-manage the text-area widget */
1178 XtManageChild (x
->edit_widget
);
1180 /* and now thrash the sizes */
1181 XawPanedSetRefigureMode (x
->column_widget
, 1);
1183 /* Force the pane widget to resize itself with the right values. */
1184 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1190 set_frame_menubar (f
, first_time
)
1194 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
1196 Lisp_Object tail
, items
;
1197 widget_value
*wv
, *save_wv
, *first_wv
, *prev_wv
= 0;
1202 wv
= malloc_widget_value ();
1203 wv
->name
= "menubar";
1206 save_wv
= first_wv
= wv
;
1208 if (NILP (items
= FRAME_MENU_BAR_ITEMS (f
)))
1209 items
= FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1211 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1215 string
= XVECTOR (items
)->contents
[i
+ 1];
1219 wv
= malloc_widget_value ();
1223 save_wv
->contents
= wv
;
1224 wv
->name
= (char *) XSTRING (string
)->data
;
1231 lw_modify_all_widgets (id
, first_wv
, False
);
1234 menubar_widget
= lw_create_widget ("menubar", "menubar",
1236 f
->display
.x
->column_widget
,
1239 f
->display
.x
->menubar_widget
= menubar_widget
;
1240 XtVaSetValues (menubar_widget
,
1242 XtNresizeToPreferred
, 1,
1247 free_menubar_widget_value_tree (first_wv
);
1249 /* Don't update the menubar the first time it is created via x_window. */
1251 update_frame_menubar (f
);
1257 free_frame_menubar (f
)
1260 Widget menubar_widget
;
1263 menubar_widget
= f
->display
.x
->menubar_widget
;
1269 lw_destroy_all_widgets (id
);
1273 /* Called from Fx_create_frame to create the inital menubar of a frame
1274 before it is mapped, so that the window is mapped with the menubar already
1275 there instead of us tacking it on later and thrashing the window after it
1278 initialize_frame_menubar (f
)
1281 set_frame_menubar (f
, 1);
1284 /* Horizontal bounds of the current menu bar item. */
1286 static int this_menu_bar_item_beg
;
1287 static int this_menu_bar_item_end
;
1289 /* Horizontal position of the end of the last menu bar item. */
1291 static int last_menu_bar_item_end
;
1293 /* Nonzero if position X, Y is in the menu bar and in some menu bar item
1294 but not in the current menu bar item. */
1297 other_menu_bar_item_p (f
, x
, y
)
1302 && f
->display
.x
->menubar_widget
!= 0
1303 && y
< f
->display
.x
->menubar_widget
->core
.height
1305 && x
< last_menu_bar_item_end
1306 && (x
>= this_menu_bar_item_end
1307 || x
< this_menu_bar_item_beg
));
1310 /* Unread a button-press event in the menu bar of frame F
1311 at x position XPOS relative to the inside of the frame. */
1314 unread_menu_bar_button (f
, xpos
)
1320 event
.type
= ButtonPress
;
1321 event
.xbutton
.display
= x_current_display
;
1322 event
.xbutton
.serial
= 0;
1323 event
.xbutton
.send_event
= 0;
1324 event
.xbutton
.time
= CurrentTime
;
1325 event
.xbutton
.button
= Button1
;
1326 event
.xbutton
.window
= XtWindow (f
->display
.x
->menubar_widget
);
1327 event
.xbutton
.x
= xpos
;
1328 XPutBackEvent (XDISPLAY
&event
);
1331 /* If the mouse has moved to another menu bar item,
1332 return 1 and unread a button press event for that item.
1333 Otherwise return 0. */
1336 check_mouse_other_menu_bar (f
)
1340 Lisp_Object bar_window
;
1345 (*mouse_position_hook
) (&new_f
, &bar_window
, &part
, &x
, &y
, &time
);
1347 if (f
== new_f
&& other_menu_bar_item_p (f
, x
, y
))
1349 unread_menu_bar_button (f
, x
);
1355 #endif /* USE_X_TOOLKIT */
1357 /* xmenu_show actually displays a menu using the panes and items in menu_items
1358 and returns the value selected from it.
1359 There are two versions of xmenu_show, one for Xt and one for Xlib.
1360 Both assume input is blocked by the caller. */
1362 /* F is the frame the menu is for.
1363 X and Y are the frame-relative specified position,
1364 relative to the inside upper left corner of the frame F.
1365 MENUBARP is 1 if the click that asked for this menu came from the menu bar.
1366 KEYMAPS is 1 if this menu was specified with keymaps;
1367 in that case, we return a list containing the chosen item's value
1368 and perhaps also the pane's prefix.
1369 TITLE is the specified menu title.
1370 ERROR is a place to store an error message string in case of failure.
1371 (We return nil on failure, but the value doesn't actually matter.) */
1373 #ifdef USE_X_TOOLKIT
1375 extern unsigned last_event_timestamp
;
1376 extern Lisp_Object Vdouble_click_time
;
1378 extern unsigned int x_mouse_grabbed
;
1379 extern Lisp_Object Vmouse_depressed
;
1382 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
1394 XlwMenuWidget menubar
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1398 /* This is the menu bar item (if any) that led to this menu. */
1399 widget_value
*menubar_item
= 0;
1401 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1402 widget_value
**submenu_stack
1403 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1404 Lisp_Object
*subprefix_stack
1405 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1406 int submenu_depth
= 0;
1408 /* Define a queue to save up for later unreading
1409 all X events that don't pertain to the menu. */
1413 struct event_queue
*next
;
1416 struct event_queue
*queue
= NULL
;
1417 struct event_queue
*queue_tmp
;
1419 Position root_x
, root_y
;
1422 int next_release_must_exit
= 0;
1426 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1428 *error
= "Empty menu";
1431 this_menu_bar_item_beg
= -1;
1432 this_menu_bar_item_end
= -1;
1433 last_menu_bar_item_end
= -1;
1435 /* Figure out which menu bar item, if any, this menu is for. */
1440 widget_value
*mb_item
= 0;
1442 for (mb_item
= menubar
->menu
.old_stack
[0]->contents
;
1444 mb_item
= mb_item
->next
)
1447 xend
+= (string_width (menubar
, mb_item
->name
)
1448 + 2 * (menubar
->menu
.horizontal_spacing
1449 + menubar
->menu
.shadow_thickness
));
1450 if (x
>= xbeg
&& x
< xend
)
1454 menubar_item
= mb_item
;
1455 /* Arrange to show a different menu if we move in the menu bar
1456 to a different item. */
1457 this_menu_bar_item_beg
= xbeg
;
1458 this_menu_bar_item_end
= xend
;
1461 last_menu_bar_item_end
= xend
;
1463 if (menubar_item
== 0)
1466 /* Offset the coordinates to root-relative. */
1467 if (f
->display
.x
->menubar_widget
!= 0)
1468 y
+= f
->display
.x
->menubar_widget
->core
.height
;
1469 XtTranslateCoords (f
->display
.x
->widget
,
1470 x
, y
, &root_x
, &root_y
);
1474 /* Create a tree of widget_value objects
1475 representing the panes and their items. */
1476 wv
= malloc_widget_value ();
1483 /* Loop over all panes and items, filling in the tree. */
1485 while (i
< menu_items_used
)
1487 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1489 submenu_stack
[submenu_depth
++] = save_wv
;
1495 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1498 save_wv
= submenu_stack
[--submenu_depth
];
1502 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1503 && submenu_depth
!= 0)
1504 i
+= MENU_ITEMS_PANE_LENGTH
;
1505 /* Ignore a nil in the item list.
1506 It's meaningful only for dialog boxes. */
1507 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1509 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1511 /* Create a new pane. */
1512 Lisp_Object pane_name
, prefix
;
1514 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1515 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1516 pane_string
= (NILP (pane_name
)
1517 ? "" : (char *) XSTRING (pane_name
)->data
);
1518 /* If there is just one top-level pane, put all its items directly
1519 under the top-level menu. */
1520 if (menu_items_n_panes
== 1)
1523 /* If the pane has a meaningful name,
1524 make the pane a top-level menu item
1525 with its items as a submenu beneath it. */
1526 if (!keymaps
&& strcmp (pane_string
, ""))
1528 wv
= malloc_widget_value ();
1532 first_wv
->contents
= wv
;
1533 wv
->name
= pane_string
;
1534 if (keymaps
&& !NILP (prefix
))
1541 else if (first_pane
)
1547 i
+= MENU_ITEMS_PANE_LENGTH
;
1551 /* Create a new item within current pane. */
1552 Lisp_Object item_name
, enable
, descrip
;
1553 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1554 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1556 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1558 wv
= malloc_widget_value ();
1562 save_wv
->contents
= wv
;
1563 wv
->name
= (char *) XSTRING (item_name
)->data
;
1564 if (!NILP (descrip
))
1565 wv
->key
= (char *) XSTRING (descrip
)->data
;
1567 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1568 wv
->enabled
= !NILP (enable
);
1571 i
+= MENU_ITEMS_ITEM_LENGTH
;
1575 /* Actually create the menu. */
1576 menu_id
= ++popup_id_tick
;
1577 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1578 f
->display
.x
->widget
, 1, 0,
1579 popup_selection_callback
, popup_down_callback
);
1581 /* Don't allow any geometry request from the user. */
1582 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1583 XtSetValues (menu
, av
, ac
);
1585 /* Free the widget_value objects we used to specify the contents. */
1586 free_menubar_widget_value_tree (first_wv
);
1588 /* No selection has been chosen yet. */
1589 menu_item_selection
= 0;
1591 /* If the mouse moves out of the menu before we show the menu,
1592 don't show it at all. */
1593 if (check_mouse_other_menu_bar (f
))
1595 lw_destroy_all_widgets (menu_id
);
1600 /* Highlight the menu bar item (if any) that led to this menu. */
1603 menubar_item
->call_data
= (XtPointer
) 1;
1604 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
1607 /* Display the menu. */
1609 XButtonPressedEvent dummy
;
1612 mw
= (XlwMenuWidget
) ((CompositeWidget
)menu
)->composite
.children
[0];
1614 dummy
.type
= ButtonPress
;
1616 dummy
.send_event
= 0;
1617 dummy
.display
= XtDisplay (menu
);
1618 dummy
.window
= XtWindow (XtParent (menu
));
1619 dummy
.time
= CurrentTime
;
1624 /* We activate directly the lucid implementation. */
1625 pop_up_menu (mw
, &dummy
);
1628 /* No need to check a second time since this is done in the XEvent loop.
1629 This slows done the execution. */
1631 /* Check again whether the mouse has moved to another menu bar item. */
1632 if (check_mouse_other_menu_bar (f
))
1634 /* The mouse moved into a different menu bar item.
1635 We should bring up that item's menu instead.
1636 First pop down this menu. */
1637 #if 0 /* xlwmenu.c now does this. */
1638 XtUngrabPointer ((Widget
)
1640 ((CompositeWidget
)menu
)->composite
.children
[0]),
1643 lw_destroy_all_widgets (menu_id
);
1648 /* Process events that apply to the menu. */
1652 int queue_and_exit
= 0;
1654 XtAppNextEvent (Xt_app_con
, &event
);
1655 if (event
.type
== ButtonRelease
)
1657 XtDispatchEvent (&event
);
1659 /* Do the work of construct_mouse_click since it can't
1660 be called. Initially, the popup menu has been called
1661 from a ButtonPress in the edit_widget. Then the mouse
1662 has been set to grabbed. Reset it now. */
1663 x_mouse_grabbed
&= ~(1 << event
.xbutton
.button
);
1664 if (!x_mouse_grabbed
)
1665 Vmouse_depressed
= Qnil
;
1667 /* If we release the button soon without selecting anything,
1668 stay in the loop--that is, leave the menu posted.
1669 Otherwise, exit this loop and thus pop down the menu. */
1670 if (! (menu_item_selection
== 0
1671 && !next_release_must_exit
1672 && (((XButtonEvent
*) (&event
))->time
- last_event_timestamp
1673 < XINT (Vdouble_click_time
))))
1675 /* Don't call XtDispatchEvent again for the same event! */
1678 else if (event
.type
== ButtonPress
)
1680 next_release_must_exit
= 1;
1682 XtDispatchEvent (&event
);
1683 /* Any mouse button activity that doesn't select in the menu
1684 should unpost the menu. */
1685 if (menu_item_selection
== 0)
1688 /* Don't call XtDispatchEvent for the down event.
1689 Doing so seems to give strange results
1690 when you click on the menu bar while a menu is posted. */
1693 else if (event
.type
== KeyPress
)
1695 /* Exit the loop, but first queue this event for reuse. */
1698 else if (event
.type
== Expose
)
1699 process_expose_from_menu (event
);
1700 /* If the mouse moves to a different menu bar item, switch to
1701 that item's menu. But only if the button is still held down. */
1702 else if (event
.type
== MotionNotify
1705 int event_x
= (event
.xmotion
.x_root
1706 - (f
->display
.x
->widget
->core
.x
1707 + f
->display
.x
->widget
->core
.border_width
));
1708 int event_y
= (event
.xmotion
.y_root
1709 - (f
->display
.x
->widget
->core
.y
1710 + f
->display
.x
->widget
->core
.border_width
));
1712 if (other_menu_bar_item_p (f
, event_x
, event_y
))
1714 /* The mouse moved into a different menu bar item.
1715 We should bring up that item's menu instead.
1716 First pop down this menu. */
1717 #if 0 /* xlwmenu.c now does this. */
1718 XtUngrabPointer ((Widget
)
1720 ((CompositeWidget
)menu
)->composite
.children
[0]),
1721 event
.xbutton
.time
);
1723 lw_destroy_all_widgets (menu_id
);
1725 /* Put back an event that will bring up the other item's menu. */
1726 unread_menu_bar_button (f
, event_x
);
1727 /* Don't let us select anything in this case. */
1728 menu_item_selection
= 0;
1732 else if (event
.type
== UnmapNotify
)
1734 /* If the menu disappears, there is no need to stay in the
1736 if (event
.xunmap
.window
== menu
->core
.window
)
1740 XtDispatchEvent (&event
);
1742 || XtWindowToWidget (XDISPLAY event
.xany
.window
) != menu
)
1745 = (struct event_queue
*) malloc (sizeof (struct event_queue
));
1747 if (queue_tmp
!= NULL
)
1749 queue_tmp
->event
= event
;
1750 queue_tmp
->next
= queue
;
1759 /* Unhighlight the menu bar item (if any) that led to this menu. */
1762 menubar_item
->call_data
= (XtPointer
) 0;
1763 dispatch_dummy_expose (f
->display
.x
->menubar_widget
, x
, y
);
1766 /* fp turned off the following statement and wrote a comment
1767 that it is unnecessary--that the menu has already disappeared.
1768 I observer that is not so. -- rms. */
1769 /* Make sure the menu disappears. */
1770 lw_destroy_all_widgets (menu_id
);
1772 /* Unread any events that we got but did not handle. */
1773 while (queue
!= NULL
)
1776 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
1777 queue
= queue_tmp
->next
;
1778 free ((char *)queue_tmp
);
1779 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1780 interrupt_input_pending
= 1;
1783 /* Find the selected item, and its pane, to return
1784 the proper value. */
1785 if (menu_item_selection
!= 0)
1791 while (i
< menu_items_used
)
1795 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1797 subprefix_stack
[submenu_depth
++] = prefix
;
1801 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1803 prefix
= subprefix_stack
[--submenu_depth
];
1806 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1809 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1810 i
+= MENU_ITEMS_PANE_LENGTH
;
1815 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1816 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1822 entry
= Fcons (entry
, Qnil
);
1824 entry
= Fcons (prefix
, entry
);
1825 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1826 if (!NILP (subprefix_stack
[j
]))
1827 entry
= Fcons (subprefix_stack
[j
], entry
);
1831 i
+= MENU_ITEMS_ITEM_LENGTH
;
1839 static char * button_names
[] = {
1840 "button1", "button2", "button3", "button4", "button5",
1841 "button6", "button7", "button8", "button9", "button10" };
1844 xdialog_show (f
, menubarp
, keymaps
, title
, error
)
1851 int i
, nb_buttons
=0;
1854 XlwMenuWidget menubar
= (XlwMenuWidget
) f
->display
.x
->menubar_widget
;
1855 char dialog_name
[6];
1857 /* This is the menu bar item (if any) that led to this menu. */
1858 widget_value
*menubar_item
= 0;
1860 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1862 /* Define a queue to save up for later unreading
1863 all X events that don't pertain to the menu. */
1867 struct event_queue
*next
;
1870 struct event_queue
*queue
= NULL
;
1871 struct event_queue
*queue_tmp
;
1873 /* Number of elements seen so far, before boundary. */
1875 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1876 int boundary_seen
= 0;
1880 if (menu_items_n_panes
> 1)
1882 *error
= "Multiple panes in dialog box";
1886 /* Create a tree of widget_value objects
1887 representing the text label and buttons. */
1889 Lisp_Object pane_name
, prefix
;
1891 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
1892 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
1893 pane_string
= (NILP (pane_name
)
1894 ? "" : (char *) XSTRING (pane_name
)->data
);
1895 prev_wv
= malloc_widget_value ();
1896 prev_wv
->value
= pane_string
;
1897 if (keymaps
&& !NILP (prefix
))
1899 prev_wv
->enabled
= 1;
1900 prev_wv
->name
= "message";
1903 /* Loop over all panes and items, filling in the tree. */
1904 i
= MENU_ITEMS_PANE_LENGTH
;
1905 while (i
< menu_items_used
)
1908 /* Create a new item within current pane. */
1909 Lisp_Object item_name
, enable
, descrip
;
1910 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1911 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1913 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1915 if (NILP (item_name
))
1917 free_menubar_widget_value_tree (first_wv
);
1918 *error
= "Submenu in dialog items";
1921 if (EQ (item_name
, Qquote
))
1923 /* This is the boundary between left-side elts
1924 and right-side elts. Stop incrementing right_count. */
1929 if (nb_buttons
>= 10)
1931 free_menubar_widget_value_tree (first_wv
);
1932 *error
= "Too many dialog items";
1936 wv
= malloc_widget_value ();
1938 wv
->name
= (char *) button_names
[nb_buttons
];
1939 if (!NILP (descrip
))
1940 wv
->key
= (char *) XSTRING (descrip
)->data
;
1941 wv
->value
= (char *) XSTRING (item_name
)->data
;
1942 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
1943 wv
->enabled
= !NILP (enable
);
1946 if (! boundary_seen
)
1950 i
+= MENU_ITEMS_ITEM_LENGTH
;
1953 /* If the boundary was not specified,
1954 by default put half on the left and half on the right. */
1955 if (! boundary_seen
)
1956 left_count
= nb_buttons
- nb_buttons
/ 2;
1958 wv
= malloc_widget_value ();
1959 wv
->name
= dialog_name
;
1961 /* Dialog boxes use a really stupid name encoding
1962 which specifies how many buttons to use
1963 and how many buttons are on the right.
1964 The Q means something also. */
1965 dialog_name
[0] = 'Q';
1966 dialog_name
[1] = '0' + nb_buttons
;
1967 dialog_name
[2] = 'B';
1968 dialog_name
[3] = 'R';
1969 /* Number of buttons to put on the right. */
1970 dialog_name
[4] = '0' + nb_buttons
- left_count
;
1972 wv
->contents
= first_wv
;
1976 /* Actually create the dialog. */
1977 dialog_id
= ++popup_id_tick
;
1978 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1979 f
->display
.x
->widget
, 1, 0,
1980 dialog_selection_callback
, 0);
1981 #if 0 /* This causes crashes, and seems to be redundant -- rms. */
1982 lw_modify_all_widgets (dialog_id
, first_wv
, True
);
1984 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1985 /* Free the widget_value objects we used to specify the contents. */
1986 free_menubar_widget_value_tree (first_wv
);
1988 /* No selection has been chosen yet. */
1989 menu_item_selection
= 0;
1991 /* Display the menu. */
1992 lw_pop_up_all_widgets (dialog_id
);
1994 /* Process events that apply to the menu. */
1999 XtAppNextEvent (Xt_app_con
, &event
);
2000 if (event
.type
== ButtonRelease
)
2002 XtDispatchEvent (&event
);
2005 else if (event
.type
== Expose
)
2006 process_expose_from_menu (event
);
2007 XtDispatchEvent (&event
);
2008 if (XtWindowToWidget(XDISPLAY event
.xany
.window
) != menu
)
2010 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
2012 if (queue_tmp
!= NULL
)
2014 queue_tmp
->event
= event
;
2015 queue_tmp
->next
= queue
;
2022 /* State that no mouse buttons are now held.
2023 That is not necessarily true, but the fiction leads to reasonable
2024 results, and it is a pain to ask which are actually held now
2025 or track this in the loop above. */
2026 x_mouse_grabbed
= 0;
2028 /* Unread any events that we got but did not handle. */
2029 while (queue
!= NULL
)
2032 XPutBackEvent (XDISPLAY
&queue_tmp
->event
);
2033 queue
= queue_tmp
->next
;
2034 free ((char *)queue_tmp
);
2035 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
2036 interrupt_input_pending
= 1;
2039 /* Find the selected item, and its pane, to return
2040 the proper value. */
2041 if (menu_item_selection
!= 0)
2047 while (i
< menu_items_used
)
2051 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2054 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2055 i
+= MENU_ITEMS_PANE_LENGTH
;
2060 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2061 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2065 entry
= Fcons (entry
, Qnil
);
2067 entry
= Fcons (prefix
, entry
);
2071 i
+= MENU_ITEMS_ITEM_LENGTH
;
2078 #else /* not USE_X_TOOLKIT */
2081 xmenu_show (f
, x
, y
, menubarp
, keymaps
, title
, error
)
2091 int pane
, selidx
, lpane
, status
;
2092 Lisp_Object entry
, pane_prefix
;
2094 int ulx
, uly
, width
, height
;
2095 int dispwidth
, dispheight
;
2099 unsigned int dummy_uint
;
2102 if (menu_items_n_panes
== 0)
2105 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2107 *error
= "Empty menu";
2111 /* Figure out which root window F is on. */
2112 XGetGeometry (x_current_display
, FRAME_X_WINDOW (f
), &root
,
2113 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2114 &dummy_uint
, &dummy_uint
);
2116 /* Make the menu on that window. */
2117 menu
= XMenuCreate (XDISPLAY root
, "emacs");
2120 *error
= "Can't create menu";
2124 /* Adjust coordinates to relative to the outer (window manager) window. */
2128 int win_x
= 0, win_y
= 0;
2130 /* Find the position of the outside upper-left corner of
2131 the inner window, with respect to the outer window. */
2132 if (f
->display
.x
->parent_desc
!= ROOT_WINDOW
)
2135 XTranslateCoordinates (x_current_display
,
2137 /* From-window, to-window. */
2138 f
->display
.x
->window_desc
,
2139 f
->display
.x
->parent_desc
,
2141 /* From-position, to-position. */
2142 0, 0, &win_x
, &win_y
,
2144 /* Child of window. */
2151 #endif /* HAVE_X11 */
2153 /* Adjust coordinates to be root-window-relative. */
2154 x
+= f
->display
.x
->left_pos
;
2155 y
+= f
->display
.x
->top_pos
;
2157 /* Create all the necessary panes and their items. */
2159 while (i
< menu_items_used
)
2161 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2163 /* Create a new pane. */
2164 Lisp_Object pane_name
, prefix
;
2167 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2168 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2169 pane_string
= (NILP (pane_name
)
2170 ? "" : (char *) XSTRING (pane_name
)->data
);
2171 if (keymaps
&& !NILP (prefix
))
2174 lpane
= XMenuAddPane (XDISPLAY menu
, pane_string
, TRUE
);
2175 if (lpane
== XM_FAILURE
)
2177 XMenuDestroy (XDISPLAY menu
);
2178 *error
= "Can't create pane";
2181 i
+= MENU_ITEMS_PANE_LENGTH
;
2183 /* Find the width of the widest item in this pane. */
2186 while (j
< menu_items_used
)
2189 item
= XVECTOR (menu_items
)->contents
[j
];
2197 width
= XSTRING (item
)->size
;
2198 if (width
> maxwidth
)
2201 j
+= MENU_ITEMS_ITEM_LENGTH
;
2204 /* Ignore a nil in the item list.
2205 It's meaningful only for dialog boxes. */
2206 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2210 /* Create a new item within current pane. */
2211 Lisp_Object item_name
, enable
, descrip
;
2212 unsigned char *item_data
;
2214 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2215 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2217 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2218 if (!NILP (descrip
))
2220 int gap
= maxwidth
- XSTRING (item_name
)->size
;
2223 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2224 item_name
= concat2 (item_name
, spacer
);
2225 item_name
= concat2 (item_name
, descrip
);
2226 item_data
= XSTRING (item_name
)->data
;
2228 /* if alloca is fast, use that to make the space,
2229 to reduce gc needs. */
2231 = (unsigned char *) alloca (maxwidth
2232 + XSTRING (descrip
)->size
+ 1);
2233 bcopy (XSTRING (item_name
)->data
, item_data
,
2234 XSTRING (item_name
)->size
);
2235 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2237 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2238 XSTRING (descrip
)->size
);
2239 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2243 item_data
= XSTRING (item_name
)->data
;
2245 if (XMenuAddSelection (XDISPLAY menu
, lpane
, 0, item_data
,
2249 XMenuDestroy (XDISPLAY menu
);
2250 *error
= "Can't add selection to menu";
2253 i
+= MENU_ITEMS_ITEM_LENGTH
;
2257 /* All set and ready to fly. */
2258 XMenuRecompute (XDISPLAY menu
);
2259 dispwidth
= DisplayWidth (x_current_display
, XDefaultScreen (x_current_display
));
2260 dispheight
= DisplayHeight (x_current_display
, XDefaultScreen (x_current_display
));
2261 x
= min (x
, dispwidth
);
2262 y
= min (y
, dispheight
);
2265 XMenuLocate (XDISPLAY menu
, 0, 0, x
, y
,
2266 &ulx
, &uly
, &width
, &height
);
2267 if (ulx
+width
> dispwidth
)
2269 x
-= (ulx
+ width
) - dispwidth
;
2270 ulx
= dispwidth
- width
;
2272 if (uly
+height
> dispheight
)
2274 y
-= (uly
+ height
) - dispheight
;
2275 uly
= dispheight
- height
;
2277 if (ulx
< 0) x
-= ulx
;
2278 if (uly
< 0) y
-= uly
;
2280 XMenuSetAEQ (menu
, TRUE
);
2281 XMenuSetFreeze (menu
, TRUE
);
2284 status
= XMenuActivate (XDISPLAY menu
, &pane
, &selidx
,
2285 x
, y
, ButtonReleaseMask
, &datap
);
2290 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2293 /* Find the item number SELIDX in pane number PANE. */
2295 while (i
< menu_items_used
)
2297 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2301 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2303 i
+= MENU_ITEMS_PANE_LENGTH
;
2312 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2315 entry
= Fcons (entry
, Qnil
);
2316 if (!NILP (pane_prefix
))
2317 entry
= Fcons (pane_prefix
, entry
);
2323 i
+= MENU_ITEMS_ITEM_LENGTH
;
2329 *error
= "Can't activate menu";
2335 XMenuDestroy (XDISPLAY menu
);
2337 /* State that no mouse buttons are now held.
2338 (The oldXMenu code doesn't track this info for us.)
2339 That is not necessarily true, but the fiction leads to reasonable
2340 results, and it is a pain to ask which are actually held now. */
2341 x_mouse_grabbed
= 0;
2345 #endif /* not USE_X_TOOLKIT */
2349 staticpro (&menu_items
);
2352 popup_id_tick
= (1<<16);
2353 defsubr (&Sx_popup_menu
);
2354 defsubr (&Sx_popup_dialog
);