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