Fix compilation errors in previous commit.
[bpt/emacs.git] / src / xmenu.c
CommitLineData
dcfdbac7 1/* X Communication module for terminals which understand the X protocol.
0b5538bd
TTN
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 Free Software Foundation, Inc.
dcfdbac7
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
774910eb 9the Free Software Foundation; either version 2, or (at your option)
dcfdbac7
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
dcfdbac7 21
c9e60620 22/* X pop-up deck-of-cards menu facility for GNU Emacs.
dcfdbac7
JB
23 *
24 * Written by Jon Arnold and Roman Budzianowski
25 * Mods and rewrite by Robert Krawitz
26 *
27 */
28
18686d47
RS
29/* Modified by Fred Pierresteguy on December 93
30 to make the popup menus and menubar use the Xt. */
31
78589e07
RS
32/* Rewritten for clarity and GC protection by rms in Feb 94. */
33
68c45bf0
PE
34#include <config.h>
35
8faf6057 36#if 0 /* Why was this included? And without syssignal.h? */
dcfdbac7
JB
37/* On 4.3 this loses if it comes after xterm.h. */
38#include <signal.h>
8faf6057 39#endif
565620a5
RS
40
41#include <stdio.h>
7ee72033 42
dcfdbac7 43#include "lisp.h"
02067692 44#include "keyboard.h"
5ee707b8 45#include "keymap.h"
7708e9bd 46#include "frame.h"
428a555e 47#include "termhooks.h"
dcfdbac7 48#include "window.h"
9ac0d9e0 49#include "blockinput.h"
88766961 50#include "buffer.h"
cc45fb40
SM
51#include "charset.h"
52#include "coding.h"
c3438661 53#include "sysselect.h"
dcfdbac7 54
eeee3112
RS
55#ifdef MSDOS
56#include "msdos.h"
57#endif
58
87485d6f 59#ifdef HAVE_X_WINDOWS
dcfdbac7
JB
60/* This may include sys/types.h, and that somehow loses
61 if this is not done before the other system files. */
62#include "xterm.h"
87485d6f 63#endif
dcfdbac7
JB
64
65/* Load sys/types.h if not already loaded.
66 In some systems loading it twice is suicidal. */
67#ifndef makedev
68#include <sys/types.h>
69#endif
70
71#include "dispextern.h"
72
87485d6f 73#ifdef HAVE_X_WINDOWS
646f98ec
DL
74/* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
75 code accepts the Emacs internal encoding. */
703dc2a8 76#undef HAVE_MULTILINGUAL_MENU
18686d47 77#ifdef USE_X_TOOLKIT
991786dd 78#include "widget.h"
18686d47
RS
79#include <X11/Xlib.h>
80#include <X11/IntrinsicP.h>
81#include <X11/CoreP.h>
82#include <X11/StringDefs.h>
60f312e2 83#include <X11/Shell.h>
5ee80bd3 84#ifdef USE_LUCID
1d1c1567 85#include <X11/Xaw/Paned.h>
5ee80bd3 86#endif /* USE_LUCID */
18686d47 87#include "../lwlib/lwlib.h"
a352a815 88#else /* not USE_X_TOOLKIT */
488dd4c4 89#ifndef USE_GTK
a352a815 90#include "../oldXMenu/XMenu.h"
488dd4c4 91#endif
a352a815
RS
92#endif /* not USE_X_TOOLKIT */
93#endif /* HAVE_X_WINDOWS */
18686d47 94
dcfdbac7
JB
95#ifndef TRUE
96#define TRUE 1
97#define FALSE 0
78589e07 98#endif /* no TRUE */
dcfdbac7 99
bfc524bc
RS
100Lisp_Object Vmenu_updating_frame;
101
0314aacb
RS
102Lisp_Object Qdebug_on_next_call;
103
18686d47 104extern Lisp_Object Qmenu_bar;
78589e07 105
de57a39c 106extern Lisp_Object QCtoggle, QCradio;
8fbc986d 107
88766961
RS
108extern Lisp_Object Voverriding_local_map;
109extern Lisp_Object Voverriding_local_map_menu_flag;
110
111extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
112
113extern Lisp_Object Qmenu_bar_update_hook;
114
18686d47 115#ifdef USE_X_TOOLKIT
495ef86b 116extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
78589e07
RS
117extern XtAppContext Xt_app_con;
118
6a040d6a
NR
119static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, Lisp_Object,
120 char **));
495ef86b 121static void popup_get_selection P_ ((XEvent *, struct x_display_info *,
95bdef2e 122 LWLIB_ID, int));
3427a3db 123
c9e60620 124/* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
3427a3db
GM
125
126#define HAVE_BOXES 1
488dd4c4
JD
127#endif /* USE_X_TOOLKIT */
128
129#ifdef USE_GTK
130#include "gtkutil.h"
131#define HAVE_BOXES 1
495ef86b 132extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
6a040d6a
NR
133static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, Lisp_Object,
134 char **));
3427a3db
GM
135#endif
136
646f98ec
DL
137/* This is how to deal with multibyte text if HAVE_MULTILINGUAL_MENU
138 isn't defined. The use of HAVE_MULTILINGUAL_MENU could probably be
139 confined to an extended version of this with sections of code below
140 using it unconditionally. */
141#ifdef USE_GTK
142/* gtk just uses utf-8. */
143# define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
b64b78cc
SM
144#elif defined HAVE_X_I18N
145# define ENCODE_MENU_STRING(str) ENCODE_SYSTEM (str)
646f98ec 146#else
8af9fa55 147# define ENCODE_MENU_STRING(str) string_make_unibyte (str)
646f98ec
DL
148#endif
149
3427a3db
GM
150static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
151 Lisp_Object, Lisp_Object, Lisp_Object,
850df50b 152 Lisp_Object, Lisp_Object));
f61a541b
GM
153static int update_frame_menubar P_ ((struct frame *));
154static Lisp_Object xmenu_show P_ ((struct frame *, int, int, int, int,
155 Lisp_Object, char **));
156static void keymap_panes P_ ((Lisp_Object *, int, int));
157static void single_keymap_panes P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
158 int, int));
f61a541b
GM
159static void list_of_panes P_ ((Lisp_Object));
160static void list_of_items P_ ((Lisp_Object));
f02cac82 161
78589e07
RS
162\f
163/* This holds a Lisp vector that holds the results of decoding
164 the keymaps or alist-of-alists that specify a menu.
dcfdbac7 165
78589e07 166 It describes the panes and items within the panes.
dcfdbac7 167
78589e07
RS
168 Each pane is described by 3 elements in the vector:
169 t, the pane name, the pane's prefix key.
a352a815 170 Then follow the pane's items, with 5 elements per item:
78589e07 171 the item string, the enable flag, the item's value,
a352a815 172 the definition, and the equivalent keyboard key's description string.
dcfdbac7 173
101bb4a5
RS
174 In some cases, multiple levels of menus may be described.
175 A single vector slot containing nil indicates the start of a submenu.
176 A single vector slot containing lambda indicates the end of a submenu.
177 The submenu follows a menu item which is the way to reach the submenu.
178
fcaa7665
RS
179 A single vector slot containing quote indicates that the
180 following items should appear on the right of a dialog box.
181
78589e07
RS
182 Using a Lisp vector to hold this information while we decode it
183 takes care of protecting all the data from GC. */
dcfdbac7 184
78589e07
RS
185#define MENU_ITEMS_PANE_NAME 1
186#define MENU_ITEMS_PANE_PREFIX 2
187#define MENU_ITEMS_PANE_LENGTH 3
088831f6 188
850df50b
GM
189enum menu_item_idx
190{
191 MENU_ITEMS_ITEM_NAME = 0,
192 MENU_ITEMS_ITEM_ENABLE,
193 MENU_ITEMS_ITEM_VALUE,
194 MENU_ITEMS_ITEM_EQUIV_KEY,
195 MENU_ITEMS_ITEM_DEFINITION,
196 MENU_ITEMS_ITEM_TYPE,
197 MENU_ITEMS_ITEM_SELECTED,
198 MENU_ITEMS_ITEM_HELP,
199 MENU_ITEMS_ITEM_LENGTH
200};
7da99777 201
78589e07 202static Lisp_Object menu_items;
18686d47 203
86fad4ec
SM
204/* If non-nil, means that the global vars defined here are already in use.
205 Used to detect cases where we try to re-enter this non-reentrant code. */
206static Lisp_Object menu_items_inuse;
207
78589e07
RS
208/* Number of slots currently allocated in menu_items. */
209static int menu_items_allocated;
18686d47 210
78589e07
RS
211/* This is the index in menu_items of the first empty slot. */
212static int menu_items_used;
18686d47 213
101bb4a5
RS
214/* The number of panes currently recorded in menu_items,
215 excluding those within submenus. */
78589e07 216static int menu_items_n_panes;
18686d47 217
101bb4a5
RS
218/* Current depth within submenus. */
219static int menu_items_submenu_depth;
220
4dedbfe0 221/* Flag which when set indicates a dialog or menu has been posted by
c98fcf4b 222 Xt on behalf of one of the widget sets. */
488dd4c4 223static int popup_activated_flag;
4dedbfe0 224
88766961 225static int next_menubar_widget_id;
745c34fb 226
a9be6839
RS
227/* This is set nonzero after the user activates the menu bar, and set
228 to zero again after the menu bars are redisplayed by prepare_menu_bar.
229 While it is nonzero, all calls to set_frame_menubar go deep.
230
231 I don't understand why this is needed, but it does seem to be
232 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
233
234int pending_menu_activation;
bd3a4da2 235\f
88766961 236#ifdef USE_X_TOOLKIT
bd3a4da2 237
7556890b 238/* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
bd3a4da2 239
88766961
RS
240static struct frame *
241menubar_id_to_frame (id)
242 LWLIB_ID id;
243{
244 Lisp_Object tail, frame;
245 FRAME_PTR f;
bd3a4da2 246
8e713be6 247 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
bd3a4da2 248 {
8e713be6 249 frame = XCAR (tail);
88766961
RS
250 if (!GC_FRAMEP (frame))
251 continue;
252 f = XFRAME (frame);
2d764c78 253 if (!FRAME_WINDOW_P (f))
88766961 254 continue;
7556890b 255 if (f->output_data.x->id == id)
88766961 256 return f;
bd3a4da2 257 }
88766961 258 return 0;
bd3a4da2 259}
88766961
RS
260
261#endif
4dedbfe0 262\f
78589e07
RS
263/* Initialize the menu_items structure if we haven't already done so.
264 Also mark it as currently empty. */
265
266static void
267init_menu_items ()
268{
269 if (NILP (menu_items))
270 {
271 menu_items_allocated = 60;
272 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
18686d47
RS
273 }
274
86fad4ec
SM
275 if (!NILP (menu_items_inuse))
276 error ("Trying to use a menu from within a menu-entry");
277 menu_items_inuse = Qt;
78589e07
RS
278 menu_items_used = 0;
279 menu_items_n_panes = 0;
101bb4a5 280 menu_items_submenu_depth = 0;
78589e07 281}
18686d47 282
0b1f4572 283/* Call at the end of generating the data in menu_items. */
1658603c 284
78589e07
RS
285static void
286finish_menu_items ()
287{
288}
1658603c 289
86fad4ec
SM
290static Lisp_Object
291unuse_menu_items (dummy)
af89e871 292 Lisp_Object dummy;
86fad4ec
SM
293{
294 return menu_items_inuse = Qnil;
295}
296
78589e07
RS
297/* Call when finished using the data for the current menu
298 in menu_items. */
1658603c 299
78589e07
RS
300static void
301discard_menu_items ()
302{
303 /* Free the structure if it is especially large.
304 Otherwise, hold on to it, to save time. */
305 if (menu_items_allocated > 200)
306 {
307 menu_items = Qnil;
308 menu_items_allocated = 0;
309 }
86fad4ec 310 xassert (NILP (menu_items_inuse));
78589e07 311}
1658603c 312
101bb4a5
RS
313/* Make the menu_items vector twice as large. */
314
315static void
316grow_menu_items ()
317{
318 Lisp_Object old;
319 int old_size = menu_items_allocated;
320 old = menu_items;
321
322 menu_items_allocated *= 2;
323 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
324 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
325 old_size * sizeof (Lisp_Object));
326}
327
328/* Begin a submenu. */
329
330static void
331push_submenu_start ()
332{
333 if (menu_items_used + 1 > menu_items_allocated)
334 grow_menu_items ();
335
336 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
337 menu_items_submenu_depth++;
338}
339
340/* End a submenu. */
341
342static void
343push_submenu_end ()
344{
345 if (menu_items_used + 1 > menu_items_allocated)
346 grow_menu_items ();
347
348 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
349 menu_items_submenu_depth--;
350}
351
fcaa7665
RS
352/* Indicate boundary between left and right. */
353
354static void
355push_left_right_boundary ()
356{
357 if (menu_items_used + 1 > menu_items_allocated)
358 grow_menu_items ();
359
360 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
361}
362
c9e60620 363/* Start a new menu pane in menu_items.
78589e07 364 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
1658603c 365
78589e07
RS
366static void
367push_menu_pane (name, prefix_vec)
368 Lisp_Object name, prefix_vec;
369{
370 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
101bb4a5 371 grow_menu_items ();
dcfdbac7 372
101bb4a5
RS
373 if (menu_items_submenu_depth == 0)
374 menu_items_n_panes++;
78589e07
RS
375 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
376 XVECTOR (menu_items)->contents[menu_items_used++] = name;
377 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
378}
dcfdbac7 379
3427a3db
GM
380/* Push one menu item into the current pane. NAME is the string to
381 display. ENABLE if non-nil means this item can be selected. KEY
382 is the key generated by choosing this item, or nil if this item
383 doesn't really have a definition. DEF is the definition of this
384 item. EQUIV is the textual description of the keyboard equivalent
385 for this item (or nil if none). TYPE is the type of this menu
386 item, one of nil, `toggle' or `radio'. */
18686d47 387
78589e07 388static void
850df50b
GM
389push_menu_item (name, enable, key, def, equiv, type, selected, help)
390 Lisp_Object name, enable, key, def, equiv, type, selected, help;
78589e07
RS
391{
392 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
101bb4a5 393 grow_menu_items ();
088831f6 394
78589e07
RS
395 XVECTOR (menu_items)->contents[menu_items_used++] = name;
396 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
397 XVECTOR (menu_items)->contents[menu_items_used++] = key;
398 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
a352a815 399 XVECTOR (menu_items)->contents[menu_items_used++] = def;
3427a3db
GM
400 XVECTOR (menu_items)->contents[menu_items_used++] = type;
401 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
850df50b 402 XVECTOR (menu_items)->contents[menu_items_used++] = help;
78589e07
RS
403}
404\f
78589e07 405/* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
101bb4a5
RS
406 and generate menu panes for them in menu_items.
407 If NOTREAL is nonzero,
408 don't bother really computing whether an item is enabled. */
18686d47 409
78589e07 410static void
101bb4a5 411keymap_panes (keymaps, nmaps, notreal)
78589e07
RS
412 Lisp_Object *keymaps;
413 int nmaps;
101bb4a5 414 int notreal;
18686d47 415{
78589e07 416 int mapno;
18686d47 417
78589e07 418 init_menu_items ();
18686d47 419
78589e07
RS
420 /* Loop over the given keymaps, making a pane for each map.
421 But don't make a pane that is empty--ignore that map instead.
422 P is the number of panes we have made so far. */
423 for (mapno = 0; mapno < nmaps; mapno++)
488205c0 424 single_keymap_panes (keymaps[mapno],
5ee707b8 425 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
78589e07
RS
426
427 finish_menu_items ();
428}
429
0695ce91
SM
430/* Args passed between single_keymap_panes and single_menu_item. */
431struct skp
432 {
433 Lisp_Object pending_maps;
434 int maxdepth, notreal;
435 int notbuttons;
436 };
437
438static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
aea448d2 439 void *));
0695ce91 440
78589e07
RS
441/* This is a recursive subroutine of keymap_panes.
442 It handles one keymap, KEYMAP.
443 The other arguments are passed along
101bb4a5 444 or point to local variables of the previous function.
de57a39c
RS
445 If NOTREAL is nonzero, only check for equivalent key bindings, don't
446 evaluate expressions in menu items and don't make any menu.
98381e42
RS
447
448 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
78589e07
RS
449
450static void
98381e42 451single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
78589e07
RS
452 Lisp_Object keymap;
453 Lisp_Object pane_name;
454 Lisp_Object prefix;
101bb4a5 455 int notreal;
98381e42 456 int maxdepth;
78589e07 457{
0695ce91
SM
458 struct skp skp;
459 struct gcpro gcpro1;
460
461 skp.pending_maps = Qnil;
462 skp.maxdepth = maxdepth;
463 skp.notreal = notreal;
464 skp.notbuttons = 0;
78589e07 465
98381e42
RS
466 if (maxdepth <= 0)
467 return;
468
78589e07
RS
469 push_menu_pane (pane_name, prefix);
470
de57a39c
RS
471#ifndef HAVE_BOXES
472 /* Remember index for first item in this pane so we can go back and
473 add a prefix when (if) we see the first button. After that, notbuttons
474 is set to 0, to mark that we have seen a button and all non button
475 items need a prefix. */
0695ce91 476 skp.notbuttons = menu_items_used;
de57a39c
RS
477#endif
478
0695ce91
SM
479 GCPRO1 (skp.pending_maps);
480 map_keymap (keymap, single_menu_item, Qnil, &skp, 1);
481 UNGCPRO;
78589e07
RS
482
483 /* Process now any submenus which want to be panes at this level. */
0695ce91 484 while (CONSP (skp.pending_maps))
78589e07 485 {
101bb4a5 486 Lisp_Object elt, eltcdr, string;
0695ce91 487 elt = XCAR (skp.pending_maps);
8e713be6
KR
488 eltcdr = XCDR (elt);
489 string = XCAR (eltcdr);
101bb4a5
RS
490 /* We no longer discard the @ from the beginning of the string here.
491 Instead, we do this in xmenu_show. */
492 single_keymap_panes (Fcar (elt), string,
8e713be6 493 XCDR (eltcdr), notreal, maxdepth - 1);
0695ce91 494 skp.pending_maps = XCDR (skp.pending_maps);
78589e07 495 }
18686d47 496}
78589e07 497\f
de57a39c
RS
498/* This is a subroutine of single_keymap_panes that handles one
499 keymap entry.
177c0ea7 500 KEY is a key in a keymap and ITEM is its binding.
0695ce91 501 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
de57a39c 502 separate panes.
0695ce91 503 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
de57a39c 504 evaluate expressions in menu items and don't make any menu.
0695ce91
SM
505 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them.
506 SKP->NOTBUTTONS is only used when simulating toggle boxes and radio
507 buttons. It keeps track of if we have seen a button in this menu or
508 not. */
de57a39c
RS
509
510static void
aea448d2 511single_menu_item (key, item, dummy, skp_v)
0695ce91 512 Lisp_Object key, item, dummy;
aea448d2 513 void *skp_v;
de57a39c 514{
faa935b6 515 Lisp_Object map, item_string, enabled;
de57a39c
RS
516 struct gcpro gcpro1, gcpro2;
517 int res;
aea448d2 518 struct skp *skp = skp_v;
177c0ea7 519
de57a39c
RS
520 /* Parse the menu item and leave the result in item_properties. */
521 GCPRO2 (key, item);
0695ce91 522 res = parse_menu_item (item, skp->notreal, 0);
de57a39c
RS
523 UNGCPRO;
524 if (!res)
525 return; /* Not a menu item. */
526
527 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
b9de078a 528
0695ce91 529 if (skp->notreal)
de57a39c
RS
530 {
531 /* We don't want to make a menu, just traverse the keymaps to
532 precompute equivalent key bindings. */
533 if (!NILP (map))
0695ce91 534 single_keymap_panes (map, Qnil, key, 1, skp->maxdepth - 1);
de57a39c
RS
535 return;
536 }
537
538 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
177c0ea7 539 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
de57a39c 540
d5db4077 541 if (!NILP (map) && SREF (item_string, 0) == '@')
de57a39c
RS
542 {
543 if (!NILP (enabled))
544 /* An enabled separate pane. Remember this to handle it later. */
0695ce91
SM
545 skp->pending_maps = Fcons (Fcons (map, Fcons (item_string, key)),
546 skp->pending_maps);
de57a39c
RS
547 return;
548 }
549
550#ifndef HAVE_BOXES
551 /* Simulate radio buttons and toggle boxes by putting a prefix in
552 front of them. */
553 {
554 Lisp_Object prefix = Qnil;
555 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
556 if (!NILP (type))
557 {
558 Lisp_Object selected
559 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
560
0695ce91 561 if (skp->notbuttons)
de57a39c
RS
562 /* The first button. Line up previous items in this menu. */
563 {
0695ce91 564 int index = skp->notbuttons; /* Index for first item this menu. */
de57a39c
RS
565 int submenu = 0;
566 Lisp_Object tem;
567 while (index < menu_items_used)
568 {
569 tem
570 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
571 if (NILP (tem))
572 {
573 index++;
574 submenu++; /* Skip sub menu. */
575 }
576 else if (EQ (tem, Qlambda))
577 {
578 index++;
579 submenu--; /* End sub menu. */
580 }
581 else if (EQ (tem, Qt))
582 index += 3; /* Skip new pane marker. */
583 else if (EQ (tem, Qquote))
584 index++; /* Skip a left, right divider. */
585 else
586 {
d5db4077
KR
587 if (!submenu && SREF (tem, 0) != '\0'
588 && SREF (tem, 0) != '-')
de57a39c
RS
589 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
590 = concat2 (build_string (" "), tem);
591 index += MENU_ITEMS_ITEM_LENGTH;
592 }
593 }
0695ce91 594 skp->notbuttons = 0;
de57a39c
RS
595 }
596
597 /* Calculate prefix, if any, for this item. */
598 if (EQ (type, QCtoggle))
599 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
600 else if (EQ (type, QCradio))
601 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
602 }
603 /* Not a button. If we have earlier buttons, then we need a prefix. */
0695ce91 604 else if (!skp->notbuttons && SREF (item_string, 0) != '\0'
d5db4077 605 && SREF (item_string, 0) != '-')
de57a39c
RS
606 prefix = build_string (" ");
607
608 if (!NILP (prefix))
609 item_string = concat2 (prefix, item_string);
610 }
611#endif /* not HAVE_BOXES */
177c0ea7 612
488dd4c4 613#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
810f2256 614 if (!NILP (map))
de57a39c
RS
615 /* Indicate visually that this is a submenu. */
616 item_string = concat2 (item_string, build_string (" >"));
617#endif
618
619 push_menu_item (item_string, enabled, key,
620 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
3427a3db
GM
621 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
622 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
850df50b
GM
623 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
624 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
de57a39c 625
488dd4c4 626#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
de57a39c
RS
627 /* Display a submenu using the toolkit. */
628 if (! (NILP (map) || NILP (enabled)))
629 {
630 push_submenu_start ();
0695ce91 631 single_keymap_panes (map, Qnil, key, 0, skp->maxdepth - 1);
de57a39c
RS
632 push_submenu_end ();
633 }
634#endif
635}
636\f
8e6208c5 637/* Push all the panes and items of a menu described by the
78589e07
RS
638 alist-of-alists MENU.
639 This handles old-fashioned calls to x-popup-menu. */
18686d47 640
78589e07
RS
641static void
642list_of_panes (menu)
18686d47 643 Lisp_Object menu;
18686d47 644{
78589e07
RS
645 Lisp_Object tail;
646
647 init_menu_items ();
648
8af9fa55 649 for (tail = menu; CONSP (tail); tail = XCDR (tail))
78589e07
RS
650 {
651 Lisp_Object elt, pane_name, pane_data;
8af9fa55 652 elt = XCAR (tail);
78589e07 653 pane_name = Fcar (elt);
b7826503 654 CHECK_STRING (pane_name);
646f98ec 655 push_menu_pane (ENCODE_MENU_STRING (pane_name), Qnil);
78589e07 656 pane_data = Fcdr (elt);
b7826503 657 CHECK_CONS (pane_data);
78589e07
RS
658 list_of_items (pane_data);
659 }
660
661 finish_menu_items ();
662}
663
664/* Push the items in a single pane defined by the alist PANE. */
665
666static void
667list_of_items (pane)
668 Lisp_Object pane;
669{
670 Lisp_Object tail, item, item1;
671
8af9fa55 672 for (tail = pane; CONSP (tail); tail = XCDR (tail))
78589e07 673 {
8af9fa55 674 item = XCAR (tail);
78589e07 675 if (STRINGP (item))
646f98ec
DL
676 push_menu_item (ENCODE_MENU_STRING (item), Qnil, Qnil, Qt,
677 Qnil, Qnil, Qnil, Qnil);
8af9fa55 678 else if (CONSP (item))
78589e07 679 {
8af9fa55 680 item1 = XCAR (item);
b7826503 681 CHECK_STRING (item1);
8af9fa55 682 push_menu_item (ENCODE_MENU_STRING (item1), Qt, XCDR (item),
646f98ec 683 Qt, Qnil, Qnil, Qnil, Qnil);
78589e07 684 }
8af9fa55
SM
685 else
686 push_left_right_boundary ();
687
78589e07
RS
688 }
689}
690\f
a894238b
JD
691#ifdef HAVE_X_WINDOWS
692/* Return the mouse position in *X and *Y. The coordinates are window
693 relative for the edit window in frame F.
694 This is for Fx_popup_menu. The mouse_position_hook can not
695 be used for X, as it returns window relative coordinates
696 for the window where the mouse is in. This could be the menu bar,
697 the scroll bar or the edit window. Fx_popup_menu needs to be
698 sure it is the edit window. */
699static void
810f2256 700mouse_position_for_popup (f, x, y)
a894238b
JD
701 FRAME_PTR f;
702 int *x;
703 int *y;
704{
705 Window root, dummy_window;
706 int dummy;
707
62af879c
KL
708 if (! FRAME_X_P (f))
709 abort ();
710
a894238b 711 BLOCK_INPUT;
177c0ea7 712
a894238b
JD
713 XQueryPointer (FRAME_X_DISPLAY (f),
714 DefaultRootWindow (FRAME_X_DISPLAY (f)),
715
716 /* The root window which contains the pointer. */
717 &root,
718
719 /* Window pointer is on, not used */
720 &dummy_window,
721
722 /* The position on that root window. */
723 x, y,
724
725 /* x/y in dummy_window coordinates, not used. */
726 &dummy, &dummy,
727
728 /* Modifier keys and pointer buttons, about which
729 we don't care. */
730 (unsigned int *) &dummy);
731
732 UNBLOCK_INPUT;
733
734 /* xmenu_show expects window coordinates, not root window
735 coordinates. Translate. */
9882535b
KS
736 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
737 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
a894238b
JD
738}
739
740#endif /* HAVE_X_WINDOWS */
741
540e52d1 742DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
f6e34773 743 doc: /* Pop up a deck-of-cards menu and return user's selection.
228299fa
GM
744POSITION is a position specification. This is either a mouse button event
745or a list ((XOFFSET YOFFSET) WINDOW)
746where XOFFSET and YOFFSET are positions in pixels from the top left
e3fbac65 747corner of WINDOW. (WINDOW may be a window or a frame object.)
453a4f1b 748This controls the position of the top left of the menu as a whole.
228299fa
GM
749If POSITION is t, it means to use the current mouse position.
750
751MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
752The menu items come from key bindings that have a menu string as well as
753a definition; actually, the "definition" in such a key binding looks like
754\(STRING . REAL-DEFINITION). To give the menu a title, put a string into
755the keymap as a top-level element.
756
757If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
758Otherwise, REAL-DEFINITION should be a valid key binding definition.
759
760You can also use a list of keymaps as MENU.
761 Then each keymap makes a separate pane.
e3fbac65
LT
762
763When MENU is a keymap or a list of keymaps, the return value is the
764list of events corresponding to the user's choice. Note that
765`x-popup-menu' does not actually execute the command bound to that
766sequence of events.
228299fa
GM
767
768Alternatively, you can specify a menu of multiple panes
769 with a list of the form (TITLE PANE1 PANE2...),
770where each pane is a list of form (TITLE ITEM1 ITEM2...).
771Each ITEM is normally a cons cell (STRING . VALUE);
772but a string can appear as an item--that makes a nonselectable line
773in the menu.
774With this form of menu, the return value is VALUE from the chosen item.
775
776If POSITION is nil, don't display the menu at all, just precalculate the
c05fe5d5
LT
777cached information about equivalent key sequences.
778
779If the user gets rid of the menu without making a valid choice, for
780instance by clicking the mouse away from a valid choice or by typing
781keyboard input, then this normally results in a quit and
782`x-popup-menu' does not return. But if POSITION is a mouse button
783event (indicating that the user invoked the menu with the mouse) then
784no quit occurs and `x-popup-menu' returns nil. */)
7ee72033 785 (position, menu)
78589e07
RS
786 Lisp_Object position, menu;
787{
18686d47 788 Lisp_Object keymap, tem;
6bbd7a29 789 int xpos = 0, ypos = 0;
78589e07 790 Lisp_Object title;
999cdb8f 791 char *error_name = NULL;
78589e07 792 Lisp_Object selection;
cc45fb40 793 FRAME_PTR f = NULL;
78589e07
RS
794 Lisp_Object x, y, window;
795 int keymaps = 0;
9685a93f 796 int for_click = 0;
86fad4ec 797 int specpdl_count = SPECPDL_INDEX ();
78589e07
RS
798 struct gcpro gcpro1;
799
1e659e4c 800#ifdef HAVE_MENUS
78589e07
RS
801 if (! NILP (position))
802 {
488dd4c4 803 int get_current_pos_p = 0;
101bb4a5
RS
804 check_x ();
805
78589e07 806 /* Decode the first argument: find the window and the coordinates. */
9572375b 807 if (EQ (position, Qt)
2b06561a
DL
808 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
809 || EQ (XCAR (position), Qtool_bar))))
78589e07 810 {
488dd4c4
JD
811 get_current_pos_p = 1;
812 }
813 else
814 {
815 tem = Fcar (position);
816 if (CONSP (tem))
817 {
818 window = Fcar (Fcdr (position));
8af9fa55
SM
819 x = XCAR (tem);
820 y = Fcar (XCDR (tem));
488dd4c4
JD
821 }
822 else
823 {
824 for_click = 1;
825 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
826 window = Fcar (tem); /* POSN_WINDOW (tem) */
827 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
828 x = Fcar (tem);
829 y = Fcdr (tem);
830 }
831
832 /* If a click happens in an external tool bar or a detached
833 tool bar, x and y is NIL. In that case, use the current
834 mouse position. This happens for the help button in the
835 tool bar. Ideally popup-menu should pass NIL to
836 this function, but it doesn't. */
837 if (NILP (x) && NILP (y))
838 get_current_pos_p = 1;
839 }
840
841 if (get_current_pos_p)
842 {
78589e07 843 /* Use the mouse's current position. */
b404828f 844 FRAME_PTR new_f = SELECTED_FRAME ();
a894238b
JD
845#ifdef HAVE_X_WINDOWS
846 /* Can't use mouse_position_hook for X since it returns
847 coordinates relative to the window the mouse is in,
848 we need coordinates relative to the edit widget always. */
849 if (new_f != 0)
850 {
851 int cur_x, cur_y;
852
853 mouse_position_for_popup (new_f, &cur_x, &cur_y);
854 /* cur_x/y may be negative, so use make_number. */
855 x = make_number (cur_x);
856 y = make_number (cur_y);
857 }
177c0ea7 858
a894238b
JD
859#else /* not HAVE_X_WINDOWS */
860 Lisp_Object bar_window;
861 enum scroll_bar_part part;
862 unsigned long time;
863
864 if (mouse_position_hook)
865 (*mouse_position_hook) (&new_f, 1, &bar_window,
866 &part, &x, &y, &time);
867#endif /* not HAVE_X_WINDOWS */
868
869 if (new_f != 0)
870 XSETFRAME (window, new_f);
871 else
872 {
873 window = selected_window;
874 XSETFASTINT (x, 0);
875 XSETFASTINT (y, 0);
876 }
78589e07 877 }
78589e07 878
b7826503
PJ
879 CHECK_NUMBER (x);
880 CHECK_NUMBER (y);
78589e07
RS
881
882 /* Decode where to put the menu. */
883
b5bb2705 884 if (FRAMEP (window))
78589e07
RS
885 {
886 f = XFRAME (window);
78589e07
RS
887 xpos = 0;
888 ypos = 0;
889 }
b5bb2705 890 else if (WINDOWP (window))
78589e07 891 {
b7826503 892 CHECK_LIVE_WINDOW (window);
78589e07
RS
893 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
894
9882535b
KS
895 xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window));
896 ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window));
78589e07
RS
897 }
898 else
899 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
900 but I don't want to make one now. */
b7826503 901 CHECK_WINDOW (window);
78589e07
RS
902
903 xpos += XINT (x);
904 ypos += XINT (y);
62af879c
KL
905
906 if (! FRAME_X_P (f))
907 error ("Can not put X menu on non-X terminal");
78589e07 908 }
bfc524bc 909 Vmenu_updating_frame = Qnil;
1e659e4c 910#endif /* HAVE_MENUS */
78589e07 911
86fad4ec 912 record_unwind_protect (unuse_menu_items, Qnil);
78589e07
RS
913 title = Qnil;
914 GCPRO1 (title);
915
916 /* Decode the menu items from what was specified. */
18686d47 917
02067692
SM
918 keymap = get_keymap (menu, 0, 0);
919 if (CONSP (keymap))
18686d47
RS
920 {
921 /* We were given a keymap. Extract menu info from the keymap. */
922 Lisp_Object prompt;
18686d47 923
78589e07 924 /* Extract the detailed info to make one pane. */
101bb4a5 925 keymap_panes (&menu, 1, NILP (position));
78589e07 926
18686d47
RS
927 /* Search for a string appearing directly as an element of the keymap.
928 That string is the title of the menu. */
5ee707b8 929 prompt = Fkeymap_prompt (keymap);
4f9ad016
RS
930 if (NILP (title) && !NILP (prompt))
931 title = prompt;
18686d47 932
78589e07
RS
933 /* Make that be the pane title of the first pane. */
934 if (!NILP (prompt) && menu_items_n_panes >= 0)
935 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
936
937 keymaps = 1;
18686d47 938 }
02067692 939 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
18686d47
RS
940 {
941 /* We were given a list of keymaps. */
18686d47
RS
942 int nmaps = XFASTINT (Flength (menu));
943 Lisp_Object *maps
944 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
945 int i;
78589e07
RS
946
947 title = Qnil;
18686d47
RS
948
949 /* The first keymap that has a prompt string
950 supplies the menu title. */
8af9fa55 951 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
18686d47 952 {
78589e07
RS
953 Lisp_Object prompt;
954
8af9fa55 955 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
18686d47 956
5ee707b8 957 prompt = Fkeymap_prompt (keymap);
78589e07
RS
958 if (NILP (title) && !NILP (prompt))
959 title = prompt;
18686d47
RS
960 }
961
962 /* Extract the detailed info to make one pane. */
101bb4a5 963 keymap_panes (maps, nmaps, NILP (position));
78589e07
RS
964
965 /* Make the title be the pane title of the first pane. */
966 if (!NILP (title) && menu_items_n_panes >= 0)
967 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
968
969 keymaps = 1;
18686d47
RS
970 }
971 else
972 {
973 /* We were given an old-fashioned menu. */
78589e07 974 title = Fcar (menu);
b7826503 975 CHECK_STRING (title);
18686d47 976
78589e07 977 list_of_panes (Fcdr (menu));
18686d47 978
78589e07
RS
979 keymaps = 0;
980 }
177c0ea7 981
86fad4ec
SM
982 unbind_to (specpdl_count, Qnil);
983
78589e07 984 if (NILP (position))
18686d47 985 {
78589e07
RS
986 discard_menu_items ();
987 UNGCPRO;
988 return Qnil;
18686d47
RS
989 }
990
1e659e4c 991#ifdef HAVE_MENUS
78589e07
RS
992 /* Display them in a menu. */
993 BLOCK_INPUT;
18686d47 994
673a6211 995 selection = xmenu_show (f, xpos, ypos, for_click,
78589e07
RS
996 keymaps, title, &error_name);
997 UNBLOCK_INPUT;
18686d47 998
78589e07 999 discard_menu_items ();
18686d47 1000
78589e07 1001 UNGCPRO;
1e659e4c 1002#endif /* HAVE_MENUS */
18686d47 1003
78589e07
RS
1004 if (error_name) error (error_name);
1005 return selection;
18686d47 1006}
165e1749 1007
1e659e4c
RS
1008#ifdef HAVE_MENUS
1009
6a040d6a 1010DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
f6e34773 1011 doc: /* Pop up a dialog box and return user's selection.
228299fa
GM
1012POSITION specifies which frame to use.
1013This is normally a mouse button event or a window or frame.
1014If POSITION is t, it means to use the frame the mouse is on.
1015The dialog box appears in the middle of the specified frame.
1016
1017CONTENTS specifies the alternatives to display in the dialog box.
6a040d6a 1018It is a list of the form (DIALOG ITEM1 ITEM2...).
228299fa
GM
1019Each ITEM is a cons cell (STRING . VALUE).
1020The return value is VALUE from the chosen item.
1021
1022An ITEM may also be just a string--that makes a nonselectable item.
1023An ITEM may also be nil--that means to put all preceding items
1024on the left of the dialog box and all following items on the right.
c05fe5d5
LT
1025\(By default, approximately half appear on each side.)
1026
6a040d6a
NR
1027If HEADER is non-nil, the frame title for the box is "Information",
1028otherwise it is "Question".
1029
c05fe5d5
LT
1030If the user gets rid of the dialog box without making a valid choice,
1031for instance using the window manager, then this produces a quit and
1032`x-popup-dialog' does not return. */)
6a040d6a
NR
1033 (position, contents, header)
1034 Lisp_Object position, contents, header;
165e1749 1035{
cc45fb40 1036 FRAME_PTR f = NULL;
99fe880d 1037 Lisp_Object window;
165e1749
FP
1038
1039 check_x ();
1040
99fe880d 1041 /* Decode the first argument: find the window or frame to use. */
88d4f6ec 1042 if (EQ (position, Qt)
2b06561a
DL
1043 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1044 || EQ (XCAR (position), Qtool_bar))))
165e1749 1045 {
b14db4d7 1046#if 0 /* Using the frame the mouse is on may not be right. */
99fe880d 1047 /* Use the mouse's current position. */
b404828f 1048 FRAME_PTR new_f = SELECTED_FRAME ();
99fe880d 1049 Lisp_Object bar_window;
cc45fb40 1050 enum scroll_bar_part part;
99fe880d
RS
1051 unsigned long time;
1052 Lisp_Object x, y;
165e1749 1053
46b657e2 1054 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
5ca2ef64 1055
99fe880d 1056 if (new_f != 0)
a39f0477 1057 XSETFRAME (window, new_f);
99fe880d
RS
1058 else
1059 window = selected_window;
b14db4d7 1060#endif
88d4f6ec 1061 window = selected_window;
99fe880d
RS
1062 }
1063 else if (CONSP (position))
1064 {
1065 Lisp_Object tem;
1066 tem = Fcar (position);
b5bb2705 1067 if (CONSP (tem))
99fe880d 1068 window = Fcar (Fcdr (position));
80670155
RS
1069 else
1070 {
99fe880d
RS
1071 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1072 window = Fcar (tem); /* POSN_WINDOW (tem) */
165e1749 1073 }
165e1749 1074 }
99fe880d
RS
1075 else if (WINDOWP (position) || FRAMEP (position))
1076 window = position;
a9be6839
RS
1077 else
1078 window = Qnil;
165e1749 1079
99fe880d 1080 /* Decode where to put the menu. */
165e1749 1081
b5bb2705 1082 if (FRAMEP (window))
99fe880d 1083 f = XFRAME (window);
b5bb2705 1084 else if (WINDOWP (window))
165e1749 1085 {
b7826503 1086 CHECK_LIVE_WINDOW (window);
99fe880d 1087 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
165e1749 1088 }
99fe880d
RS
1089 else
1090 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1091 but I don't want to make one now. */
b7826503 1092 CHECK_WINDOW (window);
165e1749 1093
62af879c
KL
1094 if (! FRAME_X_P (f))
1095 error ("Can not put X dialog on non-X terminal");
1096
488dd4c4 1097#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
99fe880d
RS
1098 /* Display a menu with these alternatives
1099 in the middle of frame F. */
1100 {
1101 Lisp_Object x, y, frame, newpos;
a39f0477
KH
1102 XSETFRAME (frame, f);
1103 XSETINT (x, x_pixel_width (f) / 2);
1104 XSETINT (y, x_pixel_height (f) / 2);
99fe880d
RS
1105 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1106
1107 return Fx_popup_menu (newpos,
1108 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1109 }
1110#else
1111 {
1112 Lisp_Object title;
1113 char *error_name;
1114 Lisp_Object selection;
86fad4ec 1115 int specpdl_count = SPECPDL_INDEX ();
165e1749 1116
99fe880d
RS
1117 /* Decode the dialog items from what was specified. */
1118 title = Fcar (contents);
b7826503 1119 CHECK_STRING (title);
86fad4ec 1120 record_unwind_protect (unuse_menu_items, Qnil);
165e1749 1121
160b1b50
JD
1122 if (NILP (Fcar (Fcdr (contents))))
1123 /* No buttons specified, add an "Ok" button so users can pop down
1124 the dialog. Also, the lesstif/motif version crashes if there are
1125 no buttons. */
1126 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
b9de078a 1127
99fe880d 1128 list_of_panes (Fcons (contents, Qnil));
165e1749 1129
99fe880d
RS
1130 /* Display them in a dialog box. */
1131 BLOCK_INPUT;
6a040d6a 1132 selection = xdialog_show (f, 0, title, header, &error_name);
99fe880d 1133 UNBLOCK_INPUT;
165e1749 1134
86fad4ec 1135 unbind_to (specpdl_count, Qnil);
99fe880d
RS
1136 discard_menu_items ();
1137
1138 if (error_name) error (error_name);
1139 return selection;
1140 }
7464b131 1141#endif
392d3f4b 1142}
c3438661
JD
1143
1144
1145#ifndef MSDOS
1146
b3935289
JD
1147/* Set menu_items_inuse so no other popup menu or dialog is created. */
1148
1149void
1150x_menu_set_in_use (in_use)
1151 int in_use;
1152{
1153 menu_items_inuse = in_use ? Qt : Qnil;
5ae53dcf 1154 popup_activated_flag = in_use;
b3935289
JD
1155}
1156
c3438661
JD
1157/* Wait for an X event to arrive or for a timer to expire. */
1158
b3935289 1159void
c3438661
JD
1160x_menu_wait_for_event (void *data)
1161{
1162 extern EMACS_TIME timer_check P_ ((int));
1163
1164 /* Another way to do this is to register a timer callback, that can be
1165 done in GTK and Xt. But we have to do it like this when using only X
1166 anyway, and with callbacks we would have three variants for timer handling
1167 instead of the small ifdefs below. */
1168
1169 while (
1170#ifdef USE_X_TOOLKIT
f1d1cd24 1171 ! XtAppPending (Xt_app_con)
c3438661
JD
1172#elif defined USE_GTK
1173 ! gtk_events_pending ()
1174#else
1175 ! XPending ((Display*) data)
1176#endif
1177 )
1178 {
1179 EMACS_TIME next_time = timer_check (1);
1180 long secs = EMACS_SECS (next_time);
1181 long usecs = EMACS_USECS (next_time);
1182 SELECT_TYPE read_fds;
1183 struct x_display_info *dpyinfo;
1184 int n = 0;
1185
1186 FD_ZERO (&read_fds);
1187 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
1188 {
1189 int fd = ConnectionNumber (dpyinfo->display);
1190 FD_SET (fd, &read_fds);
1191 if (fd > n) n = fd;
1192 }
1193
1194 if (secs < 0 || (secs == 0 && usecs == 0))
1195 {
1196 /* Sometimes timer_check returns -1 (no timers) even if there are
1197 timers. So do a timeout anyway. */
1198 EMACS_SET_SECS (next_time, 1);
1199 EMACS_SET_USECS (next_time, 0);
1200 }
1201
1202 select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, &next_time);
1203 }
1204}
1205#endif /* ! MSDOS */
1206
78589e07 1207\f
488dd4c4 1208#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18686d47 1209
f1d1cd24
JD
1210#ifdef USE_X_TOOLKIT
1211
4dedbfe0 1212/* Loop in Xt until the menu pulldown or dialog popup has been
f56e7ad2 1213 popped down (deactivated). This is used for x-popup-menu
f02cac82
RS
1214 and x-popup-dialog; it is not used for the menu bar.
1215
2e2b8e22 1216 NOTE: All calls to popup_get_selection should be protected
c98fcf4b 1217 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
aa669def 1218
f02cac82 1219static void
95bdef2e 1220popup_get_selection (initial_event, dpyinfo, id, do_timers)
4dedbfe0 1221 XEvent *initial_event;
aa669def 1222 struct x_display_info *dpyinfo;
2e2b8e22 1223 LWLIB_ID id;
df470e3b 1224 int do_timers;
78589e07 1225{
4dedbfe0 1226 XEvent event;
f02cac82 1227
8b806a85
JD
1228 while (popup_activated_flag)
1229 {
c0b0f54b 1230 if (initial_event)
8b806a85
JD
1231 {
1232 event = *initial_event;
1233 initial_event = 0;
1234 }
1235 else
c3438661
JD
1236 {
1237 if (do_timers) x_menu_wait_for_event (0);
1238 XtAppNextEvent (Xt_app_con, &event);
1239 }
78589e07 1240
10624005 1241 /* Make sure we don't consider buttons grabbed after menu goes.
8b806a85
JD
1242 And make sure to deactivate for any ButtonRelease,
1243 even if XtDispatchEvent doesn't do that. */
1244 if (event.type == ButtonRelease
1245 && dpyinfo->display == event.xbutton.display)
10624005
KH
1246 {
1247 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
177c0ea7 1248#ifdef USE_MOTIF /* Pretending that the event came from a
8b806a85
JD
1249 Btn1Down seems the only way to convince Motif to
1250 activate its callbacks; setting the XmNmenuPost
1251 isn't working. --marcus@sysc.pdx.edu. */
1252 event.xbutton.button = 1;
1253 /* Motif only pops down menus when no Ctrl, Alt or Mod
1254 key is pressed and the button is released. So reset key state
1255 so Motif thinks this is the case. */
1256 event.xbutton.state = 0;
c8b5aa3d 1257#endif
10624005 1258 }
33385c6f 1259 /* Pop down on C-g and Escape. */
2e2b8e22 1260 else if (event.type == KeyPress
10624005 1261 && dpyinfo->display == event.xbutton.display)
8b806a85
JD
1262 {
1263 KeySym keysym = XLookupKeysym (&event.xkey, 0);
9f6fcdc5 1264
33385c6f
JD
1265 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
1266 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
9f6fcdc5 1267 popup_activated_flag = 0;
8b806a85 1268 }
177c0ea7 1269
8b806a85 1270 x_dispatch_event (&event, event.xany.display);
aa669def 1271 }
78589e07
RS
1272}
1273
488dd4c4
JD
1274#endif /* USE_X_TOOLKIT */
1275
1276#ifdef USE_GTK
1277/* Loop util popup_activated_flag is set to zero in a callback.
1278 Used for popup menus and dialogs. */
f1d1cd24 1279
488dd4c4 1280static void
f1d1cd24 1281popup_widget_loop (do_timers, widget)
c3438661 1282 int do_timers;
f1d1cd24 1283 GtkWidget *widget;
488dd4c4
JD
1284{
1285 ++popup_activated_flag;
1286
1287 /* Process events in the Gtk event loop until done. */
1288 while (popup_activated_flag)
1289 {
c3438661 1290 if (do_timers) x_menu_wait_for_event (0);
488dd4c4
JD
1291 gtk_main_iteration ();
1292 }
1293}
1294#endif
1295
88766961
RS
1296/* Activate the menu bar of frame F.
1297 This is called from keyboard.c when it gets the
3b8f9651 1298 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
88766961
RS
1299
1300 To activate the menu bar, we use the X button-press event
ac78b144 1301 that was saved in saved_menu_event.
88766961
RS
1302 That makes the toolkit do its thing.
1303
1304 But first we recompute the menu bar contents (the whole tree).
1305
1306 The reason for saving the button event until here, instead of
1307 passing it to the toolkit right away, is that we can safely
1308 execute Lisp code. */
177c0ea7 1309
dfcf069d 1310void
88766961
RS
1311x_activate_menubar (f)
1312 FRAME_PTR f;
1313{
62af879c
KL
1314 if (! FRAME_X_P (f))
1315 abort ();
1316
ac78b144 1317 if (!f->output_data.x->saved_menu_event->type)
88766961
RS
1318 return;
1319
177c0ea7 1320#ifdef USE_GTK
810f2256
JD
1321 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
1322 f->output_data.x->saved_menu_event->xany.window))
488dd4c4
JD
1323 return;
1324#endif
177c0ea7 1325
a9be6839 1326 set_frame_menubar (f, 0, 1);
88766961 1327 BLOCK_INPUT;
488dd4c4
JD
1328#ifdef USE_GTK
1329 XPutBackEvent (f->output_data.x->display_info->display,
1330 f->output_data.x->saved_menu_event);
1331 popup_activated_flag = 1;
1332#else
86fad4ec 1333 XtDispatchEvent (f->output_data.x->saved_menu_event);
488dd4c4 1334#endif
88766961 1335 UNBLOCK_INPUT;
a9be6839 1336#ifdef USE_MOTIF
745c34fb
RS
1337 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1338 pending_menu_activation = 1;
a9be6839 1339#endif
177c0ea7 1340
88766961 1341 /* Ignore this if we get it a second time. */
ac78b144 1342 f->output_data.x->saved_menu_event->type = 0;
88766961
RS
1343}
1344
c98fcf4b 1345/* Detect if a dialog or menu has been posted. */
aa669def 1346
4dedbfe0
PR
1347int
1348popup_activated ()
1349{
1350 return popup_activated_flag;
1351}
1352
4dedbfe0
PR
1353/* This callback is invoked when the user selects a menubar cascade
1354 pushbutton, but before the pulldown menu is posted. */
78589e07 1355
488dd4c4 1356#ifndef USE_GTK
78589e07 1357static void
4dedbfe0 1358popup_activate_callback (widget, id, client_data)
78589e07
RS
1359 Widget widget;
1360 LWLIB_ID id;
1361 XtPointer client_data;
1362{
4dedbfe0 1363 popup_activated_flag = 1;
7555d825 1364}
488dd4c4 1365#endif
7555d825
GM
1366
1367/* This callback is invoked when a dialog or menu is finished being
1368 used and has been unposted. */
1369
488dd4c4
JD
1370#ifdef USE_GTK
1371static void
1372popup_deactivate_callback (widget, client_data)
1373 GtkWidget *widget;
1374 gpointer client_data;
1375{
1376 popup_activated_flag = 0;
1377}
1378#else
7555d825
GM
1379static void
1380popup_deactivate_callback (widget, id, client_data)
1381 Widget widget;
1382 LWLIB_ID id;
1383 XtPointer client_data;
1384{
7555d825 1385 popup_activated_flag = 0;
78589e07 1386}
488dd4c4 1387#endif
78589e07 1388
850df50b 1389
488dd4c4
JD
1390/* Function that finds the frame for WIDGET and shows the HELP text
1391 for that widget.
1392 F is the frame if known, or NULL if not known. */
1393static void
1394show_help_event (f, widget, help)
1395 FRAME_PTR f;
1396 xt_or_gtk_widget widget;
1397 Lisp_Object help;
850df50b 1398{
488dd4c4 1399 Lisp_Object frame;
850df50b 1400
850df50b 1401 if (f)
9cd50434
GM
1402 {
1403 XSETFRAME (frame, f);
1404 kbd_buffer_store_help_event (frame, help);
1405 }
850df50b
GM
1406 else
1407 {
9fc63a89 1408#if 0 /* This code doesn't do anything useful. ++kfs */
177c0ea7 1409 /* WIDGET is the popup menu. It's parent is the frame's
850df50b 1410 widget. See which frame that is. */
488dd4c4 1411 xt_or_gtk_widget frame_widget = XtParent (widget);
850df50b
GM
1412 Lisp_Object tail;
1413
1414 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1415 {
1416 frame = XCAR (tail);
1417 if (GC_FRAMEP (frame)
1418 && (f = XFRAME (frame),
1419 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1420 break;
1421 }
9fc63a89 1422#endif
9cd50434
GM
1423 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1424 }
850df50b
GM
1425}
1426
488dd4c4
JD
1427/* Callback called when menu items are highlighted/unhighlighted
1428 while moving the mouse over them. WIDGET is the menu bar or menu
1429 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1430 the data structure for the menu item, or null in case of
1431 unhighlighting. */
4dedbfe0 1432
488dd4c4
JD
1433#ifdef USE_GTK
1434void
1435menu_highlight_callback (widget, call_data)
1436 GtkWidget *widget;
1437 gpointer call_data;
1438{
1439 xg_menu_item_cb_data *cb_data;
1440 Lisp_Object help;
177c0ea7 1441
488dd4c4
JD
1442 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
1443 XG_ITEM_DATA);
1444 if (! cb_data) return;
1445
1446 help = call_data ? cb_data->help : Qnil;
1447
1448 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1449 Don't show help for them, they won't appear before the
1450 popup is popped down. */
1451 if (popup_activated_flag <= 1)
1452 show_help_event (cb_data->cl_data->f, widget, help);
1453}
1454#else
1455void
1456menu_highlight_callback (widget, id, call_data)
78589e07
RS
1457 Widget widget;
1458 LWLIB_ID id;
488dd4c4
JD
1459 void *call_data;
1460{
1461 struct frame *f;
1462 Lisp_Object help;
1463
1464 widget_value *wv = (widget_value *) call_data;
1465
1466 help = wv ? wv->help : Qnil;
177c0ea7 1467
488dd4c4
JD
1468 /* Determine the frame for the help event. */
1469 f = menubar_id_to_frame (id);
1470
1471 show_help_event (f, widget, help);
1472}
1473#endif
1474
1475/* Find the menu selection and store it in the keyboard buffer.
1476 F is the frame the menu is on.
1477 MENU_BAR_ITEMS_USED is the length of VECTOR.
81f09877
JD
1478 VECTOR is an array of menu events for the whole menu. */
1479
1480static void
488dd4c4
JD
1481find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
1482 FRAME_PTR f;
1483 int menu_bar_items_used;
1484 Lisp_Object vector;
1485 void *client_data;
78589e07 1486{
c63f6952 1487 Lisp_Object prefix, entry;
4dedbfe0
PR
1488 Lisp_Object *subprefix_stack;
1489 int submenu_depth = 0;
1490 int i;
1491
6bbd7a29 1492 entry = Qnil;
488dd4c4 1493 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
4dedbfe0
PR
1494 prefix = Qnil;
1495 i = 0;
488dd4c4
JD
1496
1497 while (i < menu_bar_items_used)
4dedbfe0 1498 {
4dedbfe0
PR
1499 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1500 {
1501 subprefix_stack[submenu_depth++] = prefix;
1502 prefix = entry;
1503 i++;
1504 }
1505 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1506 {
1507 prefix = subprefix_stack[--submenu_depth];
1508 i++;
1509 }
1510 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1511 {
4cb35c39 1512 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
4dedbfe0
PR
1513 i += MENU_ITEMS_PANE_LENGTH;
1514 }
1515 else
1516 {
4cb35c39 1517 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
01d5e892
RS
1518 /* The EMACS_INT cast avoids a warning. There's no problem
1519 as long as pointers have enough bits to hold small integers. */
1520 if ((int) (EMACS_INT) client_data == i)
4dedbfe0
PR
1521 {
1522 int j;
1523 struct input_event buf;
4cb35c39 1524 Lisp_Object frame;
38f7dce3 1525 EVENT_INIT (buf);
4dedbfe0 1526
4cb35c39 1527 XSETFRAME (frame, f);
9979315d
GM
1528 buf.kind = MENU_BAR_EVENT;
1529 buf.frame_or_window = frame;
1530 buf.arg = frame;
4dedbfe0
PR
1531 kbd_buffer_store_event (&buf);
1532
1533 for (j = 0; j < submenu_depth; j++)
1534 if (!NILP (subprefix_stack[j]))
1535 {
9979315d
GM
1536 buf.kind = MENU_BAR_EVENT;
1537 buf.frame_or_window = frame;
1538 buf.arg = subprefix_stack[j];
4dedbfe0
PR
1539 kbd_buffer_store_event (&buf);
1540 }
1541
1542 if (!NILP (prefix))
1543 {
9979315d
GM
1544 buf.kind = MENU_BAR_EVENT;
1545 buf.frame_or_window = frame;
1546 buf.arg = prefix;
4dedbfe0
PR
1547 kbd_buffer_store_event (&buf);
1548 }
1549
9979315d
GM
1550 buf.kind = MENU_BAR_EVENT;
1551 buf.frame_or_window = frame;
1552 buf.arg = entry;
4dedbfe0
PR
1553 kbd_buffer_store_event (&buf);
1554
1555 return;
1556 }
1557 i += MENU_ITEMS_ITEM_LENGTH;
1558 }
1559 }
18686d47
RS
1560}
1561
488dd4c4
JD
1562
1563#ifdef USE_GTK
1564/* Gtk calls callbacks just because we tell it what item should be
1565 selected in a radio group. If this variable is set to a non-zero
1566 value, we are creating menus and don't want callbacks right now.
1567*/
1568static int xg_crazy_callback_abort;
1569
1570/* This callback is called from the menu bar pulldown menu
1571 when the user makes a selection.
1572 Figure out what the user chose
1573 and put the appropriate events into the keyboard buffer. */
1574static void
1575menubar_selection_callback (widget, client_data)
1576 GtkWidget *widget;
1577 gpointer client_data;
1578{
1579 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1580
1581 if (xg_crazy_callback_abort)
1582 return;
1583
1584 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
1585 return;
1586
b79c8219
JD
1587 /* For a group of radio buttons, GTK calls the selection callback first
1588 for the item that was active before the selection and then for the one that
1589 is active after the selection. For C-h k this means we get the help on
1590 the deselected item and then the selected item is executed. Prevent that
1591 by ignoring the non-active item. */
1592 if (GTK_IS_RADIO_MENU_ITEM (widget)
1593 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
1594 return;
1595
81f09877
JD
1596 /* When a menu is popped down, X generates a focus event (i.e. focus
1597 goes back to the frame below the menu). Since GTK buffers events,
1598 we force it out here before the menu selection event. Otherwise
1599 sit-for will exit at once if the focus event follows the menu selection
1600 event. */
1601
1602 BLOCK_INPUT;
1603 while (gtk_events_pending ())
1604 gtk_main_iteration ();
1605 UNBLOCK_INPUT;
1606
488dd4c4
JD
1607 find_and_call_menu_selection (cb_data->cl_data->f,
1608 cb_data->cl_data->menu_bar_items_used,
1609 cb_data->cl_data->menu_bar_vector,
1610 cb_data->call_data);
1611}
1612
1613#else /* not USE_GTK */
1614
1615/* This callback is called from the menu bar pulldown menu
1616 when the user makes a selection.
1617 Figure out what the user chose
1618 and put the appropriate events into the keyboard buffer. */
1619static void
1620menubar_selection_callback (widget, id, client_data)
1621 Widget widget;
1622 LWLIB_ID id;
1623 XtPointer client_data;
1624{
1625 FRAME_PTR f;
1626
1627 f = menubar_id_to_frame (id);
1628 if (!f)
1629 return;
1630 find_and_call_menu_selection (f, f->menu_bar_items_used,
1631 f->menu_bar_vector, client_data);
1632}
1633#endif /* not USE_GTK */
1634
f9655c60
RS
1635/* Allocate a widget_value, blocking input. */
1636
1637widget_value *
1638xmalloc_widget_value ()
1639{
1640 widget_value *value;
1641
1642 BLOCK_INPUT;
1643 value = malloc_widget_value ();
1644 UNBLOCK_INPUT;
1645
1646 return value;
1647}
4dedbfe0
PR
1648
1649/* This recursively calls free_widget_value on the tree of widgets.
18686d47 1650 It must free all data that was malloc'ed for these widget_values.
78589e07
RS
1651 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1652 must be left alone. */
1653
18686d47
RS
1654void
1655free_menubar_widget_value_tree (wv)
1656 widget_value *wv;
1657{
1658 if (! wv) return;
18686d47
RS
1659
1660 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1661
1662 if (wv->contents && (wv->contents != (widget_value*)1))
1663 {
1664 free_menubar_widget_value_tree (wv->contents);
1665 wv->contents = (widget_value *) 0xDEADBEEF;
1666 }
1667 if (wv->next)
1668 {
1669 free_menubar_widget_value_tree (wv->next);
1670 wv->next = (widget_value *) 0xDEADBEEF;
1671 }
1672 BLOCK_INPUT;
1673 free_widget_value (wv);
1674 UNBLOCK_INPUT;
1675}
4dedbfe0 1676\f
e6c87a0f 1677/* Set up data in menu_items for a menu bar item
4dedbfe0
PR
1678 whose event type is ITEM_KEY (with string ITEM_NAME)
1679 and whose contents come from the list of keymaps MAPS. */
1680
ced89c24
RS
1681static int
1682parse_single_submenu (item_key, item_name, maps)
4dedbfe0
PR
1683 Lisp_Object item_key, item_name, maps;
1684{
4dedbfe0
PR
1685 Lisp_Object length;
1686 int len;
1687 Lisp_Object *mapvec;
ced89c24 1688 int i;
71dca3e3 1689 int top_level_items = 0;
4dedbfe0
PR
1690
1691 length = Flength (maps);
1692 len = XINT (length);
1693
1694 /* Convert the list MAPS into a vector MAPVEC. */
1695 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1696 for (i = 0; i < len; i++)
1697 {
1698 mapvec[i] = Fcar (maps);
1699 maps = Fcdr (maps);
1700 }
1701
4dedbfe0
PR
1702 /* Loop over the given keymaps, making a pane for each map.
1703 But don't make a pane that is empty--ignore that map instead. */
1704 for (i = 0; i < len; i++)
71dca3e3 1705 {
e6c87a0f 1706 if (!KEYMAPP (mapvec[i]))
71dca3e3 1707 {
846e8c10
RS
1708 /* Here we have a command at top level in the menu bar
1709 as opposed to a submenu. */
71dca3e3
RS
1710 top_level_items = 1;
1711 push_menu_pane (Qnil, Qnil);
850df50b
GM
1712 push_menu_item (item_name, Qt, item_key, mapvec[i],
1713 Qnil, Qnil, Qnil, Qnil);
71dca3e3
RS
1714 }
1715 else
37dc84ff
RS
1716 {
1717 Lisp_Object prompt;
1718 prompt = Fkeymap_prompt (mapvec[i]);
1719 single_keymap_panes (mapvec[i],
1720 !NILP (prompt) ? prompt : item_name,
1721 item_key, 0, 10);
1722 }
71dca3e3 1723 }
4dedbfe0 1724
ced89c24
RS
1725 return top_level_items;
1726}
1727
1728/* Create a tree of widget_value objects
1729 representing the panes and items
1730 in menu_items starting at index START, up to index END. */
1731
1732static widget_value *
1733digest_single_submenu (start, end, top_level_items)
7448803e 1734 int start, end, top_level_items;
ced89c24
RS
1735{
1736 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1737 int i;
1738 int submenu_depth = 0;
1739 widget_value **submenu_stack;
4dedbfe0
PR
1740
1741 submenu_stack
1742 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
f7fab165 1743 wv = xmalloc_widget_value ();
4dedbfe0
PR
1744 wv->name = "menu";
1745 wv->value = 0;
1746 wv->enabled = 1;
3427a3db 1747 wv->button_type = BUTTON_TYPE_NONE;
0b1e6b54 1748 wv->help = Qnil;
4dedbfe0
PR
1749 first_wv = wv;
1750 save_wv = 0;
71dca3e3 1751 prev_wv = 0;
177c0ea7 1752
37dc84ff
RS
1753 /* Loop over all panes and items made by the preceding call
1754 to parse_single_submenu and construct a tree of widget_value objects.
1755 Ignore the panes and items used by previous calls to
1756 digest_single_submenu, even though those are also in menu_items. */
ced89c24
RS
1757 i = start;
1758 while (i < end)
4dedbfe0
PR
1759 {
1760 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1761 {
1762 submenu_stack[submenu_depth++] = save_wv;
1763 save_wv = prev_wv;
1764 prev_wv = 0;
1765 i++;
1766 }
1767 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1768 {
1769 prev_wv = save_wv;
1770 save_wv = submenu_stack[--submenu_depth];
1771 i++;
1772 }
1773 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1774 && submenu_depth != 0)
1775 i += MENU_ITEMS_PANE_LENGTH;
1776 /* Ignore a nil in the item list.
1777 It's meaningful only for dialog boxes. */
1778 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1779 i += 1;
1780 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1781 {
1782 /* Create a new pane. */
1783 Lisp_Object pane_name, prefix;
1784 char *pane_string;
177c0ea7 1785
4dedbfe0
PR
1786 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1787 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
177c0ea7 1788
703dc2a8
KH
1789#ifndef HAVE_MULTILINGUAL_MENU
1790 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
4c329aa8 1791 {
8af9fa55 1792 pane_name = ENCODE_MENU_STRING (pane_name);
4c329aa8
GM
1793 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1794 }
703dc2a8 1795#endif
4dedbfe0 1796 pane_string = (NILP (pane_name)
d5db4077 1797 ? "" : (char *) SDATA (pane_name));
4dedbfe0
PR
1798 /* If there is just one top-level pane, put all its items directly
1799 under the top-level menu. */
1800 if (menu_items_n_panes == 1)
1801 pane_string = "";
1802
1803 /* If the pane has a meaningful name,
1804 make the pane a top-level menu item
1805 with its items as a submenu beneath it. */
1806 if (strcmp (pane_string, ""))
1807 {
f7fab165 1808 wv = xmalloc_widget_value ();
4dedbfe0
PR
1809 if (save_wv)
1810 save_wv->next = wv;
1811 else
1812 first_wv->contents = wv;
2bf436c3
JD
1813 wv->lname = pane_name;
1814 /* Set value to 1 so update_submenu_strings can handle '@' */
1815 wv->value = (char *)1;
4dedbfe0 1816 wv->enabled = 1;
3427a3db 1817 wv->button_type = BUTTON_TYPE_NONE;
0b1e6b54 1818 wv->help = Qnil;
4dedbfe0
PR
1819 }
1820 save_wv = wv;
1821 prev_wv = 0;
1822 i += MENU_ITEMS_PANE_LENGTH;
1823 }
1824 else
1825 {
1826 /* Create a new item within current pane. */
3427a3db 1827 Lisp_Object item_name, enable, descrip, def, type, selected;
850df50b 1828 Lisp_Object help;
177c0ea7 1829
4c329aa8
GM
1830 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1831 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1832 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1833 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1834 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1835 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1836 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3427a3db 1837
703dc2a8 1838#ifndef HAVE_MULTILINGUAL_MENU
3427a3db 1839 if (STRING_MULTIBYTE (item_name))
4c329aa8 1840 {
646f98ec 1841 item_name = ENCODE_MENU_STRING (item_name);
4c329aa8
GM
1842 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1843 }
177c0ea7 1844
3427a3db 1845 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
4c329aa8 1846 {
646f98ec 1847 descrip = ENCODE_MENU_STRING (descrip);
4c329aa8
GM
1848 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1849 }
1850#endif /* not HAVE_MULTILINGUAL_MENU */
3427a3db 1851
f7fab165 1852 wv = xmalloc_widget_value ();
177c0ea7 1853 if (prev_wv)
4dedbfe0 1854 prev_wv->next = wv;
71dca3e3 1855 else
4dedbfe0 1856 save_wv->contents = wv;
71dca3e3 1857
2bf436c3 1858 wv->lname = item_name;
4dedbfe0 1859 if (!NILP (descrip))
2bf436c3 1860 wv->lkey = descrip;
4dedbfe0 1861 wv->value = 0;
01d5e892
RS
1862 /* The EMACS_INT cast avoids a warning. There's no problem
1863 as long as pointers have enough bits to hold small integers. */
1864 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
4dedbfe0 1865 wv->enabled = !NILP (enable);
177c0ea7 1866
3427a3db
GM
1867 if (NILP (type))
1868 wv->button_type = BUTTON_TYPE_NONE;
1869 else if (EQ (type, QCradio))
1870 wv->button_type = BUTTON_TYPE_RADIO;
1871 else if (EQ (type, QCtoggle))
1872 wv->button_type = BUTTON_TYPE_TOGGLE;
1873 else
1874 abort ();
1875
1876 wv->selected = !NILP (selected);
0b1f4572
RS
1877 if (! STRINGP (help))
1878 help = Qnil;
1879
1880 wv->help = help;
cc45fb40 1881
4dedbfe0
PR
1882 prev_wv = wv;
1883
1884 i += MENU_ITEMS_ITEM_LENGTH;
1885 }
1886 }
1887
71dca3e3
RS
1888 /* If we have just one "menu item"
1889 that was originally a button, return it by itself. */
1890 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1891 {
1892 wv = first_wv->contents;
1893 free_widget_value (first_wv);
1894 return wv;
1895 }
1896
4dedbfe0
PR
1897 return first_wv;
1898}
2bf436c3 1899
5560d460 1900/* Walk through the widget_value tree starting at FIRST_WV and update
2bf436c3
JD
1901 the char * pointers from the corresponding lisp values.
1902 We do this after building the whole tree, since GC may happen while the
1903 tree is constructed, and small strings are relocated. So we must wait
1904 until no GC can happen before storing pointers into lisp values. */
1905static void
1906update_submenu_strings (first_wv)
1907 widget_value *first_wv;
1908{
1909 widget_value *wv;
1910
1911 for (wv = first_wv; wv; wv = wv->next)
1912 {
1806714a 1913 if (STRINGP (wv->lname))
2bf436c3 1914 {
b79c8219 1915 wv->name = (char *) SDATA (wv->lname);
2bf436c3
JD
1916
1917 /* Ignore the @ that means "separate pane".
1918 This is a kludge, but this isn't worth more time. */
1919 if (wv->value == (char *)1)
1920 {
1921 if (wv->name[0] == '@')
1922 wv->name++;
1923 wv->value = 0;
1924 }
1925 }
1926
1806714a 1927 if (STRINGP (wv->lkey))
b79c8219 1928 wv->key = (char *) SDATA (wv->lkey);
2bf436c3
JD
1929
1930 if (wv->contents)
1931 update_submenu_strings (wv->contents);
1932 }
1933}
1934
f61a541b
GM
1935\f
1936/* Recompute all the widgets of frame F, when the menu bar has been
1937 changed. Value is non-zero if widgets were updated. */
1938
1939static int
6af6cbb5 1940update_frame_menubar (f)
18686d47
RS
1941 FRAME_PTR f;
1942{
488dd4c4
JD
1943#ifdef USE_GTK
1944 return xg_update_frame_menubar (f);
1945#else
62af879c 1946 struct x_output *x;
cffa74ea 1947 int columns, rows;
177c0ea7 1948
62af879c
KL
1949 if (! FRAME_X_P (f))
1950 abort ();
1951
1952 x = f->output_data.x;
1953
f61a541b
GM
1954 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
1955 return 0;
18686d47
RS
1956
1957 BLOCK_INPUT;
f61a541b
GM
1958 /* Save the size of the frame because the pane widget doesn't accept
1959 to resize itself. So force it. */
9882535b
KS
1960 columns = FRAME_COLS (f);
1961 rows = FRAME_LINES (f);
cffa74ea 1962
f61a541b
GM
1963 /* Do the voodoo which means "I'm changing lots of things, don't try
1964 to refigure sizes until I'm done." */
4dedbfe0 1965 lw_refigure_widget (x->column_widget, False);
cffa74ea 1966
f61a541b
GM
1967 /* The order in which children are managed is the top to bottom
1968 order in which they are displayed in the paned window. First,
1969 remove the text-area widget. */
18686d47
RS
1970 XtUnmanageChild (x->edit_widget);
1971
f61a541b
GM
1972 /* Remove the menubar that is there now, and put up the menubar that
1973 should be there. */
1974 XtManageChild (x->menubar_widget);
1975 XtMapWidget (x->menubar_widget);
1976 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
18686d47 1977
c98fcf4b 1978 /* Re-manage the text-area widget, and then thrash the sizes. */
18686d47 1979 XtManageChild (x->edit_widget);
4dedbfe0 1980 lw_refigure_widget (x->column_widget, True);
cffa74ea
FP
1981
1982 /* Force the pane widget to resize itself with the right values. */
1983 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
18686d47 1984 UNBLOCK_INPUT;
488dd4c4 1985#endif
f61a541b 1986 return 1;
18686d47
RS
1987}
1988
4bcdbab1
KH
1989/* Set the contents of the menubar widgets of frame F.
1990 The argument FIRST_TIME is currently ignored;
1991 it is set the first time this is called, from initialize_frame_menubar. */
1992
18686d47 1993void
88766961 1994set_frame_menubar (f, first_time, deep_p)
18686d47 1995 FRAME_PTR f;
706aa2f2 1996 int first_time;
88766961 1997 int deep_p;
18686d47 1998{
62af879c 1999 xt_or_gtk_widget menubar_widget;
488dd4c4
JD
2000#ifdef USE_X_TOOLKIT
2001 LWLIB_ID id;
2002#endif
faa935b6 2003 Lisp_Object items;
4d19cb8e 2004 widget_value *wv, *first_wv, *prev_wv = 0;
1806714a 2005 int i, last_i = 0;
ced89c24 2006 int *submenu_start, *submenu_end;
37dc84ff 2007 int *submenu_top_level_items, *submenu_n_panes;
cc45fb40 2008
62af879c
KL
2009 if (! FRAME_X_P (f))
2010 abort ();
2011
2012 menubar_widget = f->output_data.x->menubar_widget;
18686d47 2013
bfc524bc
RS
2014 XSETFRAME (Vmenu_updating_frame, f);
2015
488dd4c4 2016#ifdef USE_X_TOOLKIT
7556890b
RS
2017 if (f->output_data.x->id == 0)
2018 f->output_data.x->id = next_menubar_widget_id++;
2019 id = f->output_data.x->id;
488dd4c4 2020#endif
177c0ea7 2021
88766961
RS
2022 if (! menubar_widget)
2023 deep_p = 1;
745c34fb 2024 else if (pending_menu_activation && !deep_p)
a9be6839
RS
2025 deep_p = 1;
2026 /* Make the first call for any given frame always go deep. */
2027 else if (!f->output_data.x->saved_menu_event && !deep_p)
745c34fb
RS
2028 {
2029 deep_p = 1;
a9be6839
RS
2030 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
2031 f->output_data.x->saved_menu_event->type = 0;
745c34fb 2032 }
18686d47 2033
da18b5ac
JD
2034#ifdef USE_GTK
2035 /* If we have detached menus, we must update deep so detached menus
2036 also gets updated. */
2037 deep_p = deep_p || xg_have_tear_offs ();
2038#endif
2039
88766961 2040 if (deep_p)
18686d47 2041 {
88766961
RS
2042 /* Make a widget-value tree representing the entire menu trees. */
2043
2044 struct buffer *prev = current_buffer;
2045 Lisp_Object buffer;
aed13378 2046 int specpdl_count = SPECPDL_INDEX ();
88766961
RS
2047 int previous_menu_items_used = f->menu_bar_items_used;
2048 Lisp_Object *previous_items
2049 = (Lisp_Object *) alloca (previous_menu_items_used
2050 * sizeof (Lisp_Object));
2051
0b1cf399
RS
2052 /* If we are making a new widget, its contents are empty,
2053 do always reinitialize them. */
2054 if (! menubar_widget)
2055 previous_menu_items_used = 0;
2056
88766961
RS
2057 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
2058 specbind (Qinhibit_quit, Qt);
2059 /* Don't let the debugger step into this code
2060 because it is not reentrant. */
2061 specbind (Qdebug_on_next_call, Qnil);
2062
89f2614d 2063 record_unwind_save_match_data ();
86fad4ec 2064 record_unwind_protect (unuse_menu_items, Qnil);
88766961
RS
2065 if (NILP (Voverriding_local_map_menu_flag))
2066 {
2067 specbind (Qoverriding_terminal_local_map, Qnil);
2068 specbind (Qoverriding_local_map, Qnil);
2069 }
18686d47 2070
88766961 2071 set_buffer_internal_1 (XBUFFER (buffer));
18686d47 2072
88766961 2073 /* Run the Lucid hook. */
950eaee7 2074 safe_run_hooks (Qactivate_menubar_hook);
177c0ea7 2075
88766961
RS
2076 /* If it has changed current-menubar from previous value,
2077 really recompute the menubar from the value. */
2078 if (! NILP (Vlucid_menu_bar_dirty_flag))
2079 call0 (Qrecompute_lucid_menubar);
a57634d4 2080 safe_run_hooks (Qmenu_bar_update_hook);
88766961 2081 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
18686d47 2082
88766961 2083 items = FRAME_MENU_BAR_ITEMS (f);
8d8a3494 2084
88766961 2085 /* Save the frame's previous menu bar contents data. */
86c04183
GM
2086 if (previous_menu_items_used)
2087 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
2088 previous_menu_items_used * sizeof (Lisp_Object));
8d8a3494 2089
ced89c24
RS
2090 /* Fill in menu_items with the current menu bar contents.
2091 This can evaluate Lisp code. */
88766961 2092 menu_items = f->menu_bar_vector;
86c04183 2093 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
ced89c24
RS
2094 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2095 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
37dc84ff 2096 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
ced89c24
RS
2097 submenu_top_level_items
2098 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
88766961 2099 init_menu_items ();
f04366cb 2100 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
2101 {
2102 Lisp_Object key, string, maps;
2103
ced89c24
RS
2104 last_i = i;
2105
88766961
RS
2106 key = XVECTOR (items)->contents[i];
2107 string = XVECTOR (items)->contents[i + 1];
2108 maps = XVECTOR (items)->contents[i + 2];
2109 if (NILP (string))
2110 break;
2111
ced89c24
RS
2112 submenu_start[i] = menu_items_used;
2113
2114 menu_items_n_panes = 0;
2115 submenu_top_level_items[i]
2116 = parse_single_submenu (key, string, maps);
37dc84ff 2117 submenu_n_panes[i] = menu_items_n_panes;
ced89c24
RS
2118
2119 submenu_end[i] = menu_items_used;
2120 }
2121
2122 finish_menu_items ();
2123
2124 /* Convert menu_items into widget_value trees
2125 to display the menu. This cannot evaluate Lisp code. */
2126
2127 wv = xmalloc_widget_value ();
2128 wv->name = "menubar";
2129 wv->value = 0;
2130 wv->enabled = 1;
2131 wv->button_type = BUTTON_TYPE_NONE;
2132 wv->help = Qnil;
2133 first_wv = wv;
2134
2135 for (i = 0; i < last_i; i += 4)
2136 {
37dc84ff 2137 menu_items_n_panes = submenu_n_panes[i];
ced89c24
RS
2138 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
2139 submenu_top_level_items[i]);
177c0ea7 2140 if (prev_wv)
88766961
RS
2141 prev_wv->next = wv;
2142 else
2143 first_wv->contents = wv;
2144 /* Don't set wv->name here; GC during the loop might relocate it. */
2145 wv->enabled = 1;
3427a3db 2146 wv->button_type = BUTTON_TYPE_NONE;
88766961
RS
2147 prev_wv = wv;
2148 }
2149
88766961 2150 set_buffer_internal_1 (prev);
8d8a3494 2151 unbind_to (specpdl_count, Qnil);
8d8a3494 2152
88766961
RS
2153 /* If there has been no change in the Lisp-level contents
2154 of the menu bar, skip redisplaying it. Just exit. */
2155
2156 for (i = 0; i < previous_menu_items_used; i++)
2157 if (menu_items_used == i
99d3fac7 2158 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
88766961 2159 break;
62555c22 2160 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
88766961
RS
2161 {
2162 free_menubar_widget_value_tree (first_wv);
86fad4ec 2163 discard_menu_items ();
88766961
RS
2164
2165 return;
2166 }
2167
2168 /* Now GC cannot happen during the lifetime of the widget_value,
2169 so it's safe to store data from a Lisp_String. */
2170 wv = first_wv->contents;
f04366cb 2171 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
2172 {
2173 Lisp_Object string;
2174 string = XVECTOR (items)->contents[i + 1];
2175 if (NILP (string))
2bf436c3 2176 break;
ec867797 2177 wv->name = (char *) SDATA (string);
2bf436c3
JD
2178 update_submenu_strings (wv->contents);
2179 wv = wv->next;
88766961
RS
2180 }
2181
2182 f->menu_bar_vector = menu_items;
2183 f->menu_bar_items_used = menu_items_used;
86fad4ec 2184 discard_menu_items ();
4d19cb8e 2185 }
88766961
RS
2186 else
2187 {
2188 /* Make a widget-value tree containing
2189 just the top level menu bar strings. */
4d19cb8e 2190
ced89c24
RS
2191 wv = xmalloc_widget_value ();
2192 wv->name = "menubar";
2193 wv->value = 0;
2194 wv->enabled = 1;
2195 wv->button_type = BUTTON_TYPE_NONE;
2196 wv->help = Qnil;
2197 first_wv = wv;
2198
88766961 2199 items = FRAME_MENU_BAR_ITEMS (f);
f04366cb 2200 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
2201 {
2202 Lisp_Object string;
2203
2204 string = XVECTOR (items)->contents[i + 1];
2205 if (NILP (string))
2206 break;
2207
f7fab165 2208 wv = xmalloc_widget_value ();
d5db4077 2209 wv->name = (char *) SDATA (string);
88766961
RS
2210 wv->value = 0;
2211 wv->enabled = 1;
3427a3db 2212 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 2213 wv->help = Qnil;
fe8fa62f
RS
2214 /* This prevents lwlib from assuming this
2215 menu item is really supposed to be empty. */
2216 /* The EMACS_INT cast avoids a warning.
2217 This value just has to be different from small integers. */
2218 wv->call_data = (void *) (EMACS_INT) (-1);
88766961 2219
177c0ea7 2220 if (prev_wv)
88766961
RS
2221 prev_wv->next = wv;
2222 else
2223 first_wv->contents = wv;
2224 prev_wv = wv;
2225 }
62555c22
RS
2226
2227 /* Forget what we thought we knew about what is in the
2228 detailed contents of the menu bar menus.
2229 Changing the top level always destroys the contents. */
2230 f->menu_bar_items_used = 0;
88766961 2231 }
4dedbfe0 2232
88766961 2233 /* Create or update the menu bar widget. */
aa669def
RS
2234
2235 BLOCK_INPUT;
2236
488dd4c4
JD
2237#ifdef USE_GTK
2238 xg_crazy_callback_abort = 1;
2239 if (menubar_widget)
2240 {
da18b5ac 2241 /* The fourth arg is DEEP_P, which says to consider the entire
488dd4c4
JD
2242 menu trees we supply, rather than just the menu bar item names. */
2243 xg_modify_menubar_widgets (menubar_widget,
2244 f,
2245 first_wv,
2246 deep_p,
2247 G_CALLBACK (menubar_selection_callback),
2248 G_CALLBACK (popup_deactivate_callback),
2249 G_CALLBACK (menu_highlight_callback));
2250 }
2251 else
2252 {
2253 GtkWidget *wvbox = f->output_data.x->vbox_widget;
177c0ea7 2254
488dd4c4 2255 menubar_widget
177c0ea7 2256 = xg_create_widget ("menubar", "menubar", f, first_wv,
488dd4c4
JD
2257 G_CALLBACK (menubar_selection_callback),
2258 G_CALLBACK (popup_deactivate_callback),
2259 G_CALLBACK (menu_highlight_callback));
2260
2261 f->output_data.x->menubar_widget = menubar_widget;
2262 }
2263
177c0ea7 2264
488dd4c4 2265#else /* not USE_GTK */
18686d47 2266 if (menubar_widget)
4dedbfe0
PR
2267 {
2268 /* Disable resizing (done for Motif!) */
7556890b 2269 lw_allow_resizing (f->output_data.x->widget, False);
4dedbfe0
PR
2270
2271 /* The third arg is DEEP_P, which says to consider the entire
2272 menu trees we supply, rather than just the menu bar item names. */
88766961 2273 lw_modify_all_widgets (id, first_wv, deep_p);
4dedbfe0 2274
c98fcf4b 2275 /* Re-enable the edit widget to resize. */
7556890b 2276 lw_allow_resizing (f->output_data.x->widget, True);
4dedbfe0 2277 }
18686d47
RS
2278 else
2279 {
9f6fcdc5
JD
2280 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2281 XtTranslations override = XtParseTranslationTable (menuOverride);
2282
177c0ea7 2283 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
7556890b 2284 f->output_data.x->column_widget,
4dedbfe0
PR
2285 0,
2286 popup_activate_callback,
2287 menubar_selection_callback,
850df50b
GM
2288 popup_deactivate_callback,
2289 menu_highlight_callback);
7556890b 2290 f->output_data.x->menubar_widget = menubar_widget;
9f6fcdc5
JD
2291
2292 /* Make menu pop down on C-g. */
2293 XtOverrideTranslations (menubar_widget, override);
18686d47 2294 }
1d1c1567
KH
2295
2296 {
177c0ea7 2297 int menubar_size
7556890b
RS
2298 = (f->output_data.x->menubar_widget
2299 ? (f->output_data.x->menubar_widget->core.height
2300 + f->output_data.x->menubar_widget->core.border_width)
1d1c1567
KH
2301 : 0);
2302
f42aa681
RS
2303#if 0 /* Experimentally, we now get the right results
2304 for -geometry -0-0 without this. 24 Aug 96, rms. */
5ee80bd3 2305#ifdef USE_LUCID
1d1c1567
KH
2306 if (FRAME_EXTERNAL_MENU_BAR (f))
2307 {
2308 Dimension ibw = 0;
7556890b 2309 XtVaGetValues (f->output_data.x->column_widget,
1d1c1567
KH
2310 XtNinternalBorderWidth, &ibw, NULL);
2311 menubar_size += ibw;
2312 }
5ee80bd3 2313#endif /* USE_LUCID */
f42aa681 2314#endif /* 0 */
1d1c1567 2315
7556890b 2316 f->output_data.x->menubar_height = menubar_size;
1d1c1567 2317 }
488dd4c4 2318#endif /* not USE_GTK */
177c0ea7 2319
18686d47 2320 free_menubar_widget_value_tree (first_wv);
364cd450 2321 update_frame_menubar (f);
18686d47 2322
488dd4c4
JD
2323#ifdef USE_GTK
2324 xg_crazy_callback_abort = 0;
2325#endif
2326
18686d47
RS
2327 UNBLOCK_INPUT;
2328}
85f487d1 2329
8e6208c5 2330/* Called from Fx_create_frame to create the initial menubar of a frame
4dedbfe0
PR
2331 before it is mapped, so that the window is mapped with the menubar already
2332 there instead of us tacking it on later and thrashing the window after it
2333 is visible. */
2334
2335void
2336initialize_frame_menubar (f)
2337 FRAME_PTR f;
2338{
2339 /* This function is called before the first chance to redisplay
2340 the frame. It has to be, so the frame will have the right size. */
2341 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
88766961 2342 set_frame_menubar (f, 1, 1);
4dedbfe0
PR
2343}
2344
aba25348 2345
4dedbfe0 2346/* Get rid of the menu bar of frame F, and free its storage.
488dd4c4
JD
2347 This is used when deleting a frame, and when turning off the menu bar.
2348 For GTK this function is in gtkutil.c. */
4dedbfe0 2349
488dd4c4 2350#ifndef USE_GTK
85f487d1
FP
2351void
2352free_frame_menubar (f)
2353 FRAME_PTR f;
2354{
2355 Widget menubar_widget;
85f487d1 2356
62af879c
KL
2357 if (! FRAME_X_P (f))
2358 abort ();
2359
7556890b 2360 menubar_widget = f->output_data.x->menubar_widget;
a45bad2a
RS
2361
2362 f->output_data.x->menubar_height = 0;
177c0ea7 2363
85f487d1
FP
2364 if (menubar_widget)
2365 {
aba25348
GM
2366#ifdef USE_MOTIF
2367 /* Removing the menu bar magically changes the shell widget's x
2368 and y position of (0, 0) which, when the menu bar is turned
2369 on again, leads to pull-down menuss appearing in strange
2370 positions near the upper-left corner of the display. This
2371 happens only with some window managers like twm and ctwm,
2372 but not with other like Motif's mwm or kwm, because the
2373 latter generate ConfigureNotify events when the menu bar
2374 is switched off, which fixes the shell position. */
2375 Position x0, y0, x1, y1;
2376#endif
177c0ea7 2377
85f487d1 2378 BLOCK_INPUT;
aba25348
GM
2379
2380#ifdef USE_MOTIF
ae556422
GM
2381 if (f->output_data.x->widget)
2382 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
aba25348 2383#endif
177c0ea7 2384
7556890b 2385 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
0c05dcd3 2386 f->output_data.x->menubar_widget = NULL;
aba25348
GM
2387
2388#ifdef USE_MOTIF
ae556422
GM
2389 if (f->output_data.x->widget)
2390 {
2391 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2392 if (x1 == 0 && y1 == 0)
2393 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2394 }
aba25348 2395#endif
177c0ea7 2396
85f487d1
FP
2397 UNBLOCK_INPUT;
2398 }
2399}
488dd4c4 2400#endif /* not USE_GTK */
78589e07 2401
488dd4c4 2402#endif /* USE_X_TOOLKIT || USE_GTK */
78589e07
RS
2403\f
2404/* xmenu_show actually displays a menu using the panes and items in menu_items
2405 and returns the value selected from it.
2406 There are two versions of xmenu_show, one for Xt and one for Xlib.
2407 Both assume input is blocked by the caller. */
2408
2409/* F is the frame the menu is for.
2410 X and Y are the frame-relative specified position,
2411 relative to the inside upper left corner of the frame F.
c8b5aa3d 2412 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
78589e07
RS
2413 KEYMAPS is 1 if this menu was specified with keymaps;
2414 in that case, we return a list containing the chosen item's value
2415 and perhaps also the pane's prefix.
2416 TITLE is the specified menu title.
2417 ERROR is a place to store an error message string in case of failure.
2418 (We return nil on failure, but the value doesn't actually matter.) */
18686d47 2419
488dd4c4
JD
2420#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2421
2422/* The item selected in the popup menu. */
2423static Lisp_Object *volatile menu_item_selection;
2424
2425#ifdef USE_GTK
2426
2427/* Used when position a popup menu. See menu_position_func and
2428 create_and_show_popup_menu below. */
2429struct next_popup_x_y
2430{
7b76ca1c 2431 FRAME_PTR f;
488dd4c4
JD
2432 int x;
2433 int y;
2434};
2435
2436/* The menu position function to use if we are not putting a popup
2437 menu where the pointer is.
2438 MENU is the menu to pop up.
2439 X and Y shall on exit contain x/y where the menu shall pop up.
2440 PUSH_IN is not documented in the GTK manual.
2441 USER_DATA is any data passed in when calling gtk_menu_popup.
2442 Here it points to a struct next_popup_x_y where the coordinates
7b76ca1c 2443 to store in *X and *Y are as well as the frame for the popup.
488dd4c4
JD
2444
2445 Here only X and Y are used. */
2446static void
2447menu_position_func (menu, x, y, push_in, user_data)
2448 GtkMenu *menu;
2449 gint *x;
2450 gint *y;
2451 gboolean *push_in;
2452 gpointer user_data;
2453{
7b76ca1c
JD
2454 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2455 GtkRequisition req;
2456 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2457 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
b9de078a 2458
7b76ca1c
JD
2459 *x = data->x;
2460 *y = data->y;
2461
2462 /* Check if there is room for the menu. If not, adjust x/y so that
2463 the menu is fully visible. */
2464 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2465 if (data->x + req.width > disp_width)
2466 *x -= data->x + req.width - disp_width;
2467 if (data->y + req.height > disp_height)
2468 *y -= data->y + req.height - disp_height;
488dd4c4
JD
2469}
2470
2471static void
2472popup_selection_callback (widget, client_data)
2473 GtkWidget *widget;
2474 gpointer client_data;
2475{
2476 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2477
2478 if (xg_crazy_callback_abort) return;
2479 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2480}
2481
af89e871 2482static Lisp_Object
a130b901
JD
2483pop_down_menu (arg)
2484 Lisp_Object arg;
af89e871 2485{
a130b901
JD
2486 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
2487
af89e871 2488 popup_activated_flag = 0;
a130b901
JD
2489 BLOCK_INPUT;
2490 gtk_widget_destroy (GTK_WIDGET (p->pointer));
2491 UNBLOCK_INPUT;
af89e871
JD
2492 return Qnil;
2493}
2494
488dd4c4
JD
2495/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2496 menu pops down.
2497 menu_item_selection will be set to the selection. */
2498static void
2499create_and_show_popup_menu (f, first_wv, x, y, for_click)
2500 FRAME_PTR f;
2501 widget_value *first_wv;
2502 int x;
2503 int y;
2504 int for_click;
2505{
2506 int i;
2507 GtkWidget *menu;
2508 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2509 struct next_popup_x_y popup_x_y;
af89e871 2510 int specpdl_count = SPECPDL_INDEX ();
488dd4c4 2511
62af879c
KL
2512 if (! FRAME_X_P (f))
2513 abort ();
2514
488dd4c4
JD
2515 xg_crazy_callback_abort = 1;
2516 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2517 G_CALLBACK (popup_selection_callback),
2518 G_CALLBACK (popup_deactivate_callback),
2519 G_CALLBACK (menu_highlight_callback));
2520 xg_crazy_callback_abort = 0;
177c0ea7 2521
488dd4c4
JD
2522 if (! for_click)
2523 {
2524 /* Not invoked by a click. pop up at x/y. */
2525 pos_func = menu_position_func;
2526
2527 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
2528 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2529 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
488dd4c4
JD
2530
2531 popup_x_y.x = x;
2532 popup_x_y.y = y;
7b76ca1c 2533 popup_x_y.f = f;
488dd4c4 2534
9b85e63d
JD
2535 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2536 }
2537 else
2538 {
2539 for (i = 0; i < 5; i++)
2540 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2541 break;
2542 }
59cfb104 2543
488dd4c4
JD
2544 /* Display the menu. */
2545 gtk_widget_show_all (menu);
2546 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
177c0ea7 2547
a130b901 2548 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
af89e871 2549
ff18668f
JD
2550 if (GTK_WIDGET_MAPPED (menu))
2551 {
2552 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2553 two. show_help_echo uses this to detect popup menus. */
2554 popup_activated_flag = 1;
2555 /* Process events that apply to the menu. */
2556 popup_widget_loop (1, menu);
2557 }
488dd4c4 2558
af89e871 2559 unbind_to (specpdl_count, Qnil);
177c0ea7 2560
488dd4c4
JD
2561 /* Must reset this manually because the button release event is not passed
2562 to Emacs event loop. */
2563 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2564}
2565
2566#else /* not USE_GTK */
18686d47 2567
8ed87156 2568/* We need a unique id for each widget handled by the Lucid Widget
cc17e9bf
KH
2569 library.
2570
2571 For the main windows, and popup menus, we use this counter,
88766961 2572 which we increment each time after use. This starts from 1<<16.
cc17e9bf 2573
88766961
RS
2574 For menu bars, we use numbers starting at 0, counted in
2575 next_menubar_widget_id. */
8ed87156 2576LWLIB_ID widget_id_tick;
165e1749 2577
4dedbfe0
PR
2578static void
2579popup_selection_callback (widget, id, client_data)
2580 Widget widget;
2581 LWLIB_ID id;
2582 XtPointer client_data;
2583{
2584 menu_item_selection = (Lisp_Object *) client_data;
2585}
2586
af89e871
JD
2587/* ARG is the LWLIB ID of the dialog box, represented
2588 as a Lisp object as (HIGHPART . LOWPART). */
2589
2590static Lisp_Object
2591pop_down_menu (arg)
2592 Lisp_Object arg;
2593{
2594 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2595 | XINT (XCDR (arg)));
2596
2597 BLOCK_INPUT;
2598 lw_destroy_all_widgets (id);
2599 UNBLOCK_INPUT;
2600 popup_activated_flag = 0;
2601
2602 return Qnil;
2603}
2604
488dd4c4
JD
2605/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2606 menu pops down.
2607 menu_item_selection will be set to the selection. */
2608static void
2609create_and_show_popup_menu (f, first_wv, x, y, for_click)
2610 FRAME_PTR f;
2611 widget_value *first_wv;
2612 int x;
2613 int y;
2614 int for_click;
2615{
2616 int i;
2617 Arg av[2];
2618 int ac = 0;
2619 XButtonPressedEvent dummy;
2620 LWLIB_ID menu_id;
2621 Widget menu;
488dd4c4 2622
62af879c
KL
2623 if (! FRAME_X_P (f))
2624 abort ();
2625
488dd4c4
JD
2626 menu_id = widget_id_tick++;
2627 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2628 f->output_data.x->widget, 1, 0,
2629 popup_selection_callback,
2630 popup_deactivate_callback,
2631 menu_highlight_callback);
2632
2633 dummy.type = ButtonPress;
2634 dummy.serial = 0;
2635 dummy.send_event = 0;
2636 dummy.display = FRAME_X_DISPLAY (f);
2637 dummy.time = CurrentTime;
2638 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2639 dummy.window = dummy.root;
2640 dummy.subwindow = dummy.root;
2641 dummy.x = x;
2642 dummy.y = y;
2643
2644 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
2645 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2646 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
488dd4c4
JD
2647
2648 dummy.x_root = x;
2649 dummy.y_root = y;
2650
2651 dummy.state = 0;
2652 dummy.button = 0;
2653 for (i = 0; i < 5; i++)
2654 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2655 dummy.button = i;
2656
2657 /* Don't allow any geometry request from the user. */
2658 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2659 XtSetValues (menu, av, ac);
2660
2661 /* Display the menu. */
2662 lw_popup_menu (menu, (XEvent *) &dummy);
2663 popup_activated_flag = 1;
59cfb104 2664
af89e871
JD
2665 {
2666 int fact = 4 * sizeof (LWLIB_ID);
2667 int specpdl_count = SPECPDL_INDEX ();
2668 record_unwind_protect (pop_down_menu,
2669 Fcons (make_number (menu_id >> (fact)),
2670 make_number (menu_id & ~(-1 << (fact)))));
488dd4c4 2671
af89e871 2672 /* Process events that apply to the menu. */
95bdef2e 2673 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
488dd4c4 2674
af89e871
JD
2675 unbind_to (specpdl_count, Qnil);
2676 }
488dd4c4
JD
2677}
2678
2679#endif /* not USE_GTK */
2680
78589e07 2681static Lisp_Object
673a6211 2682xmenu_show (f, x, y, for_click, keymaps, title, error)
18686d47 2683 FRAME_PTR f;
18686d47
RS
2684 int x;
2685 int y;
9685a93f 2686 int for_click;
78589e07
RS
2687 int keymaps;
2688 Lisp_Object title;
2689 char **error;
18686d47 2690{
78589e07 2691 int i;
78589e07 2692 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
101bb4a5
RS
2693 widget_value **submenu_stack
2694 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2695 Lisp_Object *subprefix_stack
2696 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2697 int submenu_depth = 0;
4e8d3549 2698
78c8278d
RS
2699 int first_pane;
2700
62af879c
KL
2701 if (! FRAME_X_P (f))
2702 abort ();
2703
78589e07
RS
2704 *error = NULL;
2705
742f715d
KH
2706 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2707 {
2708 *error = "Empty menu";
2709 return Qnil;
2710 }
63c414df 2711
78589e07
RS
2712 /* Create a tree of widget_value objects
2713 representing the panes and their items. */
f7fab165 2714 wv = xmalloc_widget_value ();
78589e07
RS
2715 wv->name = "menu";
2716 wv->value = 0;
2717 wv->enabled = 1;
3427a3db 2718 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 2719 wv->help =Qnil;
78589e07 2720 first_wv = wv;
78c8278d 2721 first_pane = 1;
177c0ea7 2722
78589e07
RS
2723 /* Loop over all panes and items, filling in the tree. */
2724 i = 0;
2725 while (i < menu_items_used)
2726 {
101bb4a5
RS
2727 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2728 {
2729 submenu_stack[submenu_depth++] = save_wv;
2730 save_wv = prev_wv;
2731 prev_wv = 0;
78c8278d 2732 first_pane = 1;
101bb4a5
RS
2733 i++;
2734 }
2735 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2736 {
2737 prev_wv = save_wv;
2738 save_wv = submenu_stack[--submenu_depth];
78c8278d 2739 first_pane = 0;
101bb4a5
RS
2740 i++;
2741 }
2742 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2743 && submenu_depth != 0)
2744 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
2745 /* Ignore a nil in the item list.
2746 It's meaningful only for dialog boxes. */
2747 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2748 i += 1;
101bb4a5 2749 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2750 {
2751 /* Create a new pane. */
2752 Lisp_Object pane_name, prefix;
2753 char *pane_string;
177c0ea7 2754
4c329aa8
GM
2755 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2756 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
177c0ea7 2757
703dc2a8 2758#ifndef HAVE_MULTILINGUAL_MENU
4c329aa8
GM
2759 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2760 {
8af9fa55 2761 pane_name = ENCODE_MENU_STRING (pane_name);
4c329aa8
GM
2762 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2763 }
703dc2a8 2764#endif
78589e07 2765 pane_string = (NILP (pane_name)
d5db4077 2766 ? "" : (char *) SDATA (pane_name));
101bb4a5 2767 /* If there is just one top-level pane, put all its items directly
78589e07
RS
2768 under the top-level menu. */
2769 if (menu_items_n_panes == 1)
2770 pane_string = "";
2771
2772 /* If the pane has a meaningful name,
2773 make the pane a top-level menu item
2774 with its items as a submenu beneath it. */
78c8278d 2775 if (!keymaps && strcmp (pane_string, ""))
78589e07 2776 {
f7fab165 2777 wv = xmalloc_widget_value ();
78589e07
RS
2778 if (save_wv)
2779 save_wv->next = wv;
2780 else
2781 first_wv->contents = wv;
2782 wv->name = pane_string;
2783 if (keymaps && !NILP (prefix))
2784 wv->name++;
2785 wv->value = 0;
2786 wv->enabled = 1;
3427a3db 2787 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 2788 wv->help = Qnil;
78c8278d
RS
2789 save_wv = wv;
2790 prev_wv = 0;
78589e07 2791 }
78c8278d
RS
2792 else if (first_pane)
2793 {
2794 save_wv = wv;
2795 prev_wv = 0;
2796 }
2797 first_pane = 0;
78589e07
RS
2798 i += MENU_ITEMS_PANE_LENGTH;
2799 }
2800 else
2801 {
2802 /* Create a new item within current pane. */
9cd50434 2803 Lisp_Object item_name, enable, descrip, def, type, selected, help;
4c329aa8
GM
2804 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2805 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2806 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2807 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2808 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2809 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2810 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3427a3db 2811
703dc2a8 2812#ifndef HAVE_MULTILINGUAL_MENU
3427a3db 2813 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
4c329aa8 2814 {
646f98ec 2815 item_name = ENCODE_MENU_STRING (item_name);
4c329aa8
GM
2816 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2817 }
177c0ea7 2818
3427a3db 2819 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
4c329aa8 2820 {
646f98ec 2821 descrip = ENCODE_MENU_STRING (descrip);
4c329aa8
GM
2822 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2823 }
2824#endif /* not HAVE_MULTILINGUAL_MENU */
177c0ea7 2825
f7fab165 2826 wv = xmalloc_widget_value ();
177c0ea7 2827 if (prev_wv)
78589e07 2828 prev_wv->next = wv;
177c0ea7 2829 else
78589e07 2830 save_wv->contents = wv;
d5db4077 2831 wv->name = (char *) SDATA (item_name);
78589e07 2832 if (!NILP (descrip))
d5db4077 2833 wv->key = (char *) SDATA (descrip);
78589e07 2834 wv->value = 0;
a352a815
RS
2835 /* If this item has a null value,
2836 make the call_data null so that it won't display a box
2837 when the mouse is on it. */
2838 wv->call_data
2839 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
78589e07 2840 wv->enabled = !NILP (enable);
3427a3db
GM
2841
2842 if (NILP (type))
2843 wv->button_type = BUTTON_TYPE_NONE;
2844 else if (EQ (type, QCtoggle))
2845 wv->button_type = BUTTON_TYPE_TOGGLE;
2846 else if (EQ (type, QCradio))
2847 wv->button_type = BUTTON_TYPE_RADIO;
2848 else
2849 abort ();
2850
2851 wv->selected = !NILP (selected);
cc45fb40 2852
0b1f4572
RS
2853 if (! STRINGP (help))
2854 help = Qnil;
2855
2856 wv->help = help;
cc45fb40 2857
78589e07
RS
2858 prev_wv = wv;
2859
2860 i += MENU_ITEMS_ITEM_LENGTH;
2861 }
2862 }
2863
c98fcf4b 2864 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
2865 if (!NILP (title))
2866 {
f7fab165
RS
2867 widget_value *wv_title = xmalloc_widget_value ();
2868 widget_value *wv_sep1 = xmalloc_widget_value ();
2869 widget_value *wv_sep2 = xmalloc_widget_value ();
4dedbfe0
PR
2870
2871 wv_sep2->name = "--";
2872 wv_sep2->next = first_wv->contents;
27ad7b52 2873 wv_sep2->help = Qnil;
4dedbfe0
PR
2874
2875 wv_sep1->name = "--";
2876 wv_sep1->next = wv_sep2;
27ad7b52 2877 wv_sep1->help = Qnil;
4dedbfe0 2878
703dc2a8
KH
2879#ifndef HAVE_MULTILINGUAL_MENU
2880 if (STRING_MULTIBYTE (title))
646f98ec 2881 title = ENCODE_MENU_STRING (title);
703dc2a8 2882#endif
177c0ea7 2883
d5db4077 2884 wv_title->name = (char *) SDATA (title);
cc45fb40 2885 wv_title->enabled = TRUE;
3427a3db 2886 wv_title->button_type = BUTTON_TYPE_NONE;
4dedbfe0 2887 wv_title->next = wv_sep1;
27ad7b52 2888 wv_title->help = Qnil;
4dedbfe0
PR
2889 first_wv->contents = wv_title;
2890 }
2891
78589e07
RS
2892 /* No selection has been chosen yet. */
2893 menu_item_selection = 0;
2894
488dd4c4
JD
2895 /* Actually create and show the menu until popped down. */
2896 create_and_show_popup_menu (f, first_wv, x, y, for_click);
18686d47 2897
488dd4c4
JD
2898 /* Free the widget_value objects we used to specify the contents. */
2899 free_menubar_widget_value_tree (first_wv);
18686d47 2900
78589e07
RS
2901 /* Find the selected item, and its pane, to return
2902 the proper value. */
2903 if (menu_item_selection != 0)
2904 {
c63f6952 2905 Lisp_Object prefix, entry;
78589e07 2906
6bbd7a29 2907 prefix = entry = Qnil;
78589e07
RS
2908 i = 0;
2909 while (i < menu_items_used)
2910 {
101bb4a5
RS
2911 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2912 {
2913 subprefix_stack[submenu_depth++] = prefix;
2914 prefix = entry;
2915 i++;
2916 }
2917 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2918 {
2919 prefix = subprefix_stack[--submenu_depth];
2920 i++;
2921 }
2922 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2923 {
2924 prefix
2925 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2926 i += MENU_ITEMS_PANE_LENGTH;
2927 }
d31d42cc
RS
2928 /* Ignore a nil in the item list.
2929 It's meaningful only for dialog boxes. */
2930 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2931 i += 1;
78589e07
RS
2932 else
2933 {
2934 entry
2935 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2936 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2937 {
2938 if (keymaps != 0)
2939 {
101bb4a5
RS
2940 int j;
2941
78589e07
RS
2942 entry = Fcons (entry, Qnil);
2943 if (!NILP (prefix))
2944 entry = Fcons (prefix, entry);
101bb4a5 2945 for (j = submenu_depth - 1; j >= 0; j--)
e48087b7 2946 if (!NILP (subprefix_stack[j]))
5964e450 2947 entry = Fcons (subprefix_stack[j], entry);
78589e07
RS
2948 }
2949 return entry;
2950 }
2951 i += MENU_ITEMS_ITEM_LENGTH;
2952 }
2953 }
2954 }
be6ed24a
RS
2955 else if (!for_click)
2956 /* Make "Cancel" equivalent to C-g. */
2957 Fsignal (Qquit, Qnil);
78589e07
RS
2958
2959 return Qnil;
18686d47 2960}
4dedbfe0 2961\f
488dd4c4
JD
2962#ifdef USE_GTK
2963static void
2964dialog_selection_callback (widget, client_data)
2965 GtkWidget *widget;
2966 gpointer client_data;
2967{
2968 /* The EMACS_INT cast avoids a warning. There's no problem
2969 as long as pointers have enough bits to hold small integers. */
2970 if ((int) (EMACS_INT) client_data != -1)
2971 menu_item_selection = (Lisp_Object *) client_data;
2972
2973 popup_activated_flag = 0;
2974}
2975
2976/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2977 dialog pops down.
2978 menu_item_selection will be set to the selection. */
2979static void
2980create_and_show_dialog (f, first_wv)
2981 FRAME_PTR f;
2982 widget_value *first_wv;
2983{
2984 GtkWidget *menu;
2985
62af879c
KL
2986 if (! FRAME_X_P (f))
2987 abort ();
2988
488dd4c4
JD
2989 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
2990 G_CALLBACK (dialog_selection_callback),
2991 G_CALLBACK (popup_deactivate_callback),
2992 0);
2993
2994 if (menu)
2995 {
af89e871 2996 int specpdl_count = SPECPDL_INDEX ();
a130b901 2997 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
af89e871 2998
488dd4c4
JD
2999 /* Display the menu. */
3000 gtk_widget_show_all (menu);
3001
3002 /* Process events that apply to the menu. */
f1d1cd24 3003 popup_widget_loop (1, menu);
177c0ea7 3004
af89e871 3005 unbind_to (specpdl_count, Qnil);
488dd4c4
JD
3006 }
3007}
3008
3009#else /* not USE_GTK */
4dedbfe0
PR
3010static void
3011dialog_selection_callback (widget, id, client_data)
3012 Widget widget;
3013 LWLIB_ID id;
3014 XtPointer client_data;
3015{
01d5e892
RS
3016 /* The EMACS_INT cast avoids a warning. There's no problem
3017 as long as pointers have enough bits to hold small integers. */
3018 if ((int) (EMACS_INT) client_data != -1)
4dedbfe0 3019 menu_item_selection = (Lisp_Object *) client_data;
488dd4c4 3020
4dedbfe0
PR
3021 BLOCK_INPUT;
3022 lw_destroy_all_widgets (id);
3023 UNBLOCK_INPUT;
9572375b 3024 popup_activated_flag = 0;
4dedbfe0 3025}
18686d47 3026
488dd4c4 3027
488dd4c4
JD
3028/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3029 dialog pops down.
3030 menu_item_selection will be set to the selection. */
3031static void
3032create_and_show_dialog (f, first_wv)
3033 FRAME_PTR f;
3034 widget_value *first_wv;
3035{
3036 LWLIB_ID dialog_id;
3037
62af879c
KL
3038 if (!FRAME_X_P (f))
3039 abort();
3040
488dd4c4
JD
3041 dialog_id = widget_id_tick++;
3042 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
3043 f->output_data.x->widget, 1, 0,
3044 dialog_selection_callback, 0, 0);
3045 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
3046
3047 /* Display the dialog box. */
3048 lw_pop_up_all_widgets (dialog_id);
3049 popup_activated_flag = 1;
3050
3051 /* Process events that apply to the dialog box.
3052 Also handle timers. */
3053 {
3054 int count = SPECPDL_INDEX ();
3055 int fact = 4 * sizeof (LWLIB_ID);
177c0ea7 3056
488dd4c4 3057 /* xdialog_show_unwind is responsible for popping the dialog box down. */
af89e871 3058 record_unwind_protect (pop_down_menu,
488dd4c4
JD
3059 Fcons (make_number (dialog_id >> (fact)),
3060 make_number (dialog_id & ~(-1 << (fact)))));
3061
df470e3b 3062 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
95bdef2e 3063 dialog_id, 1);
488dd4c4
JD
3064
3065 unbind_to (count, Qnil);
3066 }
3067}
3068
3069#endif /* not USE_GTK */
3070
165e1749
FP
3071static char * button_names [] = {
3072 "button1", "button2", "button3", "button4", "button5",
3073 "button6", "button7", "button8", "button9", "button10" };
3074
3075static Lisp_Object
6a040d6a 3076xdialog_show (f, keymaps, title, header, error_name)
165e1749 3077 FRAME_PTR f;
165e1749 3078 int keymaps;
6a040d6a
NR
3079 Lisp_Object title, header;
3080 char **error_name;
165e1749
FP
3081{
3082 int i, nb_buttons=0;
80670155 3083 char dialog_name[6];
165e1749 3084
faa935b6 3085 widget_value *wv, *first_wv = 0, *prev_wv = 0;
165e1749 3086
fcaa7665
RS
3087 /* Number of elements seen so far, before boundary. */
3088 int left_count = 0;
3089 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3090 int boundary_seen = 0;
3091
62af879c
KL
3092 if (! FRAME_X_P (f))
3093 abort ();
3094
6a040d6a 3095 *error_name = NULL;
165e1749 3096
80670155
RS
3097 if (menu_items_n_panes > 1)
3098 {
6a040d6a 3099 *error_name = "Multiple panes in dialog box";
80670155
RS
3100 return Qnil;
3101 }
3102
165e1749
FP
3103 /* Create a tree of widget_value objects
3104 representing the text label and buttons. */
3105 {
3106 Lisp_Object pane_name, prefix;
3107 char *pane_string;
3108 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
3109 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
3110 pane_string = (NILP (pane_name)
177c0ea7 3111 ? "" : (char *) SDATA (pane_name));
f7fab165 3112 prev_wv = xmalloc_widget_value ();
165e1749
FP
3113 prev_wv->value = pane_string;
3114 if (keymaps && !NILP (prefix))
3115 prev_wv->name++;
3116 prev_wv->enabled = 1;
3117 prev_wv->name = "message";
27ad7b52 3118 prev_wv->help = Qnil;
165e1749 3119 first_wv = prev_wv;
177c0ea7 3120
165e1749
FP
3121 /* Loop over all panes and items, filling in the tree. */
3122 i = MENU_ITEMS_PANE_LENGTH;
3123 while (i < menu_items_used)
3124 {
177c0ea7 3125
165e1749
FP
3126 /* Create a new item within current pane. */
3127 Lisp_Object item_name, enable, descrip;
3128 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3129 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3130 descrip
3131 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
177c0ea7 3132
80670155
RS
3133 if (NILP (item_name))
3134 {
3135 free_menubar_widget_value_tree (first_wv);
6a040d6a 3136 *error_name = "Submenu in dialog items";
80670155
RS
3137 return Qnil;
3138 }
fcaa7665
RS
3139 if (EQ (item_name, Qquote))
3140 {
3141 /* This is the boundary between left-side elts
3142 and right-side elts. Stop incrementing right_count. */
3143 boundary_seen = 1;
3144 i++;
3145 continue;
3146 }
86e71abf 3147 if (nb_buttons >= 9)
80670155
RS
3148 {
3149 free_menubar_widget_value_tree (first_wv);
6a040d6a 3150 *error_name = "Too many dialog items";
80670155
RS
3151 return Qnil;
3152 }
3153
f7fab165 3154 wv = xmalloc_widget_value ();
165e1749 3155 prev_wv->next = wv;
80670155 3156 wv->name = (char *) button_names[nb_buttons];
165e1749 3157 if (!NILP (descrip))
d5db4077
KR
3158 wv->key = (char *) SDATA (descrip);
3159 wv->value = (char *) SDATA (item_name);
165e1749
FP
3160 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
3161 wv->enabled = !NILP (enable);
27ad7b52 3162 wv->help = Qnil;
165e1749
FP
3163 prev_wv = wv;
3164
fcaa7665
RS
3165 if (! boundary_seen)
3166 left_count++;
3167
165e1749
FP
3168 nb_buttons++;
3169 i += MENU_ITEMS_ITEM_LENGTH;
3170 }
3171
fcaa7665
RS
3172 /* If the boundary was not specified,
3173 by default put half on the left and half on the right. */
3174 if (! boundary_seen)
3175 left_count = nb_buttons - nb_buttons / 2;
3176
f7fab165 3177 wv = xmalloc_widget_value ();
80670155 3178 wv->name = dialog_name;
27ad7b52 3179 wv->help = Qnil;
6a040d6a
NR
3180
3181 /* Frame title: 'Q' = Question, 'I' = Information.
3182 Can also have 'E' = Error if, one day, we want
3183 a popup for errors. */
3184 if (NILP(header))
3185 dialog_name[0] = 'Q';
3186 else
3187 dialog_name[0] = 'I';
3188
80670155
RS
3189 /* Dialog boxes use a really stupid name encoding
3190 which specifies how many buttons to use
6a040d6a 3191 and how many buttons are on the right. */
80670155
RS
3192 dialog_name[1] = '0' + nb_buttons;
3193 dialog_name[2] = 'B';
3194 dialog_name[3] = 'R';
fcaa7665
RS
3195 /* Number of buttons to put on the right. */
3196 dialog_name[4] = '0' + nb_buttons - left_count;
80670155 3197 dialog_name[5] = 0;
165e1749
FP
3198 wv->contents = first_wv;
3199 first_wv = wv;
165e1749
FP
3200 }
3201
165e1749
FP
3202 /* No selection has been chosen yet. */
3203 menu_item_selection = 0;
3204
488dd4c4
JD
3205 /* Actually create and show the dialog. */
3206 create_and_show_dialog (f, first_wv);
f02cac82 3207
488dd4c4
JD
3208 /* Free the widget_value objects we used to specify the contents. */
3209 free_menubar_widget_value_tree (first_wv);
177c0ea7 3210
488dd4c4
JD
3211 /* Find the selected item, and its pane, to return
3212 the proper value. */
165e1749
FP
3213 if (menu_item_selection != 0)
3214 {
3215 Lisp_Object prefix;
3216
3217 prefix = Qnil;
3218 i = 0;
3219 while (i < menu_items_used)
3220 {
3221 Lisp_Object entry;
3222
3223 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3224 {
3225 prefix
3226 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3227 i += MENU_ITEMS_PANE_LENGTH;
3228 }
85996cfb
GM
3229 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3230 {
3231 /* This is the boundary between left-side elts and
3232 right-side elts. */
3233 ++i;
3234 }
165e1749
FP
3235 else
3236 {
3237 entry
3238 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3239 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3240 {
3241 if (keymaps != 0)
3242 {
3243 entry = Fcons (entry, Qnil);
3244 if (!NILP (prefix))
3245 entry = Fcons (prefix, entry);
3246 }
3247 return entry;
3248 }
3249 i += MENU_ITEMS_ITEM_LENGTH;
3250 }
3251 }
3252 }
9f6fcdc5
JD
3253 else
3254 /* Make "Cancel" equivalent to C-g. */
3255 Fsignal (Qquit, Qnil);
165e1749
FP
3256
3257 return Qnil;
3258}
ba461919 3259
488dd4c4 3260#else /* not USE_X_TOOLKIT && not USE_GTK */
78589e07 3261
3e703b25
GM
3262/* The frame of the last activated non-toolkit menu bar.
3263 Used to generate menu help events. */
3264
3265static struct frame *menu_help_frame;
3266
3267
62145073
GM
3268/* Show help HELP_STRING, or clear help if HELP_STRING is null.
3269
3270 PANE is the pane number, and ITEM is the menu item number in
3271 the menu (currently not used).
177c0ea7 3272
62145073
GM
3273 This cannot be done with generating a HELP_EVENT because
3274 XMenuActivate contains a loop that doesn't let Emacs process
3275 keyboard events. */
3e703b25
GM
3276
3277static void
62145073 3278menu_help_callback (help_string, pane, item)
3e703b25 3279 char *help_string;
62145073 3280 int pane, item;
3e703b25 3281{
62145073
GM
3282 extern Lisp_Object Qmenu_item;
3283 Lisp_Object *first_item;
3284 Lisp_Object pane_name;
3285 Lisp_Object menu_object;
177c0ea7 3286
62145073
GM
3287 first_item = XVECTOR (menu_items)->contents;
3288 if (EQ (first_item[0], Qt))
3289 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3290 else if (EQ (first_item[0], Qquote))
3291 /* This shouldn't happen, see xmenu_show. */
cc45fb40 3292 pane_name = empty_string;
62145073
GM
3293 else
3294 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
177c0ea7 3295
62145073
GM
3296 /* (menu-item MENU-NAME PANE-NUMBER) */
3297 menu_object = Fcons (Qmenu_item,
3298 Fcons (pane_name,
3299 Fcons (make_number (pane), Qnil)));
ba461919 3300 show_help_echo (help_string ? build_string (help_string) : Qnil,
62145073 3301 Qnil, menu_object, make_number (item), 1);
3e703b25 3302}
177c0ea7 3303
af89e871 3304static Lisp_Object
a130b901
JD
3305pop_down_menu (arg)
3306 Lisp_Object arg;
af89e871 3307{
a130b901
JD
3308 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
3309 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
59cfb104 3310
a130b901
JD
3311 FRAME_PTR f = p1->pointer;
3312 XMenu *menu = p2->pointer;
af89e871
JD
3313
3314 BLOCK_INPUT;
af89e871 3315#ifndef MSDOS
a130b901
JD
3316 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
3317 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
af89e871 3318#endif
a130b901 3319 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
af89e871
JD
3320
3321#ifdef HAVE_X_WINDOWS
3322 /* Assume the mouse has moved out of the X window.
3323 If it has actually moved in, we will get an EnterNotify. */
3324 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
3325
3326 /* State that no mouse buttons are now held.
3327 (The oldXMenu code doesn't track this info for us.)
3328 That is not necessarily true, but the fiction leads to reasonable
3329 results, and it is a pain to ask which are actually held now. */
3330 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
3331
3332#endif /* HAVE_X_WINDOWS */
3333
3334 UNBLOCK_INPUT;
3335
3336 return Qnil;
3337}
3338
3e703b25 3339
78589e07 3340static Lisp_Object
673a6211 3341xmenu_show (f, x, y, for_click, keymaps, title, error)
78589e07
RS
3342 FRAME_PTR f;
3343 int x, y;
9685a93f
RS
3344 int for_click;
3345 int keymaps;
78589e07
RS
3346 Lisp_Object title;
3347 char **error;
dcfdbac7 3348{
177c0ea7 3349 Window root;
78589e07
RS
3350 XMenu *menu;
3351 int pane, selidx, lpane, status;
3352 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
3353 char *datap;
3354 int ulx, uly, width, height;
3355 int dispwidth, dispheight;
453a4f1b 3356 int i, j, lines, maxlines;
4e8d3549 3357 int maxwidth;
78589e07
RS
3358 int dummy_int;
3359 unsigned int dummy_uint;
af89e871 3360 int specpdl_count = SPECPDL_INDEX ();
088831f6 3361
62af879c
KL
3362 if (! FRAME_X_P (f))
3363 abort ();
3364
07a675b7 3365 *error = 0;
78589e07
RS
3366 if (menu_items_n_panes == 0)
3367 return Qnil;
088831f6 3368
742f715d
KH
3369 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3370 {
3371 *error = "Empty menu";
3372 return Qnil;
3373 }
3374
78589e07 3375 /* Figure out which root window F is on. */
92280f67 3376 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
3377 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3378 &dummy_uint, &dummy_uint);
18686d47 3379
78589e07 3380 /* Make the menu on that window. */
92280f67 3381 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 3382 if (menu == NULL)
dcfdbac7
JB
3383 {
3384 *error = "Can't create menu";
78589e07 3385 return Qnil;
dcfdbac7 3386 }
78589e07 3387
87485d6f 3388#ifdef HAVE_X_WINDOWS
78589e07 3389 /* Adjust coordinates to relative to the outer (window manager) window. */
453a4f1b
JD
3390 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
3391 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
87485d6f 3392#endif /* HAVE_X_WINDOWS */
78589e07
RS
3393
3394 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
3395 x += f->left_pos;
3396 y += f->top_pos;
177c0ea7 3397
78589e07 3398 /* Create all the necessary panes and their items. */
453a4f1b 3399 maxlines = lines = i = 0;
78589e07 3400 while (i < menu_items_used)
dcfdbac7 3401 {
78589e07 3402 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
dcfdbac7 3403 {
78589e07
RS
3404 /* Create a new pane. */
3405 Lisp_Object pane_name, prefix;
3406 char *pane_string;
3407
453a4f1b
JD
3408 maxlines = max (maxlines, lines);
3409 lines = 0;
78589e07
RS
3410 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3411 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3412 pane_string = (NILP (pane_name)
d5db4077 3413 ? "" : (char *) SDATA (pane_name));
78589e07
RS
3414 if (keymaps && !NILP (prefix))
3415 pane_string++;
3416
92280f67 3417 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
3418 if (lpane == XM_FAILURE)
3419 {
92280f67 3420 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
78589e07
RS
3421 *error = "Can't create pane";
3422 return Qnil;
3423 }
3424 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
3425
3426 /* Find the width of the widest item in this pane. */
3427 maxwidth = 0;
3428 j = i;
3429 while (j < menu_items_used)
3430 {
3431 Lisp_Object item;
3432 item = XVECTOR (menu_items)->contents[j];
3433 if (EQ (item, Qt))
3434 break;
3435 if (NILP (item))
3436 {
3437 j++;
3438 continue;
3439 }
d5db4077 3440 width = SBYTES (item);
4e8d3549
RS
3441 if (width > maxwidth)
3442 maxwidth = width;
3443
3444 j += MENU_ITEMS_ITEM_LENGTH;
3445 }
dcfdbac7 3446 }
fcaa7665
RS
3447 /* Ignore a nil in the item list.
3448 It's meaningful only for dialog boxes. */
3449 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3450 i += 1;
78589e07 3451 else
dcfdbac7 3452 {
78589e07 3453 /* Create a new item within current pane. */
3e703b25 3454 Lisp_Object item_name, enable, descrip, help;
4e8d3549 3455 unsigned char *item_data;
3e703b25 3456 char *help_string;
78589e07
RS
3457
3458 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3459 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3460 descrip
3461 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3e703b25 3462 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
d5db4077 3463 help_string = STRINGP (help) ? SDATA (help) : NULL;
177c0ea7 3464
78589e07 3465 if (!NILP (descrip))
4e8d3549 3466 {
d5db4077 3467 int gap = maxwidth - SBYTES (item_name);
4e8d3549
RS
3468#ifdef C_ALLOCA
3469 Lisp_Object spacer;
3470 spacer = Fmake_string (make_number (gap), make_number (' '));
3471 item_name = concat2 (item_name, spacer);
3472 item_name = concat2 (item_name, descrip);
d5db4077 3473 item_data = SDATA (item_name);
4e8d3549
RS
3474#else
3475 /* if alloca is fast, use that to make the space,
3476 to reduce gc needs. */
3477 item_data
3478 = (unsigned char *) alloca (maxwidth
d5db4077
KR
3479 + SBYTES (descrip) + 1);
3480 bcopy (SDATA (item_name), item_data,
3481 SBYTES (item_name));
3482 for (j = SCHARS (item_name); j < maxwidth; j++)
4e8d3549 3483 item_data[j] = ' ';
d5db4077
KR
3484 bcopy (SDATA (descrip), item_data + j,
3485 SBYTES (descrip));
3486 item_data[j + SBYTES (descrip)] = 0;
4e8d3549
RS
3487#endif
3488 }
3489 else
d5db4077 3490 item_data = SDATA (item_name);
78589e07 3491
92280f67
RS
3492 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3493 menu, lpane, 0, item_data,
3e703b25 3494 !NILP (enable), help_string)
dcfdbac7
JB
3495 == XM_FAILURE)
3496 {
92280f67 3497 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
dcfdbac7 3498 *error = "Can't add selection to menu";
78589e07 3499 return Qnil;
dcfdbac7 3500 }
78589e07 3501 i += MENU_ITEMS_ITEM_LENGTH;
453a4f1b 3502 lines++;
dcfdbac7
JB
3503 }
3504 }
4e8d3549 3505
453a4f1b
JD
3506 maxlines = max (maxlines, lines);
3507
78589e07 3508 /* All set and ready to fly. */
92280f67 3509 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
63685b9d
GM
3510 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3511 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
78589e07
RS
3512 x = min (x, dispwidth);
3513 y = min (y, dispheight);
3514 x = max (x, 1);
3515 y = max (y, 1);
92280f67 3516 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
3517 &ulx, &uly, &width, &height);
3518 if (ulx+width > dispwidth)
3519 {
78589e07 3520 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
3521 ulx = dispwidth - width;
3522 }
3523 if (uly+height > dispheight)
3524 {
78589e07 3525 y -= (uly + height) - dispheight;
dcfdbac7
JB
3526 uly = dispheight - height;
3527 }
78589e07
RS
3528 if (ulx < 0) x -= ulx;
3529 if (uly < 0) y -= uly;
121e4555 3530
453a4f1b
JD
3531 if (! for_click)
3532 {
3533 /* If position was not given by a mouse click, adjust so upper left
3534 corner of the menu as a whole ends up at given coordinates. This
3535 is what x-popup-menu says in its documentation. */
3536 x += width/2;
3537 y += 1.5*height/(maxlines+2);
3538 }
3539
121e4555 3540 XMenuSetAEQ (menu, TRUE);
78589e07
RS
3541 XMenuSetFreeze (menu, TRUE);
3542 pane = selidx = 0;
3e703b25 3543
c3438661
JD
3544#ifndef MSDOS
3545 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
3546#endif
59cfb104 3547
a130b901
JD
3548 record_unwind_protect (pop_down_menu,
3549 Fcons (make_save_value (f, 0),
3550 make_save_value (menu, 0)));
c3438661 3551
3e703b25
GM
3552 /* Help display under X won't work because XMenuActivate contains
3553 a loop that doesn't give Emacs a chance to process it. */
3554 menu_help_frame = f;
92280f67 3555 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
af89e871
JD
3556 x, y, ButtonReleaseMask, &datap,
3557 menu_help_callback);
a352a815 3558
dcfdbac7
JB
3559 switch (status)
3560 {
3561 case XM_SUCCESS:
3562#ifdef XDEBUG
3563 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3564#endif
fa6d54d9 3565
78589e07
RS
3566 /* Find the item number SELIDX in pane number PANE. */
3567 i = 0;
3568 while (i < menu_items_used)
fa6d54d9 3569 {
78589e07 3570 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
088831f6 3571 {
78589e07
RS
3572 if (pane == 0)
3573 pane_prefix
3574 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3575 pane--;
3576 i += MENU_ITEMS_PANE_LENGTH;
088831f6 3577 }
78589e07 3578 else
ab6ee1a0 3579 {
78589e07 3580 if (pane == -1)
ab6ee1a0 3581 {
78589e07 3582 if (selidx == 0)
ab6ee1a0 3583 {
78589e07
RS
3584 entry
3585 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3586 if (keymaps != 0)
ab6ee1a0 3587 {
78589e07
RS
3588 entry = Fcons (entry, Qnil);
3589 if (!NILP (pane_prefix))
3590 entry = Fcons (pane_prefix, entry);
ab6ee1a0 3591 }
78589e07 3592 break;
ab6ee1a0 3593 }
78589e07 3594 selidx--;
ab6ee1a0 3595 }
78589e07 3596 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
3597 }
3598 }
78589e07 3599 break;
dcfdbac7 3600
78589e07 3601 case XM_FAILURE:
78589e07
RS
3602 *error = "Can't activate menu";
3603 case XM_IA_SELECT:
9f6fcdc5
JD
3604 entry = Qnil;
3605 break;
78589e07 3606 case XM_NO_SELECT:
be6ed24a
RS
3607 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3608 the menu was invoked with a mouse event as POSITION). */
9f6fcdc5
JD
3609 if (! for_click)
3610 Fsignal (Qquit, Qnil);
78589e07
RS
3611 entry = Qnil;
3612 break;
dcfdbac7 3613 }
a5285df3 3614
af89e871 3615 unbind_to (specpdl_count, Qnil);
a5285df3 3616
78589e07 3617 return entry;
dcfdbac7 3618}
4dedbfe0 3619
78589e07 3620#endif /* not USE_X_TOOLKIT */
1e659e4c
RS
3621
3622#endif /* HAVE_MENUS */
088831f6 3623\f
dfcf069d 3624void
78589e07 3625syms_of_xmenu ()
dcfdbac7 3626{
78589e07
RS
3627 staticpro (&menu_items);
3628 menu_items = Qnil;
86fad4ec 3629 menu_items_inuse = Qnil;
dcfdbac7 3630
0314aacb
RS
3631 Qdebug_on_next_call = intern ("debug-on-next-call");
3632 staticpro (&Qdebug_on_next_call);
3633
7ee72033
MB
3634 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3635 doc: /* Frame for which we are updating a menu.
228299fa 3636The enable predicate for a menu command should check this variable. */);
bfc524bc
RS
3637 Vmenu_updating_frame = Qnil;
3638
8ed87156 3639#ifdef USE_X_TOOLKIT
177c0ea7 3640 widget_id_tick = (1<<16);
88766961 3641 next_menubar_widget_id = 1;
8ed87156
RS
3642#endif
3643
78589e07 3644 defsubr (&Sx_popup_menu);
1e659e4c 3645#ifdef HAVE_MENUS
165e1749 3646 defsubr (&Sx_popup_dialog);
1e659e4c 3647#endif
dcfdbac7 3648}
ab5796a9
MB
3649
3650/* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3651 (do not change this comment) */