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"
50 /* This may include sys/types.h, and that somehow loses
51 if this is not done before the other system files. */
55 /* Load sys/types.h if not already loaded.
56 In some systems loading it twice is suicidal. */
58 #include <sys/types.h>
61 #include "dispextern.h"
66 #include <X11/IntrinsicP.h>
67 #include <X11/CoreP.h>
68 #include <X11/StringDefs.h>
69 #include <X11/Shell.h>
70 #include "../lwlib/lwlib.h"
71 #else /* not USE_X_TOOLKIT */
72 #include "../oldXMenu/XMenu.h"
73 #endif /* not USE_X_TOOLKIT */
74 #endif /* HAVE_X_WINDOWS */
76 #define min(x,y) (((x) < (y)) ? (x) : (y))
77 #define max(x,y) (((x) > (y)) ? (x) : (y))
84 extern Lisp_Object Qmenu_enable
;
85 extern Lisp_Object Qmenu_bar
;
86 extern Lisp_Object Qmouse_click
, Qevent_kind
;
89 extern void process_expose_from_menu ();
90 extern XtAppContext Xt_app_con
;
92 static Lisp_Object
xdialog_show ();
93 void popup_get_selection ();
96 static Lisp_Object
xmenu_show ();
97 static void keymap_panes ();
98 static void single_keymap_panes ();
99 static void list_of_panes ();
100 static void list_of_items ();
102 /* This holds a Lisp vector that holds the results of decoding
103 the keymaps or alist-of-alists that specify a menu.
105 It describes the panes and items within the panes.
107 Each pane is described by 3 elements in the vector:
108 t, the pane name, the pane's prefix key.
109 Then follow the pane's items, with 5 elements per item:
110 the item string, the enable flag, the item's value,
111 the definition, and the equivalent keyboard key's description string.
113 In some cases, multiple levels of menus may be described.
114 A single vector slot containing nil indicates the start of a submenu.
115 A single vector slot containing lambda indicates the end of a submenu.
116 The submenu follows a menu item which is the way to reach the submenu.
118 A single vector slot containing quote indicates that the
119 following items should appear on the right of a dialog box.
121 Using a Lisp vector to hold this information while we decode it
122 takes care of protecting all the data from GC. */
124 #define MENU_ITEMS_PANE_NAME 1
125 #define MENU_ITEMS_PANE_PREFIX 2
126 #define MENU_ITEMS_PANE_LENGTH 3
128 #define MENU_ITEMS_ITEM_NAME 0
129 #define MENU_ITEMS_ITEM_ENABLE 1
130 #define MENU_ITEMS_ITEM_VALUE 2
131 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
132 #define MENU_ITEMS_ITEM_DEFINITION 4
133 #define MENU_ITEMS_ITEM_LENGTH 5
135 static Lisp_Object menu_items
;
137 /* Number of slots currently allocated in menu_items. */
138 static int menu_items_allocated
;
140 /* This is the index in menu_items of the first empty slot. */
141 static int menu_items_used
;
143 /* The number of panes currently recorded in menu_items,
144 excluding those within submenus. */
145 static int menu_items_n_panes
;
147 /* Current depth within submenus. */
148 static int menu_items_submenu_depth
;
150 /* Flag which when set indicates a dialog or menu has been posted by
151 Xt on behalf of one of the widget sets. */
152 static int popup_activated_flag
;
154 /* This holds a Lisp vector
155 which contains frames that have menu bars.
156 Each frame that has a menu bar is found at some index in this vector
157 and the menu bar widget refers to the frame through that index. */
158 static Lisp_Object frame_vector
;
160 /* Return the index of FRAME in frame_vector.
161 If FRAME isn't in frame_vector yet, put it in,
162 lengthening the vector if necessary. */
165 frame_vector_add_frame (f
)
168 int length
= XVECTOR (frame_vector
)->size
;
170 Lisp_Object
new, frame
;
172 XSETFRAME (frame
, f
);
174 for (i
= 0; i
< length
; i
++)
176 if (EQ (frame
, XVECTOR (frame_vector
)->contents
[i
]))
178 if (NILP (XVECTOR (frame_vector
)->contents
[i
]))
184 XVECTOR (frame_vector
)->contents
[empty
] = frame
;
188 new = Fmake_vector (make_number (length
* 2), Qnil
);
189 bcopy (XVECTOR (frame_vector
)->contents
,
190 XVECTOR (new)->contents
, sizeof (Lisp_Object
) * length
);
192 XVECTOR (frame_vector
)->contents
[length
] = frame
;
196 /* Initialize the menu_items structure if we haven't already done so.
197 Also mark it as currently empty. */
202 if (NILP (menu_items
))
204 menu_items_allocated
= 60;
205 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
209 menu_items_n_panes
= 0;
210 menu_items_submenu_depth
= 0;
213 /* Call at the end of generating the data in menu_items.
214 This fills in the number of items in the last pane. */
221 /* Call when finished using the data for the current menu
225 discard_menu_items ()
227 /* Free the structure if it is especially large.
228 Otherwise, hold on to it, to save time. */
229 if (menu_items_allocated
> 200)
232 menu_items_allocated
= 0;
236 /* Make the menu_items vector twice as large. */
242 int old_size
= menu_items_allocated
;
245 menu_items_allocated
*= 2;
246 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
247 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
248 old_size
* sizeof (Lisp_Object
));
251 /* Begin a submenu. */
254 push_submenu_start ()
256 if (menu_items_used
+ 1 > menu_items_allocated
)
259 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
260 menu_items_submenu_depth
++;
268 if (menu_items_used
+ 1 > menu_items_allocated
)
271 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
272 menu_items_submenu_depth
--;
275 /* Indicate boundary between left and right. */
278 push_left_right_boundary ()
280 if (menu_items_used
+ 1 > menu_items_allocated
)
283 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
286 /* Start a new menu pane in menu_items..
287 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
290 push_menu_pane (name
, prefix_vec
)
291 Lisp_Object name
, prefix_vec
;
293 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
296 if (menu_items_submenu_depth
== 0)
297 menu_items_n_panes
++;
298 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
299 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
300 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
303 /* Push one menu item into the current pane.
304 NAME is the string to display. ENABLE if non-nil means
305 this item can be selected. KEY is the key generated by
306 choosing this item, or nil if this item doesn't really have a definition.
307 DEF is the definition of this item.
308 EQUIV is the textual description of the keyboard equivalent for
309 this item (or nil if none). */
312 push_menu_item (name
, enable
, key
, def
, equiv
)
313 Lisp_Object name
, enable
, key
, def
, equiv
;
315 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
318 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
319 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
320 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
321 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
322 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
325 /* Figure out the current keyboard equivalent of a menu item ITEM1.
326 The item string for menu display should be ITEM_STRING.
327 Store the equivalent keyboard key sequence's
328 textual description into *DESCRIP_PTR.
329 Also cache them in the item itself.
330 Return the real definition to execute. */
333 menu_item_equiv_key (item_string
, item1
, descrip_ptr
)
334 Lisp_Object item_string
;
336 Lisp_Object
*descrip_ptr
;
338 /* This is the real definition--the function to run. */
340 /* This is the sublist that records cached equiv key data
341 so we can save time. */
342 Lisp_Object cachelist
;
343 /* These are the saved equivalent keyboard key sequence
344 and its key-description. */
345 Lisp_Object savedkey
, descrip
;
348 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
350 /* If a help string follows the item string, skip it. */
351 if (CONSP (XCONS (item1
)->cdr
)
352 && STRINGP (XCONS (XCONS (item1
)->cdr
)->car
))
353 item1
= XCONS (item1
)->cdr
;
357 /* Get out the saved equivalent-keyboard-key info. */
358 cachelist
= savedkey
= descrip
= Qnil
;
359 if (CONSP (def
) && CONSP (XCONS (def
)->car
)
360 && (NILP (XCONS (XCONS (def
)->car
)->car
)
361 || VECTORP (XCONS (XCONS (def
)->car
)->car
)))
363 cachelist
= XCONS (def
)->car
;
364 def
= XCONS (def
)->cdr
;
365 savedkey
= XCONS (cachelist
)->car
;
366 descrip
= XCONS (cachelist
)->cdr
;
369 GCPRO4 (def
, def1
, savedkey
, descrip
);
371 /* Is it still valid? */
373 if (!NILP (savedkey
))
374 def1
= Fkey_binding (savedkey
, Qnil
);
375 /* If not, update it. */
377 /* If the command is an alias for another
378 (such as easymenu.el and lmenu.el set it up),
379 check if the original command matches the cached command. */
380 && !(SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
)
381 && EQ (def1
, XSYMBOL (def
)->function
))
382 /* If something had no key binding before, don't recheck it--
383 doing that takes too much time and makes menus too slow. */
384 && !(!NILP (cachelist
) && NILP (savedkey
)))
388 /* If the command is an alias for another
389 (such as easymenu.el and lmenu.el set it up),
390 see if the original command name has equivalent keys. */
391 if (SYMBOLP (def
) && SYMBOLP (XSYMBOL (def
)->function
))
392 savedkey
= Fwhere_is_internal (XSYMBOL (def
)->function
,
395 /* Otherwise look up the specified command itself.
396 We don't try both, because that makes easymenu menus slow. */
397 savedkey
= Fwhere_is_internal (def
, Qnil
, Qt
, Qnil
);
399 if (!NILP (savedkey
))
401 descrip
= Fkey_description (savedkey
);
402 descrip
= concat2 (make_string (" (", 3), descrip
);
403 descrip
= concat2 (descrip
, make_string (")", 1));
407 /* Cache the data we just got in a sublist of the menu binding. */
408 if (NILP (cachelist
))
410 CHECK_IMPURE (item1
);
411 XCONS (item1
)->cdr
= Fcons (Fcons (savedkey
, descrip
), def
);
415 XCONS (cachelist
)->car
= savedkey
;
416 XCONS (cachelist
)->cdr
= descrip
;
420 *descrip_ptr
= descrip
;
424 /* This is used as the handler when calling internal_condition_case_1. */
427 menu_item_enabled_p_1 (arg
)
433 /* Return non-nil if the command DEF is enabled when used as a menu item.
434 This is based on looking for a menu-enable property.
435 If NOTREAL is set, don't bother really computing this. */
438 menu_item_enabled_p (def
, notreal
)
442 Lisp_Object enabled
, tem
;
449 /* No property, or nil, means enable.
450 Otherwise, enable if value is not nil. */
451 tem
= Fget (def
, Qmenu_enable
);
453 /* (condition-case nil (eval tem)
455 enabled
= internal_condition_case_1 (Feval
, tem
, Qerror
,
456 menu_item_enabled_p_1
);
461 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
462 and generate menu panes for them in menu_items.
463 If NOTREAL is nonzero,
464 don't bother really computing whether an item is enabled. */
467 keymap_panes (keymaps
, nmaps
, notreal
)
468 Lisp_Object
*keymaps
;
476 /* Loop over the given keymaps, making a pane for each map.
477 But don't make a pane that is empty--ignore that map instead.
478 P is the number of panes we have made so far. */
479 for (mapno
= 0; mapno
< nmaps
; mapno
++)
480 single_keymap_panes (keymaps
[mapno
], Qnil
, Qnil
, notreal
);
482 finish_menu_items ();
485 /* This is a recursive subroutine of keymap_panes.
486 It handles one keymap, KEYMAP.
487 The other arguments are passed along
488 or point to local variables of the previous function.
489 If NOTREAL is nonzero,
490 don't bother really computing whether an item is enabled. */
493 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
)
495 Lisp_Object pane_name
;
499 Lisp_Object pending_maps
;
500 Lisp_Object tail
, item
, item1
, item_string
, table
;
501 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
505 push_menu_pane (pane_name
, prefix
);
507 for (tail
= keymap
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
509 /* Look at each key binding, and if it has a menu string,
510 make a menu item from it. */
511 item
= XCONS (tail
)->car
;
514 item1
= XCONS (item
)->cdr
;
517 item_string
= XCONS (item1
)->car
;
518 if (STRINGP (item_string
))
520 /* This is the real definition--the function to run. */
522 /* These are the saved equivalent keyboard key sequence
523 and its key-description. */
525 Lisp_Object tem
, enabled
;
527 /* GCPRO because ...enabled_p will call eval
528 and ..._equiv_key may autoload something.
529 Protecting KEYMAP preserves everything we use;
530 aside from that, must protect whatever might be
531 a string. Since there's no GCPRO5, we refetch
532 item_string instead of protecting it. */
533 descrip
= def
= Qnil
;
534 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
536 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
537 enabled
= menu_item_enabled_p (def
, notreal
);
541 item_string
= XCONS (item1
)->car
;
543 tem
= Fkeymapp (def
);
544 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
545 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, XCONS (item
)->car
)),
550 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
551 submap
= get_keymap_1 (def
, 0, 1);
553 #ifndef USE_X_TOOLKIT
554 /* Indicate visually that this is a submenu. */
556 item_string
= concat2 (item_string
,
557 build_string (" >"));
559 /* If definition is nil, pass nil as the key. */
560 push_menu_item (item_string
, enabled
,
561 XCONS (item
)->car
, def
,
564 /* Display a submenu using the toolkit. */
567 push_submenu_start ();
568 single_keymap_panes (submap
, Qnil
,
569 XCONS (item
)->car
, notreal
);
577 else if (VECTORP (item
))
579 /* Loop over the char values represented in the vector. */
580 int len
= XVECTOR (item
)->size
;
582 for (c
= 0; c
< len
; c
++)
584 Lisp_Object character
;
585 XSETFASTINT (character
, c
);
586 item1
= XVECTOR (item
)->contents
[c
];
589 item_string
= XCONS (item1
)->car
;
590 if (STRINGP (item_string
))
594 /* These are the saved equivalent keyboard key sequence
595 and its key-description. */
597 Lisp_Object tem
, enabled
;
599 /* GCPRO because ...enabled_p will call eval
600 and ..._equiv_key may autoload something.
601 Protecting KEYMAP preserves everything we use;
602 aside from that, must protect whatever might be
603 a string. Since there's no GCPRO5, we refetch
604 item_string instead of protecting it. */
605 GCPRO4 (keymap
, pending_maps
, def
, descrip
);
606 descrip
= def
= Qnil
;
608 def
= menu_item_equiv_key (item_string
, item1
, &descrip
);
609 enabled
= menu_item_enabled_p (def
, notreal
);
613 item_string
= XCONS (item1
)->car
;
615 tem
= Fkeymapp (def
);
616 if (XSTRING (item_string
)->data
[0] == '@' && !NILP (tem
))
617 pending_maps
= Fcons (Fcons (def
, Fcons (item_string
, character
)),
622 GCPRO4 (keymap
, pending_maps
, descrip
, item_string
);
623 submap
= get_keymap_1 (def
, 0, 1);
625 #ifndef USE_X_TOOLKIT
627 item_string
= concat2 (item_string
,
628 build_string (" >"));
630 /* If definition is nil, pass nil as the key. */
631 push_menu_item (item_string
, enabled
, character
,
636 push_submenu_start ();
637 single_keymap_panes (submap
, Qnil
,
649 /* Process now any submenus which want to be panes at this level. */
650 while (!NILP (pending_maps
))
652 Lisp_Object elt
, eltcdr
, string
;
653 elt
= Fcar (pending_maps
);
654 eltcdr
= XCONS (elt
)->cdr
;
655 string
= XCONS (eltcdr
)->car
;
656 /* We no longer discard the @ from the beginning of the string here.
657 Instead, we do this in xmenu_show. */
658 single_keymap_panes (Fcar (elt
), string
,
659 XCONS (eltcdr
)->cdr
, notreal
);
660 pending_maps
= Fcdr (pending_maps
);
664 /* Push all the panes and items of a menu decsribed by the
665 alist-of-alists MENU.
666 This handles old-fashioned calls to x-popup-menu. */
676 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
678 Lisp_Object elt
, pane_name
, pane_data
;
680 pane_name
= Fcar (elt
);
681 CHECK_STRING (pane_name
, 0);
682 push_menu_pane (pane_name
, Qnil
);
683 pane_data
= Fcdr (elt
);
684 CHECK_CONS (pane_data
, 0);
685 list_of_items (pane_data
);
688 finish_menu_items ();
691 /* Push the items in a single pane defined by the alist PANE. */
697 Lisp_Object tail
, item
, item1
;
699 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
703 push_menu_item (item
, Qnil
, Qnil
, Qt
, Qnil
);
704 else if (NILP (item
))
705 push_left_right_boundary ();
708 CHECK_CONS (item
, 0);
710 CHECK_STRING (item1
, 1);
711 push_menu_item (item1
, Qt
, Fcdr (item
), Qt
, Qnil
);
716 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
717 "Pop up a deck-of-cards menu and return user's selection.\n\
718 POSITION is a position specification. This is either a mouse button event\n\
719 or a list ((XOFFSET YOFFSET) WINDOW)\n\
720 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
721 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
722 This controls the position of the center of the first line\n\
723 in the first pane of the menu, not the top left of the menu as a whole.\n\
724 If POSITION is t, it means to use the current mouse position.\n\
726 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
727 The menu items come from key bindings that have a menu string as well as\n\
728 a definition; actually, the \"definition\" in such a key binding looks like\n\
729 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
730 the keymap as a top-level element.\n\n\
731 You can also use a list of keymaps as MENU.\n\
732 Then each keymap makes a separate pane.\n\
733 When MENU is a keymap or a list of keymaps, the return value\n\
734 is a list of events.\n\n\
735 Alternatively, you can specify a menu of multiple panes\n\
736 with a list of the form (TITLE PANE1 PANE2...),\n\
737 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
738 Each ITEM is normally a cons cell (STRING . VALUE);\n\
739 but a string can appear as an item--that makes a nonselectable line\n\
741 With this form of menu, the return value is VALUE from the chosen item.\n\
743 If POSITION is nil, don't display the menu at all, just precalculate the\n\
744 cached information about equivalent key sequences.")
746 Lisp_Object position
, menu
;
748 int number_of_panes
, panes
;
749 Lisp_Object keymap
, tem
;
753 Lisp_Object selection
;
756 Lisp_Object x
, y
, window
;
762 if (! NILP (position
))
766 /* Decode the first argument: find the window and the coordinates. */
767 if (EQ (position
, Qt
))
769 /* Use the mouse's current position. */
770 FRAME_PTR new_f
= selected_frame
;
771 Lisp_Object bar_window
;
775 if (mouse_position_hook
)
776 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
777 &part
, &x
, &y
, &time
);
779 XSETFRAME (window
, new_f
);
782 window
= selected_window
;
789 tem
= Fcar (position
);
792 window
= Fcar (Fcdr (position
));
794 y
= Fcar (Fcdr (tem
));
799 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
800 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
801 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
805 /* Determine whether this menu is handling a menu bar click. */
806 tem
= Fcar (Fcdr (Fcar (Fcdr (position
))));
807 if (CONSP (tem
) && EQ (Fcar (tem
), Qmenu_bar
))
815 /* Decode where to put the menu. */
823 else if (WINDOWP (window
))
825 CHECK_LIVE_WINDOW (window
, 0);
826 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
828 xpos
= (FONT_WIDTH (f
->display
.x
->font
) * XWINDOW (window
)->left
);
829 ypos
= (f
->display
.x
->line_height
* XWINDOW (window
)->top
);
832 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
833 but I don't want to make one now. */
834 CHECK_WINDOW (window
, 0);
843 /* Decode the menu items from what was specified. */
845 keymap
= Fkeymapp (menu
);
848 tem
= Fkeymapp (Fcar (menu
));
851 /* We were given a keymap. Extract menu info from the keymap. */
853 keymap
= get_keymap (menu
);
855 /* Extract the detailed info to make one pane. */
856 keymap_panes (&menu
, 1, NILP (position
));
858 /* Search for a string appearing directly as an element of the keymap.
859 That string is the title of the menu. */
860 prompt
= map_prompt (keymap
);
862 /* Make that be the pane title of the first pane. */
863 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
864 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
868 else if (!NILP (tem
))
870 /* We were given a list of keymaps. */
871 int nmaps
= XFASTINT (Flength (menu
));
873 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
878 /* The first keymap that has a prompt string
879 supplies the menu title. */
880 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= Fcdr (tem
))
884 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
886 prompt
= map_prompt (keymap
);
887 if (NILP (title
) && !NILP (prompt
))
891 /* Extract the detailed info to make one pane. */
892 keymap_panes (maps
, nmaps
, NILP (position
));
894 /* Make the title be the pane title of the first pane. */
895 if (!NILP (title
) && menu_items_n_panes
>= 0)
896 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
902 /* We were given an old-fashioned menu. */
904 CHECK_STRING (title
, 1);
906 list_of_panes (Fcdr (menu
));
913 discard_menu_items ();
918 /* Display them in a menu. */
921 selection
= xmenu_show (f
, xpos
, ypos
, menubarp
, for_click
,
922 keymaps
, title
, &error_name
);
925 discard_menu_items ();
929 if (error_name
) error (error_name
);
933 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
934 "Pop up a dialog box and return user's selection.\n\
935 POSITION specifies which frame to use.\n\
936 This is normally a mouse button event or a window or frame.\n\
937 If POSITION is t, it means to use the frame the mouse is on.\n\
938 The dialog box appears in the middle of the specified frame.\n\
940 CONTENTS specifies the alternatives to display in the dialog box.\n\
941 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
942 Each ITEM is a cons cell (STRING . VALUE).\n\
943 The return value is VALUE from the chosen item.\n\n\
944 An ITEM may also be just a string--that makes a nonselectable item.\n\
945 An ITEM may also be nil--that means to put all preceding items\n\
946 on the left of the dialog box and all following items on the right.\n\
947 \(By default, approximately half appear on each side.)")
949 Lisp_Object position
, contents
;
956 /* Decode the first argument: find the window or frame to use. */
957 if (EQ (position
, Qt
))
959 #if 0 /* Using the frame the mouse is on may not be right. */
960 /* Use the mouse's current position. */
961 FRAME_PTR new_f
= selected_frame
;
962 Lisp_Object bar_window
;
967 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
970 XSETFRAME (window
, new_f
);
972 window
= selected_window
;
974 /* Decode the first argument: find the window and the coordinates. */
975 if (EQ (position
, Qt
))
976 window
= selected_window
;
978 else if (CONSP (position
))
981 tem
= Fcar (position
);
983 window
= Fcar (Fcdr (position
));
986 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
987 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
990 else if (WINDOWP (position
) || FRAMEP (position
))
993 /* Decode where to put the menu. */
997 else if (WINDOWP (window
))
999 CHECK_LIVE_WINDOW (window
, 0);
1000 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
1003 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1004 but I don't want to make one now. */
1005 CHECK_WINDOW (window
, 0);
1007 #ifndef USE_X_TOOLKIT
1008 /* Display a menu with these alternatives
1009 in the middle of frame F. */
1011 Lisp_Object x
, y
, frame
, newpos
;
1012 XSETFRAME (frame
, f
);
1013 XSETINT (x
, x_pixel_width (f
) / 2);
1014 XSETINT (y
, x_pixel_height (f
) / 2);
1015 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
1017 return Fx_popup_menu (newpos
,
1018 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
1024 Lisp_Object selection
;
1026 /* Decode the dialog items from what was specified. */
1027 title
= Fcar (contents
);
1028 CHECK_STRING (title
, 1);
1030 list_of_panes (Fcons (contents
, Qnil
));
1032 /* Display them in a dialog box. */
1034 selection
= xdialog_show (f
, 0, 0, title
, &error_name
);
1037 discard_menu_items ();
1039 if (error_name
) error (error_name
);
1045 #ifdef USE_X_TOOLKIT
1047 /* Loop in Xt until the menu pulldown or dialog popup has been
1048 popped down (deactivated).
1050 NOTE: All calls to popup_get_selection() should be protected
1051 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1054 popup_get_selection (initial_event
, dpyinfo
)
1055 XEvent
*initial_event
;
1056 struct x_display_info
*dpyinfo
;
1060 /* Define a queue to save up for later unreading
1061 all X events that don't pertain to the menu. */
1065 struct event_queue
*next
;
1068 struct event_queue
*queue
= NULL
;
1069 struct event_queue
*queue_tmp
;
1072 event
= *initial_event
;
1074 XtAppNextEvent (Xt_app_con
, &event
);
1078 /* Handle expose events for editor frames right away. */
1079 if (event
.type
== Expose
)
1080 process_expose_from_menu (event
);
1081 /* Make sure we don't consider buttons grabbed after menu goes. */
1082 else if (event
.type
== ButtonRelease
1083 && dpyinfo
->display
== event
.xbutton
.display
)
1084 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1086 /* Queue all events not for this popup,
1087 except for Expose, which we've already handled. */
1088 if (event
.type
!= Expose
1089 && (event
.xany
.display
!= dpyinfo
->display
1090 || ! x_any_window_to_frame (dpyinfo
, event
.xany
.window
)))
1093 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
1095 if (queue_tmp
!= NULL
)
1097 queue_tmp
->event
= event
;
1098 queue_tmp
->next
= queue
;
1103 XtDispatchEvent (&event
);
1105 if (!popup_activated ())
1107 XtAppNextEvent (Xt_app_con
, &event
);
1110 /* Unread any events that we got but did not handle. */
1111 while (queue
!= NULL
)
1114 XPutBackEvent (queue_tmp
->event
.xany
.display
, &queue_tmp
->event
);
1115 queue
= queue_tmp
->next
;
1116 free ((char *)queue_tmp
);
1117 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1118 interrupt_input_pending
= 1;
1122 /* Detect if a dialog or menu has been posted. */
1127 return popup_activated_flag
;
1131 /* This callback is invoked when the user selects a menubar cascade
1132 pushbutton, but before the pulldown menu is posted. */
1135 popup_activate_callback (widget
, id
, client_data
)
1138 XtPointer client_data
;
1140 popup_activated_flag
= 1;
1143 /* This callback is called from the menu bar pulldown menu
1144 when the user makes a selection.
1145 Figure out what the user chose
1146 and put the appropriate events into the keyboard buffer. */
1149 menubar_selection_callback (widget
, id
, client_data
)
1152 XtPointer client_data
;
1155 FRAME_PTR f
= XFRAME (XVECTOR (frame_vector
)->contents
[id
]);
1157 Lisp_Object
*subprefix_stack
;
1158 int submenu_depth
= 0;
1163 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* sizeof (Lisp_Object
));
1164 vector
= f
->menu_bar_vector
;
1167 while (i
< f
->menu_bar_items_used
)
1171 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1173 subprefix_stack
[submenu_depth
++] = prefix
;
1177 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1179 prefix
= subprefix_stack
[--submenu_depth
];
1182 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1184 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1185 i
+= MENU_ITEMS_PANE_LENGTH
;
1189 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1190 /* The EMACS_INT cast avoids a warning. There's no problem
1191 as long as pointers have enough bits to hold small integers. */
1192 if ((int) (EMACS_INT
) client_data
== i
)
1195 struct input_event buf
;
1198 XSETFRAME (frame
, f
);
1199 buf
.kind
= menu_bar_event
;
1200 buf
.frame_or_window
= Fcons (frame
, Qmenu_bar
);
1201 kbd_buffer_store_event (&buf
);
1203 for (j
= 0; j
< submenu_depth
; j
++)
1204 if (!NILP (subprefix_stack
[j
]))
1206 buf
.kind
= menu_bar_event
;
1207 buf
.frame_or_window
= Fcons (frame
, subprefix_stack
[j
]);
1208 kbd_buffer_store_event (&buf
);
1213 buf
.kind
= menu_bar_event
;
1214 buf
.frame_or_window
= Fcons (frame
, prefix
);
1215 kbd_buffer_store_event (&buf
);
1218 buf
.kind
= menu_bar_event
;
1219 buf
.frame_or_window
= Fcons (frame
, entry
);
1220 kbd_buffer_store_event (&buf
);
1224 i
+= MENU_ITEMS_ITEM_LENGTH
;
1229 /* This callback is invoked when a dialog or menu is finished being
1230 used and has been unposted. */
1233 popup_deactivate_callback (widget
, id
, client_data
)
1236 XtPointer client_data
;
1238 popup_activated_flag
= 0;
1242 /* This recursively calls free_widget_value on the tree of widgets.
1243 It must free all data that was malloc'ed for these widget_values.
1244 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1245 must be left alone. */
1248 free_menubar_widget_value_tree (wv
)
1253 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1255 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1257 free_menubar_widget_value_tree (wv
->contents
);
1258 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1262 free_menubar_widget_value_tree (wv
->next
);
1263 wv
->next
= (widget_value
*) 0xDEADBEEF;
1266 free_widget_value (wv
);
1270 /* Return a tree of widget_value structures for a menu bar item
1271 whose event type is ITEM_KEY (with string ITEM_NAME)
1272 and whose contents come from the list of keymaps MAPS. */
1274 static widget_value
*
1275 single_submenu (item_key
, item_name
, maps
)
1276 Lisp_Object item_key
, item_name
, maps
;
1278 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1280 int submenu_depth
= 0;
1283 Lisp_Object
*mapvec
;
1284 widget_value
**submenu_stack
;
1286 int previous_items
= menu_items_used
;
1288 length
= Flength (maps
);
1289 len
= XINT (length
);
1291 /* Convert the list MAPS into a vector MAPVEC. */
1292 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1293 for (i
= 0; i
< len
; i
++)
1295 mapvec
[i
] = Fcar (maps
);
1299 menu_items_n_panes
= 0;
1301 /* Loop over the given keymaps, making a pane for each map.
1302 But don't make a pane that is empty--ignore that map instead. */
1303 for (i
= 0; i
< len
; i
++)
1304 single_keymap_panes (mapvec
[i
], item_name
, item_key
, 0);
1306 /* Create a tree of widget_value objects
1307 representing the panes and their items. */
1310 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1311 wv
= malloc_widget_value ();
1318 /* Loop over all panes and items made during this call
1319 and construct a tree of widget_value objects.
1320 Ignore the panes and items made by previous calls to
1321 single_submenu, even though those are also in menu_items. */
1323 while (i
< menu_items_used
)
1325 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1327 submenu_stack
[submenu_depth
++] = save_wv
;
1332 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1335 save_wv
= submenu_stack
[--submenu_depth
];
1338 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1339 && submenu_depth
!= 0)
1340 i
+= MENU_ITEMS_PANE_LENGTH
;
1341 /* Ignore a nil in the item list.
1342 It's meaningful only for dialog boxes. */
1343 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1345 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1347 /* Create a new pane. */
1348 Lisp_Object pane_name
, prefix
;
1350 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1351 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1352 pane_string
= (NILP (pane_name
)
1353 ? "" : (char *) XSTRING (pane_name
)->data
);
1354 /* If there is just one top-level pane, put all its items directly
1355 under the top-level menu. */
1356 if (menu_items_n_panes
== 1)
1359 /* If the pane has a meaningful name,
1360 make the pane a top-level menu item
1361 with its items as a submenu beneath it. */
1362 if (strcmp (pane_string
, ""))
1364 wv
= malloc_widget_value ();
1368 first_wv
->contents
= wv
;
1369 wv
->name
= pane_string
;
1377 i
+= MENU_ITEMS_PANE_LENGTH
;
1381 /* Create a new item within current pane. */
1382 Lisp_Object item_name
, enable
, descrip
, def
;
1383 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1384 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1386 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1387 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
1389 wv
= malloc_widget_value ();
1393 save_wv
->contents
= wv
;
1394 wv
->name
= (char *) XSTRING (item_name
)->data
;
1395 if (!NILP (descrip
))
1396 wv
->key
= (char *) XSTRING (descrip
)->data
;
1398 /* The EMACS_INT cast avoids a warning. There's no problem
1399 as long as pointers have enough bits to hold small integers. */
1400 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1401 wv
->enabled
= !NILP (enable
);
1404 i
+= MENU_ITEMS_ITEM_LENGTH
;
1411 extern void EmacsFrameSetCharSize ();
1413 /* Recompute the menu bar of frame F. */
1416 update_frame_menubar (f
)
1419 struct x_display
*x
= f
->display
.x
;
1421 int menubar_changed
;
1423 Dimension shell_height
;
1425 /* We assume the menubar contents has changed if the global flag is set,
1426 or if the current buffer has changed, or if the menubar has never
1427 been updated before.
1429 menubar_changed
= (x
->menubar_widget
1430 && !XtIsManaged (x
->menubar_widget
));
1432 if (! (menubar_changed
))
1436 /* Save the size of the frame because the pane widget doesn't accept to
1437 resize itself. So force it. */
1441 /* Do the voodoo which means "I'm changing lots of things, don't try to
1442 refigure sizes until I'm done." */
1443 lw_refigure_widget (x
->column_widget
, False
);
1445 /* the order in which children are managed is the top to
1446 bottom order in which they are displayed in the paned window.
1447 First, remove the text-area widget.
1449 XtUnmanageChild (x
->edit_widget
);
1451 /* remove the menubar that is there now, and put up the menubar that
1454 if (menubar_changed
)
1456 XtManageChild (x
->menubar_widget
);
1457 XtMapWidget (x
->menubar_widget
);
1458 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1461 /* Re-manage the text-area widget, and then thrash the sizes. */
1462 XtManageChild (x
->edit_widget
);
1463 lw_refigure_widget (x
->column_widget
, True
);
1465 /* Force the pane widget to resize itself with the right values. */
1466 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1472 set_frame_menubar (f
, first_time
)
1476 Widget menubar_widget
= f
->display
.x
->menubar_widget
;
1477 Lisp_Object tail
, items
, frame
;
1478 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1483 count
= inhibit_garbage_collection ();
1485 id
= frame_vector_add_frame (f
);
1487 wv
= malloc_widget_value ();
1488 wv
->name
= "menubar";
1492 items
= FRAME_MENU_BAR_ITEMS (f
);
1493 menu_items
= f
->menu_bar_vector
;
1494 menu_items_allocated
= XVECTOR (menu_items
)->size
;
1497 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1499 Lisp_Object key
, string
, maps
;
1501 key
= XVECTOR (items
)->contents
[i
];
1502 string
= XVECTOR (items
)->contents
[i
+ 1];
1503 maps
= XVECTOR (items
)->contents
[i
+ 2];
1507 wv
= single_submenu (key
, string
, maps
);
1511 first_wv
->contents
= wv
;
1512 /* Don't set wv->name here; GC during the loop might relocate it. */
1517 /* Now GC cannot happen during the lifetime of the widget_value,
1518 so it's safe to store data from a Lisp_String. */
1519 wv
= first_wv
->contents
;
1520 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
1523 string
= XVECTOR (items
)->contents
[i
+ 1];
1526 wv
->name
= (char *) XSTRING (string
)->data
;
1530 finish_menu_items ();
1532 f
->menu_bar_vector
= menu_items
;
1533 f
->menu_bar_items_used
= menu_items_used
;
1536 unbind_to (count
, Qnil
);
1542 /* Disable resizing (done for Motif!) */
1543 lw_allow_resizing (f
->display
.x
->widget
, False
);
1545 /* The third arg is DEEP_P, which says to consider the entire
1546 menu trees we supply, rather than just the menu bar item names. */
1547 lw_modify_all_widgets (id
, first_wv
, 1);
1549 /* Re-enable the edit widget to resize. */
1550 lw_allow_resizing (f
->display
.x
->widget
, True
);
1554 menubar_widget
= lw_create_widget ("menubar", "menubar",
1556 f
->display
.x
->column_widget
,
1558 popup_activate_callback
,
1559 menubar_selection_callback
,
1560 popup_deactivate_callback
);
1561 f
->display
.x
->menubar_widget
= menubar_widget
;
1564 free_menubar_widget_value_tree (first_wv
);
1566 /* Don't update the menubar the first time it is created via x_window. */
1568 update_frame_menubar (f
);
1573 /* Called from Fx_create_frame to create the inital menubar of a frame
1574 before it is mapped, so that the window is mapped with the menubar already
1575 there instead of us tacking it on later and thrashing the window after it
1579 initialize_frame_menubar (f
)
1582 /* This function is called before the first chance to redisplay
1583 the frame. It has to be, so the frame will have the right size. */
1584 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1585 set_frame_menubar (f
, 1);
1588 /* Get rid of the menu bar of frame F, and free its storage.
1589 This is used when deleting a frame, and when turning off the menu bar. */
1592 free_frame_menubar (f
)
1595 Widget menubar_widget
;
1598 menubar_widget
= f
->display
.x
->menubar_widget
;
1602 id
= frame_vector_add_frame (f
);
1604 lw_destroy_all_widgets (id
);
1605 XVECTOR (frame_vector
)->contents
[id
] = Qnil
;
1610 #endif /* USE_X_TOOLKIT */
1612 /* xmenu_show actually displays a menu using the panes and items in menu_items
1613 and returns the value selected from it.
1614 There are two versions of xmenu_show, one for Xt and one for Xlib.
1615 Both assume input is blocked by the caller. */
1617 /* F is the frame the menu is for.
1618 X and Y are the frame-relative specified position,
1619 relative to the inside upper left corner of the frame F.
1620 MENUBARP is 1 if this menu came from the menu bar.
1621 FOR_CLICK if this menu was invoked for a mouse click.
1622 KEYMAPS is 1 if this menu was specified with keymaps;
1623 in that case, we return a list containing the chosen item's value
1624 and perhaps also the pane's prefix.
1625 TITLE is the specified menu title.
1626 ERROR is a place to store an error message string in case of failure.
1627 (We return nil on failure, but the value doesn't actually matter.) */
1629 #ifdef USE_X_TOOLKIT
1631 /* We need a unique id for each widget handled by the Lucid Widget
1632 library. This includes the frame main windows, popup menu and
1634 LWLIB_ID widget_id_tick
;
1637 static Lisp_Object
*volatile menu_item_selection
;
1639 static Lisp_Object
*menu_item_selection
;
1643 popup_selection_callback (widget
, id
, client_data
)
1646 XtPointer client_data
;
1648 menu_item_selection
= (Lisp_Object
*) client_data
;
1652 xmenu_show (f
, x
, y
, menubarp
, for_click
, keymaps
, title
, error
)
1656 int menubarp
; /* This arg is unused in Xt version. */
1667 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1668 widget_value
**submenu_stack
1669 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1670 Lisp_Object
*subprefix_stack
1671 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1672 int submenu_depth
= 0;
1674 Position root_x
, root_y
;
1677 int next_release_must_exit
= 0;
1681 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1683 *error
= "Empty menu";
1687 /* Create a tree of widget_value objects
1688 representing the panes and their items. */
1689 wv
= malloc_widget_value ();
1696 /* Loop over all panes and items, filling in the tree. */
1698 while (i
< menu_items_used
)
1700 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1702 submenu_stack
[submenu_depth
++] = save_wv
;
1708 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1711 save_wv
= submenu_stack
[--submenu_depth
];
1715 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1716 && submenu_depth
!= 0)
1717 i
+= MENU_ITEMS_PANE_LENGTH
;
1718 /* Ignore a nil in the item list.
1719 It's meaningful only for dialog boxes. */
1720 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1722 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1724 /* Create a new pane. */
1725 Lisp_Object pane_name
, prefix
;
1727 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1728 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1729 pane_string
= (NILP (pane_name
)
1730 ? "" : (char *) XSTRING (pane_name
)->data
);
1731 /* If there is just one top-level pane, put all its items directly
1732 under the top-level menu. */
1733 if (menu_items_n_panes
== 1)
1736 /* If the pane has a meaningful name,
1737 make the pane a top-level menu item
1738 with its items as a submenu beneath it. */
1739 if (!keymaps
&& strcmp (pane_string
, ""))
1741 wv
= malloc_widget_value ();
1745 first_wv
->contents
= wv
;
1746 wv
->name
= pane_string
;
1747 if (keymaps
&& !NILP (prefix
))
1754 else if (first_pane
)
1760 i
+= MENU_ITEMS_PANE_LENGTH
;
1764 /* Create a new item within current pane. */
1765 Lisp_Object item_name
, enable
, descrip
, def
;
1766 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1767 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1769 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1770 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
1772 wv
= malloc_widget_value ();
1776 save_wv
->contents
= wv
;
1777 wv
->name
= (char *) XSTRING (item_name
)->data
;
1778 if (!NILP (descrip
))
1779 wv
->key
= (char *) XSTRING (descrip
)->data
;
1781 /* If this item has a null value,
1782 make the call_data null so that it won't display a box
1783 when the mouse is on it. */
1785 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
1786 wv
->enabled
= !NILP (enable
);
1789 i
+= MENU_ITEMS_ITEM_LENGTH
;
1793 /* Deal with the title, if it is non-nil. */
1796 widget_value
*wv_title
= malloc_widget_value ();
1797 widget_value
*wv_sep1
= malloc_widget_value ();
1798 widget_value
*wv_sep2
= malloc_widget_value ();
1800 wv_sep2
->name
= "--";
1801 wv_sep2
->next
= first_wv
->contents
;
1803 wv_sep1
->name
= "--";
1804 wv_sep1
->next
= wv_sep2
;
1806 wv_title
->name
= (char *) XSTRING (title
)->data
;
1807 wv_title
->enabled
= True
;
1808 wv_title
->next
= wv_sep1
;
1809 first_wv
->contents
= wv_title
;
1812 /* Actually create the menu. */
1813 menu_id
= ++widget_id_tick
;
1814 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1815 f
->display
.x
->widget
, 1, 0,
1816 popup_selection_callback
,
1817 popup_deactivate_callback
);
1819 /* Don't allow any geometry request from the user. */
1820 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1821 XtSetValues (menu
, av
, ac
);
1823 /* Free the widget_value objects we used to specify the contents. */
1824 free_menubar_widget_value_tree (first_wv
);
1826 /* No selection has been chosen yet. */
1827 menu_item_selection
= 0;
1829 /* Display the menu. */
1830 lw_popup_menu (menu
);
1831 popup_activated_flag
= 1;
1833 /* Process events that apply to the menu. */
1834 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
));
1837 /* fp turned off the following statement and wrote a comment
1838 that it is unnecessary--that the menu has already disappeared.
1839 I observer that is not so. -- rms. */
1840 /* Make sure the menu disappears. */
1841 lw_destroy_all_widgets (menu_id
);
1844 /* Find the selected item, and its pane, to return
1845 the proper value. */
1846 if (menu_item_selection
!= 0)
1852 while (i
< menu_items_used
)
1856 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1858 subprefix_stack
[submenu_depth
++] = prefix
;
1862 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1864 prefix
= subprefix_stack
[--submenu_depth
];
1867 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1870 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1871 i
+= MENU_ITEMS_PANE_LENGTH
;
1876 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1877 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1883 entry
= Fcons (entry
, Qnil
);
1885 entry
= Fcons (prefix
, entry
);
1886 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1887 if (!NILP (subprefix_stack
[j
]))
1888 entry
= Fcons (subprefix_stack
[j
], entry
);
1892 i
+= MENU_ITEMS_ITEM_LENGTH
;
1901 dialog_selection_callback (widget
, id
, client_data
)
1904 XtPointer client_data
;
1906 /* The EMACS_INT cast avoids a warning. There's no problem
1907 as long as pointers have enough bits to hold small integers. */
1908 if ((int) (EMACS_INT
) client_data
!= -1)
1909 menu_item_selection
= (Lisp_Object
*) client_data
;
1911 lw_destroy_all_widgets (id
);
1915 static char * button_names
[] = {
1916 "button1", "button2", "button3", "button4", "button5",
1917 "button6", "button7", "button8", "button9", "button10" };
1920 xdialog_show (f
, menubarp
, keymaps
, title
, error
)
1927 int i
, nb_buttons
=0;
1930 char dialog_name
[6];
1932 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1934 /* Number of elements seen so far, before boundary. */
1936 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1937 int boundary_seen
= 0;
1941 if (menu_items_n_panes
> 1)
1943 *error
= "Multiple panes in dialog box";
1947 /* Create a tree of widget_value objects
1948 representing the text label and buttons. */
1950 Lisp_Object pane_name
, prefix
;
1952 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
1953 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
1954 pane_string
= (NILP (pane_name
)
1955 ? "" : (char *) XSTRING (pane_name
)->data
);
1956 prev_wv
= malloc_widget_value ();
1957 prev_wv
->value
= pane_string
;
1958 if (keymaps
&& !NILP (prefix
))
1960 prev_wv
->enabled
= 1;
1961 prev_wv
->name
= "message";
1964 /* Loop over all panes and items, filling in the tree. */
1965 i
= MENU_ITEMS_PANE_LENGTH
;
1966 while (i
< menu_items_used
)
1969 /* Create a new item within current pane. */
1970 Lisp_Object item_name
, enable
, descrip
;
1971 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1972 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1974 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1976 if (NILP (item_name
))
1978 free_menubar_widget_value_tree (first_wv
);
1979 *error
= "Submenu in dialog items";
1982 if (EQ (item_name
, Qquote
))
1984 /* This is the boundary between left-side elts
1985 and right-side elts. Stop incrementing right_count. */
1990 if (nb_buttons
>= 10)
1992 free_menubar_widget_value_tree (first_wv
);
1993 *error
= "Too many dialog items";
1997 wv
= malloc_widget_value ();
1999 wv
->name
= (char *) button_names
[nb_buttons
];
2000 if (!NILP (descrip
))
2001 wv
->key
= (char *) XSTRING (descrip
)->data
;
2002 wv
->value
= (char *) XSTRING (item_name
)->data
;
2003 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
2004 wv
->enabled
= !NILP (enable
);
2007 if (! boundary_seen
)
2011 i
+= MENU_ITEMS_ITEM_LENGTH
;
2014 /* If the boundary was not specified,
2015 by default put half on the left and half on the right. */
2016 if (! boundary_seen
)
2017 left_count
= nb_buttons
- nb_buttons
/ 2;
2019 wv
= malloc_widget_value ();
2020 wv
->name
= dialog_name
;
2022 /* Dialog boxes use a really stupid name encoding
2023 which specifies how many buttons to use
2024 and how many buttons are on the right.
2025 The Q means something also. */
2026 dialog_name
[0] = 'Q';
2027 dialog_name
[1] = '0' + nb_buttons
;
2028 dialog_name
[2] = 'B';
2029 dialog_name
[3] = 'R';
2030 /* Number of buttons to put on the right. */
2031 dialog_name
[4] = '0' + nb_buttons
- left_count
;
2033 wv
->contents
= first_wv
;
2037 /* Actually create the dialog. */
2038 dialog_id
= ++widget_id_tick
;
2039 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
2040 f
->display
.x
->widget
, 1, 0,
2041 dialog_selection_callback
, 0);
2042 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
2043 /* Free the widget_value objects we used to specify the contents. */
2044 free_menubar_widget_value_tree (first_wv
);
2046 /* No selection has been chosen yet. */
2047 menu_item_selection
= 0;
2049 /* Display the menu. */
2050 lw_pop_up_all_widgets (dialog_id
);
2051 popup_activated_flag
= 1;
2053 /* Process events that apply to the menu. */
2054 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
));
2056 /* Find the selected item, and its pane, to return
2057 the proper value. */
2058 if (menu_item_selection
!= 0)
2064 while (i
< menu_items_used
)
2068 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2071 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2072 i
+= MENU_ITEMS_PANE_LENGTH
;
2077 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2078 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2082 entry
= Fcons (entry
, Qnil
);
2084 entry
= Fcons (prefix
, entry
);
2088 i
+= MENU_ITEMS_ITEM_LENGTH
;
2095 #else /* not USE_X_TOOLKIT */
2098 xmenu_show (f
, x
, y
, menubarp
, for_click
, keymaps
, title
, error
)
2109 int pane
, selidx
, lpane
, status
;
2110 Lisp_Object entry
, pane_prefix
;
2112 int ulx
, uly
, width
, height
;
2113 int dispwidth
, dispheight
;
2117 unsigned int dummy_uint
;
2120 if (menu_items_n_panes
== 0)
2123 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2125 *error
= "Empty menu";
2129 /* Figure out which root window F is on. */
2130 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2131 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2132 &dummy_uint
, &dummy_uint
);
2134 /* Make the menu on that window. */
2135 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2138 *error
= "Can't create menu";
2142 #ifdef HAVE_X_WINDOWS
2143 /* Adjust coordinates to relative to the outer (window manager) window. */
2146 int win_x
= 0, win_y
= 0;
2148 /* Find the position of the outside upper-left corner of
2149 the inner window, with respect to the outer window. */
2150 if (f
->display
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2153 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2155 /* From-window, to-window. */
2156 f
->display
.x
->window_desc
,
2157 f
->display
.x
->parent_desc
,
2159 /* From-position, to-position. */
2160 0, 0, &win_x
, &win_y
,
2162 /* Child of window. */
2169 #endif /* HAVE_X_WINDOWS */
2171 /* Adjust coordinates to be root-window-relative. */
2172 x
+= f
->display
.x
->left_pos
;
2173 y
+= f
->display
.x
->top_pos
;
2175 /* Create all the necessary panes and their items. */
2177 while (i
< menu_items_used
)
2179 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2181 /* Create a new pane. */
2182 Lisp_Object pane_name
, prefix
;
2185 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2186 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2187 pane_string
= (NILP (pane_name
)
2188 ? "" : (char *) XSTRING (pane_name
)->data
);
2189 if (keymaps
&& !NILP (prefix
))
2192 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
2193 if (lpane
== XM_FAILURE
)
2195 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2196 *error
= "Can't create pane";
2199 i
+= MENU_ITEMS_PANE_LENGTH
;
2201 /* Find the width of the widest item in this pane. */
2204 while (j
< menu_items_used
)
2207 item
= XVECTOR (menu_items
)->contents
[j
];
2215 width
= XSTRING (item
)->size
;
2216 if (width
> maxwidth
)
2219 j
+= MENU_ITEMS_ITEM_LENGTH
;
2222 /* Ignore a nil in the item list.
2223 It's meaningful only for dialog boxes. */
2224 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2228 /* Create a new item within current pane. */
2229 Lisp_Object item_name
, enable
, descrip
;
2230 unsigned char *item_data
;
2232 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2233 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2235 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2236 if (!NILP (descrip
))
2238 int gap
= maxwidth
- XSTRING (item_name
)->size
;
2241 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2242 item_name
= concat2 (item_name
, spacer
);
2243 item_name
= concat2 (item_name
, descrip
);
2244 item_data
= XSTRING (item_name
)->data
;
2246 /* if alloca is fast, use that to make the space,
2247 to reduce gc needs. */
2249 = (unsigned char *) alloca (maxwidth
2250 + XSTRING (descrip
)->size
+ 1);
2251 bcopy (XSTRING (item_name
)->data
, item_data
,
2252 XSTRING (item_name
)->size
);
2253 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2255 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2256 XSTRING (descrip
)->size
);
2257 item_data
[j
+ XSTRING (descrip
)->size
] = 0;
2261 item_data
= XSTRING (item_name
)->data
;
2263 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2264 menu
, lpane
, 0, item_data
,
2268 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2269 *error
= "Can't add selection to menu";
2272 i
+= MENU_ITEMS_ITEM_LENGTH
;
2276 /* All set and ready to fly. */
2277 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2278 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
),
2279 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2280 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
),
2281 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2282 x
= min (x
, dispwidth
);
2283 y
= min (y
, dispheight
);
2286 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2287 &ulx
, &uly
, &width
, &height
);
2288 if (ulx
+width
> dispwidth
)
2290 x
-= (ulx
+ width
) - dispwidth
;
2291 ulx
= dispwidth
- width
;
2293 if (uly
+height
> dispheight
)
2295 y
-= (uly
+ height
) - dispheight
;
2296 uly
= dispheight
- height
;
2298 if (ulx
< 0) x
-= ulx
;
2299 if (uly
< 0) y
-= uly
;
2301 XMenuSetAEQ (menu
, TRUE
);
2302 XMenuSetFreeze (menu
, TRUE
);
2305 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2306 x
, y
, ButtonReleaseMask
, &datap
);
2309 /* Assume the mouse has moved out of the X window.
2310 If it has actually moved in, we will get an EnterNotify. */
2317 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2320 /* Find the item number SELIDX in pane number PANE. */
2322 while (i
< menu_items_used
)
2324 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2328 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2330 i
+= MENU_ITEMS_PANE_LENGTH
;
2339 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2342 entry
= Fcons (entry
, Qnil
);
2343 if (!NILP (pane_prefix
))
2344 entry
= Fcons (pane_prefix
, entry
);
2350 i
+= MENU_ITEMS_ITEM_LENGTH
;
2356 *error
= "Can't activate menu";
2362 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2364 #ifdef HAVE_X_WINDOWS
2365 /* State that no mouse buttons are now held.
2366 (The oldXMenu code doesn't track this info for us.)
2367 That is not necessarily true, but the fiction leads to reasonable
2368 results, and it is a pain to ask which are actually held now. */
2369 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2375 #endif /* not USE_X_TOOLKIT */
2379 staticpro (&menu_items
);
2382 #ifdef USE_X_TOOLKIT
2383 widget_id_tick
= (1<<16);
2386 staticpro (&frame_vector
);
2387 frame_vector
= Fmake_vector (make_number (10), Qnil
);
2389 defsubr (&Sx_popup_menu
);
2390 defsubr (&Sx_popup_dialog
);