1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996 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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* X pop-up deck-of-cards menu facility for gnuemacs.
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
28 /* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
31 /* Rewritten for clarity and GC protection by rms in Feb 94. */
33 /* On 4.3 this loses if it comes after xterm.h. */
42 #include "termhooks.h"
46 #include "blockinput.h"
54 /* This may include sys/types.h, and that somehow loses
55 if this is not done before the other system files. */
59 /* Load sys/types.h if not already loaded.
60 In some systems loading it twice is suicidal. */
62 #include <sys/types.h>
65 #include "dispextern.h"
68 #undef HAVE_MULTILINGUAL_MENU
71 #include <X11/IntrinsicP.h>
72 #include <X11/CoreP.h>
73 #include <X11/StringDefs.h>
74 #include <X11/Shell.h>
76 #include <X11/Xaw/Paned.h>
77 #endif /* USE_LUCID */
78 #include "../lwlib/lwlib.h"
79 #else /* not USE_X_TOOLKIT */
80 #include "../oldXMenu/XMenu.h"
81 #endif /* not USE_X_TOOLKIT */
82 #endif /* HAVE_X_WINDOWS */
85 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
88 #define min(x,y) (((x) < (y)) ? (x) : (y))
89 #define max(x,y) (((x) > (y)) ? (x) : (y))
96 Lisp_Object Vmenu_updating_frame
;
98 Lisp_Object Qdebug_on_next_call
;
100 extern Lisp_Object Qmenu_bar
;
101 extern Lisp_Object Qmouse_click
, Qevent_kind
;
103 extern Lisp_Object QCtoggle
, QCradio
;
105 extern Lisp_Object Voverriding_local_map
;
106 extern Lisp_Object Voverriding_local_map_menu_flag
;
108 extern Lisp_Object Qoverriding_local_map
, Qoverriding_terminal_local_map
;
110 extern Lisp_Object Qmenu_bar_update_hook
;
113 extern void set_frame_menubar ();
114 extern void process_expose_from_menu ();
115 extern XtAppContext Xt_app_con
;
117 static Lisp_Object
xdialog_show ();
118 void popup_get_selection ();
123 /* Define HAVE_BOXES if meus can handle radio and toggle buttons. */
128 static void push_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
129 Lisp_Object
, Lisp_Object
, Lisp_Object
,
131 static Lisp_Object
xmenu_show ();
132 static void keymap_panes ();
133 static void single_keymap_panes ();
134 static void single_menu_item ();
135 static void list_of_panes ();
136 static void list_of_items ();
138 /* This holds a Lisp vector that holds the results of decoding
139 the keymaps or alist-of-alists that specify a menu.
141 It describes the panes and items within the panes.
143 Each pane is described by 3 elements in the vector:
144 t, the pane name, the pane's prefix key.
145 Then follow the pane's items, with 5 elements per item:
146 the item string, the enable flag, the item's value,
147 the definition, and the equivalent keyboard key's description string.
149 In some cases, multiple levels of menus may be described.
150 A single vector slot containing nil indicates the start of a submenu.
151 A single vector slot containing lambda indicates the end of a submenu.
152 The submenu follows a menu item which is the way to reach the submenu.
154 A single vector slot containing quote indicates that the
155 following items should appear on the right of a dialog box.
157 Using a Lisp vector to hold this information while we decode it
158 takes care of protecting all the data from GC. */
160 #define MENU_ITEMS_PANE_NAME 1
161 #define MENU_ITEMS_PANE_PREFIX 2
162 #define MENU_ITEMS_PANE_LENGTH 3
164 #define MENU_ITEMS_ITEM_NAME 0
165 #define MENU_ITEMS_ITEM_ENABLE 1
166 #define MENU_ITEMS_ITEM_VALUE 2
167 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
168 #define MENU_ITEMS_ITEM_DEFINITION 4
169 #define MENU_ITEMS_ITEM_TYPE 5
170 #define MENU_ITEMS_ITEM_SELECTED 6
171 #define MENU_ITEMS_ITEM_LENGTH 7
173 static Lisp_Object menu_items
;
175 /* Number of slots currently allocated in menu_items. */
176 static int menu_items_allocated
;
178 /* This is the index in menu_items of the first empty slot. */
179 static int menu_items_used
;
181 /* The number of panes currently recorded in menu_items,
182 excluding those within submenus. */
183 static int menu_items_n_panes
;
185 /* Current depth within submenus. */
186 static int menu_items_submenu_depth
;
188 /* Flag which when set indicates a dialog or menu has been posted by
189 Xt on behalf of one of the widget sets. */
190 static int popup_activated_flag
;
192 static int next_menubar_widget_id
;
194 /* This is set nonzero after the user activates the menu bar, and set
195 to zero again after the menu bars are redisplayed by prepare_menu_bar.
196 While it is nonzero, all calls to set_frame_menubar go deep.
198 I don't understand why this is needed, but it does seem to be
199 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
201 int pending_menu_activation
;
205 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
207 static struct frame
*
208 menubar_id_to_frame (id
)
211 Lisp_Object tail
, frame
;
214 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
217 if (!GC_FRAMEP (frame
))
220 if (f
->output_data
.nothing
== 1)
222 if (f
->output_data
.x
->id
== id
)
230 /* Initialize the menu_items structure if we haven't already done so.
231 Also mark it as currently empty. */
236 if (NILP (menu_items
))
238 menu_items_allocated
= 60;
239 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
243 menu_items_n_panes
= 0;
244 menu_items_submenu_depth
= 0;
247 /* Call at the end of generating the data in menu_items.
248 This fills in the number of items in the last pane. */
255 /* Call when finished using the data for the current menu
259 discard_menu_items ()
261 /* Free the structure if it is especially large.
262 Otherwise, hold on to it, to save time. */
263 if (menu_items_allocated
> 200)
266 menu_items_allocated
= 0;
270 /* Make the menu_items vector twice as large. */
276 int old_size
= menu_items_allocated
;
279 menu_items_allocated
*= 2;
280 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
281 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
282 old_size
* sizeof (Lisp_Object
));
285 /* Begin a submenu. */
288 push_submenu_start ()
290 if (menu_items_used
+ 1 > menu_items_allocated
)
293 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
294 menu_items_submenu_depth
++;
302 if (menu_items_used
+ 1 > menu_items_allocated
)
305 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
306 menu_items_submenu_depth
--;
309 /* Indicate boundary between left and right. */
312 push_left_right_boundary ()
314 if (menu_items_used
+ 1 > menu_items_allocated
)
317 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
320 /* Start a new menu pane in menu_items..
321 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
324 push_menu_pane (name
, prefix_vec
)
325 Lisp_Object name
, prefix_vec
;
327 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
330 if (menu_items_submenu_depth
== 0)
331 menu_items_n_panes
++;
332 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
333 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
334 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
337 /* Push one menu item into the current pane. NAME is the string to
338 display. ENABLE if non-nil means this item can be selected. KEY
339 is the key generated by choosing this item, or nil if this item
340 doesn't really have a definition. DEF is the definition of this
341 item. EQUIV is the textual description of the keyboard equivalent
342 for this item (or nil if none). TYPE is the type of this menu
343 item, one of nil, `toggle' or `radio'. */
346 push_menu_item (name
, enable
, key
, def
, equiv
, type
, selected
)
347 Lisp_Object name
, enable
, key
, def
, equiv
, type
, selected
;
349 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
352 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
353 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
354 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
355 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
356 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
357 XVECTOR (menu_items
)->contents
[menu_items_used
++] = type
;
358 XVECTOR (menu_items
)->contents
[menu_items_used
++] = selected
;
361 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
362 and generate menu panes for them in menu_items.
363 If NOTREAL is nonzero,
364 don't bother really computing whether an item is enabled. */
367 keymap_panes (keymaps
, nmaps
, notreal
)
368 Lisp_Object
*keymaps
;
376 /* Loop over the given keymaps, making a pane for each map.
377 But don't make a pane that is empty--ignore that map instead.
378 P is the number of panes we have made so far. */
379 for (mapno
= 0; mapno
< nmaps
; mapno
++)
380 single_keymap_panes (keymaps
[mapno
], Qnil
, Qnil
, notreal
, 10);
382 finish_menu_items ();
385 /* This is a recursive subroutine of keymap_panes.
386 It handles one keymap, KEYMAP.
387 The other arguments are passed along
388 or point to local variables of the previous function.
389 If NOTREAL is nonzero, only check for equivalent key bindings, don't
390 evaluate expressions in menu items and don't make any menu.
392 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
395 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
397 Lisp_Object pane_name
;
402 Lisp_Object pending_maps
= Qnil
;
403 Lisp_Object tail
, item
;
404 struct gcpro gcpro1
, gcpro2
;
410 push_menu_pane (pane_name
, prefix
);
413 /* Remember index for first item in this pane so we can go back and
414 add a prefix when (if) we see the first button. After that, notbuttons
415 is set to 0, to mark that we have seen a button and all non button
416 items need a prefix. */
417 notbuttons
= menu_items_used
;
420 for (tail
= keymap
; CONSP (tail
); tail
= XCDR (tail
))
422 GCPRO2 (keymap
, pending_maps
);
423 /* Look at each key binding, and if it is a menu item add it
427 single_menu_item (XCAR (item
), XCDR (item
),
428 &pending_maps
, notreal
, maxdepth
, ¬buttons
);
429 else if (VECTORP (item
))
431 /* Loop over the char values represented in the vector. */
432 int len
= XVECTOR (item
)->size
;
434 for (c
= 0; c
< len
; c
++)
436 Lisp_Object character
;
437 XSETFASTINT (character
, c
);
438 single_menu_item (character
, XVECTOR (item
)->contents
[c
],
439 &pending_maps
, notreal
, maxdepth
, ¬buttons
);
445 /* Process now any submenus which want to be panes at this level. */
446 while (!NILP (pending_maps
))
448 Lisp_Object elt
, eltcdr
, string
;
449 elt
= Fcar (pending_maps
);
451 string
= XCAR (eltcdr
);
452 /* We no longer discard the @ from the beginning of the string here.
453 Instead, we do this in xmenu_show. */
454 single_keymap_panes (Fcar (elt
), string
,
455 XCDR (eltcdr
), notreal
, maxdepth
- 1);
456 pending_maps
= Fcdr (pending_maps
);
460 /* This is a subroutine of single_keymap_panes that handles one
462 KEY is a key in a keymap and ITEM is its binding.
463 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
465 If NOTREAL is nonzero, only check for equivalent key bindings, don't
466 evaluate expressions in menu items and don't make any menu.
467 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
468 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
469 buttons. It points to variable notbuttons in single_keymap_panes,
470 which keeps track of if we have seen a button in this menu or not. */
473 single_menu_item (key
, item
, pending_maps_ptr
, notreal
, maxdepth
,
475 Lisp_Object key
, item
;
476 Lisp_Object
*pending_maps_ptr
;
477 int maxdepth
, notreal
;
480 Lisp_Object map
, item_string
, enabled
;
481 struct gcpro gcpro1
, gcpro2
;
484 /* Parse the menu item and leave the result in item_properties. */
486 res
= parse_menu_item (item
, notreal
, 0);
489 return; /* Not a menu item. */
491 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
495 /* We don't want to make a menu, just traverse the keymaps to
496 precompute equivalent key bindings. */
498 single_keymap_panes (map
, Qnil
, key
, 1, maxdepth
- 1);
502 enabled
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_ENABLE
];
503 item_string
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_NAME
];
505 if (!NILP (map
) && XSTRING (item_string
)->data
[0] == '@')
508 /* An enabled separate pane. Remember this to handle it later. */
509 *pending_maps_ptr
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
515 /* Simulate radio buttons and toggle boxes by putting a prefix in
518 Lisp_Object prefix
= Qnil
;
519 Lisp_Object type
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
];
523 = XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
];
526 /* The first button. Line up previous items in this menu. */
528 int index
= *notbuttons_ptr
; /* Index for first item this menu. */
531 while (index
< menu_items_used
)
534 = XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
];
538 submenu
++; /* Skip sub menu. */
540 else if (EQ (tem
, Qlambda
))
543 submenu
--; /* End sub menu. */
545 else if (EQ (tem
, Qt
))
546 index
+= 3; /* Skip new pane marker. */
547 else if (EQ (tem
, Qquote
))
548 index
++; /* Skip a left, right divider. */
551 if (!submenu
&& XSTRING (tem
)->data
[0] != '\0'
552 && XSTRING (tem
)->data
[0] != '-')
553 XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
]
554 = concat2 (build_string (" "), tem
);
555 index
+= MENU_ITEMS_ITEM_LENGTH
;
561 /* Calculate prefix, if any, for this item. */
562 if (EQ (type
, QCtoggle
))
563 prefix
= build_string (NILP (selected
) ? "[ ] " : "[X] ");
564 else if (EQ (type
, QCradio
))
565 prefix
= build_string (NILP (selected
) ? "( ) " : "(*) ");
567 /* Not a button. If we have earlier buttons, then we need a prefix. */
568 else if (!*notbuttons_ptr
&& XSTRING (item_string
)->data
[0] != '\0'
569 && XSTRING (item_string
)->data
[0] != '-')
570 prefix
= build_string (" ");
573 item_string
= concat2 (prefix
, item_string
);
575 #endif /* not HAVE_BOXES */
577 #ifndef USE_X_TOOLKIT
579 /* Indicate visually that this is a submenu. */
580 item_string
= concat2 (item_string
, build_string (" >"));
583 push_menu_item (item_string
, enabled
, key
,
584 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_DEF
],
585 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_KEYEQ
],
586 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
],
587 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
]);
590 /* Display a submenu using the toolkit. */
591 if (! (NILP (map
) || NILP (enabled
)))
593 push_submenu_start ();
594 single_keymap_panes (map
, Qnil
, key
, 0, maxdepth
- 1);
600 /* Push all the panes and items of a menu described by the
601 alist-of-alists MENU.
602 This handles old-fashioned calls to x-popup-menu. */
612 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
614 Lisp_Object elt
, pane_name
, pane_data
;
616 pane_name
= Fcar (elt
);
617 CHECK_STRING (pane_name
, 0);
618 push_menu_pane (pane_name
, Qnil
);
619 pane_data
= Fcdr (elt
);
620 CHECK_CONS (pane_data
, 0);
621 list_of_items (pane_data
);
624 finish_menu_items ();
627 /* Push the items in a single pane defined by the alist PANE. */
633 Lisp_Object tail
, item
, item1
;
635 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
639 push_menu_item (item
, Qnil
, Qnil
, Qt
, Qnil
, Qnil
, Qnil
);
640 else if (NILP (item
))
641 push_left_right_boundary ();
644 CHECK_CONS (item
, 0);
646 CHECK_STRING (item1
, 1);
647 push_menu_item (item1
, Qt
, Fcdr (item
), Qt
, Qnil
, Qnil
, Qnil
);
652 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
653 "Pop up a deck-of-cards menu and return user's selection.\n\
654 POSITION is a position specification. This is either a mouse button event\n\
655 or a list ((XOFFSET YOFFSET) WINDOW)\n\
656 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
657 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
658 This controls the position of the center of the first line\n\
659 in the first pane of the menu, not the top left of the menu as a whole.\n\
660 If POSITION is t, it means to use the current mouse position.\n\
662 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
663 The menu items come from key bindings that have a menu string as well as\n\
664 a definition; actually, the \"definition\" in such a key binding looks like\n\
665 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
666 the keymap as a top-level element.\n\n\
667 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
668 Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
670 You can also use a list of keymaps as MENU.\n\
671 Then each keymap makes a separate pane.\n\
672 When MENU is a keymap or a list of keymaps, the return value\n\
673 is a list of events.\n\n\
675 Alternatively, you can specify a menu of multiple panes\n\
676 with a list of the form (TITLE PANE1 PANE2...),\n\
677 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
678 Each ITEM is normally a cons cell (STRING . VALUE);\n\
679 but a string can appear as an item--that makes a nonselectable line\n\
681 With this form of menu, the return value is VALUE from the chosen item.\n\
683 If POSITION is nil, don't display the menu at all, just precalculate the\n\
684 cached information about equivalent key sequences.")
686 Lisp_Object position
, menu
;
688 Lisp_Object keymap
, tem
;
692 Lisp_Object selection
;
694 Lisp_Object x
, y
, window
;
700 if (! NILP (position
))
704 /* Decode the first argument: find the window and the coordinates. */
705 if (EQ (position
, Qt
)
706 || (CONSP (position
) && EQ (XCAR (position
), Qmenu_bar
)))
708 /* Use the mouse's current position. */
709 FRAME_PTR new_f
= SELECTED_FRAME ();
710 Lisp_Object bar_window
;
711 enum scroll_bar_part part
;
714 if (mouse_position_hook
)
715 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
716 &part
, &x
, &y
, &time
);
718 XSETFRAME (window
, new_f
);
721 window
= selected_window
;
728 tem
= Fcar (position
);
731 window
= Fcar (Fcdr (position
));
733 y
= Fcar (Fcdr (tem
));
738 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
739 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
740 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
749 /* Decode where to put the menu. */
757 else if (WINDOWP (window
))
759 CHECK_LIVE_WINDOW (window
, 0);
760 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
762 xpos
= (FONT_WIDTH (f
->output_data
.x
->font
)
763 * XFASTINT (XWINDOW (window
)->left
));
764 ypos
= (f
->output_data
.x
->line_height
765 * XFASTINT (XWINDOW (window
)->top
));
768 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
769 but I don't want to make one now. */
770 CHECK_WINDOW (window
, 0);
775 XSETFRAME (Vmenu_updating_frame
, f
);
777 Vmenu_updating_frame
= Qnil
;
778 #endif /* HAVE_MENUS */
783 /* Decode the menu items from what was specified. */
785 keymap
= Fkeymapp (menu
);
788 tem
= Fkeymapp (Fcar (menu
));
791 /* We were given a keymap. Extract menu info from the keymap. */
793 keymap
= get_keymap (menu
);
795 /* Extract the detailed info to make one pane. */
796 keymap_panes (&menu
, 1, NILP (position
));
798 /* Search for a string appearing directly as an element of the keymap.
799 That string is the title of the menu. */
800 prompt
= map_prompt (keymap
);
801 if (NILP (title
) && !NILP (prompt
))
804 /* Make that be the pane title of the first pane. */
805 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
806 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
810 else if (!NILP (tem
))
812 /* We were given a list of keymaps. */
813 int nmaps
= XFASTINT (Flength (menu
));
815 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
820 /* The first keymap that has a prompt string
821 supplies the menu title. */
822 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= Fcdr (tem
))
826 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
828 prompt
= map_prompt (keymap
);
829 if (NILP (title
) && !NILP (prompt
))
833 /* Extract the detailed info to make one pane. */
834 keymap_panes (maps
, nmaps
, NILP (position
));
836 /* Make the title be the pane title of the first pane. */
837 if (!NILP (title
) && menu_items_n_panes
>= 0)
838 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
844 /* We were given an old-fashioned menu. */
846 CHECK_STRING (title
, 1);
848 list_of_panes (Fcdr (menu
));
855 discard_menu_items ();
861 /* Display them in a menu. */
864 selection
= xmenu_show (f
, xpos
, ypos
, for_click
,
865 keymaps
, title
, &error_name
);
868 discard_menu_items ();
871 #endif /* HAVE_MENUS */
873 if (error_name
) error (error_name
);
879 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
880 "Pop up a dialog box and return user's selection.\n\
881 POSITION specifies which frame to use.\n\
882 This is normally a mouse button event or a window or frame.\n\
883 If POSITION is t, it means to use the frame the mouse is on.\n\
884 The dialog box appears in the middle of the specified frame.\n\
886 CONTENTS specifies the alternatives to display in the dialog box.\n\
887 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
888 Each ITEM is a cons cell (STRING . VALUE).\n\
889 The return value is VALUE from the chosen item.\n\n\
890 An ITEM may also be just a string--that makes a nonselectable item.\n\
891 An ITEM may also be nil--that means to put all preceding items\n\
892 on the left of the dialog box and all following items on the right.\n\
893 \(By default, approximately half appear on each side.)")
895 Lisp_Object position
, contents
;
902 /* Decode the first argument: find the window or frame to use. */
903 if (EQ (position
, Qt
)
904 || (CONSP (position
) && EQ (XCAR (position
), Qmenu_bar
)))
906 #if 0 /* Using the frame the mouse is on may not be right. */
907 /* Use the mouse's current position. */
908 FRAME_PTR new_f
= SELECTED_FRAME ();
909 Lisp_Object bar_window
;
914 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
917 XSETFRAME (window
, new_f
);
919 window
= selected_window
;
921 window
= selected_window
;
923 else if (CONSP (position
))
926 tem
= Fcar (position
);
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
))
940 /* Decode where to put the menu. */
944 else if (WINDOWP (window
))
946 CHECK_LIVE_WINDOW (window
, 0);
947 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
950 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
951 but I don't want to make one now. */
952 CHECK_WINDOW (window
, 0);
954 #ifndef USE_X_TOOLKIT
955 /* Display a menu with these alternatives
956 in the middle of frame F. */
958 Lisp_Object x
, y
, frame
, newpos
;
959 XSETFRAME (frame
, f
);
960 XSETINT (x
, x_pixel_width (f
) / 2);
961 XSETINT (y
, x_pixel_height (f
) / 2);
962 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
964 return Fx_popup_menu (newpos
,
965 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
971 Lisp_Object selection
;
973 /* Decode the dialog items from what was specified. */
974 title
= Fcar (contents
);
975 CHECK_STRING (title
, 1);
977 list_of_panes (Fcons (contents
, Qnil
));
979 /* Display them in a dialog box. */
981 selection
= xdialog_show (f
, 0, title
, &error_name
);
984 discard_menu_items ();
986 if (error_name
) error (error_name
);
994 /* Loop in Xt until the menu pulldown or dialog popup has been
995 popped down (deactivated). This is used for x-popup-menu
996 and x-popup-dialog; it is not used for the menu bar any more.
998 NOTE: All calls to popup_get_selection should be protected
999 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1002 popup_get_selection (initial_event
, dpyinfo
, id
)
1003 XEvent
*initial_event
;
1004 struct x_display_info
*dpyinfo
;
1009 /* Define a queue to save up for later unreading
1010 all X events that don't pertain to the menu. */
1014 struct event_queue
*next
;
1017 struct event_queue
*queue
= NULL
;
1018 struct event_queue
*queue_tmp
;
1021 event
= *initial_event
;
1023 XtAppNextEvent (Xt_app_con
, &event
);
1027 /* Handle expose events for editor frames right away. */
1028 if (event
.type
== Expose
)
1029 process_expose_from_menu (event
);
1030 /* Make sure we don't consider buttons grabbed after menu goes.
1031 And make sure to deactivate for any ButtonRelease,
1032 even if XtDispatchEvent doesn't do that. */
1033 else if (event
.type
== ButtonRelease
1034 && dpyinfo
->display
== event
.xbutton
.display
)
1036 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1037 popup_activated_flag
= 0;
1038 #ifdef USE_MOTIF /* Pretending that the event came from a
1039 Btn1Down seems the only way to convince Motif to
1040 activate its callbacks; setting the XmNmenuPost
1041 isn't working. --marcus@sysc.pdx.edu. */
1042 event
.xbutton
.button
= 1;
1045 /* If the user presses a key, deactivate the menu.
1046 The user is likely to do that if we get wedged. */
1047 else if (event
.type
== KeyPress
1048 && dpyinfo
->display
== event
.xbutton
.display
)
1050 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
1051 if (!IsModifierKey (keysym
))
1053 popup_activated_flag
= 0;
1057 /* Button presses outside the menu also pop it down. */
1058 else if (event
.type
== ButtonPress
1059 && event
.xany
.display
== dpyinfo
->display
1060 && x_any_window_to_frame (dpyinfo
, event
.xany
.window
))
1062 popup_activated_flag
= 0;
1066 /* Queue all events not for this popup,
1067 except for Expose, which we've already handled, and ButtonRelease.
1068 Note that the X window is associated with the frame if this
1069 is a menu bar popup, but not if it's a dialog box. So we use
1070 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1071 if (event
.type
!= Expose
1072 && !(event
.type
== ButtonRelease
1073 && dpyinfo
->display
== event
.xbutton
.display
)
1074 && (event
.xany
.display
!= dpyinfo
->display
1075 || x_non_menubar_window_to_frame (dpyinfo
, event
.xany
.window
)))
1077 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
1079 if (queue_tmp
!= NULL
)
1081 queue_tmp
->event
= event
;
1082 queue_tmp
->next
= queue
;
1087 XtDispatchEvent (&event
);
1089 if (!popup_activated ())
1091 XtAppNextEvent (Xt_app_con
, &event
);
1094 /* Unread any events that we got but did not handle. */
1095 while (queue
!= NULL
)
1098 XPutBackEvent (queue_tmp
->event
.xany
.display
, &queue_tmp
->event
);
1099 queue
= queue_tmp
->next
;
1100 free ((char *)queue_tmp
);
1101 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1102 interrupt_input_pending
= 1;
1106 /* Activate the menu bar of frame F.
1107 This is called from keyboard.c when it gets the
1108 menu_bar_activate_event out of the Emacs event queue.
1110 To activate the menu bar, we use the X button-press event
1111 that was saved in saved_menu_event.
1112 That makes the toolkit do its thing.
1114 But first we recompute the menu bar contents (the whole tree).
1116 The reason for saving the button event until here, instead of
1117 passing it to the toolkit right away, is that we can safely
1118 execute Lisp code. */
1121 x_activate_menubar (f
)
1124 if (!f
->output_data
.x
->saved_menu_event
->type
)
1127 set_frame_menubar (f
, 0, 1);
1129 XtDispatchEvent ((XEvent
*) f
->output_data
.x
->saved_menu_event
);
1132 if (f
->output_data
.x
->saved_menu_event
->type
== ButtonRelease
)
1133 pending_menu_activation
= 1;
1136 /* Ignore this if we get it a second time. */
1137 f
->output_data
.x
->saved_menu_event
->type
= 0;
1140 /* Detect if a dialog or menu has been posted. */
1145 return popup_activated_flag
;
1149 /* This callback is invoked when the user selects a menubar cascade
1150 pushbutton, but before the pulldown menu is posted. */
1153 popup_activate_callback (widget
, id
, client_data
)
1156 XtPointer client_data
;
1158 popup_activated_flag
= 1;
1161 /* This callback is called from the menu bar pulldown menu
1162 when the user makes a selection.
1163 Figure out what the user chose
1164 and put the appropriate events into the keyboard buffer. */
1167 menubar_selection_callback (widget
, id
, client_data
)
1170 XtPointer client_data
;
1172 Lisp_Object prefix
, entry
;
1173 FRAME_PTR f
= menubar_id_to_frame (id
);
1175 Lisp_Object
*subprefix_stack
;
1176 int submenu_depth
= 0;
1181 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* sizeof (Lisp_Object
));
1182 vector
= f
->menu_bar_vector
;
1185 while (i
< f
->menu_bar_items_used
)
1187 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1189 subprefix_stack
[submenu_depth
++] = prefix
;
1193 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1195 prefix
= subprefix_stack
[--submenu_depth
];
1198 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1200 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1201 i
+= MENU_ITEMS_PANE_LENGTH
;
1205 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1206 /* The EMACS_INT cast avoids a warning. There's no problem
1207 as long as pointers have enough bits to hold small integers. */
1208 if ((int) (EMACS_INT
) client_data
== i
)
1211 struct input_event buf
;
1214 XSETFRAME (frame
, f
);
1215 buf
.kind
= menu_bar_event
;
1216 buf
.frame_or_window
= Fcons (frame
, Fcons (Qmenu_bar
, Qnil
));
1217 kbd_buffer_store_event (&buf
);
1219 for (j
= 0; j
< submenu_depth
; j
++)
1220 if (!NILP (subprefix_stack
[j
]))
1222 buf
.kind
= menu_bar_event
;
1223 buf
.frame_or_window
= Fcons (frame
, subprefix_stack
[j
]);
1224 kbd_buffer_store_event (&buf
);
1229 buf
.kind
= menu_bar_event
;
1230 buf
.frame_or_window
= Fcons (frame
, prefix
);
1231 kbd_buffer_store_event (&buf
);
1234 buf
.kind
= menu_bar_event
;
1235 buf
.frame_or_window
= Fcons (frame
, entry
);
1236 kbd_buffer_store_event (&buf
);
1240 i
+= MENU_ITEMS_ITEM_LENGTH
;
1245 /* This callback is invoked when a dialog or menu is finished being
1246 used and has been unposted. */
1249 popup_deactivate_callback (widget
, id
, client_data
)
1252 XtPointer client_data
;
1254 popup_activated_flag
= 0;
1257 /* Allocate a widget_value, blocking input. */
1260 xmalloc_widget_value ()
1262 widget_value
*value
;
1265 value
= malloc_widget_value ();
1271 /* This recursively calls free_widget_value on the tree of widgets.
1272 It must free all data that was malloc'ed for these widget_values.
1273 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1274 must be left alone. */
1277 free_menubar_widget_value_tree (wv
)
1282 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1284 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1286 free_menubar_widget_value_tree (wv
->contents
);
1287 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1291 free_menubar_widget_value_tree (wv
->next
);
1292 wv
->next
= (widget_value
*) 0xDEADBEEF;
1295 free_widget_value (wv
);
1299 /* Return a tree of widget_value structures for a menu bar item
1300 whose event type is ITEM_KEY (with string ITEM_NAME)
1301 and whose contents come from the list of keymaps MAPS. */
1303 static widget_value
*
1304 single_submenu (item_key
, item_name
, maps
)
1305 Lisp_Object item_key
, item_name
, maps
;
1307 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1309 int submenu_depth
= 0;
1312 Lisp_Object
*mapvec
;
1313 widget_value
**submenu_stack
;
1314 int previous_items
= menu_items_used
;
1315 int top_level_items
= 0;
1317 length
= Flength (maps
);
1318 len
= XINT (length
);
1320 /* Convert the list MAPS into a vector MAPVEC. */
1321 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1322 for (i
= 0; i
< len
; i
++)
1324 mapvec
[i
] = Fcar (maps
);
1328 menu_items_n_panes
= 0;
1330 /* Loop over the given keymaps, making a pane for each map.
1331 But don't make a pane that is empty--ignore that map instead. */
1332 for (i
= 0; i
< len
; i
++)
1334 if (SYMBOLP (mapvec
[i
])
1335 || (CONSP (mapvec
[i
])
1336 && NILP (Fkeymapp (mapvec
[i
]))))
1338 /* Here we have a command at top level in the menu bar
1339 as opposed to a submenu. */
1340 top_level_items
= 1;
1341 push_menu_pane (Qnil
, Qnil
);
1342 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
], Qnil
, Qnil
, Qnil
);
1345 single_keymap_panes (mapvec
[i
], item_name
, item_key
, 0, 10);
1348 /* Create a tree of widget_value objects
1349 representing the panes and their items. */
1352 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1353 wv
= xmalloc_widget_value ();
1357 wv
->button_type
= BUTTON_TYPE_NONE
;
1362 /* Loop over all panes and items made during this call
1363 and construct a tree of widget_value objects.
1364 Ignore the panes and items made by previous calls to
1365 single_submenu, even though those are also in menu_items. */
1367 while (i
< menu_items_used
)
1369 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1371 submenu_stack
[submenu_depth
++] = save_wv
;
1376 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1379 save_wv
= submenu_stack
[--submenu_depth
];
1382 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1383 && submenu_depth
!= 0)
1384 i
+= MENU_ITEMS_PANE_LENGTH
;
1385 /* Ignore a nil in the item list.
1386 It's meaningful only for dialog boxes. */
1387 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1389 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1391 /* Create a new pane. */
1392 Lisp_Object pane_name
, prefix
;
1394 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1395 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1396 #ifndef HAVE_MULTILINGUAL_MENU
1397 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1398 pane_name
= string_make_unibyte (pane_name
);
1400 pane_string
= (NILP (pane_name
)
1401 ? "" : (char *) XSTRING (pane_name
)->data
);
1402 /* If there is just one top-level pane, put all its items directly
1403 under the top-level menu. */
1404 if (menu_items_n_panes
== 1)
1407 /* If the pane has a meaningful name,
1408 make the pane a top-level menu item
1409 with its items as a submenu beneath it. */
1410 if (strcmp (pane_string
, ""))
1412 wv
= xmalloc_widget_value ();
1416 first_wv
->contents
= wv
;
1417 wv
->name
= pane_string
;
1418 /* Ignore the @ that means "separate pane".
1419 This is a kludge, but this isn't worth more time. */
1420 if (!NILP (prefix
) && wv
->name
[0] == '@')
1424 wv
->button_type
= BUTTON_TYPE_NONE
;
1428 i
+= MENU_ITEMS_PANE_LENGTH
;
1432 /* Create a new item within current pane. */
1433 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1434 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1435 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1437 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1438 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
1439 type
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_TYPE
];
1440 selected
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_SELECTED
];
1442 #ifndef HAVE_MULTILINGUAL_MENU
1443 if (STRING_MULTIBYTE (item_name
))
1444 item_name
= string_make_unibyte (item_name
);
1445 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1446 descrip
= string_make_unibyte (descrip
);
1449 wv
= xmalloc_widget_value ();
1453 save_wv
->contents
= wv
;
1455 wv
->name
= (char *) XSTRING (item_name
)->data
;
1456 if (!NILP (descrip
))
1457 wv
->key
= (char *) XSTRING (descrip
)->data
;
1459 /* The EMACS_INT cast avoids a warning. There's no problem
1460 as long as pointers have enough bits to hold small integers. */
1461 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1462 wv
->enabled
= !NILP (enable
);
1465 wv
->button_type
= BUTTON_TYPE_NONE
;
1466 else if (EQ (type
, QCradio
))
1467 wv
->button_type
= BUTTON_TYPE_RADIO
;
1468 else if (EQ (type
, QCtoggle
))
1469 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1473 wv
->selected
= !NILP (selected
);
1477 i
+= MENU_ITEMS_ITEM_LENGTH
;
1481 /* If we have just one "menu item"
1482 that was originally a button, return it by itself. */
1483 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
1485 wv
= first_wv
->contents
;
1486 free_widget_value (first_wv
);
1493 extern void EmacsFrameSetCharSize ();
1495 /* Recompute all the widgets of frame F, when the menu bar
1496 has been changed. */
1499 update_frame_menubar (f
)
1502 struct x_output
*x
= f
->output_data
.x
;
1504 int menubar_changed
;
1506 /* We assume the menubar contents has changed if the global flag is set,
1507 or if the current buffer has changed, or if the menubar has never
1508 been updated before.
1510 menubar_changed
= (x
->menubar_widget
1511 && !XtIsManaged (x
->menubar_widget
));
1513 if (! (menubar_changed
))
1517 /* Save the size of the frame because the pane widget doesn't accept to
1518 resize itself. So force it. */
1522 /* Do the voodoo which means "I'm changing lots of things, don't try to
1523 refigure sizes until I'm done." */
1524 lw_refigure_widget (x
->column_widget
, False
);
1526 /* the order in which children are managed is the top to
1527 bottom order in which they are displayed in the paned window.
1528 First, remove the text-area widget.
1530 XtUnmanageChild (x
->edit_widget
);
1532 /* remove the menubar that is there now, and put up the menubar that
1535 if (menubar_changed
)
1537 XtManageChild (x
->menubar_widget
);
1538 XtMapWidget (x
->menubar_widget
);
1539 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1542 /* Re-manage the text-area widget, and then thrash the sizes. */
1543 XtManageChild (x
->edit_widget
);
1544 lw_refigure_widget (x
->column_widget
, True
);
1546 /* Force the pane widget to resize itself with the right values. */
1547 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1551 /* Set the contents of the menubar widgets of frame F.
1552 The argument FIRST_TIME is currently ignored;
1553 it is set the first time this is called, from initialize_frame_menubar. */
1556 set_frame_menubar (f
, first_time
, deep_p
)
1561 Widget menubar_widget
= f
->output_data
.x
->menubar_widget
;
1563 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1567 XSETFRAME (Vmenu_updating_frame
, f
);
1569 if (f
->output_data
.x
->id
== 0)
1570 f
->output_data
.x
->id
= next_menubar_widget_id
++;
1571 id
= f
->output_data
.x
->id
;
1573 if (! menubar_widget
)
1575 else if (pending_menu_activation
&& !deep_p
)
1577 /* Make the first call for any given frame always go deep. */
1578 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
1581 f
->output_data
.x
->saved_menu_event
= (XEvent
*)xmalloc (sizeof (XEvent
));
1582 f
->output_data
.x
->saved_menu_event
->type
= 0;
1585 wv
= xmalloc_widget_value ();
1586 wv
->name
= "menubar";
1589 wv
->button_type
= BUTTON_TYPE_NONE
;
1594 /* Make a widget-value tree representing the entire menu trees. */
1596 struct buffer
*prev
= current_buffer
;
1598 int specpdl_count
= specpdl_ptr
- specpdl
;
1599 int previous_menu_items_used
= f
->menu_bar_items_used
;
1600 Lisp_Object
*previous_items
1601 = (Lisp_Object
*) alloca (previous_menu_items_used
1602 * sizeof (Lisp_Object
));
1604 /* If we are making a new widget, its contents are empty,
1605 do always reinitialize them. */
1606 if (! menubar_widget
)
1607 previous_menu_items_used
= 0;
1609 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
1610 specbind (Qinhibit_quit
, Qt
);
1611 /* Don't let the debugger step into this code
1612 because it is not reentrant. */
1613 specbind (Qdebug_on_next_call
, Qnil
);
1615 record_unwind_protect (Fset_match_data
, Fmatch_data (Qnil
, Qnil
));
1616 if (NILP (Voverriding_local_map_menu_flag
))
1618 specbind (Qoverriding_terminal_local_map
, Qnil
);
1619 specbind (Qoverriding_local_map
, Qnil
);
1622 set_buffer_internal_1 (XBUFFER (buffer
));
1624 /* Run the Lucid hook. */
1625 call1 (Vrun_hooks
, Qactivate_menubar_hook
);
1626 /* If it has changed current-menubar from previous value,
1627 really recompute the menubar from the value. */
1628 if (! NILP (Vlucid_menu_bar_dirty_flag
))
1629 call0 (Qrecompute_lucid_menubar
);
1630 safe_run_hooks (Qmenu_bar_update_hook
);
1631 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1633 items
= FRAME_MENU_BAR_ITEMS (f
);
1635 inhibit_garbage_collection ();
1637 /* Save the frame's previous menu bar contents data. */
1638 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
1639 previous_menu_items_used
* sizeof (Lisp_Object
));
1641 /* Fill in the current menu bar contents. */
1642 menu_items
= f
->menu_bar_vector
;
1643 menu_items_allocated
= XVECTOR (menu_items
)->size
;
1645 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1647 Lisp_Object key
, string
, maps
;
1649 key
= XVECTOR (items
)->contents
[i
];
1650 string
= XVECTOR (items
)->contents
[i
+ 1];
1651 maps
= XVECTOR (items
)->contents
[i
+ 2];
1655 wv
= single_submenu (key
, string
, maps
);
1659 first_wv
->contents
= wv
;
1660 /* Don't set wv->name here; GC during the loop might relocate it. */
1662 wv
->button_type
= BUTTON_TYPE_NONE
;
1666 finish_menu_items ();
1668 set_buffer_internal_1 (prev
);
1669 unbind_to (specpdl_count
, Qnil
);
1671 /* If there has been no change in the Lisp-level contents
1672 of the menu bar, skip redisplaying it. Just exit. */
1674 for (i
= 0; i
< previous_menu_items_used
; i
++)
1675 if (menu_items_used
== i
1676 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
1678 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
1680 free_menubar_widget_value_tree (first_wv
);
1686 /* Now GC cannot happen during the lifetime of the widget_value,
1687 so it's safe to store data from a Lisp_String. */
1688 wv
= first_wv
->contents
;
1689 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1692 string
= XVECTOR (items
)->contents
[i
+ 1];
1695 wv
->name
= (char *) XSTRING (string
)->data
;
1699 f
->menu_bar_vector
= menu_items
;
1700 f
->menu_bar_items_used
= menu_items_used
;
1705 /* Make a widget-value tree containing
1706 just the top level menu bar strings. */
1708 items
= FRAME_MENU_BAR_ITEMS (f
);
1709 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1713 string
= XVECTOR (items
)->contents
[i
+ 1];
1717 wv
= xmalloc_widget_value ();
1718 wv
->name
= (char *) XSTRING (string
)->data
;
1721 wv
->button_type
= BUTTON_TYPE_NONE
;
1722 /* This prevents lwlib from assuming this
1723 menu item is really supposed to be empty. */
1724 /* The EMACS_INT cast avoids a warning.
1725 This value just has to be different from small integers. */
1726 wv
->call_data
= (void *) (EMACS_INT
) (-1);
1731 first_wv
->contents
= wv
;
1735 /* Forget what we thought we knew about what is in the
1736 detailed contents of the menu bar menus.
1737 Changing the top level always destroys the contents. */
1738 f
->menu_bar_items_used
= 0;
1741 /* Create or update the menu bar widget. */
1747 /* Disable resizing (done for Motif!) */
1748 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
1750 /* The third arg is DEEP_P, which says to consider the entire
1751 menu trees we supply, rather than just the menu bar item names. */
1752 lw_modify_all_widgets (id
, first_wv
, deep_p
);
1754 /* Re-enable the edit widget to resize. */
1755 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
1759 menubar_widget
= lw_create_widget ("menubar", "menubar", id
, first_wv
,
1760 f
->output_data
.x
->column_widget
,
1762 popup_activate_callback
,
1763 menubar_selection_callback
,
1764 popup_deactivate_callback
);
1765 f
->output_data
.x
->menubar_widget
= menubar_widget
;
1770 = (f
->output_data
.x
->menubar_widget
1771 ? (f
->output_data
.x
->menubar_widget
->core
.height
1772 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
1775 #if 0 /* Experimentally, we now get the right results
1776 for -geometry -0-0 without this. 24 Aug 96, rms. */
1778 if (FRAME_EXTERNAL_MENU_BAR (f
))
1781 XtVaGetValues (f
->output_data
.x
->column_widget
,
1782 XtNinternalBorderWidth
, &ibw
, NULL
);
1783 menubar_size
+= ibw
;
1785 #endif /* USE_LUCID */
1788 f
->output_data
.x
->menubar_height
= menubar_size
;
1791 x_set_menu_resources_from_menu_face (f
, f
->output_data
.x
->menubar_widget
);
1792 free_menubar_widget_value_tree (first_wv
);
1793 update_frame_menubar (f
);
1798 /* Called from Fx_create_frame to create the initial menubar of a frame
1799 before it is mapped, so that the window is mapped with the menubar already
1800 there instead of us tacking it on later and thrashing the window after it
1804 initialize_frame_menubar (f
)
1807 /* This function is called before the first chance to redisplay
1808 the frame. It has to be, so the frame will have the right size. */
1809 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1810 set_frame_menubar (f
, 1, 1);
1813 /* Get rid of the menu bar of frame F, and free its storage.
1814 This is used when deleting a frame, and when turning off the menu bar. */
1817 free_frame_menubar (f
)
1820 Widget menubar_widget
;
1822 menubar_widget
= f
->output_data
.x
->menubar_widget
;
1824 f
->output_data
.x
->menubar_height
= 0;
1829 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
1834 #endif /* USE_X_TOOLKIT */
1836 /* xmenu_show actually displays a menu using the panes and items in menu_items
1837 and returns the value selected from it.
1838 There are two versions of xmenu_show, one for Xt and one for Xlib.
1839 Both assume input is blocked by the caller. */
1841 /* F is the frame the menu is for.
1842 X and Y are the frame-relative specified position,
1843 relative to the inside upper left corner of the frame F.
1844 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1845 KEYMAPS is 1 if this menu was specified with keymaps;
1846 in that case, we return a list containing the chosen item's value
1847 and perhaps also the pane's prefix.
1848 TITLE is the specified menu title.
1849 ERROR is a place to store an error message string in case of failure.
1850 (We return nil on failure, but the value doesn't actually matter.) */
1852 #ifdef USE_X_TOOLKIT
1854 /* We need a unique id for each widget handled by the Lucid Widget
1857 For the main windows, and popup menus, we use this counter,
1858 which we increment each time after use. This starts from 1<<16.
1860 For menu bars, we use numbers starting at 0, counted in
1861 next_menubar_widget_id. */
1862 LWLIB_ID widget_id_tick
;
1865 static Lisp_Object
*volatile menu_item_selection
;
1867 static Lisp_Object
*menu_item_selection
;
1871 popup_selection_callback (widget
, id
, client_data
)
1874 XtPointer client_data
;
1876 menu_item_selection
= (Lisp_Object
*) client_data
;
1880 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
1894 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1895 widget_value
**submenu_stack
1896 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1897 Lisp_Object
*subprefix_stack
1898 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1899 int submenu_depth
= 0;
1900 XButtonPressedEvent dummy
;
1906 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1908 *error
= "Empty menu";
1912 /* Create a tree of widget_value objects
1913 representing the panes and their items. */
1914 wv
= xmalloc_widget_value ();
1918 wv
->button_type
= BUTTON_TYPE_NONE
;
1922 /* Loop over all panes and items, filling in the tree. */
1924 while (i
< menu_items_used
)
1926 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1928 submenu_stack
[submenu_depth
++] = save_wv
;
1934 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1937 save_wv
= submenu_stack
[--submenu_depth
];
1941 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1942 && submenu_depth
!= 0)
1943 i
+= MENU_ITEMS_PANE_LENGTH
;
1944 /* Ignore a nil in the item list.
1945 It's meaningful only for dialog boxes. */
1946 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1948 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1950 /* Create a new pane. */
1951 Lisp_Object pane_name
, prefix
;
1953 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1954 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1955 #ifndef HAVE_MULTILINGUAL_MENU
1956 if (!NILP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1957 pane_name
= string_make_unibyte (pane_name
);
1959 pane_string
= (NILP (pane_name
)
1960 ? "" : (char *) XSTRING (pane_name
)->data
);
1961 /* If there is just one top-level pane, put all its items directly
1962 under the top-level menu. */
1963 if (menu_items_n_panes
== 1)
1966 /* If the pane has a meaningful name,
1967 make the pane a top-level menu item
1968 with its items as a submenu beneath it. */
1969 if (!keymaps
&& strcmp (pane_string
, ""))
1971 wv
= xmalloc_widget_value ();
1975 first_wv
->contents
= wv
;
1976 wv
->name
= pane_string
;
1977 if (keymaps
&& !NILP (prefix
))
1981 wv
->button_type
= BUTTON_TYPE_NONE
;
1985 else if (first_pane
)
1991 i
+= MENU_ITEMS_PANE_LENGTH
;
1995 /* Create a new item within current pane. */
1996 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1997 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1998 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2000 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2001 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
2002 type
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_TYPE
];
2003 selected
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_SELECTED
];
2005 #ifndef HAVE_MULTILINGUAL_MENU
2006 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
2007 item_name
= string_make_unibyte (item_name
);
2008 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
2009 item_name
= string_make_unibyte (descrip
);
2012 wv
= xmalloc_widget_value ();
2016 save_wv
->contents
= wv
;
2017 wv
->name
= (char *) XSTRING (item_name
)->data
;
2018 if (!NILP (descrip
))
2019 wv
->key
= (char *) XSTRING (descrip
)->data
;
2021 /* If this item has a null value,
2022 make the call_data null so that it won't display a box
2023 when the mouse is on it. */
2025 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
2026 wv
->enabled
= !NILP (enable
);
2029 wv
->button_type
= BUTTON_TYPE_NONE
;
2030 else if (EQ (type
, QCtoggle
))
2031 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
2032 else if (EQ (type
, QCradio
))
2033 wv
->button_type
= BUTTON_TYPE_RADIO
;
2037 wv
->selected
= !NILP (selected
);
2041 i
+= MENU_ITEMS_ITEM_LENGTH
;
2045 /* Deal with the title, if it is non-nil. */
2048 widget_value
*wv_title
= xmalloc_widget_value ();
2049 widget_value
*wv_sep1
= xmalloc_widget_value ();
2050 widget_value
*wv_sep2
= xmalloc_widget_value ();
2052 wv_sep2
->name
= "--";
2053 wv_sep2
->next
= first_wv
->contents
;
2055 wv_sep1
->name
= "--";
2056 wv_sep1
->next
= wv_sep2
;
2058 #ifndef HAVE_MULTILINGUAL_MENU
2059 if (STRING_MULTIBYTE (title
))
2060 title
= string_make_unibyte (title
);
2062 wv_title
->name
= (char *) XSTRING (title
)->data
;
2063 wv_title
->enabled
= True
;
2064 wv_title
->button_type
= BUTTON_TYPE_NONE
;
2065 wv_title
->next
= wv_sep1
;
2066 first_wv
->contents
= wv_title
;
2069 /* Actually create the menu. */
2070 menu_id
= widget_id_tick
++;
2071 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
2072 f
->output_data
.x
->widget
, 1, 0,
2073 popup_selection_callback
,
2074 popup_deactivate_callback
);
2076 /* Adjust coordinates to relative to the outer (window manager) window. */
2079 int win_x
= 0, win_y
= 0;
2081 /* Find the position of the outside upper-left corner of
2082 the inner window, with respect to the outer window. */
2083 if (f
->output_data
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2086 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2088 /* From-window, to-window. */
2089 f
->output_data
.x
->window_desc
,
2090 f
->output_data
.x
->parent_desc
,
2092 /* From-position, to-position. */
2093 0, 0, &win_x
, &win_y
,
2095 /* Child of window. */
2103 /* Adjust coordinates to be root-window-relative. */
2104 x
+= f
->output_data
.x
->left_pos
;
2105 y
+= f
->output_data
.x
->top_pos
;
2107 dummy
.type
= ButtonPress
;
2109 dummy
.send_event
= 0;
2110 dummy
.display
= FRAME_X_DISPLAY (f
);
2111 dummy
.time
= CurrentTime
;
2112 dummy
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
2113 dummy
.window
= dummy
.root
;
2114 dummy
.subwindow
= dummy
.root
;
2119 dummy
.state
= (FRAME_X_DISPLAY_INFO (f
)->grabbed
>> 1) * Button1Mask
;
2121 for (i
= 0; i
< 5; i
++)
2122 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2125 /* Don't allow any geometry request from the user. */
2126 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
2127 XtSetValues (menu
, av
, ac
);
2129 /* Free the widget_value objects we used to specify the contents. */
2130 free_menubar_widget_value_tree (first_wv
);
2132 /* No selection has been chosen yet. */
2133 menu_item_selection
= 0;
2135 /* Display the menu. */
2136 lw_popup_menu (menu
, &dummy
);
2137 x_set_menu_resources_from_menu_face (f
, menu
);
2138 popup_activated_flag
= 1;
2140 /* Process events that apply to the menu. */
2141 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
);
2143 #ifdef LESSTIF_VERSION
2144 /* Nov 1998: For an unknown reason a button grab remains active
2145 after the popup menu has gone. */
2146 XUngrabButton (XtDisplay (f
->output_data
.x
->widget
),
2147 AnyButton
, AnyModifier
,
2148 XtWindow (f
->output_data
.x
->widget
));
2149 XUngrabButton (XtDisplay (f
->output_data
.x
->edit_widget
),
2150 AnyButton
, AnyModifier
,
2151 XtWindow (f
->output_data
.x
->edit_widget
));
2152 #endif /* LESSTIF_VERSION */
2154 /* fp turned off the following statement and wrote a comment
2155 that it is unnecessary--that the menu has already disappeared.
2156 Nowadays the menu disappears ok, all right, but
2157 we need to delete the widgets or multiple ones will pile up. */
2158 lw_destroy_all_widgets (menu_id
);
2160 /* Find the selected item, and its pane, to return
2161 the proper value. */
2162 if (menu_item_selection
!= 0)
2164 Lisp_Object prefix
, entry
;
2168 while (i
< menu_items_used
)
2170 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2172 subprefix_stack
[submenu_depth
++] = prefix
;
2176 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2178 prefix
= subprefix_stack
[--submenu_depth
];
2181 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2184 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2185 i
+= MENU_ITEMS_PANE_LENGTH
;
2187 /* Ignore a nil in the item list.
2188 It's meaningful only for dialog boxes. */
2189 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2194 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2195 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2201 entry
= Fcons (entry
, Qnil
);
2203 entry
= Fcons (prefix
, entry
);
2204 for (j
= submenu_depth
- 1; j
>= 0; j
--)
2205 if (!NILP (subprefix_stack
[j
]))
2206 entry
= Fcons (subprefix_stack
[j
], entry
);
2210 i
+= MENU_ITEMS_ITEM_LENGTH
;
2219 dialog_selection_callback (widget
, id
, client_data
)
2222 XtPointer client_data
;
2224 /* The EMACS_INT cast avoids a warning. There's no problem
2225 as long as pointers have enough bits to hold small integers. */
2226 if ((int) (EMACS_INT
) client_data
!= -1)
2227 menu_item_selection
= (Lisp_Object
*) client_data
;
2229 lw_destroy_all_widgets (id
);
2231 popup_activated_flag
= 0;
2234 static char * button_names
[] = {
2235 "button1", "button2", "button3", "button4", "button5",
2236 "button6", "button7", "button8", "button9", "button10" };
2239 xdialog_show (f
, keymaps
, title
, error
)
2245 int i
, nb_buttons
=0;
2248 char dialog_name
[6];
2250 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
2252 /* Number of elements seen so far, before boundary. */
2254 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2255 int boundary_seen
= 0;
2259 if (menu_items_n_panes
> 1)
2261 *error
= "Multiple panes in dialog box";
2265 /* Create a tree of widget_value objects
2266 representing the text label and buttons. */
2268 Lisp_Object pane_name
, prefix
;
2270 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
2271 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
2272 pane_string
= (NILP (pane_name
)
2273 ? "" : (char *) XSTRING (pane_name
)->data
);
2274 prev_wv
= xmalloc_widget_value ();
2275 prev_wv
->value
= pane_string
;
2276 if (keymaps
&& !NILP (prefix
))
2278 prev_wv
->enabled
= 1;
2279 prev_wv
->name
= "message";
2282 /* Loop over all panes and items, filling in the tree. */
2283 i
= MENU_ITEMS_PANE_LENGTH
;
2284 while (i
< menu_items_used
)
2287 /* Create a new item within current pane. */
2288 Lisp_Object item_name
, enable
, descrip
;
2289 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2290 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2292 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2294 if (NILP (item_name
))
2296 free_menubar_widget_value_tree (first_wv
);
2297 *error
= "Submenu in dialog items";
2300 if (EQ (item_name
, Qquote
))
2302 /* This is the boundary between left-side elts
2303 and right-side elts. Stop incrementing right_count. */
2308 if (nb_buttons
>= 9)
2310 free_menubar_widget_value_tree (first_wv
);
2311 *error
= "Too many dialog items";
2315 wv
= xmalloc_widget_value ();
2317 wv
->name
= (char *) button_names
[nb_buttons
];
2318 if (!NILP (descrip
))
2319 wv
->key
= (char *) XSTRING (descrip
)->data
;
2320 wv
->value
= (char *) XSTRING (item_name
)->data
;
2321 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
2322 wv
->enabled
= !NILP (enable
);
2325 if (! boundary_seen
)
2329 i
+= MENU_ITEMS_ITEM_LENGTH
;
2332 /* If the boundary was not specified,
2333 by default put half on the left and half on the right. */
2334 if (! boundary_seen
)
2335 left_count
= nb_buttons
- nb_buttons
/ 2;
2337 wv
= xmalloc_widget_value ();
2338 wv
->name
= dialog_name
;
2340 /* Dialog boxes use a really stupid name encoding
2341 which specifies how many buttons to use
2342 and how many buttons are on the right.
2343 The Q means something also. */
2344 dialog_name
[0] = 'Q';
2345 dialog_name
[1] = '0' + nb_buttons
;
2346 dialog_name
[2] = 'B';
2347 dialog_name
[3] = 'R';
2348 /* Number of buttons to put on the right. */
2349 dialog_name
[4] = '0' + nb_buttons
- left_count
;
2351 wv
->contents
= first_wv
;
2355 /* Actually create the dialog. */
2356 dialog_id
= widget_id_tick
++;
2357 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
2358 f
->output_data
.x
->widget
, 1, 0,
2359 dialog_selection_callback
, 0);
2360 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
2361 /* Free the widget_value objects we used to specify the contents. */
2362 free_menubar_widget_value_tree (first_wv
);
2364 /* No selection has been chosen yet. */
2365 menu_item_selection
= 0;
2367 /* Display the menu. */
2368 lw_pop_up_all_widgets (dialog_id
);
2369 popup_activated_flag
= 1;
2371 /* Process events that apply to the menu. */
2372 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), dialog_id
);
2374 lw_destroy_all_widgets (dialog_id
);
2376 /* Find the selected item, and its pane, to return
2377 the proper value. */
2378 if (menu_item_selection
!= 0)
2384 while (i
< menu_items_used
)
2388 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2391 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2392 i
+= MENU_ITEMS_PANE_LENGTH
;
2397 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2398 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2402 entry
= Fcons (entry
, Qnil
);
2404 entry
= Fcons (prefix
, entry
);
2408 i
+= MENU_ITEMS_ITEM_LENGTH
;
2415 #else /* not USE_X_TOOLKIT */
2418 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
2428 int pane
, selidx
, lpane
, status
;
2429 Lisp_Object entry
, pane_prefix
;
2431 int ulx
, uly
, width
, height
;
2432 int dispwidth
, dispheight
;
2436 unsigned int dummy_uint
;
2439 if (menu_items_n_panes
== 0)
2442 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2444 *error
= "Empty menu";
2448 /* Figure out which root window F is on. */
2449 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2450 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2451 &dummy_uint
, &dummy_uint
);
2453 /* Make the menu on that window. */
2454 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2457 *error
= "Can't create menu";
2461 #ifdef HAVE_X_WINDOWS
2462 /* Adjust coordinates to relative to the outer (window manager) window. */
2465 int win_x
= 0, win_y
= 0;
2467 /* Find the position of the outside upper-left corner of
2468 the inner window, with respect to the outer window. */
2469 if (f
->output_data
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2472 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2474 /* From-window, to-window. */
2475 f
->output_data
.x
->window_desc
,
2476 f
->output_data
.x
->parent_desc
,
2478 /* From-position, to-position. */
2479 0, 0, &win_x
, &win_y
,
2481 /* Child of window. */
2488 #endif /* HAVE_X_WINDOWS */
2490 /* Adjust coordinates to be root-window-relative. */
2491 x
+= f
->output_data
.x
->left_pos
;
2492 y
+= f
->output_data
.x
->top_pos
;
2494 /* Create all the necessary panes and their items. */
2496 while (i
< menu_items_used
)
2498 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2500 /* Create a new pane. */
2501 Lisp_Object pane_name
, prefix
;
2504 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2505 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2506 pane_string
= (NILP (pane_name
)
2507 ? "" : (char *) XSTRING (pane_name
)->data
);
2508 if (keymaps
&& !NILP (prefix
))
2511 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
2512 if (lpane
== XM_FAILURE
)
2514 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2515 *error
= "Can't create pane";
2518 i
+= MENU_ITEMS_PANE_LENGTH
;
2520 /* Find the width of the widest item in this pane. */
2523 while (j
< menu_items_used
)
2526 item
= XVECTOR (menu_items
)->contents
[j
];
2534 width
= STRING_BYTES (XSTRING (item
));
2535 if (width
> maxwidth
)
2538 j
+= MENU_ITEMS_ITEM_LENGTH
;
2541 /* Ignore a nil in the item list.
2542 It's meaningful only for dialog boxes. */
2543 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2547 /* Create a new item within current pane. */
2548 Lisp_Object item_name
, enable
, descrip
;
2549 unsigned char *item_data
;
2551 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2552 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2554 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2555 if (!NILP (descrip
))
2557 int gap
= maxwidth
- STRING_BYTES (XSTRING (item_name
));
2560 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2561 item_name
= concat2 (item_name
, spacer
);
2562 item_name
= concat2 (item_name
, descrip
);
2563 item_data
= XSTRING (item_name
)->data
;
2565 /* if alloca is fast, use that to make the space,
2566 to reduce gc needs. */
2568 = (unsigned char *) alloca (maxwidth
2569 + STRING_BYTES (XSTRING (descrip
)) + 1);
2570 bcopy (XSTRING (item_name
)->data
, item_data
,
2571 STRING_BYTES (XSTRING (item_name
)));
2572 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2574 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2575 STRING_BYTES (XSTRING (descrip
)));
2576 item_data
[j
+ STRING_BYTES (XSTRING (descrip
))] = 0;
2580 item_data
= XSTRING (item_name
)->data
;
2582 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2583 menu
, lpane
, 0, item_data
,
2587 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2588 *error
= "Can't add selection to menu";
2591 i
+= MENU_ITEMS_ITEM_LENGTH
;
2595 /* All set and ready to fly. */
2596 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2597 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
),
2598 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2599 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
),
2600 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2601 x
= min (x
, dispwidth
);
2602 y
= min (y
, dispheight
);
2605 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2606 &ulx
, &uly
, &width
, &height
);
2607 if (ulx
+width
> dispwidth
)
2609 x
-= (ulx
+ width
) - dispwidth
;
2610 ulx
= dispwidth
- width
;
2612 if (uly
+height
> dispheight
)
2614 y
-= (uly
+ height
) - dispheight
;
2615 uly
= dispheight
- height
;
2617 if (ulx
< 0) x
-= ulx
;
2618 if (uly
< 0) y
-= uly
;
2620 XMenuSetAEQ (menu
, TRUE
);
2621 XMenuSetFreeze (menu
, TRUE
);
2624 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2625 x
, y
, ButtonReleaseMask
, &datap
);
2628 #ifdef HAVE_X_WINDOWS
2629 /* Assume the mouse has moved out of the X window.
2630 If it has actually moved in, we will get an EnterNotify. */
2631 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
2638 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2641 /* Find the item number SELIDX in pane number PANE. */
2643 while (i
< menu_items_used
)
2645 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2649 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2651 i
+= MENU_ITEMS_PANE_LENGTH
;
2660 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2663 entry
= Fcons (entry
, Qnil
);
2664 if (!NILP (pane_prefix
))
2665 entry
= Fcons (pane_prefix
, entry
);
2671 i
+= MENU_ITEMS_ITEM_LENGTH
;
2677 *error
= "Can't activate menu";
2683 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2685 #ifdef HAVE_X_WINDOWS
2686 /* State that no mouse buttons are now held.
2687 (The oldXMenu code doesn't track this info for us.)
2688 That is not necessarily true, but the fiction leads to reasonable
2689 results, and it is a pain to ask which are actually held now. */
2690 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2696 #endif /* not USE_X_TOOLKIT */
2698 #endif /* HAVE_MENUS */
2703 staticpro (&menu_items
);
2706 Qdebug_on_next_call
= intern ("debug-on-next-call");
2707 staticpro (&Qdebug_on_next_call
);
2709 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame
,
2710 "Frame for which we are updating a menu.\n\
2711 The enable predicate for a menu command should check this variable.");
2712 Vmenu_updating_frame
= Qnil
;
2714 #ifdef USE_X_TOOLKIT
2715 widget_id_tick
= (1<<16);
2716 next_menubar_widget_id
= 1;
2719 defsubr (&Sx_popup_menu
);
2721 defsubr (&Sx_popup_dialog
);