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