Typo.
[bpt/emacs.git] / src / xmenu.c
CommitLineData
dcfdbac7 1/* X Communication module for terminals which understand the X protocol.
0b5538bd
TTN
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 Free Software Foundation, Inc.
dcfdbac7
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
774910eb 9the Free Software Foundation; either version 2, or (at your option)
dcfdbac7
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
dcfdbac7 21
c9e60620 22/* X pop-up deck-of-cards menu facility for GNU Emacs.
dcfdbac7
JB
23 *
24 * Written by Jon Arnold and Roman Budzianowski
25 * Mods and rewrite by Robert Krawitz
26 *
27 */
28
18686d47
RS
29/* Modified by Fred Pierresteguy on December 93
30 to make the popup menus and menubar use the Xt. */
31
78589e07
RS
32/* Rewritten for clarity and GC protection by rms in Feb 94. */
33
68c45bf0
PE
34#include <config.h>
35
8faf6057 36#if 0 /* Why was this included? And without syssignal.h? */
dcfdbac7
JB
37/* On 4.3 this loses if it comes after xterm.h. */
38#include <signal.h>
8faf6057 39#endif
565620a5
RS
40
41#include <stdio.h>
7ee72033 42
dcfdbac7 43#include "lisp.h"
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);
902 }
bfc524bc 903 Vmenu_updating_frame = Qnil;
1e659e4c 904#endif /* HAVE_MENUS */
78589e07 905
86fad4ec 906 record_unwind_protect (unuse_menu_items, Qnil);
78589e07
RS
907 title = Qnil;
908 GCPRO1 (title);
909
910 /* Decode the menu items from what was specified. */
18686d47 911
02067692
SM
912 keymap = get_keymap (menu, 0, 0);
913 if (CONSP (keymap))
18686d47
RS
914 {
915 /* We were given a keymap. Extract menu info from the keymap. */
916 Lisp_Object prompt;
18686d47 917
78589e07 918 /* Extract the detailed info to make one pane. */
101bb4a5 919 keymap_panes (&menu, 1, NILP (position));
78589e07 920
18686d47
RS
921 /* Search for a string appearing directly as an element of the keymap.
922 That string is the title of the menu. */
5ee707b8 923 prompt = Fkeymap_prompt (keymap);
4f9ad016
RS
924 if (NILP (title) && !NILP (prompt))
925 title = prompt;
18686d47 926
78589e07
RS
927 /* Make that be the pane title of the first pane. */
928 if (!NILP (prompt) && menu_items_n_panes >= 0)
929 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
930
931 keymaps = 1;
18686d47 932 }
02067692 933 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
18686d47
RS
934 {
935 /* We were given a list of keymaps. */
18686d47
RS
936 int nmaps = XFASTINT (Flength (menu));
937 Lisp_Object *maps
938 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
939 int i;
78589e07
RS
940
941 title = Qnil;
18686d47
RS
942
943 /* The first keymap that has a prompt string
944 supplies the menu title. */
8af9fa55 945 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
18686d47 946 {
78589e07
RS
947 Lisp_Object prompt;
948
8af9fa55 949 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
18686d47 950
5ee707b8 951 prompt = Fkeymap_prompt (keymap);
78589e07
RS
952 if (NILP (title) && !NILP (prompt))
953 title = prompt;
18686d47
RS
954 }
955
956 /* Extract the detailed info to make one pane. */
101bb4a5 957 keymap_panes (maps, nmaps, NILP (position));
78589e07
RS
958
959 /* Make the title be the pane title of the first pane. */
960 if (!NILP (title) && menu_items_n_panes >= 0)
961 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
962
963 keymaps = 1;
18686d47
RS
964 }
965 else
966 {
967 /* We were given an old-fashioned menu. */
78589e07 968 title = Fcar (menu);
b7826503 969 CHECK_STRING (title);
18686d47 970
78589e07 971 list_of_panes (Fcdr (menu));
18686d47 972
78589e07
RS
973 keymaps = 0;
974 }
177c0ea7 975
86fad4ec
SM
976 unbind_to (specpdl_count, Qnil);
977
78589e07 978 if (NILP (position))
18686d47 979 {
78589e07
RS
980 discard_menu_items ();
981 UNGCPRO;
982 return Qnil;
18686d47
RS
983 }
984
1e659e4c 985#ifdef HAVE_MENUS
78589e07
RS
986 /* Display them in a menu. */
987 BLOCK_INPUT;
18686d47 988
673a6211 989 selection = xmenu_show (f, xpos, ypos, for_click,
78589e07
RS
990 keymaps, title, &error_name);
991 UNBLOCK_INPUT;
18686d47 992
78589e07 993 discard_menu_items ();
18686d47 994
78589e07 995 UNGCPRO;
1e659e4c 996#endif /* HAVE_MENUS */
18686d47 997
78589e07
RS
998 if (error_name) error (error_name);
999 return selection;
18686d47 1000}
165e1749 1001
1e659e4c
RS
1002#ifdef HAVE_MENUS
1003
6a040d6a 1004DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
f6e34773 1005 doc: /* Pop up a dialog box and return user's selection.
228299fa
GM
1006POSITION specifies which frame to use.
1007This is normally a mouse button event or a window or frame.
1008If POSITION is t, it means to use the frame the mouse is on.
1009The dialog box appears in the middle of the specified frame.
1010
1011CONTENTS specifies the alternatives to display in the dialog box.
6a040d6a 1012It is a list of the form (DIALOG ITEM1 ITEM2...).
228299fa
GM
1013Each ITEM is a cons cell (STRING . VALUE).
1014The return value is VALUE from the chosen item.
1015
1016An ITEM may also be just a string--that makes a nonselectable item.
1017An ITEM may also be nil--that means to put all preceding items
1018on the left of the dialog box and all following items on the right.
c05fe5d5
LT
1019\(By default, approximately half appear on each side.)
1020
6a040d6a
NR
1021If HEADER is non-nil, the frame title for the box is "Information",
1022otherwise it is "Question".
1023
c05fe5d5
LT
1024If the user gets rid of the dialog box without making a valid choice,
1025for instance using the window manager, then this produces a quit and
1026`x-popup-dialog' does not return. */)
6a040d6a
NR
1027 (position, contents, header)
1028 Lisp_Object position, contents, header;
165e1749 1029{
cc45fb40 1030 FRAME_PTR f = NULL;
99fe880d 1031 Lisp_Object window;
165e1749
FP
1032
1033 check_x ();
1034
99fe880d 1035 /* Decode the first argument: find the window or frame to use. */
88d4f6ec 1036 if (EQ (position, Qt)
2b06561a
DL
1037 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1038 || EQ (XCAR (position), Qtool_bar))))
165e1749 1039 {
b14db4d7 1040#if 0 /* Using the frame the mouse is on may not be right. */
99fe880d 1041 /* Use the mouse's current position. */
b404828f 1042 FRAME_PTR new_f = SELECTED_FRAME ();
99fe880d 1043 Lisp_Object bar_window;
cc45fb40 1044 enum scroll_bar_part part;
99fe880d
RS
1045 unsigned long time;
1046 Lisp_Object x, y;
165e1749 1047
46b657e2 1048 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
5ca2ef64 1049
99fe880d 1050 if (new_f != 0)
a39f0477 1051 XSETFRAME (window, new_f);
99fe880d
RS
1052 else
1053 window = selected_window;
b14db4d7 1054#endif
88d4f6ec 1055 window = selected_window;
99fe880d
RS
1056 }
1057 else if (CONSP (position))
1058 {
1059 Lisp_Object tem;
1060 tem = Fcar (position);
b5bb2705 1061 if (CONSP (tem))
99fe880d 1062 window = Fcar (Fcdr (position));
80670155
RS
1063 else
1064 {
99fe880d
RS
1065 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1066 window = Fcar (tem); /* POSN_WINDOW (tem) */
165e1749 1067 }
165e1749 1068 }
99fe880d
RS
1069 else if (WINDOWP (position) || FRAMEP (position))
1070 window = position;
a9be6839
RS
1071 else
1072 window = Qnil;
165e1749 1073
99fe880d 1074 /* Decode where to put the menu. */
165e1749 1075
b5bb2705 1076 if (FRAMEP (window))
99fe880d 1077 f = XFRAME (window);
b5bb2705 1078 else if (WINDOWP (window))
165e1749 1079 {
b7826503 1080 CHECK_LIVE_WINDOW (window);
99fe880d 1081 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
165e1749 1082 }
99fe880d
RS
1083 else
1084 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1085 but I don't want to make one now. */
b7826503 1086 CHECK_WINDOW (window);
165e1749 1087
488dd4c4 1088#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
99fe880d
RS
1089 /* Display a menu with these alternatives
1090 in the middle of frame F. */
1091 {
1092 Lisp_Object x, y, frame, newpos;
a39f0477
KH
1093 XSETFRAME (frame, f);
1094 XSETINT (x, x_pixel_width (f) / 2);
1095 XSETINT (y, x_pixel_height (f) / 2);
99fe880d
RS
1096 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1097
1098 return Fx_popup_menu (newpos,
1099 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1100 }
1101#else
1102 {
1103 Lisp_Object title;
1104 char *error_name;
1105 Lisp_Object selection;
86fad4ec 1106 int specpdl_count = SPECPDL_INDEX ();
165e1749 1107
99fe880d
RS
1108 /* Decode the dialog items from what was specified. */
1109 title = Fcar (contents);
b7826503 1110 CHECK_STRING (title);
86fad4ec 1111 record_unwind_protect (unuse_menu_items, Qnil);
165e1749 1112
160b1b50
JD
1113 if (NILP (Fcar (Fcdr (contents))))
1114 /* No buttons specified, add an "Ok" button so users can pop down
1115 the dialog. Also, the lesstif/motif version crashes if there are
1116 no buttons. */
1117 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
b9de078a 1118
99fe880d 1119 list_of_panes (Fcons (contents, Qnil));
165e1749 1120
99fe880d
RS
1121 /* Display them in a dialog box. */
1122 BLOCK_INPUT;
6a040d6a 1123 selection = xdialog_show (f, 0, title, header, &error_name);
99fe880d 1124 UNBLOCK_INPUT;
165e1749 1125
86fad4ec 1126 unbind_to (specpdl_count, Qnil);
99fe880d
RS
1127 discard_menu_items ();
1128
1129 if (error_name) error (error_name);
1130 return selection;
1131 }
7464b131 1132#endif
392d3f4b 1133}
c3438661
JD
1134
1135
1136#ifndef MSDOS
1137
b3935289
JD
1138/* Set menu_items_inuse so no other popup menu or dialog is created. */
1139
1140void
1141x_menu_set_in_use (in_use)
1142 int in_use;
1143{
1144 menu_items_inuse = in_use ? Qt : Qnil;
5ae53dcf 1145 popup_activated_flag = in_use;
b3935289
JD
1146}
1147
c3438661
JD
1148/* Wait for an X event to arrive or for a timer to expire. */
1149
b3935289 1150void
c3438661
JD
1151x_menu_wait_for_event (void *data)
1152{
1153 extern EMACS_TIME timer_check P_ ((int));
1154
1155 /* Another way to do this is to register a timer callback, that can be
1156 done in GTK and Xt. But we have to do it like this when using only X
1157 anyway, and with callbacks we would have three variants for timer handling
1158 instead of the small ifdefs below. */
1159
1160 while (
1161#ifdef USE_X_TOOLKIT
f1d1cd24 1162 ! XtAppPending (Xt_app_con)
c3438661
JD
1163#elif defined USE_GTK
1164 ! gtk_events_pending ()
1165#else
1166 ! XPending ((Display*) data)
1167#endif
1168 )
1169 {
1170 EMACS_TIME next_time = timer_check (1);
1171 long secs = EMACS_SECS (next_time);
1172 long usecs = EMACS_USECS (next_time);
1173 SELECT_TYPE read_fds;
1174 struct x_display_info *dpyinfo;
1175 int n = 0;
1176
1177 FD_ZERO (&read_fds);
1178 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
1179 {
1180 int fd = ConnectionNumber (dpyinfo->display);
1181 FD_SET (fd, &read_fds);
1182 if (fd > n) n = fd;
1183 }
1184
1185 if (secs < 0 || (secs == 0 && usecs == 0))
1186 {
1187 /* Sometimes timer_check returns -1 (no timers) even if there are
1188 timers. So do a timeout anyway. */
1189 EMACS_SET_SECS (next_time, 1);
1190 EMACS_SET_USECS (next_time, 0);
1191 }
1192
1193 select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, &next_time);
1194 }
1195}
1196#endif /* ! MSDOS */
1197
78589e07 1198\f
488dd4c4 1199#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18686d47 1200
f1d1cd24
JD
1201#ifdef USE_X_TOOLKIT
1202
4dedbfe0 1203/* Loop in Xt until the menu pulldown or dialog popup has been
f56e7ad2 1204 popped down (deactivated). This is used for x-popup-menu
f02cac82
RS
1205 and x-popup-dialog; it is not used for the menu bar.
1206
2e2b8e22 1207 NOTE: All calls to popup_get_selection should be protected
c98fcf4b 1208 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
aa669def 1209
f02cac82 1210static void
95bdef2e 1211popup_get_selection (initial_event, dpyinfo, id, do_timers)
4dedbfe0 1212 XEvent *initial_event;
aa669def 1213 struct x_display_info *dpyinfo;
2e2b8e22 1214 LWLIB_ID id;
df470e3b 1215 int do_timers;
78589e07 1216{
4dedbfe0 1217 XEvent event;
f02cac82 1218
8b806a85
JD
1219 while (popup_activated_flag)
1220 {
c0b0f54b 1221 if (initial_event)
8b806a85
JD
1222 {
1223 event = *initial_event;
1224 initial_event = 0;
1225 }
1226 else
c3438661
JD
1227 {
1228 if (do_timers) x_menu_wait_for_event (0);
1229 XtAppNextEvent (Xt_app_con, &event);
1230 }
78589e07 1231
10624005 1232 /* Make sure we don't consider buttons grabbed after menu goes.
8b806a85
JD
1233 And make sure to deactivate for any ButtonRelease,
1234 even if XtDispatchEvent doesn't do that. */
1235 if (event.type == ButtonRelease
1236 && dpyinfo->display == event.xbutton.display)
10624005
KH
1237 {
1238 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
177c0ea7 1239#ifdef USE_MOTIF /* Pretending that the event came from a
8b806a85
JD
1240 Btn1Down seems the only way to convince Motif to
1241 activate its callbacks; setting the XmNmenuPost
1242 isn't working. --marcus@sysc.pdx.edu. */
1243 event.xbutton.button = 1;
1244 /* Motif only pops down menus when no Ctrl, Alt or Mod
1245 key is pressed and the button is released. So reset key state
1246 so Motif thinks this is the case. */
1247 event.xbutton.state = 0;
c8b5aa3d 1248#endif
10624005 1249 }
33385c6f 1250 /* Pop down on C-g and Escape. */
2e2b8e22 1251 else if (event.type == KeyPress
10624005 1252 && dpyinfo->display == event.xbutton.display)
8b806a85
JD
1253 {
1254 KeySym keysym = XLookupKeysym (&event.xkey, 0);
9f6fcdc5 1255
33385c6f
JD
1256 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
1257 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
9f6fcdc5 1258 popup_activated_flag = 0;
8b806a85 1259 }
177c0ea7 1260
8b806a85 1261 x_dispatch_event (&event, event.xany.display);
aa669def 1262 }
78589e07
RS
1263}
1264
488dd4c4
JD
1265#endif /* USE_X_TOOLKIT */
1266
1267#ifdef USE_GTK
1268/* Loop util popup_activated_flag is set to zero in a callback.
1269 Used for popup menus and dialogs. */
f1d1cd24 1270
488dd4c4 1271static void
f1d1cd24 1272popup_widget_loop (do_timers, widget)
c3438661 1273 int do_timers;
f1d1cd24 1274 GtkWidget *widget;
488dd4c4
JD
1275{
1276 ++popup_activated_flag;
1277
1278 /* Process events in the Gtk event loop until done. */
1279 while (popup_activated_flag)
1280 {
c3438661 1281 if (do_timers) x_menu_wait_for_event (0);
488dd4c4
JD
1282 gtk_main_iteration ();
1283 }
1284}
1285#endif
1286
88766961
RS
1287/* Activate the menu bar of frame F.
1288 This is called from keyboard.c when it gets the
3b8f9651 1289 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
88766961
RS
1290
1291 To activate the menu bar, we use the X button-press event
ac78b144 1292 that was saved in saved_menu_event.
88766961
RS
1293 That makes the toolkit do its thing.
1294
1295 But first we recompute the menu bar contents (the whole tree).
1296
1297 The reason for saving the button event until here, instead of
1298 passing it to the toolkit right away, is that we can safely
1299 execute Lisp code. */
177c0ea7 1300
dfcf069d 1301void
88766961
RS
1302x_activate_menubar (f)
1303 FRAME_PTR f;
1304{
ac78b144 1305 if (!f->output_data.x->saved_menu_event->type)
88766961
RS
1306 return;
1307
177c0ea7 1308#ifdef USE_GTK
810f2256
JD
1309 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
1310 f->output_data.x->saved_menu_event->xany.window))
488dd4c4
JD
1311 return;
1312#endif
177c0ea7 1313
a9be6839 1314 set_frame_menubar (f, 0, 1);
88766961 1315 BLOCK_INPUT;
488dd4c4
JD
1316#ifdef USE_GTK
1317 XPutBackEvent (f->output_data.x->display_info->display,
1318 f->output_data.x->saved_menu_event);
1319 popup_activated_flag = 1;
1320#else
86fad4ec 1321 XtDispatchEvent (f->output_data.x->saved_menu_event);
488dd4c4 1322#endif
88766961 1323 UNBLOCK_INPUT;
a9be6839 1324#ifdef USE_MOTIF
745c34fb
RS
1325 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1326 pending_menu_activation = 1;
a9be6839 1327#endif
177c0ea7 1328
88766961 1329 /* Ignore this if we get it a second time. */
ac78b144 1330 f->output_data.x->saved_menu_event->type = 0;
88766961
RS
1331}
1332
c98fcf4b 1333/* Detect if a dialog or menu has been posted. */
aa669def 1334
4dedbfe0
PR
1335int
1336popup_activated ()
1337{
1338 return popup_activated_flag;
1339}
1340
4dedbfe0
PR
1341/* This callback is invoked when the user selects a menubar cascade
1342 pushbutton, but before the pulldown menu is posted. */
78589e07 1343
488dd4c4 1344#ifndef USE_GTK
78589e07 1345static void
4dedbfe0 1346popup_activate_callback (widget, id, client_data)
78589e07
RS
1347 Widget widget;
1348 LWLIB_ID id;
1349 XtPointer client_data;
1350{
4dedbfe0 1351 popup_activated_flag = 1;
7555d825 1352}
488dd4c4 1353#endif
7555d825
GM
1354
1355/* This callback is invoked when a dialog or menu is finished being
1356 used and has been unposted. */
1357
488dd4c4
JD
1358#ifdef USE_GTK
1359static void
1360popup_deactivate_callback (widget, client_data)
1361 GtkWidget *widget;
1362 gpointer client_data;
1363{
1364 popup_activated_flag = 0;
1365}
1366#else
7555d825
GM
1367static void
1368popup_deactivate_callback (widget, id, client_data)
1369 Widget widget;
1370 LWLIB_ID id;
1371 XtPointer client_data;
1372{
7555d825 1373 popup_activated_flag = 0;
78589e07 1374}
488dd4c4 1375#endif
78589e07 1376
850df50b 1377
488dd4c4
JD
1378/* Function that finds the frame for WIDGET and shows the HELP text
1379 for that widget.
1380 F is the frame if known, or NULL if not known. */
1381static void
1382show_help_event (f, widget, help)
1383 FRAME_PTR f;
1384 xt_or_gtk_widget widget;
1385 Lisp_Object help;
850df50b 1386{
488dd4c4 1387 Lisp_Object frame;
850df50b 1388
850df50b 1389 if (f)
9cd50434
GM
1390 {
1391 XSETFRAME (frame, f);
1392 kbd_buffer_store_help_event (frame, help);
1393 }
850df50b
GM
1394 else
1395 {
9fc63a89 1396#if 0 /* This code doesn't do anything useful. ++kfs */
177c0ea7 1397 /* WIDGET is the popup menu. It's parent is the frame's
850df50b 1398 widget. See which frame that is. */
488dd4c4 1399 xt_or_gtk_widget frame_widget = XtParent (widget);
850df50b
GM
1400 Lisp_Object tail;
1401
1402 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1403 {
1404 frame = XCAR (tail);
1405 if (GC_FRAMEP (frame)
1406 && (f = XFRAME (frame),
1407 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1408 break;
1409 }
9fc63a89 1410#endif
9cd50434
GM
1411 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1412 }
850df50b
GM
1413}
1414
488dd4c4
JD
1415/* Callback called when menu items are highlighted/unhighlighted
1416 while moving the mouse over them. WIDGET is the menu bar or menu
1417 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1418 the data structure for the menu item, or null in case of
1419 unhighlighting. */
4dedbfe0 1420
488dd4c4
JD
1421#ifdef USE_GTK
1422void
1423menu_highlight_callback (widget, call_data)
1424 GtkWidget *widget;
1425 gpointer call_data;
1426{
1427 xg_menu_item_cb_data *cb_data;
1428 Lisp_Object help;
177c0ea7 1429
488dd4c4
JD
1430 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
1431 XG_ITEM_DATA);
1432 if (! cb_data) return;
1433
1434 help = call_data ? cb_data->help : Qnil;
1435
1436 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1437 Don't show help for them, they won't appear before the
1438 popup is popped down. */
1439 if (popup_activated_flag <= 1)
1440 show_help_event (cb_data->cl_data->f, widget, help);
1441}
1442#else
1443void
1444menu_highlight_callback (widget, id, call_data)
78589e07
RS
1445 Widget widget;
1446 LWLIB_ID id;
488dd4c4
JD
1447 void *call_data;
1448{
1449 struct frame *f;
1450 Lisp_Object help;
1451
1452 widget_value *wv = (widget_value *) call_data;
1453
1454 help = wv ? wv->help : Qnil;
177c0ea7 1455
488dd4c4
JD
1456 /* Determine the frame for the help event. */
1457 f = menubar_id_to_frame (id);
1458
1459 show_help_event (f, widget, help);
1460}
1461#endif
1462
1463/* Find the menu selection and store it in the keyboard buffer.
1464 F is the frame the menu is on.
1465 MENU_BAR_ITEMS_USED is the length of VECTOR.
81f09877
JD
1466 VECTOR is an array of menu events for the whole menu. */
1467
1468static void
488dd4c4
JD
1469find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
1470 FRAME_PTR f;
1471 int menu_bar_items_used;
1472 Lisp_Object vector;
1473 void *client_data;
78589e07 1474{
c63f6952 1475 Lisp_Object prefix, entry;
4dedbfe0
PR
1476 Lisp_Object *subprefix_stack;
1477 int submenu_depth = 0;
1478 int i;
1479
6bbd7a29 1480 entry = Qnil;
488dd4c4 1481 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
4dedbfe0
PR
1482 prefix = Qnil;
1483 i = 0;
488dd4c4
JD
1484
1485 while (i < menu_bar_items_used)
4dedbfe0 1486 {
4dedbfe0
PR
1487 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1488 {
1489 subprefix_stack[submenu_depth++] = prefix;
1490 prefix = entry;
1491 i++;
1492 }
1493 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1494 {
1495 prefix = subprefix_stack[--submenu_depth];
1496 i++;
1497 }
1498 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1499 {
4cb35c39 1500 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
4dedbfe0
PR
1501 i += MENU_ITEMS_PANE_LENGTH;
1502 }
1503 else
1504 {
4cb35c39 1505 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
01d5e892
RS
1506 /* The EMACS_INT cast avoids a warning. There's no problem
1507 as long as pointers have enough bits to hold small integers. */
1508 if ((int) (EMACS_INT) client_data == i)
4dedbfe0
PR
1509 {
1510 int j;
1511 struct input_event buf;
4cb35c39 1512 Lisp_Object frame;
38f7dce3 1513 EVENT_INIT (buf);
4dedbfe0 1514
4cb35c39 1515 XSETFRAME (frame, f);
9979315d
GM
1516 buf.kind = MENU_BAR_EVENT;
1517 buf.frame_or_window = frame;
1518 buf.arg = frame;
4dedbfe0
PR
1519 kbd_buffer_store_event (&buf);
1520
1521 for (j = 0; j < submenu_depth; j++)
1522 if (!NILP (subprefix_stack[j]))
1523 {
9979315d
GM
1524 buf.kind = MENU_BAR_EVENT;
1525 buf.frame_or_window = frame;
1526 buf.arg = subprefix_stack[j];
4dedbfe0
PR
1527 kbd_buffer_store_event (&buf);
1528 }
1529
1530 if (!NILP (prefix))
1531 {
9979315d
GM
1532 buf.kind = MENU_BAR_EVENT;
1533 buf.frame_or_window = frame;
1534 buf.arg = prefix;
4dedbfe0
PR
1535 kbd_buffer_store_event (&buf);
1536 }
1537
9979315d
GM
1538 buf.kind = MENU_BAR_EVENT;
1539 buf.frame_or_window = frame;
1540 buf.arg = entry;
4dedbfe0
PR
1541 kbd_buffer_store_event (&buf);
1542
1543 return;
1544 }
1545 i += MENU_ITEMS_ITEM_LENGTH;
1546 }
1547 }
18686d47
RS
1548}
1549
488dd4c4
JD
1550
1551#ifdef USE_GTK
1552/* Gtk calls callbacks just because we tell it what item should be
1553 selected in a radio group. If this variable is set to a non-zero
1554 value, we are creating menus and don't want callbacks right now.
1555*/
1556static int xg_crazy_callback_abort;
1557
1558/* This callback is called from the menu bar pulldown menu
1559 when the user makes a selection.
1560 Figure out what the user chose
1561 and put the appropriate events into the keyboard buffer. */
1562static void
1563menubar_selection_callback (widget, client_data)
1564 GtkWidget *widget;
1565 gpointer client_data;
1566{
1567 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1568
1569 if (xg_crazy_callback_abort)
1570 return;
1571
1572 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
1573 return;
1574
b79c8219
JD
1575 /* For a group of radio buttons, GTK calls the selection callback first
1576 for the item that was active before the selection and then for the one that
1577 is active after the selection. For C-h k this means we get the help on
1578 the deselected item and then the selected item is executed. Prevent that
1579 by ignoring the non-active item. */
1580 if (GTK_IS_RADIO_MENU_ITEM (widget)
1581 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
1582 return;
1583
81f09877
JD
1584 /* When a menu is popped down, X generates a focus event (i.e. focus
1585 goes back to the frame below the menu). Since GTK buffers events,
1586 we force it out here before the menu selection event. Otherwise
1587 sit-for will exit at once if the focus event follows the menu selection
1588 event. */
1589
1590 BLOCK_INPUT;
1591 while (gtk_events_pending ())
1592 gtk_main_iteration ();
1593 UNBLOCK_INPUT;
1594
488dd4c4
JD
1595 find_and_call_menu_selection (cb_data->cl_data->f,
1596 cb_data->cl_data->menu_bar_items_used,
1597 cb_data->cl_data->menu_bar_vector,
1598 cb_data->call_data);
1599}
1600
1601#else /* not USE_GTK */
1602
1603/* This callback is called from the menu bar pulldown menu
1604 when the user makes a selection.
1605 Figure out what the user chose
1606 and put the appropriate events into the keyboard buffer. */
1607static void
1608menubar_selection_callback (widget, id, client_data)
1609 Widget widget;
1610 LWLIB_ID id;
1611 XtPointer client_data;
1612{
1613 FRAME_PTR f;
1614
1615 f = menubar_id_to_frame (id);
1616 if (!f)
1617 return;
1618 find_and_call_menu_selection (f, f->menu_bar_items_used,
1619 f->menu_bar_vector, client_data);
1620}
1621#endif /* not USE_GTK */
1622
f9655c60
RS
1623/* Allocate a widget_value, blocking input. */
1624
1625widget_value *
1626xmalloc_widget_value ()
1627{
1628 widget_value *value;
1629
1630 BLOCK_INPUT;
1631 value = malloc_widget_value ();
1632 UNBLOCK_INPUT;
1633
1634 return value;
1635}
4dedbfe0
PR
1636
1637/* This recursively calls free_widget_value on the tree of widgets.
18686d47 1638 It must free all data that was malloc'ed for these widget_values.
78589e07
RS
1639 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1640 must be left alone. */
1641
18686d47
RS
1642void
1643free_menubar_widget_value_tree (wv)
1644 widget_value *wv;
1645{
1646 if (! wv) return;
18686d47
RS
1647
1648 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1649
1650 if (wv->contents && (wv->contents != (widget_value*)1))
1651 {
1652 free_menubar_widget_value_tree (wv->contents);
1653 wv->contents = (widget_value *) 0xDEADBEEF;
1654 }
1655 if (wv->next)
1656 {
1657 free_menubar_widget_value_tree (wv->next);
1658 wv->next = (widget_value *) 0xDEADBEEF;
1659 }
1660 BLOCK_INPUT;
1661 free_widget_value (wv);
1662 UNBLOCK_INPUT;
1663}
4dedbfe0 1664\f
e6c87a0f 1665/* Set up data in menu_items for a menu bar item
4dedbfe0
PR
1666 whose event type is ITEM_KEY (with string ITEM_NAME)
1667 and whose contents come from the list of keymaps MAPS. */
1668
ced89c24
RS
1669static int
1670parse_single_submenu (item_key, item_name, maps)
4dedbfe0
PR
1671 Lisp_Object item_key, item_name, maps;
1672{
4dedbfe0
PR
1673 Lisp_Object length;
1674 int len;
1675 Lisp_Object *mapvec;
ced89c24 1676 int i;
71dca3e3 1677 int top_level_items = 0;
4dedbfe0
PR
1678
1679 length = Flength (maps);
1680 len = XINT (length);
1681
1682 /* Convert the list MAPS into a vector MAPVEC. */
1683 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1684 for (i = 0; i < len; i++)
1685 {
1686 mapvec[i] = Fcar (maps);
1687 maps = Fcdr (maps);
1688 }
1689
4dedbfe0
PR
1690 /* Loop over the given keymaps, making a pane for each map.
1691 But don't make a pane that is empty--ignore that map instead. */
1692 for (i = 0; i < len; i++)
71dca3e3 1693 {
e6c87a0f 1694 if (!KEYMAPP (mapvec[i]))
71dca3e3 1695 {
846e8c10
RS
1696 /* Here we have a command at top level in the menu bar
1697 as opposed to a submenu. */
71dca3e3
RS
1698 top_level_items = 1;
1699 push_menu_pane (Qnil, Qnil);
850df50b
GM
1700 push_menu_item (item_name, Qt, item_key, mapvec[i],
1701 Qnil, Qnil, Qnil, Qnil);
71dca3e3
RS
1702 }
1703 else
37dc84ff
RS
1704 {
1705 Lisp_Object prompt;
1706 prompt = Fkeymap_prompt (mapvec[i]);
1707 single_keymap_panes (mapvec[i],
1708 !NILP (prompt) ? prompt : item_name,
1709 item_key, 0, 10);
1710 }
71dca3e3 1711 }
4dedbfe0 1712
ced89c24
RS
1713 return top_level_items;
1714}
1715
1716/* Create a tree of widget_value objects
1717 representing the panes and items
1718 in menu_items starting at index START, up to index END. */
1719
1720static widget_value *
1721digest_single_submenu (start, end, top_level_items)
7448803e 1722 int start, end, top_level_items;
ced89c24
RS
1723{
1724 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1725 int i;
1726 int submenu_depth = 0;
1727 widget_value **submenu_stack;
4dedbfe0
PR
1728
1729 submenu_stack
1730 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
f7fab165 1731 wv = xmalloc_widget_value ();
4dedbfe0
PR
1732 wv->name = "menu";
1733 wv->value = 0;
1734 wv->enabled = 1;
3427a3db 1735 wv->button_type = BUTTON_TYPE_NONE;
0b1e6b54 1736 wv->help = Qnil;
4dedbfe0
PR
1737 first_wv = wv;
1738 save_wv = 0;
71dca3e3 1739 prev_wv = 0;
177c0ea7 1740
37dc84ff
RS
1741 /* Loop over all panes and items made by the preceding call
1742 to parse_single_submenu and construct a tree of widget_value objects.
1743 Ignore the panes and items used by previous calls to
1744 digest_single_submenu, even though those are also in menu_items. */
ced89c24
RS
1745 i = start;
1746 while (i < end)
4dedbfe0
PR
1747 {
1748 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1749 {
1750 submenu_stack[submenu_depth++] = save_wv;
1751 save_wv = prev_wv;
1752 prev_wv = 0;
1753 i++;
1754 }
1755 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1756 {
1757 prev_wv = save_wv;
1758 save_wv = submenu_stack[--submenu_depth];
1759 i++;
1760 }
1761 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1762 && submenu_depth != 0)
1763 i += MENU_ITEMS_PANE_LENGTH;
1764 /* Ignore a nil in the item list.
1765 It's meaningful only for dialog boxes. */
1766 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1767 i += 1;
1768 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1769 {
1770 /* Create a new pane. */
1771 Lisp_Object pane_name, prefix;
1772 char *pane_string;
177c0ea7 1773
4dedbfe0
PR
1774 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1775 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
177c0ea7 1776
703dc2a8
KH
1777#ifndef HAVE_MULTILINGUAL_MENU
1778 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
4c329aa8 1779 {
8af9fa55 1780 pane_name = ENCODE_MENU_STRING (pane_name);
4c329aa8
GM
1781 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1782 }
703dc2a8 1783#endif
4dedbfe0 1784 pane_string = (NILP (pane_name)
d5db4077 1785 ? "" : (char *) SDATA (pane_name));
4dedbfe0
PR
1786 /* If there is just one top-level pane, put all its items directly
1787 under the top-level menu. */
1788 if (menu_items_n_panes == 1)
1789 pane_string = "";
1790
1791 /* If the pane has a meaningful name,
1792 make the pane a top-level menu item
1793 with its items as a submenu beneath it. */
1794 if (strcmp (pane_string, ""))
1795 {
f7fab165 1796 wv = xmalloc_widget_value ();
4dedbfe0
PR
1797 if (save_wv)
1798 save_wv->next = wv;
1799 else
1800 first_wv->contents = wv;
2bf436c3
JD
1801 wv->lname = pane_name;
1802 /* Set value to 1 so update_submenu_strings can handle '@' */
1803 wv->value = (char *)1;
4dedbfe0 1804 wv->enabled = 1;
3427a3db 1805 wv->button_type = BUTTON_TYPE_NONE;
0b1e6b54 1806 wv->help = Qnil;
4dedbfe0
PR
1807 }
1808 save_wv = wv;
1809 prev_wv = 0;
1810 i += MENU_ITEMS_PANE_LENGTH;
1811 }
1812 else
1813 {
1814 /* Create a new item within current pane. */
3427a3db 1815 Lisp_Object item_name, enable, descrip, def, type, selected;
850df50b 1816 Lisp_Object help;
177c0ea7 1817
4c329aa8
GM
1818 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1819 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1820 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1821 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1822 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1823 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1824 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3427a3db 1825
703dc2a8 1826#ifndef HAVE_MULTILINGUAL_MENU
3427a3db 1827 if (STRING_MULTIBYTE (item_name))
4c329aa8 1828 {
646f98ec 1829 item_name = ENCODE_MENU_STRING (item_name);
4c329aa8
GM
1830 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1831 }
177c0ea7 1832
3427a3db 1833 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
4c329aa8 1834 {
646f98ec 1835 descrip = ENCODE_MENU_STRING (descrip);
4c329aa8
GM
1836 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1837 }
1838#endif /* not HAVE_MULTILINGUAL_MENU */
3427a3db 1839
f7fab165 1840 wv = xmalloc_widget_value ();
177c0ea7 1841 if (prev_wv)
4dedbfe0 1842 prev_wv->next = wv;
71dca3e3 1843 else
4dedbfe0 1844 save_wv->contents = wv;
71dca3e3 1845
2bf436c3 1846 wv->lname = item_name;
4dedbfe0 1847 if (!NILP (descrip))
2bf436c3 1848 wv->lkey = descrip;
4dedbfe0 1849 wv->value = 0;
01d5e892
RS
1850 /* The EMACS_INT cast avoids a warning. There's no problem
1851 as long as pointers have enough bits to hold small integers. */
1852 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
4dedbfe0 1853 wv->enabled = !NILP (enable);
177c0ea7 1854
3427a3db
GM
1855 if (NILP (type))
1856 wv->button_type = BUTTON_TYPE_NONE;
1857 else if (EQ (type, QCradio))
1858 wv->button_type = BUTTON_TYPE_RADIO;
1859 else if (EQ (type, QCtoggle))
1860 wv->button_type = BUTTON_TYPE_TOGGLE;
1861 else
1862 abort ();
1863
1864 wv->selected = !NILP (selected);
0b1f4572
RS
1865 if (! STRINGP (help))
1866 help = Qnil;
1867
1868 wv->help = help;
cc45fb40 1869
4dedbfe0
PR
1870 prev_wv = wv;
1871
1872 i += MENU_ITEMS_ITEM_LENGTH;
1873 }
1874 }
1875
71dca3e3
RS
1876 /* If we have just one "menu item"
1877 that was originally a button, return it by itself. */
1878 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1879 {
1880 wv = first_wv->contents;
1881 free_widget_value (first_wv);
1882 return wv;
1883 }
1884
4dedbfe0
PR
1885 return first_wv;
1886}
2bf436c3 1887
5560d460 1888/* Walk through the widget_value tree starting at FIRST_WV and update
2bf436c3
JD
1889 the char * pointers from the corresponding lisp values.
1890 We do this after building the whole tree, since GC may happen while the
1891 tree is constructed, and small strings are relocated. So we must wait
1892 until no GC can happen before storing pointers into lisp values. */
1893static void
1894update_submenu_strings (first_wv)
1895 widget_value *first_wv;
1896{
1897 widget_value *wv;
1898
1899 for (wv = first_wv; wv; wv = wv->next)
1900 {
1806714a 1901 if (STRINGP (wv->lname))
2bf436c3 1902 {
b79c8219 1903 wv->name = (char *) SDATA (wv->lname);
2bf436c3
JD
1904
1905 /* Ignore the @ that means "separate pane".
1906 This is a kludge, but this isn't worth more time. */
1907 if (wv->value == (char *)1)
1908 {
1909 if (wv->name[0] == '@')
1910 wv->name++;
1911 wv->value = 0;
1912 }
1913 }
1914
1806714a 1915 if (STRINGP (wv->lkey))
b79c8219 1916 wv->key = (char *) SDATA (wv->lkey);
2bf436c3
JD
1917
1918 if (wv->contents)
1919 update_submenu_strings (wv->contents);
1920 }
1921}
1922
f61a541b
GM
1923\f
1924/* Recompute all the widgets of frame F, when the menu bar has been
1925 changed. Value is non-zero if widgets were updated. */
1926
1927static int
6af6cbb5 1928update_frame_menubar (f)
18686d47
RS
1929 FRAME_PTR f;
1930{
488dd4c4
JD
1931#ifdef USE_GTK
1932 return xg_update_frame_menubar (f);
1933#else
7556890b 1934 struct x_output *x = f->output_data.x;
cffa74ea 1935 int columns, rows;
177c0ea7 1936
f61a541b
GM
1937 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
1938 return 0;
18686d47
RS
1939
1940 BLOCK_INPUT;
f61a541b
GM
1941 /* Save the size of the frame because the pane widget doesn't accept
1942 to resize itself. So force it. */
9882535b
KS
1943 columns = FRAME_COLS (f);
1944 rows = FRAME_LINES (f);
cffa74ea 1945
f61a541b
GM
1946 /* Do the voodoo which means "I'm changing lots of things, don't try
1947 to refigure sizes until I'm done." */
4dedbfe0 1948 lw_refigure_widget (x->column_widget, False);
cffa74ea 1949
f61a541b
GM
1950 /* The order in which children are managed is the top to bottom
1951 order in which they are displayed in the paned window. First,
1952 remove the text-area widget. */
18686d47
RS
1953 XtUnmanageChild (x->edit_widget);
1954
f61a541b
GM
1955 /* Remove the menubar that is there now, and put up the menubar that
1956 should be there. */
1957 XtManageChild (x->menubar_widget);
1958 XtMapWidget (x->menubar_widget);
1959 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
18686d47 1960
c98fcf4b 1961 /* Re-manage the text-area widget, and then thrash the sizes. */
18686d47 1962 XtManageChild (x->edit_widget);
4dedbfe0 1963 lw_refigure_widget (x->column_widget, True);
cffa74ea
FP
1964
1965 /* Force the pane widget to resize itself with the right values. */
1966 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
18686d47 1967 UNBLOCK_INPUT;
488dd4c4 1968#endif
f61a541b 1969 return 1;
18686d47
RS
1970}
1971
4bcdbab1
KH
1972/* Set the contents of the menubar widgets of frame F.
1973 The argument FIRST_TIME is currently ignored;
1974 it is set the first time this is called, from initialize_frame_menubar. */
1975
18686d47 1976void
88766961 1977set_frame_menubar (f, first_time, deep_p)
18686d47 1978 FRAME_PTR f;
706aa2f2 1979 int first_time;
88766961 1980 int deep_p;
18686d47 1981{
488dd4c4
JD
1982 xt_or_gtk_widget menubar_widget = f->output_data.x->menubar_widget;
1983#ifdef USE_X_TOOLKIT
1984 LWLIB_ID id;
1985#endif
faa935b6 1986 Lisp_Object items;
4d19cb8e 1987 widget_value *wv, *first_wv, *prev_wv = 0;
1806714a 1988 int i, last_i = 0;
ced89c24 1989 int *submenu_start, *submenu_end;
37dc84ff 1990 int *submenu_top_level_items, *submenu_n_panes;
cc45fb40 1991
18686d47 1992
bfc524bc
RS
1993 XSETFRAME (Vmenu_updating_frame, f);
1994
488dd4c4 1995#ifdef USE_X_TOOLKIT
7556890b
RS
1996 if (f->output_data.x->id == 0)
1997 f->output_data.x->id = next_menubar_widget_id++;
1998 id = f->output_data.x->id;
488dd4c4 1999#endif
177c0ea7 2000
88766961
RS
2001 if (! menubar_widget)
2002 deep_p = 1;
745c34fb 2003 else if (pending_menu_activation && !deep_p)
a9be6839
RS
2004 deep_p = 1;
2005 /* Make the first call for any given frame always go deep. */
2006 else if (!f->output_data.x->saved_menu_event && !deep_p)
745c34fb
RS
2007 {
2008 deep_p = 1;
a9be6839
RS
2009 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
2010 f->output_data.x->saved_menu_event->type = 0;
745c34fb 2011 }
18686d47 2012
da18b5ac
JD
2013#ifdef USE_GTK
2014 /* If we have detached menus, we must update deep so detached menus
2015 also gets updated. */
2016 deep_p = deep_p || xg_have_tear_offs ();
2017#endif
2018
88766961 2019 if (deep_p)
18686d47 2020 {
88766961
RS
2021 /* Make a widget-value tree representing the entire menu trees. */
2022
2023 struct buffer *prev = current_buffer;
2024 Lisp_Object buffer;
aed13378 2025 int specpdl_count = SPECPDL_INDEX ();
88766961
RS
2026 int previous_menu_items_used = f->menu_bar_items_used;
2027 Lisp_Object *previous_items
2028 = (Lisp_Object *) alloca (previous_menu_items_used
2029 * sizeof (Lisp_Object));
2030
0b1cf399
RS
2031 /* If we are making a new widget, its contents are empty,
2032 do always reinitialize them. */
2033 if (! menubar_widget)
2034 previous_menu_items_used = 0;
2035
88766961
RS
2036 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
2037 specbind (Qinhibit_quit, Qt);
2038 /* Don't let the debugger step into this code
2039 because it is not reentrant. */
2040 specbind (Qdebug_on_next_call, Qnil);
2041
89f2614d 2042 record_unwind_save_match_data ();
86fad4ec 2043 record_unwind_protect (unuse_menu_items, Qnil);
88766961
RS
2044 if (NILP (Voverriding_local_map_menu_flag))
2045 {
2046 specbind (Qoverriding_terminal_local_map, Qnil);
2047 specbind (Qoverriding_local_map, Qnil);
2048 }
18686d47 2049
88766961 2050 set_buffer_internal_1 (XBUFFER (buffer));
18686d47 2051
88766961 2052 /* Run the Lucid hook. */
950eaee7 2053 safe_run_hooks (Qactivate_menubar_hook);
177c0ea7 2054
88766961
RS
2055 /* If it has changed current-menubar from previous value,
2056 really recompute the menubar from the value. */
2057 if (! NILP (Vlucid_menu_bar_dirty_flag))
2058 call0 (Qrecompute_lucid_menubar);
a57634d4 2059 safe_run_hooks (Qmenu_bar_update_hook);
88766961 2060 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
18686d47 2061
88766961 2062 items = FRAME_MENU_BAR_ITEMS (f);
8d8a3494 2063
88766961 2064 /* Save the frame's previous menu bar contents data. */
86c04183
GM
2065 if (previous_menu_items_used)
2066 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
2067 previous_menu_items_used * sizeof (Lisp_Object));
8d8a3494 2068
ced89c24
RS
2069 /* Fill in menu_items with the current menu bar contents.
2070 This can evaluate Lisp code. */
88766961 2071 menu_items = f->menu_bar_vector;
86c04183 2072 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
ced89c24
RS
2073 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2074 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
37dc84ff 2075 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
ced89c24
RS
2076 submenu_top_level_items
2077 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
88766961 2078 init_menu_items ();
f04366cb 2079 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
2080 {
2081 Lisp_Object key, string, maps;
2082
ced89c24
RS
2083 last_i = i;
2084
88766961
RS
2085 key = XVECTOR (items)->contents[i];
2086 string = XVECTOR (items)->contents[i + 1];
2087 maps = XVECTOR (items)->contents[i + 2];
2088 if (NILP (string))
2089 break;
2090
ced89c24
RS
2091 submenu_start[i] = menu_items_used;
2092
2093 menu_items_n_panes = 0;
2094 submenu_top_level_items[i]
2095 = parse_single_submenu (key, string, maps);
37dc84ff 2096 submenu_n_panes[i] = menu_items_n_panes;
ced89c24
RS
2097
2098 submenu_end[i] = menu_items_used;
2099 }
2100
2101 finish_menu_items ();
2102
2103 /* Convert menu_items into widget_value trees
2104 to display the menu. This cannot evaluate Lisp code. */
2105
2106 wv = xmalloc_widget_value ();
2107 wv->name = "menubar";
2108 wv->value = 0;
2109 wv->enabled = 1;
2110 wv->button_type = BUTTON_TYPE_NONE;
2111 wv->help = Qnil;
2112 first_wv = wv;
2113
2114 for (i = 0; i < last_i; i += 4)
2115 {
37dc84ff 2116 menu_items_n_panes = submenu_n_panes[i];
ced89c24
RS
2117 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
2118 submenu_top_level_items[i]);
177c0ea7 2119 if (prev_wv)
88766961
RS
2120 prev_wv->next = wv;
2121 else
2122 first_wv->contents = wv;
2123 /* Don't set wv->name here; GC during the loop might relocate it. */
2124 wv->enabled = 1;
3427a3db 2125 wv->button_type = BUTTON_TYPE_NONE;
88766961
RS
2126 prev_wv = wv;
2127 }
2128
88766961 2129 set_buffer_internal_1 (prev);
8d8a3494 2130 unbind_to (specpdl_count, Qnil);
8d8a3494 2131
88766961
RS
2132 /* If there has been no change in the Lisp-level contents
2133 of the menu bar, skip redisplaying it. Just exit. */
2134
2135 for (i = 0; i < previous_menu_items_used; i++)
2136 if (menu_items_used == i
99d3fac7 2137 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
88766961 2138 break;
62555c22 2139 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
88766961
RS
2140 {
2141 free_menubar_widget_value_tree (first_wv);
86fad4ec 2142 discard_menu_items ();
88766961
RS
2143
2144 return;
2145 }
2146
2147 /* Now GC cannot happen during the lifetime of the widget_value,
2148 so it's safe to store data from a Lisp_String. */
2149 wv = first_wv->contents;
f04366cb 2150 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
2151 {
2152 Lisp_Object string;
2153 string = XVECTOR (items)->contents[i + 1];
2154 if (NILP (string))
2bf436c3 2155 break;
ec867797 2156 wv->name = (char *) SDATA (string);
2bf436c3
JD
2157 update_submenu_strings (wv->contents);
2158 wv = wv->next;
88766961
RS
2159 }
2160
2161 f->menu_bar_vector = menu_items;
2162 f->menu_bar_items_used = menu_items_used;
86fad4ec 2163 discard_menu_items ();
4d19cb8e 2164 }
88766961
RS
2165 else
2166 {
2167 /* Make a widget-value tree containing
2168 just the top level menu bar strings. */
4d19cb8e 2169
ced89c24
RS
2170 wv = xmalloc_widget_value ();
2171 wv->name = "menubar";
2172 wv->value = 0;
2173 wv->enabled = 1;
2174 wv->button_type = BUTTON_TYPE_NONE;
2175 wv->help = Qnil;
2176 first_wv = wv;
2177
88766961 2178 items = FRAME_MENU_BAR_ITEMS (f);
f04366cb 2179 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
2180 {
2181 Lisp_Object string;
2182
2183 string = XVECTOR (items)->contents[i + 1];
2184 if (NILP (string))
2185 break;
2186
f7fab165 2187 wv = xmalloc_widget_value ();
ec867797 2188 wv->name = (char *) SDATA (string);
88766961
RS
2189 wv->value = 0;
2190 wv->enabled = 1;
3427a3db 2191 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 2192 wv->help = Qnil;
fe8fa62f
RS
2193 /* This prevents lwlib from assuming this
2194 menu item is really supposed to be empty. */
2195 /* The EMACS_INT cast avoids a warning.
2196 This value just has to be different from small integers. */
2197 wv->call_data = (void *) (EMACS_INT) (-1);
88766961 2198
177c0ea7 2199 if (prev_wv)
88766961
RS
2200 prev_wv->next = wv;
2201 else
2202 first_wv->contents = wv;
2203 prev_wv = wv;
2204 }
62555c22
RS
2205
2206 /* Forget what we thought we knew about what is in the
2207 detailed contents of the menu bar menus.
2208 Changing the top level always destroys the contents. */
2209 f->menu_bar_items_used = 0;
88766961 2210 }
4dedbfe0 2211
88766961 2212 /* Create or update the menu bar widget. */
aa669def
RS
2213
2214 BLOCK_INPUT;
2215
488dd4c4
JD
2216#ifdef USE_GTK
2217 xg_crazy_callback_abort = 1;
2218 if (menubar_widget)
2219 {
da18b5ac 2220 /* The fourth arg is DEEP_P, which says to consider the entire
488dd4c4
JD
2221 menu trees we supply, rather than just the menu bar item names. */
2222 xg_modify_menubar_widgets (menubar_widget,
2223 f,
2224 first_wv,
2225 deep_p,
2226 G_CALLBACK (menubar_selection_callback),
2227 G_CALLBACK (popup_deactivate_callback),
2228 G_CALLBACK (menu_highlight_callback));
2229 }
2230 else
2231 {
2232 GtkWidget *wvbox = f->output_data.x->vbox_widget;
177c0ea7 2233
488dd4c4 2234 menubar_widget
177c0ea7 2235 = xg_create_widget ("menubar", "menubar", f, first_wv,
488dd4c4
JD
2236 G_CALLBACK (menubar_selection_callback),
2237 G_CALLBACK (popup_deactivate_callback),
2238 G_CALLBACK (menu_highlight_callback));
2239
2240 f->output_data.x->menubar_widget = menubar_widget;
2241 }
2242
177c0ea7 2243
488dd4c4 2244#else /* not USE_GTK */
18686d47 2245 if (menubar_widget)
4dedbfe0
PR
2246 {
2247 /* Disable resizing (done for Motif!) */
7556890b 2248 lw_allow_resizing (f->output_data.x->widget, False);
4dedbfe0
PR
2249
2250 /* The third arg is DEEP_P, which says to consider the entire
2251 menu trees we supply, rather than just the menu bar item names. */
88766961 2252 lw_modify_all_widgets (id, first_wv, deep_p);
4dedbfe0 2253
c98fcf4b 2254 /* Re-enable the edit widget to resize. */
7556890b 2255 lw_allow_resizing (f->output_data.x->widget, True);
4dedbfe0 2256 }
18686d47
RS
2257 else
2258 {
9f6fcdc5
JD
2259 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2260 XtTranslations override = XtParseTranslationTable (menuOverride);
2261
177c0ea7 2262 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
7556890b 2263 f->output_data.x->column_widget,
4dedbfe0
PR
2264 0,
2265 popup_activate_callback,
2266 menubar_selection_callback,
850df50b
GM
2267 popup_deactivate_callback,
2268 menu_highlight_callback);
7556890b 2269 f->output_data.x->menubar_widget = menubar_widget;
9f6fcdc5
JD
2270
2271 /* Make menu pop down on C-g. */
2272 XtOverrideTranslations (menubar_widget, override);
18686d47 2273 }
1d1c1567
KH
2274
2275 {
177c0ea7 2276 int menubar_size
7556890b
RS
2277 = (f->output_data.x->menubar_widget
2278 ? (f->output_data.x->menubar_widget->core.height
2279 + f->output_data.x->menubar_widget->core.border_width)
1d1c1567
KH
2280 : 0);
2281
f42aa681
RS
2282#if 0 /* Experimentally, we now get the right results
2283 for -geometry -0-0 without this. 24 Aug 96, rms. */
5ee80bd3 2284#ifdef USE_LUCID
1d1c1567
KH
2285 if (FRAME_EXTERNAL_MENU_BAR (f))
2286 {
2287 Dimension ibw = 0;
7556890b 2288 XtVaGetValues (f->output_data.x->column_widget,
1d1c1567
KH
2289 XtNinternalBorderWidth, &ibw, NULL);
2290 menubar_size += ibw;
2291 }
5ee80bd3 2292#endif /* USE_LUCID */
f42aa681 2293#endif /* 0 */
1d1c1567 2294
7556890b 2295 f->output_data.x->menubar_height = menubar_size;
1d1c1567 2296 }
488dd4c4 2297#endif /* not USE_GTK */
177c0ea7 2298
18686d47 2299 free_menubar_widget_value_tree (first_wv);
364cd450 2300 update_frame_menubar (f);
18686d47 2301
488dd4c4
JD
2302#ifdef USE_GTK
2303 xg_crazy_callback_abort = 0;
2304#endif
2305
18686d47
RS
2306 UNBLOCK_INPUT;
2307}
85f487d1 2308
8e6208c5 2309/* Called from Fx_create_frame to create the initial menubar of a frame
4dedbfe0
PR
2310 before it is mapped, so that the window is mapped with the menubar already
2311 there instead of us tacking it on later and thrashing the window after it
2312 is visible. */
2313
2314void
2315initialize_frame_menubar (f)
2316 FRAME_PTR f;
2317{
2318 /* This function is called before the first chance to redisplay
2319 the frame. It has to be, so the frame will have the right size. */
2320 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
88766961 2321 set_frame_menubar (f, 1, 1);
4dedbfe0
PR
2322}
2323
aba25348 2324
4dedbfe0 2325/* Get rid of the menu bar of frame F, and free its storage.
488dd4c4
JD
2326 This is used when deleting a frame, and when turning off the menu bar.
2327 For GTK this function is in gtkutil.c. */
4dedbfe0 2328
488dd4c4 2329#ifndef USE_GTK
85f487d1
FP
2330void
2331free_frame_menubar (f)
2332 FRAME_PTR f;
2333{
2334 Widget menubar_widget;
85f487d1 2335
7556890b 2336 menubar_widget = f->output_data.x->menubar_widget;
a45bad2a
RS
2337
2338 f->output_data.x->menubar_height = 0;
177c0ea7 2339
85f487d1
FP
2340 if (menubar_widget)
2341 {
aba25348
GM
2342#ifdef USE_MOTIF
2343 /* Removing the menu bar magically changes the shell widget's x
2344 and y position of (0, 0) which, when the menu bar is turned
2345 on again, leads to pull-down menuss appearing in strange
2346 positions near the upper-left corner of the display. This
2347 happens only with some window managers like twm and ctwm,
2348 but not with other like Motif's mwm or kwm, because the
2349 latter generate ConfigureNotify events when the menu bar
2350 is switched off, which fixes the shell position. */
2351 Position x0, y0, x1, y1;
2352#endif
177c0ea7 2353
85f487d1 2354 BLOCK_INPUT;
aba25348
GM
2355
2356#ifdef USE_MOTIF
ae556422
GM
2357 if (f->output_data.x->widget)
2358 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
aba25348 2359#endif
177c0ea7 2360
7556890b 2361 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
0c05dcd3 2362 f->output_data.x->menubar_widget = NULL;
aba25348
GM
2363
2364#ifdef USE_MOTIF
ae556422
GM
2365 if (f->output_data.x->widget)
2366 {
2367 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2368 if (x1 == 0 && y1 == 0)
2369 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2370 }
aba25348 2371#endif
177c0ea7 2372
85f487d1
FP
2373 UNBLOCK_INPUT;
2374 }
2375}
488dd4c4 2376#endif /* not USE_GTK */
78589e07 2377
488dd4c4 2378#endif /* USE_X_TOOLKIT || USE_GTK */
78589e07
RS
2379\f
2380/* xmenu_show actually displays a menu using the panes and items in menu_items
2381 and returns the value selected from it.
2382 There are two versions of xmenu_show, one for Xt and one for Xlib.
2383 Both assume input is blocked by the caller. */
2384
2385/* F is the frame the menu is for.
2386 X and Y are the frame-relative specified position,
2387 relative to the inside upper left corner of the frame F.
c8b5aa3d 2388 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
78589e07
RS
2389 KEYMAPS is 1 if this menu was specified with keymaps;
2390 in that case, we return a list containing the chosen item's value
2391 and perhaps also the pane's prefix.
2392 TITLE is the specified menu title.
2393 ERROR is a place to store an error message string in case of failure.
2394 (We return nil on failure, but the value doesn't actually matter.) */
18686d47 2395
488dd4c4
JD
2396#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2397
2398/* The item selected in the popup menu. */
2399static Lisp_Object *volatile menu_item_selection;
2400
2401#ifdef USE_GTK
2402
2403/* Used when position a popup menu. See menu_position_func and
2404 create_and_show_popup_menu below. */
2405struct next_popup_x_y
2406{
7b76ca1c 2407 FRAME_PTR f;
488dd4c4
JD
2408 int x;
2409 int y;
2410};
2411
2412/* The menu position function to use if we are not putting a popup
2413 menu where the pointer is.
2414 MENU is the menu to pop up.
2415 X and Y shall on exit contain x/y where the menu shall pop up.
2416 PUSH_IN is not documented in the GTK manual.
2417 USER_DATA is any data passed in when calling gtk_menu_popup.
2418 Here it points to a struct next_popup_x_y where the coordinates
7b76ca1c 2419 to store in *X and *Y are as well as the frame for the popup.
488dd4c4
JD
2420
2421 Here only X and Y are used. */
2422static void
2423menu_position_func (menu, x, y, push_in, user_data)
2424 GtkMenu *menu;
2425 gint *x;
2426 gint *y;
2427 gboolean *push_in;
2428 gpointer user_data;
2429{
7b76ca1c
JD
2430 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2431 GtkRequisition req;
2432 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2433 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
b9de078a 2434
7b76ca1c
JD
2435 *x = data->x;
2436 *y = data->y;
2437
2438 /* Check if there is room for the menu. If not, adjust x/y so that
2439 the menu is fully visible. */
2440 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2441 if (data->x + req.width > disp_width)
2442 *x -= data->x + req.width - disp_width;
2443 if (data->y + req.height > disp_height)
2444 *y -= data->y + req.height - disp_height;
488dd4c4
JD
2445}
2446
2447static void
2448popup_selection_callback (widget, client_data)
2449 GtkWidget *widget;
2450 gpointer client_data;
2451{
2452 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2453
2454 if (xg_crazy_callback_abort) return;
2455 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2456}
2457
af89e871 2458static Lisp_Object
a130b901
JD
2459pop_down_menu (arg)
2460 Lisp_Object arg;
af89e871 2461{
a130b901
JD
2462 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
2463
af89e871 2464 popup_activated_flag = 0;
a130b901
JD
2465 BLOCK_INPUT;
2466 gtk_widget_destroy (GTK_WIDGET (p->pointer));
2467 UNBLOCK_INPUT;
af89e871
JD
2468 return Qnil;
2469}
2470
488dd4c4
JD
2471/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2472 menu pops down.
2473 menu_item_selection will be set to the selection. */
2474static void
2475create_and_show_popup_menu (f, first_wv, x, y, for_click)
2476 FRAME_PTR f;
2477 widget_value *first_wv;
2478 int x;
2479 int y;
2480 int for_click;
2481{
2482 int i;
2483 GtkWidget *menu;
2484 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2485 struct next_popup_x_y popup_x_y;
af89e871 2486 int specpdl_count = SPECPDL_INDEX ();
488dd4c4
JD
2487
2488 xg_crazy_callback_abort = 1;
2489 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2490 G_CALLBACK (popup_selection_callback),
2491 G_CALLBACK (popup_deactivate_callback),
2492 G_CALLBACK (menu_highlight_callback));
2493 xg_crazy_callback_abort = 0;
177c0ea7 2494
488dd4c4
JD
2495 if (! for_click)
2496 {
2497 /* Not invoked by a click. pop up at x/y. */
2498 pos_func = menu_position_func;
2499
2500 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
2501 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2502 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
488dd4c4
JD
2503
2504 popup_x_y.x = x;
2505 popup_x_y.y = y;
7b76ca1c 2506 popup_x_y.f = f;
488dd4c4 2507
9b85e63d
JD
2508 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2509 }
2510 else
2511 {
2512 for (i = 0; i < 5; i++)
2513 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2514 break;
2515 }
59cfb104 2516
488dd4c4
JD
2517 /* Display the menu. */
2518 gtk_widget_show_all (menu);
2519 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
177c0ea7 2520
a130b901 2521 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
af89e871 2522
ff18668f
JD
2523 if (GTK_WIDGET_MAPPED (menu))
2524 {
2525 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2526 two. show_help_echo uses this to detect popup menus. */
2527 popup_activated_flag = 1;
2528 /* Process events that apply to the menu. */
2529 popup_widget_loop (1, menu);
2530 }
488dd4c4 2531
af89e871 2532 unbind_to (specpdl_count, Qnil);
177c0ea7 2533
488dd4c4
JD
2534 /* Must reset this manually because the button release event is not passed
2535 to Emacs event loop. */
2536 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2537}
2538
2539#else /* not USE_GTK */
18686d47 2540
8ed87156 2541/* We need a unique id for each widget handled by the Lucid Widget
cc17e9bf
KH
2542 library.
2543
2544 For the main windows, and popup menus, we use this counter,
88766961 2545 which we increment each time after use. This starts from 1<<16.
cc17e9bf 2546
88766961
RS
2547 For menu bars, we use numbers starting at 0, counted in
2548 next_menubar_widget_id. */
8ed87156 2549LWLIB_ID widget_id_tick;
165e1749 2550
4dedbfe0
PR
2551static void
2552popup_selection_callback (widget, id, client_data)
2553 Widget widget;
2554 LWLIB_ID id;
2555 XtPointer client_data;
2556{
2557 menu_item_selection = (Lisp_Object *) client_data;
2558}
2559
af89e871
JD
2560/* ARG is the LWLIB ID of the dialog box, represented
2561 as a Lisp object as (HIGHPART . LOWPART). */
2562
2563static Lisp_Object
2564pop_down_menu (arg)
2565 Lisp_Object arg;
2566{
2567 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2568 | XINT (XCDR (arg)));
2569
2570 BLOCK_INPUT;
2571 lw_destroy_all_widgets (id);
2572 UNBLOCK_INPUT;
2573 popup_activated_flag = 0;
2574
2575 return Qnil;
2576}
2577
488dd4c4
JD
2578/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2579 menu pops down.
2580 menu_item_selection will be set to the selection. */
2581static void
2582create_and_show_popup_menu (f, first_wv, x, y, for_click)
2583 FRAME_PTR f;
2584 widget_value *first_wv;
2585 int x;
2586 int y;
2587 int for_click;
2588{
2589 int i;
2590 Arg av[2];
2591 int ac = 0;
2592 XButtonPressedEvent dummy;
2593 LWLIB_ID menu_id;
2594 Widget menu;
488dd4c4
JD
2595
2596 menu_id = widget_id_tick++;
2597 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2598 f->output_data.x->widget, 1, 0,
2599 popup_selection_callback,
2600 popup_deactivate_callback,
2601 menu_highlight_callback);
2602
2603 dummy.type = ButtonPress;
2604 dummy.serial = 0;
2605 dummy.send_event = 0;
2606 dummy.display = FRAME_X_DISPLAY (f);
2607 dummy.time = CurrentTime;
2608 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2609 dummy.window = dummy.root;
2610 dummy.subwindow = dummy.root;
2611 dummy.x = x;
2612 dummy.y = y;
2613
2614 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
2615 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2616 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
488dd4c4
JD
2617
2618 dummy.x_root = x;
2619 dummy.y_root = y;
2620
2621 dummy.state = 0;
2622 dummy.button = 0;
2623 for (i = 0; i < 5; i++)
2624 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2625 dummy.button = i;
2626
2627 /* Don't allow any geometry request from the user. */
2628 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2629 XtSetValues (menu, av, ac);
2630
2631 /* Display the menu. */
2632 lw_popup_menu (menu, (XEvent *) &dummy);
2633 popup_activated_flag = 1;
59cfb104 2634
af89e871
JD
2635 {
2636 int fact = 4 * sizeof (LWLIB_ID);
2637 int specpdl_count = SPECPDL_INDEX ();
2638 record_unwind_protect (pop_down_menu,
2639 Fcons (make_number (menu_id >> (fact)),
2640 make_number (menu_id & ~(-1 << (fact)))));
488dd4c4 2641
af89e871 2642 /* Process events that apply to the menu. */
95bdef2e 2643 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
488dd4c4 2644
af89e871
JD
2645 unbind_to (specpdl_count, Qnil);
2646 }
488dd4c4
JD
2647}
2648
2649#endif /* not USE_GTK */
2650
78589e07 2651static Lisp_Object
673a6211 2652xmenu_show (f, x, y, for_click, keymaps, title, error)
18686d47 2653 FRAME_PTR f;
18686d47
RS
2654 int x;
2655 int y;
9685a93f 2656 int for_click;
78589e07
RS
2657 int keymaps;
2658 Lisp_Object title;
2659 char **error;
18686d47 2660{
78589e07 2661 int i;
78589e07 2662 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
101bb4a5
RS
2663 widget_value **submenu_stack
2664 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2665 Lisp_Object *subprefix_stack
2666 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2667 int submenu_depth = 0;
4e8d3549 2668
78c8278d
RS
2669 int first_pane;
2670
78589e07
RS
2671 *error = NULL;
2672
742f715d
KH
2673 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2674 {
2675 *error = "Empty menu";
2676 return Qnil;
2677 }
63c414df 2678
78589e07
RS
2679 /* Create a tree of widget_value objects
2680 representing the panes and their items. */
f7fab165 2681 wv = xmalloc_widget_value ();
78589e07
RS
2682 wv->name = "menu";
2683 wv->value = 0;
2684 wv->enabled = 1;
3427a3db 2685 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 2686 wv->help =Qnil;
78589e07 2687 first_wv = wv;
78c8278d 2688 first_pane = 1;
177c0ea7 2689
78589e07
RS
2690 /* Loop over all panes and items, filling in the tree. */
2691 i = 0;
2692 while (i < menu_items_used)
2693 {
101bb4a5
RS
2694 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2695 {
2696 submenu_stack[submenu_depth++] = save_wv;
2697 save_wv = prev_wv;
2698 prev_wv = 0;
78c8278d 2699 first_pane = 1;
101bb4a5
RS
2700 i++;
2701 }
2702 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2703 {
2704 prev_wv = save_wv;
2705 save_wv = submenu_stack[--submenu_depth];
78c8278d 2706 first_pane = 0;
101bb4a5
RS
2707 i++;
2708 }
2709 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2710 && submenu_depth != 0)
2711 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
2712 /* Ignore a nil in the item list.
2713 It's meaningful only for dialog boxes. */
2714 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2715 i += 1;
101bb4a5 2716 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2717 {
2718 /* Create a new pane. */
2719 Lisp_Object pane_name, prefix;
2720 char *pane_string;
177c0ea7 2721
4c329aa8
GM
2722 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2723 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
177c0ea7 2724
703dc2a8 2725#ifndef HAVE_MULTILINGUAL_MENU
4c329aa8
GM
2726 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2727 {
8af9fa55 2728 pane_name = ENCODE_MENU_STRING (pane_name);
4c329aa8
GM
2729 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2730 }
703dc2a8 2731#endif
78589e07 2732 pane_string = (NILP (pane_name)
d5db4077 2733 ? "" : (char *) SDATA (pane_name));
101bb4a5 2734 /* If there is just one top-level pane, put all its items directly
78589e07
RS
2735 under the top-level menu. */
2736 if (menu_items_n_panes == 1)
2737 pane_string = "";
2738
2739 /* If the pane has a meaningful name,
2740 make the pane a top-level menu item
2741 with its items as a submenu beneath it. */
78c8278d 2742 if (!keymaps && strcmp (pane_string, ""))
78589e07 2743 {
f7fab165 2744 wv = xmalloc_widget_value ();
78589e07
RS
2745 if (save_wv)
2746 save_wv->next = wv;
2747 else
2748 first_wv->contents = wv;
2749 wv->name = pane_string;
2750 if (keymaps && !NILP (prefix))
2751 wv->name++;
2752 wv->value = 0;
2753 wv->enabled = 1;
3427a3db 2754 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 2755 wv->help = Qnil;
78c8278d
RS
2756 save_wv = wv;
2757 prev_wv = 0;
78589e07 2758 }
78c8278d
RS
2759 else if (first_pane)
2760 {
2761 save_wv = wv;
2762 prev_wv = 0;
2763 }
2764 first_pane = 0;
78589e07
RS
2765 i += MENU_ITEMS_PANE_LENGTH;
2766 }
2767 else
2768 {
2769 /* Create a new item within current pane. */
9cd50434 2770 Lisp_Object item_name, enable, descrip, def, type, selected, help;
4c329aa8
GM
2771 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2772 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2773 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2774 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2775 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2776 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2777 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3427a3db 2778
703dc2a8 2779#ifndef HAVE_MULTILINGUAL_MENU
3427a3db 2780 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
4c329aa8 2781 {
646f98ec 2782 item_name = ENCODE_MENU_STRING (item_name);
4c329aa8
GM
2783 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2784 }
177c0ea7 2785
3427a3db 2786 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
4c329aa8 2787 {
646f98ec 2788 descrip = ENCODE_MENU_STRING (descrip);
4c329aa8
GM
2789 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2790 }
2791#endif /* not HAVE_MULTILINGUAL_MENU */
177c0ea7 2792
f7fab165 2793 wv = xmalloc_widget_value ();
177c0ea7 2794 if (prev_wv)
78589e07 2795 prev_wv->next = wv;
177c0ea7 2796 else
78589e07 2797 save_wv->contents = wv;
d5db4077 2798 wv->name = (char *) SDATA (item_name);
78589e07 2799 if (!NILP (descrip))
d5db4077 2800 wv->key = (char *) SDATA (descrip);
78589e07 2801 wv->value = 0;
a352a815
RS
2802 /* If this item has a null value,
2803 make the call_data null so that it won't display a box
2804 when the mouse is on it. */
2805 wv->call_data
2806 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
78589e07 2807 wv->enabled = !NILP (enable);
3427a3db
GM
2808
2809 if (NILP (type))
2810 wv->button_type = BUTTON_TYPE_NONE;
2811 else if (EQ (type, QCtoggle))
2812 wv->button_type = BUTTON_TYPE_TOGGLE;
2813 else if (EQ (type, QCradio))
2814 wv->button_type = BUTTON_TYPE_RADIO;
2815 else
2816 abort ();
2817
2818 wv->selected = !NILP (selected);
cc45fb40 2819
0b1f4572
RS
2820 if (! STRINGP (help))
2821 help = Qnil;
2822
2823 wv->help = help;
cc45fb40 2824
78589e07
RS
2825 prev_wv = wv;
2826
2827 i += MENU_ITEMS_ITEM_LENGTH;
2828 }
2829 }
2830
c98fcf4b 2831 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
2832 if (!NILP (title))
2833 {
f7fab165
RS
2834 widget_value *wv_title = xmalloc_widget_value ();
2835 widget_value *wv_sep1 = xmalloc_widget_value ();
2836 widget_value *wv_sep2 = xmalloc_widget_value ();
4dedbfe0
PR
2837
2838 wv_sep2->name = "--";
2839 wv_sep2->next = first_wv->contents;
27ad7b52 2840 wv_sep2->help = Qnil;
4dedbfe0
PR
2841
2842 wv_sep1->name = "--";
2843 wv_sep1->next = wv_sep2;
27ad7b52 2844 wv_sep1->help = Qnil;
4dedbfe0 2845
703dc2a8
KH
2846#ifndef HAVE_MULTILINGUAL_MENU
2847 if (STRING_MULTIBYTE (title))
646f98ec 2848 title = ENCODE_MENU_STRING (title);
703dc2a8 2849#endif
177c0ea7 2850
d5db4077 2851 wv_title->name = (char *) SDATA (title);
cc45fb40 2852 wv_title->enabled = TRUE;
3427a3db 2853 wv_title->button_type = BUTTON_TYPE_NONE;
4dedbfe0 2854 wv_title->next = wv_sep1;
27ad7b52 2855 wv_title->help = Qnil;
4dedbfe0
PR
2856 first_wv->contents = wv_title;
2857 }
2858
78589e07
RS
2859 /* No selection has been chosen yet. */
2860 menu_item_selection = 0;
2861
488dd4c4
JD
2862 /* Actually create and show the menu until popped down. */
2863 create_and_show_popup_menu (f, first_wv, x, y, for_click);
18686d47 2864
488dd4c4
JD
2865 /* Free the widget_value objects we used to specify the contents. */
2866 free_menubar_widget_value_tree (first_wv);
18686d47 2867
78589e07
RS
2868 /* Find the selected item, and its pane, to return
2869 the proper value. */
2870 if (menu_item_selection != 0)
2871 {
c63f6952 2872 Lisp_Object prefix, entry;
78589e07 2873
6bbd7a29 2874 prefix = entry = Qnil;
78589e07
RS
2875 i = 0;
2876 while (i < menu_items_used)
2877 {
101bb4a5
RS
2878 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2879 {
2880 subprefix_stack[submenu_depth++] = prefix;
2881 prefix = entry;
2882 i++;
2883 }
2884 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2885 {
2886 prefix = subprefix_stack[--submenu_depth];
2887 i++;
2888 }
2889 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2890 {
2891 prefix
2892 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2893 i += MENU_ITEMS_PANE_LENGTH;
2894 }
d31d42cc
RS
2895 /* Ignore a nil in the item list.
2896 It's meaningful only for dialog boxes. */
2897 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2898 i += 1;
78589e07
RS
2899 else
2900 {
2901 entry
2902 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2903 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2904 {
2905 if (keymaps != 0)
2906 {
101bb4a5
RS
2907 int j;
2908
78589e07
RS
2909 entry = Fcons (entry, Qnil);
2910 if (!NILP (prefix))
2911 entry = Fcons (prefix, entry);
101bb4a5 2912 for (j = submenu_depth - 1; j >= 0; j--)
e48087b7 2913 if (!NILP (subprefix_stack[j]))
5964e450 2914 entry = Fcons (subprefix_stack[j], entry);
78589e07
RS
2915 }
2916 return entry;
2917 }
2918 i += MENU_ITEMS_ITEM_LENGTH;
2919 }
2920 }
2921 }
be6ed24a
RS
2922 else if (!for_click)
2923 /* Make "Cancel" equivalent to C-g. */
2924 Fsignal (Qquit, Qnil);
78589e07
RS
2925
2926 return Qnil;
18686d47 2927}
4dedbfe0 2928\f
488dd4c4
JD
2929#ifdef USE_GTK
2930static void
2931dialog_selection_callback (widget, client_data)
2932 GtkWidget *widget;
2933 gpointer client_data;
2934{
2935 /* The EMACS_INT cast avoids a warning. There's no problem
2936 as long as pointers have enough bits to hold small integers. */
2937 if ((int) (EMACS_INT) client_data != -1)
2938 menu_item_selection = (Lisp_Object *) client_data;
2939
2940 popup_activated_flag = 0;
2941}
2942
2943/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2944 dialog pops down.
2945 menu_item_selection will be set to the selection. */
2946static void
2947create_and_show_dialog (f, first_wv)
2948 FRAME_PTR f;
2949 widget_value *first_wv;
2950{
2951 GtkWidget *menu;
2952
2953 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
2954 G_CALLBACK (dialog_selection_callback),
2955 G_CALLBACK (popup_deactivate_callback),
2956 0);
2957
2958 if (menu)
2959 {
af89e871 2960 int specpdl_count = SPECPDL_INDEX ();
a130b901 2961 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
af89e871 2962
488dd4c4
JD
2963 /* Display the menu. */
2964 gtk_widget_show_all (menu);
2965
2966 /* Process events that apply to the menu. */
f1d1cd24 2967 popup_widget_loop (1, menu);
177c0ea7 2968
af89e871 2969 unbind_to (specpdl_count, Qnil);
488dd4c4
JD
2970 }
2971}
2972
2973#else /* not USE_GTK */
4dedbfe0
PR
2974static void
2975dialog_selection_callback (widget, id, client_data)
2976 Widget widget;
2977 LWLIB_ID id;
2978 XtPointer client_data;
2979{
01d5e892
RS
2980 /* The EMACS_INT cast avoids a warning. There's no problem
2981 as long as pointers have enough bits to hold small integers. */
2982 if ((int) (EMACS_INT) client_data != -1)
4dedbfe0 2983 menu_item_selection = (Lisp_Object *) client_data;
488dd4c4 2984
4dedbfe0
PR
2985 BLOCK_INPUT;
2986 lw_destroy_all_widgets (id);
2987 UNBLOCK_INPUT;
9572375b 2988 popup_activated_flag = 0;
4dedbfe0 2989}
18686d47 2990
488dd4c4 2991
488dd4c4
JD
2992/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2993 dialog pops down.
2994 menu_item_selection will be set to the selection. */
2995static void
2996create_and_show_dialog (f, first_wv)
2997 FRAME_PTR f;
2998 widget_value *first_wv;
2999{
3000 LWLIB_ID dialog_id;
3001
3002 dialog_id = widget_id_tick++;
3003 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
3004 f->output_data.x->widget, 1, 0,
3005 dialog_selection_callback, 0, 0);
3006 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
3007
3008 /* Display the dialog box. */
3009 lw_pop_up_all_widgets (dialog_id);
3010 popup_activated_flag = 1;
3011
3012 /* Process events that apply to the dialog box.
3013 Also handle timers. */
3014 {
3015 int count = SPECPDL_INDEX ();
3016 int fact = 4 * sizeof (LWLIB_ID);
177c0ea7 3017
488dd4c4 3018 /* xdialog_show_unwind is responsible for popping the dialog box down. */
af89e871 3019 record_unwind_protect (pop_down_menu,
488dd4c4
JD
3020 Fcons (make_number (dialog_id >> (fact)),
3021 make_number (dialog_id & ~(-1 << (fact)))));
3022
df470e3b 3023 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
95bdef2e 3024 dialog_id, 1);
488dd4c4
JD
3025
3026 unbind_to (count, Qnil);
3027 }
3028}
3029
3030#endif /* not USE_GTK */
3031
165e1749
FP
3032static char * button_names [] = {
3033 "button1", "button2", "button3", "button4", "button5",
3034 "button6", "button7", "button8", "button9", "button10" };
3035
3036static Lisp_Object
6a040d6a 3037xdialog_show (f, keymaps, title, header, error_name)
165e1749 3038 FRAME_PTR f;
165e1749 3039 int keymaps;
6a040d6a
NR
3040 Lisp_Object title, header;
3041 char **error_name;
165e1749
FP
3042{
3043 int i, nb_buttons=0;
80670155 3044 char dialog_name[6];
165e1749 3045
faa935b6 3046 widget_value *wv, *first_wv = 0, *prev_wv = 0;
165e1749 3047
fcaa7665
RS
3048 /* Number of elements seen so far, before boundary. */
3049 int left_count = 0;
3050 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3051 int boundary_seen = 0;
3052
6a040d6a 3053 *error_name = NULL;
165e1749 3054
80670155
RS
3055 if (menu_items_n_panes > 1)
3056 {
6a040d6a 3057 *error_name = "Multiple panes in dialog box";
80670155
RS
3058 return Qnil;
3059 }
3060
165e1749
FP
3061 /* Create a tree of widget_value objects
3062 representing the text label and buttons. */
3063 {
3064 Lisp_Object pane_name, prefix;
3065 char *pane_string;
3066 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
3067 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
3068 pane_string = (NILP (pane_name)
177c0ea7 3069 ? "" : (char *) SDATA (pane_name));
f7fab165 3070 prev_wv = xmalloc_widget_value ();
165e1749
FP
3071 prev_wv->value = pane_string;
3072 if (keymaps && !NILP (prefix))
3073 prev_wv->name++;
3074 prev_wv->enabled = 1;
3075 prev_wv->name = "message";
27ad7b52 3076 prev_wv->help = Qnil;
165e1749 3077 first_wv = prev_wv;
177c0ea7 3078
165e1749
FP
3079 /* Loop over all panes and items, filling in the tree. */
3080 i = MENU_ITEMS_PANE_LENGTH;
3081 while (i < menu_items_used)
3082 {
177c0ea7 3083
165e1749
FP
3084 /* Create a new item within current pane. */
3085 Lisp_Object item_name, enable, descrip;
3086 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3087 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3088 descrip
3089 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
177c0ea7 3090
80670155
RS
3091 if (NILP (item_name))
3092 {
3093 free_menubar_widget_value_tree (first_wv);
6a040d6a 3094 *error_name = "Submenu in dialog items";
80670155
RS
3095 return Qnil;
3096 }
fcaa7665
RS
3097 if (EQ (item_name, Qquote))
3098 {
3099 /* This is the boundary between left-side elts
3100 and right-side elts. Stop incrementing right_count. */
3101 boundary_seen = 1;
3102 i++;
3103 continue;
3104 }
86e71abf 3105 if (nb_buttons >= 9)
80670155
RS
3106 {
3107 free_menubar_widget_value_tree (first_wv);
6a040d6a 3108 *error_name = "Too many dialog items";
80670155
RS
3109 return Qnil;
3110 }
3111
f7fab165 3112 wv = xmalloc_widget_value ();
165e1749 3113 prev_wv->next = wv;
80670155 3114 wv->name = (char *) button_names[nb_buttons];
165e1749 3115 if (!NILP (descrip))
d5db4077
KR
3116 wv->key = (char *) SDATA (descrip);
3117 wv->value = (char *) SDATA (item_name);
165e1749
FP
3118 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
3119 wv->enabled = !NILP (enable);
27ad7b52 3120 wv->help = Qnil;
165e1749
FP
3121 prev_wv = wv;
3122
fcaa7665
RS
3123 if (! boundary_seen)
3124 left_count++;
3125
165e1749
FP
3126 nb_buttons++;
3127 i += MENU_ITEMS_ITEM_LENGTH;
3128 }
3129
fcaa7665
RS
3130 /* If the boundary was not specified,
3131 by default put half on the left and half on the right. */
3132 if (! boundary_seen)
3133 left_count = nb_buttons - nb_buttons / 2;
3134
f7fab165 3135 wv = xmalloc_widget_value ();
80670155 3136 wv->name = dialog_name;
27ad7b52 3137 wv->help = Qnil;
6a040d6a
NR
3138
3139 /* Frame title: 'Q' = Question, 'I' = Information.
3140 Can also have 'E' = Error if, one day, we want
3141 a popup for errors. */
3142 if (NILP(header))
3143 dialog_name[0] = 'Q';
3144 else
3145 dialog_name[0] = 'I';
3146
80670155
RS
3147 /* Dialog boxes use a really stupid name encoding
3148 which specifies how many buttons to use
6a040d6a 3149 and how many buttons are on the right. */
80670155
RS
3150 dialog_name[1] = '0' + nb_buttons;
3151 dialog_name[2] = 'B';
3152 dialog_name[3] = 'R';
fcaa7665
RS
3153 /* Number of buttons to put on the right. */
3154 dialog_name[4] = '0' + nb_buttons - left_count;
80670155 3155 dialog_name[5] = 0;
165e1749
FP
3156 wv->contents = first_wv;
3157 first_wv = wv;
165e1749
FP
3158 }
3159
165e1749
FP
3160 /* No selection has been chosen yet. */
3161 menu_item_selection = 0;
3162
488dd4c4
JD
3163 /* Actually create and show the dialog. */
3164 create_and_show_dialog (f, first_wv);
f02cac82 3165
488dd4c4
JD
3166 /* Free the widget_value objects we used to specify the contents. */
3167 free_menubar_widget_value_tree (first_wv);
177c0ea7 3168
488dd4c4
JD
3169 /* Find the selected item, and its pane, to return
3170 the proper value. */
165e1749
FP
3171 if (menu_item_selection != 0)
3172 {
3173 Lisp_Object prefix;
3174
3175 prefix = Qnil;
3176 i = 0;
3177 while (i < menu_items_used)
3178 {
3179 Lisp_Object entry;
3180
3181 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3182 {
3183 prefix
3184 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3185 i += MENU_ITEMS_PANE_LENGTH;
3186 }
85996cfb
GM
3187 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3188 {
3189 /* This is the boundary between left-side elts and
3190 right-side elts. */
3191 ++i;
3192 }
165e1749
FP
3193 else
3194 {
3195 entry
3196 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3197 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3198 {
3199 if (keymaps != 0)
3200 {
3201 entry = Fcons (entry, Qnil);
3202 if (!NILP (prefix))
3203 entry = Fcons (prefix, entry);
3204 }
3205 return entry;
3206 }
3207 i += MENU_ITEMS_ITEM_LENGTH;
3208 }
3209 }
3210 }
9f6fcdc5
JD
3211 else
3212 /* Make "Cancel" equivalent to C-g. */
3213 Fsignal (Qquit, Qnil);
165e1749
FP
3214
3215 return Qnil;
3216}
ba461919 3217
488dd4c4 3218#else /* not USE_X_TOOLKIT && not USE_GTK */
78589e07 3219
3e703b25
GM
3220/* The frame of the last activated non-toolkit menu bar.
3221 Used to generate menu help events. */
3222
3223static struct frame *menu_help_frame;
3224
3225
62145073
GM
3226/* Show help HELP_STRING, or clear help if HELP_STRING is null.
3227
3228 PANE is the pane number, and ITEM is the menu item number in
3229 the menu (currently not used).
177c0ea7 3230
62145073
GM
3231 This cannot be done with generating a HELP_EVENT because
3232 XMenuActivate contains a loop that doesn't let Emacs process
3233 keyboard events. */
3e703b25
GM
3234
3235static void
62145073 3236menu_help_callback (help_string, pane, item)
3e703b25 3237 char *help_string;
62145073 3238 int pane, item;
3e703b25 3239{
62145073
GM
3240 extern Lisp_Object Qmenu_item;
3241 Lisp_Object *first_item;
3242 Lisp_Object pane_name;
3243 Lisp_Object menu_object;
177c0ea7 3244
62145073
GM
3245 first_item = XVECTOR (menu_items)->contents;
3246 if (EQ (first_item[0], Qt))
3247 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3248 else if (EQ (first_item[0], Qquote))
3249 /* This shouldn't happen, see xmenu_show. */
cc45fb40 3250 pane_name = empty_string;
62145073
GM
3251 else
3252 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
177c0ea7 3253
62145073
GM
3254 /* (menu-item MENU-NAME PANE-NUMBER) */
3255 menu_object = Fcons (Qmenu_item,
3256 Fcons (pane_name,
3257 Fcons (make_number (pane), Qnil)));
ba461919 3258 show_help_echo (help_string ? build_string (help_string) : Qnil,
62145073 3259 Qnil, menu_object, make_number (item), 1);
3e703b25 3260}
177c0ea7 3261
af89e871 3262static Lisp_Object
a130b901
JD
3263pop_down_menu (arg)
3264 Lisp_Object arg;
af89e871 3265{
a130b901
JD
3266 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
3267 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
59cfb104 3268
a130b901
JD
3269 FRAME_PTR f = p1->pointer;
3270 XMenu *menu = p2->pointer;
af89e871
JD
3271
3272 BLOCK_INPUT;
af89e871 3273#ifndef MSDOS
a130b901
JD
3274 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
3275 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
af89e871 3276#endif
a130b901 3277 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
af89e871
JD
3278
3279#ifdef HAVE_X_WINDOWS
3280 /* Assume the mouse has moved out of the X window.
3281 If it has actually moved in, we will get an EnterNotify. */
3282 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
3283
3284 /* State that no mouse buttons are now held.
3285 (The oldXMenu code doesn't track this info for us.)
3286 That is not necessarily true, but the fiction leads to reasonable
3287 results, and it is a pain to ask which are actually held now. */
3288 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
3289
3290#endif /* HAVE_X_WINDOWS */
3291
3292 UNBLOCK_INPUT;
3293
3294 return Qnil;
3295}
3296
3e703b25 3297
78589e07 3298static Lisp_Object
673a6211 3299xmenu_show (f, x, y, for_click, keymaps, title, error)
78589e07
RS
3300 FRAME_PTR f;
3301 int x, y;
9685a93f
RS
3302 int for_click;
3303 int keymaps;
78589e07
RS
3304 Lisp_Object title;
3305 char **error;
dcfdbac7 3306{
177c0ea7 3307 Window root;
78589e07
RS
3308 XMenu *menu;
3309 int pane, selidx, lpane, status;
a130b901 3310 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
3311 char *datap;
3312 int ulx, uly, width, height;
3313 int dispwidth, dispheight;
453a4f1b 3314 int i, j, lines, maxlines;
4e8d3549 3315 int maxwidth;
78589e07
RS
3316 int dummy_int;
3317 unsigned int dummy_uint;
af89e871 3318 int specpdl_count = SPECPDL_INDEX ();
088831f6 3319
07a675b7 3320 *error = 0;
78589e07
RS
3321 if (menu_items_n_panes == 0)
3322 return Qnil;
088831f6 3323
742f715d
KH
3324 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3325 {
3326 *error = "Empty menu";
3327 return Qnil;
3328 }
3329
78589e07 3330 /* Figure out which root window F is on. */
92280f67 3331 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
3332 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3333 &dummy_uint, &dummy_uint);
18686d47 3334
78589e07 3335 /* Make the menu on that window. */
92280f67 3336 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 3337 if (menu == NULL)
dcfdbac7
JB
3338 {
3339 *error = "Can't create menu";
78589e07 3340 return Qnil;
dcfdbac7 3341 }
78589e07 3342
87485d6f 3343#ifdef HAVE_X_WINDOWS
78589e07 3344 /* Adjust coordinates to relative to the outer (window manager) window. */
453a4f1b
JD
3345 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
3346 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
87485d6f 3347#endif /* HAVE_X_WINDOWS */
78589e07
RS
3348
3349 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
3350 x += f->left_pos;
3351 y += f->top_pos;
177c0ea7 3352
78589e07 3353 /* Create all the necessary panes and their items. */
453a4f1b 3354 maxlines = lines = i = 0;
78589e07 3355 while (i < menu_items_used)
dcfdbac7 3356 {
78589e07 3357 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
dcfdbac7 3358 {
78589e07
RS
3359 /* Create a new pane. */
3360 Lisp_Object pane_name, prefix;
3361 char *pane_string;
3362
453a4f1b
JD
3363 maxlines = max (maxlines, lines);
3364 lines = 0;
78589e07
RS
3365 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3366 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3367 pane_string = (NILP (pane_name)
d5db4077 3368 ? "" : (char *) SDATA (pane_name));
78589e07
RS
3369 if (keymaps && !NILP (prefix))
3370 pane_string++;
3371
92280f67 3372 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
3373 if (lpane == XM_FAILURE)
3374 {
92280f67 3375 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
78589e07
RS
3376 *error = "Can't create pane";
3377 return Qnil;
3378 }
3379 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
3380
3381 /* Find the width of the widest item in this pane. */
3382 maxwidth = 0;
3383 j = i;
3384 while (j < menu_items_used)
3385 {
3386 Lisp_Object item;
3387 item = XVECTOR (menu_items)->contents[j];
3388 if (EQ (item, Qt))
3389 break;
3390 if (NILP (item))
3391 {
3392 j++;
3393 continue;
3394 }
d5db4077 3395 width = SBYTES (item);
4e8d3549
RS
3396 if (width > maxwidth)
3397 maxwidth = width;
3398
3399 j += MENU_ITEMS_ITEM_LENGTH;
3400 }
dcfdbac7 3401 }
fcaa7665
RS
3402 /* Ignore a nil in the item list.
3403 It's meaningful only for dialog boxes. */
3404 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3405 i += 1;
78589e07 3406 else
dcfdbac7 3407 {
78589e07 3408 /* Create a new item within current pane. */
3e703b25 3409 Lisp_Object item_name, enable, descrip, help;
4e8d3549 3410 unsigned char *item_data;
3e703b25 3411 char *help_string;
78589e07
RS
3412
3413 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3414 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3415 descrip
3416 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3e703b25 3417 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
d5db4077 3418 help_string = STRINGP (help) ? SDATA (help) : NULL;
177c0ea7 3419
78589e07 3420 if (!NILP (descrip))
4e8d3549 3421 {
d5db4077 3422 int gap = maxwidth - SBYTES (item_name);
4e8d3549
RS
3423#ifdef C_ALLOCA
3424 Lisp_Object spacer;
3425 spacer = Fmake_string (make_number (gap), make_number (' '));
3426 item_name = concat2 (item_name, spacer);
3427 item_name = concat2 (item_name, descrip);
d5db4077 3428 item_data = SDATA (item_name);
4e8d3549
RS
3429#else
3430 /* if alloca is fast, use that to make the space,
3431 to reduce gc needs. */
3432 item_data
3433 = (unsigned char *) alloca (maxwidth
d5db4077
KR
3434 + SBYTES (descrip) + 1);
3435 bcopy (SDATA (item_name), item_data,
3436 SBYTES (item_name));
3437 for (j = SCHARS (item_name); j < maxwidth; j++)
4e8d3549 3438 item_data[j] = ' ';
d5db4077
KR
3439 bcopy (SDATA (descrip), item_data + j,
3440 SBYTES (descrip));
3441 item_data[j + SBYTES (descrip)] = 0;
4e8d3549
RS
3442#endif
3443 }
3444 else
d5db4077 3445 item_data = SDATA (item_name);
78589e07 3446
92280f67
RS
3447 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3448 menu, lpane, 0, item_data,
3e703b25 3449 !NILP (enable), help_string)
dcfdbac7
JB
3450 == XM_FAILURE)
3451 {
92280f67 3452 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
dcfdbac7 3453 *error = "Can't add selection to menu";
78589e07 3454 return Qnil;
dcfdbac7 3455 }
78589e07 3456 i += MENU_ITEMS_ITEM_LENGTH;
453a4f1b 3457 lines++;
dcfdbac7
JB
3458 }
3459 }
4e8d3549 3460
453a4f1b
JD
3461 maxlines = max (maxlines, lines);
3462
78589e07 3463 /* All set and ready to fly. */
92280f67 3464 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
63685b9d
GM
3465 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3466 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
78589e07
RS
3467 x = min (x, dispwidth);
3468 y = min (y, dispheight);
3469 x = max (x, 1);
3470 y = max (y, 1);
92280f67 3471 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
3472 &ulx, &uly, &width, &height);
3473 if (ulx+width > dispwidth)
3474 {
78589e07 3475 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
3476 ulx = dispwidth - width;
3477 }
3478 if (uly+height > dispheight)
3479 {
78589e07 3480 y -= (uly + height) - dispheight;
dcfdbac7
JB
3481 uly = dispheight - height;
3482 }
78589e07
RS
3483 if (ulx < 0) x -= ulx;
3484 if (uly < 0) y -= uly;
121e4555 3485
453a4f1b
JD
3486 if (! for_click)
3487 {
3488 /* If position was not given by a mouse click, adjust so upper left
3489 corner of the menu as a whole ends up at given coordinates. This
3490 is what x-popup-menu says in its documentation. */
3491 x += width/2;
3492 y += 1.5*height/(maxlines+2);
3493 }
3494
121e4555 3495 XMenuSetAEQ (menu, TRUE);
78589e07
RS
3496 XMenuSetFreeze (menu, TRUE);
3497 pane = selidx = 0;
3e703b25 3498
c3438661
JD
3499#ifndef MSDOS
3500 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
3501#endif
59cfb104 3502
a130b901
JD
3503 record_unwind_protect (pop_down_menu,
3504 Fcons (make_save_value (f, 0),
3505 make_save_value (menu, 0)));
c3438661 3506
3e703b25
GM
3507 /* Help display under X won't work because XMenuActivate contains
3508 a loop that doesn't give Emacs a chance to process it. */
3509 menu_help_frame = f;
92280f67 3510 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
af89e871
JD
3511 x, y, ButtonReleaseMask, &datap,
3512 menu_help_callback);
a352a815 3513
dcfdbac7
JB
3514 switch (status)
3515 {
3516 case XM_SUCCESS:
3517#ifdef XDEBUG
3518 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3519#endif
fa6d54d9 3520
78589e07
RS
3521 /* Find the item number SELIDX in pane number PANE. */
3522 i = 0;
3523 while (i < menu_items_used)
fa6d54d9 3524 {
78589e07 3525 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
088831f6 3526 {
78589e07
RS
3527 if (pane == 0)
3528 pane_prefix
3529 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3530 pane--;
3531 i += MENU_ITEMS_PANE_LENGTH;
088831f6 3532 }
78589e07 3533 else
ab6ee1a0 3534 {
78589e07 3535 if (pane == -1)
ab6ee1a0 3536 {
78589e07 3537 if (selidx == 0)
ab6ee1a0 3538 {
78589e07
RS
3539 entry
3540 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3541 if (keymaps != 0)
ab6ee1a0 3542 {
78589e07
RS
3543 entry = Fcons (entry, Qnil);
3544 if (!NILP (pane_prefix))
3545 entry = Fcons (pane_prefix, entry);
ab6ee1a0 3546 }
78589e07 3547 break;
ab6ee1a0 3548 }
78589e07 3549 selidx--;
ab6ee1a0 3550 }
78589e07 3551 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
3552 }
3553 }
78589e07 3554 break;
dcfdbac7 3555
78589e07 3556 case XM_FAILURE:
78589e07
RS
3557 *error = "Can't activate menu";
3558 case XM_IA_SELECT:
9f6fcdc5
JD
3559 entry = Qnil;
3560 break;
78589e07 3561 case XM_NO_SELECT:
be6ed24a
RS
3562 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3563 the menu was invoked with a mouse event as POSITION). */
9f6fcdc5
JD
3564 if (! for_click)
3565 Fsignal (Qquit, Qnil);
78589e07
RS
3566 entry = Qnil;
3567 break;
dcfdbac7 3568 }
a5285df3 3569
af89e871 3570 unbind_to (specpdl_count, Qnil);
a5285df3 3571
78589e07 3572 return entry;
dcfdbac7 3573}
4dedbfe0 3574
78589e07 3575#endif /* not USE_X_TOOLKIT */
1e659e4c
RS
3576
3577#endif /* HAVE_MENUS */
088831f6 3578\f
dfcf069d 3579void
78589e07 3580syms_of_xmenu ()
dcfdbac7 3581{
78589e07
RS
3582 staticpro (&menu_items);
3583 menu_items = Qnil;
86fad4ec 3584 menu_items_inuse = Qnil;
dcfdbac7 3585
0314aacb
RS
3586 Qdebug_on_next_call = intern ("debug-on-next-call");
3587 staticpro (&Qdebug_on_next_call);
3588
7ee72033
MB
3589 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3590 doc: /* Frame for which we are updating a menu.
228299fa 3591The enable predicate for a menu command should check this variable. */);
bfc524bc
RS
3592 Vmenu_updating_frame = Qnil;
3593
8ed87156 3594#ifdef USE_X_TOOLKIT
177c0ea7 3595 widget_id_tick = (1<<16);
88766961 3596 next_menubar_widget_id = 1;
8ed87156
RS
3597#endif
3598
78589e07 3599 defsubr (&Sx_popup_menu);
1e659e4c 3600#ifdef HAVE_MENUS
165e1749 3601 defsubr (&Sx_popup_dialog);
1e659e4c 3602#endif
dcfdbac7 3603}
ab5796a9
MB
3604
3605/* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3606 (do not change this comment) */