(Fx_file_dialog): Use NULL instead of 0 at the end of
[bpt/emacs.git] / src / xmenu.c
CommitLineData
dcfdbac7 1/* X Communication module for terminals which understand the X protocol.
68c45bf0 2 Copyright (C) 1986, 88, 93, 94, 96, 1999 Free Software Foundation, Inc.
dcfdbac7
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
774910eb 8the Free Software Foundation; either version 2, or (at your option)
dcfdbac7
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
dcfdbac7
JB
20
21/* X pop-up deck-of-cards menu facility for gnuemacs.
22 *
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
25 *
26 */
27
18686d47
RS
28/* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
30
78589e07
RS
31/* Rewritten for clarity and GC protection by rms in Feb 94. */
32
68c45bf0
PE
33#include <config.h>
34
dcfdbac7
JB
35/* On 4.3 this loses if it comes after xterm.h. */
36#include <signal.h>
565620a5
RS
37
38#include <stdio.h>
dcfdbac7 39#include "lisp.h"
18686d47 40#include "termhooks.h"
7708e9bd 41#include "frame.h"
dcfdbac7 42#include "window.h"
031b0e31 43#include "keyboard.h"
9ac0d9e0 44#include "blockinput.h"
88766961 45#include "buffer.h"
dcfdbac7 46
eeee3112
RS
47#ifdef MSDOS
48#include "msdos.h"
49#endif
50
87485d6f 51#ifdef HAVE_X_WINDOWS
dcfdbac7
JB
52/* This may include sys/types.h, and that somehow loses
53 if this is not done before the other system files. */
54#include "xterm.h"
87485d6f 55#endif
dcfdbac7
JB
56
57/* Load sys/types.h if not already loaded.
58 In some systems loading it twice is suicidal. */
59#ifndef makedev
60#include <sys/types.h>
61#endif
62
63#include "dispextern.h"
64
87485d6f 65#ifdef HAVE_X_WINDOWS
703dc2a8 66#undef HAVE_MULTILINGUAL_MENU
18686d47
RS
67#ifdef USE_X_TOOLKIT
68#include <X11/Xlib.h>
69#include <X11/IntrinsicP.h>
70#include <X11/CoreP.h>
71#include <X11/StringDefs.h>
60f312e2 72#include <X11/Shell.h>
5ee80bd3 73#ifdef USE_LUCID
1d1c1567 74#include <X11/Xaw/Paned.h>
5ee80bd3 75#endif /* USE_LUCID */
18686d47 76#include "../lwlib/lwlib.h"
a352a815
RS
77#else /* not USE_X_TOOLKIT */
78#include "../oldXMenu/XMenu.h"
79#endif /* not USE_X_TOOLKIT */
80#endif /* HAVE_X_WINDOWS */
18686d47 81
3427a3db
GM
82#ifdef USE_MOTIF
83#include <Xm/Xm.h> /* for LESSTIF_VERSION */
84#endif
85
dcfdbac7
JB
86#define min(x,y) (((x) < (y)) ? (x) : (y))
87#define max(x,y) (((x) > (y)) ? (x) : (y))
88
dcfdbac7
JB
89#ifndef TRUE
90#define TRUE 1
91#define FALSE 0
78589e07 92#endif /* no TRUE */
dcfdbac7 93
bfc524bc
RS
94Lisp_Object Vmenu_updating_frame;
95
0314aacb
RS
96Lisp_Object Qdebug_on_next_call;
97
18686d47 98extern Lisp_Object Qmenu_bar;
92280f67 99extern Lisp_Object Qmouse_click, Qevent_kind;
78589e07 100
de57a39c 101extern Lisp_Object QCtoggle, QCradio;
8fbc986d 102
88766961
RS
103extern Lisp_Object Voverriding_local_map;
104extern Lisp_Object Voverriding_local_map_menu_flag;
105
106extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
107
108extern Lisp_Object Qmenu_bar_update_hook;
109
18686d47 110#ifdef USE_X_TOOLKIT
88766961 111extern void set_frame_menubar ();
78589e07
RS
112extern void process_expose_from_menu ();
113extern XtAppContext Xt_app_con;
114
165e1749 115static Lisp_Object xdialog_show ();
4dedbfe0 116void popup_get_selection ();
18686d47
RS
117#endif
118
3427a3db
GM
119#ifdef USE_X_TOOLKIT
120
121/* Define HAVE_BOXES if meus can handle radio and toggle buttons. */
122
123#define HAVE_BOXES 1
124#endif
125
126static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
127 Lisp_Object, Lisp_Object, Lisp_Object,
850df50b 128 Lisp_Object, Lisp_Object));
78589e07
RS
129static Lisp_Object xmenu_show ();
130static void keymap_panes ();
131static void single_keymap_panes ();
de57a39c 132static void single_menu_item ();
78589e07
RS
133static void list_of_panes ();
134static void list_of_items ();
135\f
136/* This holds a Lisp vector that holds the results of decoding
137 the keymaps or alist-of-alists that specify a menu.
dcfdbac7 138
78589e07 139 It describes the panes and items within the panes.
dcfdbac7 140
78589e07
RS
141 Each pane is described by 3 elements in the vector:
142 t, the pane name, the pane's prefix key.
a352a815 143 Then follow the pane's items, with 5 elements per item:
78589e07 144 the item string, the enable flag, the item's value,
a352a815 145 the definition, and the equivalent keyboard key's description string.
dcfdbac7 146
101bb4a5
RS
147 In some cases, multiple levels of menus may be described.
148 A single vector slot containing nil indicates the start of a submenu.
149 A single vector slot containing lambda indicates the end of a submenu.
150 The submenu follows a menu item which is the way to reach the submenu.
151
fcaa7665
RS
152 A single vector slot containing quote indicates that the
153 following items should appear on the right of a dialog box.
154
78589e07
RS
155 Using a Lisp vector to hold this information while we decode it
156 takes care of protecting all the data from GC. */
dcfdbac7 157
78589e07
RS
158#define MENU_ITEMS_PANE_NAME 1
159#define MENU_ITEMS_PANE_PREFIX 2
160#define MENU_ITEMS_PANE_LENGTH 3
088831f6 161
850df50b
GM
162enum menu_item_idx
163{
164 MENU_ITEMS_ITEM_NAME = 0,
165 MENU_ITEMS_ITEM_ENABLE,
166 MENU_ITEMS_ITEM_VALUE,
167 MENU_ITEMS_ITEM_EQUIV_KEY,
168 MENU_ITEMS_ITEM_DEFINITION,
169 MENU_ITEMS_ITEM_TYPE,
170 MENU_ITEMS_ITEM_SELECTED,
171 MENU_ITEMS_ITEM_HELP,
172 MENU_ITEMS_ITEM_LENGTH
173};
7da99777 174
78589e07 175static Lisp_Object menu_items;
18686d47 176
78589e07
RS
177/* Number of slots currently allocated in menu_items. */
178static int menu_items_allocated;
18686d47 179
78589e07
RS
180/* This is the index in menu_items of the first empty slot. */
181static int menu_items_used;
18686d47 182
101bb4a5
RS
183/* The number of panes currently recorded in menu_items,
184 excluding those within submenus. */
78589e07 185static int menu_items_n_panes;
18686d47 186
101bb4a5
RS
187/* Current depth within submenus. */
188static int menu_items_submenu_depth;
189
4dedbfe0 190/* Flag which when set indicates a dialog or menu has been posted by
c98fcf4b 191 Xt on behalf of one of the widget sets. */
7555d825 192int popup_activated_flag;
4dedbfe0 193
88766961 194static int next_menubar_widget_id;
745c34fb 195
a9be6839
RS
196/* This is set nonzero after the user activates the menu bar, and set
197 to zero again after the menu bars are redisplayed by prepare_menu_bar.
198 While it is nonzero, all calls to set_frame_menubar go deep.
199
200 I don't understand why this is needed, but it does seem to be
201 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
202
203int pending_menu_activation;
bd3a4da2 204\f
88766961 205#ifdef USE_X_TOOLKIT
bd3a4da2 206
7556890b 207/* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
bd3a4da2 208
88766961
RS
209static struct frame *
210menubar_id_to_frame (id)
211 LWLIB_ID id;
212{
213 Lisp_Object tail, frame;
214 FRAME_PTR f;
bd3a4da2 215
8e713be6 216 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
bd3a4da2 217 {
8e713be6 218 frame = XCAR (tail);
88766961
RS
219 if (!GC_FRAMEP (frame))
220 continue;
221 f = XFRAME (frame);
2d764c78 222 if (!FRAME_WINDOW_P (f))
88766961 223 continue;
7556890b 224 if (f->output_data.x->id == id)
88766961 225 return f;
bd3a4da2 226 }
88766961 227 return 0;
bd3a4da2 228}
88766961
RS
229
230#endif
4dedbfe0 231\f
78589e07
RS
232/* Initialize the menu_items structure if we haven't already done so.
233 Also mark it as currently empty. */
234
235static void
236init_menu_items ()
237{
238 if (NILP (menu_items))
239 {
240 menu_items_allocated = 60;
241 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
18686d47
RS
242 }
243
78589e07
RS
244 menu_items_used = 0;
245 menu_items_n_panes = 0;
101bb4a5 246 menu_items_submenu_depth = 0;
78589e07 247}
18686d47 248
78589e07
RS
249/* Call at the end of generating the data in menu_items.
250 This fills in the number of items in the last pane. */
1658603c 251
78589e07
RS
252static void
253finish_menu_items ()
254{
255}
1658603c 256
78589e07
RS
257/* Call when finished using the data for the current menu
258 in menu_items. */
1658603c 259
78589e07
RS
260static void
261discard_menu_items ()
262{
263 /* Free the structure if it is especially large.
264 Otherwise, hold on to it, to save time. */
265 if (menu_items_allocated > 200)
266 {
267 menu_items = Qnil;
268 menu_items_allocated = 0;
269 }
270}
1658603c 271
101bb4a5
RS
272/* Make the menu_items vector twice as large. */
273
274static void
275grow_menu_items ()
276{
277 Lisp_Object old;
278 int old_size = menu_items_allocated;
279 old = menu_items;
280
281 menu_items_allocated *= 2;
282 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
283 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
284 old_size * sizeof (Lisp_Object));
285}
286
287/* Begin a submenu. */
288
289static void
290push_submenu_start ()
291{
292 if (menu_items_used + 1 > menu_items_allocated)
293 grow_menu_items ();
294
295 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
296 menu_items_submenu_depth++;
297}
298
299/* End a submenu. */
300
301static void
302push_submenu_end ()
303{
304 if (menu_items_used + 1 > menu_items_allocated)
305 grow_menu_items ();
306
307 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
308 menu_items_submenu_depth--;
309}
310
fcaa7665
RS
311/* Indicate boundary between left and right. */
312
313static void
314push_left_right_boundary ()
315{
316 if (menu_items_used + 1 > menu_items_allocated)
317 grow_menu_items ();
318
319 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
320}
321
78589e07
RS
322/* Start a new menu pane in menu_items..
323 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
1658603c 324
78589e07
RS
325static void
326push_menu_pane (name, prefix_vec)
327 Lisp_Object name, prefix_vec;
328{
329 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
101bb4a5 330 grow_menu_items ();
dcfdbac7 331
101bb4a5
RS
332 if (menu_items_submenu_depth == 0)
333 menu_items_n_panes++;
78589e07
RS
334 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
335 XVECTOR (menu_items)->contents[menu_items_used++] = name;
336 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
337}
dcfdbac7 338
3427a3db
GM
339/* Push one menu item into the current pane. NAME is the string to
340 display. ENABLE if non-nil means this item can be selected. KEY
341 is the key generated by choosing this item, or nil if this item
342 doesn't really have a definition. DEF is the definition of this
343 item. EQUIV is the textual description of the keyboard equivalent
344 for this item (or nil if none). TYPE is the type of this menu
345 item, one of nil, `toggle' or `radio'. */
18686d47 346
78589e07 347static void
850df50b
GM
348push_menu_item (name, enable, key, def, equiv, type, selected, help)
349 Lisp_Object name, enable, key, def, equiv, type, selected, help;
78589e07
RS
350{
351 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
101bb4a5 352 grow_menu_items ();
088831f6 353
78589e07
RS
354 XVECTOR (menu_items)->contents[menu_items_used++] = name;
355 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
356 XVECTOR (menu_items)->contents[menu_items_used++] = key;
357 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
a352a815 358 XVECTOR (menu_items)->contents[menu_items_used++] = def;
3427a3db
GM
359 XVECTOR (menu_items)->contents[menu_items_used++] = type;
360 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
850df50b 361 XVECTOR (menu_items)->contents[menu_items_used++] = help;
78589e07
RS
362}
363\f
78589e07 364/* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
101bb4a5
RS
365 and generate menu panes for them in menu_items.
366 If NOTREAL is nonzero,
367 don't bother really computing whether an item is enabled. */
18686d47 368
78589e07 369static void
101bb4a5 370keymap_panes (keymaps, nmaps, notreal)
78589e07
RS
371 Lisp_Object *keymaps;
372 int nmaps;
101bb4a5 373 int notreal;
18686d47 374{
78589e07 375 int mapno;
18686d47 376
78589e07 377 init_menu_items ();
18686d47 378
78589e07
RS
379 /* Loop over the given keymaps, making a pane for each map.
380 But don't make a pane that is empty--ignore that map instead.
381 P is the number of panes we have made so far. */
382 for (mapno = 0; mapno < nmaps; mapno++)
488205c0
EZ
383 single_keymap_panes (keymaps[mapno],
384 map_prompt (keymaps[mapno]), Qnil, notreal, 10);
78589e07
RS
385
386 finish_menu_items ();
387}
388
389/* This is a recursive subroutine of keymap_panes.
390 It handles one keymap, KEYMAP.
391 The other arguments are passed along
101bb4a5 392 or point to local variables of the previous function.
de57a39c
RS
393 If NOTREAL is nonzero, only check for equivalent key bindings, don't
394 evaluate expressions in menu items and don't make any menu.
98381e42
RS
395
396 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
78589e07
RS
397
398static void
98381e42 399single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
78589e07
RS
400 Lisp_Object keymap;
401 Lisp_Object pane_name;
402 Lisp_Object prefix;
101bb4a5 403 int notreal;
98381e42 404 int maxdepth;
78589e07 405{
de57a39c
RS
406 Lisp_Object pending_maps = Qnil;
407 Lisp_Object tail, item;
408 struct gcpro gcpro1, gcpro2;
409 int notbuttons = 0;
78589e07 410
98381e42
RS
411 if (maxdepth <= 0)
412 return;
413
78589e07
RS
414 push_menu_pane (pane_name, prefix);
415
de57a39c
RS
416#ifndef HAVE_BOXES
417 /* Remember index for first item in this pane so we can go back and
418 add a prefix when (if) we see the first button. After that, notbuttons
419 is set to 0, to mark that we have seen a button and all non button
420 items need a prefix. */
421 notbuttons = menu_items_used;
422#endif
423
8e713be6 424 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
18686d47 425 {
de57a39c
RS
426 GCPRO2 (keymap, pending_maps);
427 /* Look at each key binding, and if it is a menu item add it
428 to this menu. */
8e713be6 429 item = XCAR (tail);
b5bb2705 430 if (CONSP (item))
8e713be6 431 single_menu_item (XCAR (item), XCDR (item),
de57a39c 432 &pending_maps, notreal, maxdepth, &notbuttons);
b5bb2705 433 else if (VECTORP (item))
78589e07
RS
434 {
435 /* Loop over the char values represented in the vector. */
436 int len = XVECTOR (item)->size;
437 int c;
438 for (c = 0; c < len; c++)
439 {
440 Lisp_Object character;
33b43fa6 441 XSETFASTINT (character, c);
de57a39c
RS
442 single_menu_item (character, XVECTOR (item)->contents[c],
443 &pending_maps, notreal, maxdepth, &notbuttons);
78589e07 444 }
18686d47 445 }
de57a39c 446 UNGCPRO;
18686d47 447 }
78589e07
RS
448
449 /* Process now any submenus which want to be panes at this level. */
450 while (!NILP (pending_maps))
451 {
101bb4a5 452 Lisp_Object elt, eltcdr, string;
78589e07 453 elt = Fcar (pending_maps);
8e713be6
KR
454 eltcdr = XCDR (elt);
455 string = XCAR (eltcdr);
101bb4a5
RS
456 /* We no longer discard the @ from the beginning of the string here.
457 Instead, we do this in xmenu_show. */
458 single_keymap_panes (Fcar (elt), string,
8e713be6 459 XCDR (eltcdr), notreal, maxdepth - 1);
78589e07
RS
460 pending_maps = Fcdr (pending_maps);
461 }
18686d47 462}
78589e07 463\f
de57a39c
RS
464/* This is a subroutine of single_keymap_panes that handles one
465 keymap entry.
466 KEY is a key in a keymap and ITEM is its binding.
467 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
468 separate panes.
469 If NOTREAL is nonzero, only check for equivalent key bindings, don't
470 evaluate expressions in menu items and don't make any menu.
471 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
472 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
473 buttons. It points to variable notbuttons in single_keymap_panes,
474 which keeps track of if we have seen a button in this menu or not. */
475
476static void
477single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth,
478 notbuttons_ptr)
479 Lisp_Object key, item;
480 Lisp_Object *pending_maps_ptr;
481 int maxdepth, notreal;
482 int *notbuttons_ptr;
483{
faa935b6 484 Lisp_Object map, item_string, enabled;
de57a39c
RS
485 struct gcpro gcpro1, gcpro2;
486 int res;
487
488 /* Parse the menu item and leave the result in item_properties. */
489 GCPRO2 (key, item);
490 res = parse_menu_item (item, notreal, 0);
491 UNGCPRO;
492 if (!res)
493 return; /* Not a menu item. */
494
495 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
496
497 if (notreal)
498 {
499 /* We don't want to make a menu, just traverse the keymaps to
500 precompute equivalent key bindings. */
501 if (!NILP (map))
502 single_keymap_panes (map, Qnil, key, 1, maxdepth - 1);
503 return;
504 }
505
506 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
507 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
508
509 if (!NILP (map) && XSTRING (item_string)->data[0] == '@')
510 {
511 if (!NILP (enabled))
512 /* An enabled separate pane. Remember this to handle it later. */
513 *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)),
514 *pending_maps_ptr);
515 return;
516 }
517
518#ifndef HAVE_BOXES
519 /* Simulate radio buttons and toggle boxes by putting a prefix in
520 front of them. */
521 {
522 Lisp_Object prefix = Qnil;
523 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
524 if (!NILP (type))
525 {
526 Lisp_Object selected
527 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
528
529 if (*notbuttons_ptr)
530 /* The first button. Line up previous items in this menu. */
531 {
532 int index = *notbuttons_ptr; /* Index for first item this menu. */
533 int submenu = 0;
534 Lisp_Object tem;
535 while (index < menu_items_used)
536 {
537 tem
538 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
539 if (NILP (tem))
540 {
541 index++;
542 submenu++; /* Skip sub menu. */
543 }
544 else if (EQ (tem, Qlambda))
545 {
546 index++;
547 submenu--; /* End sub menu. */
548 }
549 else if (EQ (tem, Qt))
550 index += 3; /* Skip new pane marker. */
551 else if (EQ (tem, Qquote))
552 index++; /* Skip a left, right divider. */
553 else
554 {
555 if (!submenu && XSTRING (tem)->data[0] != '\0'
556 && XSTRING (tem)->data[0] != '-')
557 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
558 = concat2 (build_string (" "), tem);
559 index += MENU_ITEMS_ITEM_LENGTH;
560 }
561 }
562 *notbuttons_ptr = 0;
563 }
564
565 /* Calculate prefix, if any, for this item. */
566 if (EQ (type, QCtoggle))
567 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
568 else if (EQ (type, QCradio))
569 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
570 }
571 /* Not a button. If we have earlier buttons, then we need a prefix. */
572 else if (!*notbuttons_ptr && XSTRING (item_string)->data[0] != '\0'
573 && XSTRING (item_string)->data[0] != '-')
574 prefix = build_string (" ");
575
576 if (!NILP (prefix))
577 item_string = concat2 (prefix, item_string);
578 }
579#endif /* not HAVE_BOXES */
580
581#ifndef USE_X_TOOLKIT
582 if (!NILP(map))
583 /* Indicate visually that this is a submenu. */
584 item_string = concat2 (item_string, build_string (" >"));
585#endif
586
587 push_menu_item (item_string, enabled, key,
588 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
3427a3db
GM
589 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
590 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
850df50b
GM
591 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
592 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
de57a39c
RS
593
594#ifdef USE_X_TOOLKIT
595 /* Display a submenu using the toolkit. */
596 if (! (NILP (map) || NILP (enabled)))
597 {
598 push_submenu_start ();
599 single_keymap_panes (map, Qnil, key, 0, maxdepth - 1);
600 push_submenu_end ();
601 }
602#endif
603}
604\f
8e6208c5 605/* Push all the panes and items of a menu described by the
78589e07
RS
606 alist-of-alists MENU.
607 This handles old-fashioned calls to x-popup-menu. */
18686d47 608
78589e07
RS
609static void
610list_of_panes (menu)
18686d47 611 Lisp_Object menu;
18686d47 612{
78589e07
RS
613 Lisp_Object tail;
614
615 init_menu_items ();
616
617 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
618 {
619 Lisp_Object elt, pane_name, pane_data;
620 elt = Fcar (tail);
621 pane_name = Fcar (elt);
622 CHECK_STRING (pane_name, 0);
623 push_menu_pane (pane_name, Qnil);
624 pane_data = Fcdr (elt);
625 CHECK_CONS (pane_data, 0);
626 list_of_items (pane_data);
627 }
628
629 finish_menu_items ();
630}
631
632/* Push the items in a single pane defined by the alist PANE. */
633
634static void
635list_of_items (pane)
636 Lisp_Object pane;
637{
638 Lisp_Object tail, item, item1;
639
640 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
641 {
642 item = Fcar (tail);
643 if (STRINGP (item))
850df50b 644 push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil);
fcaa7665
RS
645 else if (NILP (item))
646 push_left_right_boundary ();
78589e07
RS
647 else
648 {
649 CHECK_CONS (item, 0);
650 item1 = Fcar (item);
651 CHECK_STRING (item1, 1);
850df50b 652 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil);
78589e07
RS
653 }
654 }
655}
656\f
540e52d1 657DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
78589e07
RS
658 "Pop up a deck-of-cards menu and return user's selection.\n\
659POSITION is a position specification. This is either a mouse button event\n\
660or a list ((XOFFSET YOFFSET) WINDOW)\n\
748a0e1f 661where XOFFSET and YOFFSET are positions in pixels from the top left\n\
78589e07
RS
662corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
663This controls the position of the center of the first line\n\
664in the first pane of the menu, not the top left of the menu as a whole.\n\
665If POSITION is t, it means to use the current mouse position.\n\
666\n\
667MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
668The menu items come from key bindings that have a menu string as well as\n\
669a definition; actually, the \"definition\" in such a key binding looks like\n\
670\(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
671the keymap as a top-level element.\n\n\
a7ebd443
RS
672If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
673Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
674\n\
78589e07
RS
675You can also use a list of keymaps as MENU.\n\
676 Then each keymap makes a separate pane.\n\
677When MENU is a keymap or a list of keymaps, the return value\n\
678is a list of events.\n\n\
a7ebd443 679\n\
78589e07
RS
680Alternatively, you can specify a menu of multiple panes\n\
681 with a list of the form (TITLE PANE1 PANE2...),\n\
682where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
683Each ITEM is normally a cons cell (STRING . VALUE);\n\
684but a string can appear as an item--that makes a nonselectable line\n\
685in the menu.\n\
686With this form of menu, the return value is VALUE from the chosen item.\n\
687\n\
688If POSITION is nil, don't display the menu at all, just precalculate the\n\
689cached information about equivalent key sequences.")
690 (position, menu)
691 Lisp_Object position, menu;
692{
18686d47 693 Lisp_Object keymap, tem;
78589e07
RS
694 int xpos, ypos;
695 Lisp_Object title;
696 char *error_name;
697 Lisp_Object selection;
78589e07
RS
698 FRAME_PTR f;
699 Lisp_Object x, y, window;
700 int keymaps = 0;
9685a93f 701 int for_click = 0;
78589e07
RS
702 struct gcpro gcpro1;
703
1e659e4c 704#ifdef HAVE_MENUS
78589e07
RS
705 if (! NILP (position))
706 {
101bb4a5
RS
707 check_x ();
708
78589e07 709 /* Decode the first argument: find the window and the coordinates. */
9572375b 710 if (EQ (position, Qt)
8e713be6 711 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
78589e07
RS
712 {
713 /* Use the mouse's current position. */
b404828f 714 FRAME_PTR new_f = SELECTED_FRAME ();
78589e07 715 Lisp_Object bar_window;
dfcf069d 716 enum scroll_bar_part part;
78589e07
RS
717 unsigned long time;
718
b137c582 719 if (mouse_position_hook)
46b657e2
RS
720 (*mouse_position_hook) (&new_f, 1, &bar_window,
721 &part, &x, &y, &time);
5ca2ef64 722 if (new_f != 0)
a39f0477 723 XSETFRAME (window, new_f);
5ca2ef64
RS
724 else
725 {
726 window = selected_window;
33b43fa6
KH
727 XSETFASTINT (x, 0);
728 XSETFASTINT (y, 0);
5ca2ef64 729 }
78589e07
RS
730 }
731 else
732 {
733 tem = Fcar (position);
b5bb2705 734 if (CONSP (tem))
78589e07
RS
735 {
736 window = Fcar (Fcdr (position));
737 x = Fcar (tem);
738 y = Fcar (Fcdr (tem));
739 }
740 else
741 {
9685a93f 742 for_click = 1;
78589e07
RS
743 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
744 window = Fcar (tem); /* POSN_WINDOW (tem) */
745 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
746 x = Fcar (tem);
747 y = Fcdr (tem);
78589e07
RS
748 }
749 }
750
751 CHECK_NUMBER (x, 0);
752 CHECK_NUMBER (y, 0);
753
754 /* Decode where to put the menu. */
755
b5bb2705 756 if (FRAMEP (window))
78589e07
RS
757 {
758 f = XFRAME (window);
78589e07
RS
759 xpos = 0;
760 ypos = 0;
761 }
b5bb2705 762 else if (WINDOWP (window))
78589e07
RS
763 {
764 CHECK_LIVE_WINDOW (window, 0);
765 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
766
99d3fac7
KH
767 xpos = (FONT_WIDTH (f->output_data.x->font)
768 * XFASTINT (XWINDOW (window)->left));
769 ypos = (f->output_data.x->line_height
770 * XFASTINT (XWINDOW (window)->top));
78589e07
RS
771 }
772 else
773 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
774 but I don't want to make one now. */
775 CHECK_WINDOW (window, 0);
776
777 xpos += XINT (x);
778 ypos += XINT (y);
bfc524bc
RS
779
780 XSETFRAME (Vmenu_updating_frame, f);
78589e07 781 }
bfc524bc 782 Vmenu_updating_frame = Qnil;
1e659e4c 783#endif /* HAVE_MENUS */
78589e07
RS
784
785 title = Qnil;
786 GCPRO1 (title);
787
788 /* Decode the menu items from what was specified. */
18686d47
RS
789
790 keymap = Fkeymapp (menu);
791 tem = Qnil;
b5bb2705 792 if (CONSP (menu))
18686d47
RS
793 tem = Fkeymapp (Fcar (menu));
794 if (!NILP (keymap))
795 {
796 /* We were given a keymap. Extract menu info from the keymap. */
797 Lisp_Object prompt;
798 keymap = get_keymap (menu);
799
78589e07 800 /* Extract the detailed info to make one pane. */
101bb4a5 801 keymap_panes (&menu, 1, NILP (position));
78589e07 802
18686d47
RS
803 /* Search for a string appearing directly as an element of the keymap.
804 That string is the title of the menu. */
805 prompt = map_prompt (keymap);
4f9ad016
RS
806 if (NILP (title) && !NILP (prompt))
807 title = prompt;
18686d47 808
78589e07
RS
809 /* Make that be the pane title of the first pane. */
810 if (!NILP (prompt) && menu_items_n_panes >= 0)
811 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
812
813 keymaps = 1;
18686d47
RS
814 }
815 else if (!NILP (tem))
816 {
817 /* We were given a list of keymaps. */
18686d47
RS
818 int nmaps = XFASTINT (Flength (menu));
819 Lisp_Object *maps
820 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
821 int i;
78589e07
RS
822
823 title = Qnil;
18686d47
RS
824
825 /* The first keymap that has a prompt string
826 supplies the menu title. */
b5bb2705 827 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
18686d47 828 {
78589e07
RS
829 Lisp_Object prompt;
830
18686d47
RS
831 maps[i++] = keymap = get_keymap (Fcar (tem));
832
833 prompt = map_prompt (keymap);
78589e07
RS
834 if (NILP (title) && !NILP (prompt))
835 title = prompt;
18686d47
RS
836 }
837
838 /* Extract the detailed info to make one pane. */
101bb4a5 839 keymap_panes (maps, nmaps, NILP (position));
78589e07
RS
840
841 /* Make the title be the pane title of the first pane. */
842 if (!NILP (title) && menu_items_n_panes >= 0)
843 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
844
845 keymaps = 1;
18686d47
RS
846 }
847 else
848 {
849 /* We were given an old-fashioned menu. */
78589e07
RS
850 title = Fcar (menu);
851 CHECK_STRING (title, 1);
18686d47 852
78589e07 853 list_of_panes (Fcdr (menu));
18686d47 854
78589e07
RS
855 keymaps = 0;
856 }
18686d47 857
78589e07 858 if (NILP (position))
18686d47 859 {
78589e07
RS
860 discard_menu_items ();
861 UNGCPRO;
862 return Qnil;
18686d47
RS
863 }
864
1e659e4c 865#ifdef HAVE_MENUS
78589e07
RS
866 /* Display them in a menu. */
867 BLOCK_INPUT;
18686d47 868
673a6211 869 selection = xmenu_show (f, xpos, ypos, for_click,
78589e07
RS
870 keymaps, title, &error_name);
871 UNBLOCK_INPUT;
18686d47 872
78589e07 873 discard_menu_items ();
18686d47 874
78589e07 875 UNGCPRO;
1e659e4c 876#endif /* HAVE_MENUS */
18686d47 877
78589e07
RS
878 if (error_name) error (error_name);
879 return selection;
18686d47 880}
165e1749 881
1e659e4c
RS
882#ifdef HAVE_MENUS
883
540e52d1 884DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
165e1749 885 "Pop up a dialog box and return user's selection.\n\
99fe880d
RS
886POSITION specifies which frame to use.\n\
887This is normally a mouse button event or a window or frame.\n\
888If POSITION is t, it means to use the frame the mouse is on.\n\
889The dialog box appears in the middle of the specified frame.\n\
165e1749 890\n\
99fe880d
RS
891CONTENTS specifies the alternatives to display in the dialog box.\n\
892It is a list of the form (TITLE ITEM1 ITEM2...).\n\
893Each ITEM is a cons cell (STRING . VALUE).\n\
fcaa7665
RS
894The return value is VALUE from the chosen item.\n\n\
895An ITEM may also be just a string--that makes a nonselectable item.\n\
896An ITEM may also be nil--that means to put all preceding items\n\
897on the left of the dialog box and all following items on the right.\n\
898\(By default, approximately half appear on each side.)")
99fe880d
RS
899 (position, contents)
900 Lisp_Object position, contents;
165e1749 901{
165e1749 902 FRAME_PTR f;
99fe880d 903 Lisp_Object window;
165e1749
FP
904
905 check_x ();
906
99fe880d 907 /* Decode the first argument: find the window or frame to use. */
88d4f6ec 908 if (EQ (position, Qt)
8e713be6 909 || (CONSP (position) && EQ (XCAR (position), Qmenu_bar)))
165e1749 910 {
b14db4d7 911#if 0 /* Using the frame the mouse is on may not be right. */
99fe880d 912 /* Use the mouse's current position. */
b404828f 913 FRAME_PTR new_f = SELECTED_FRAME ();
99fe880d
RS
914 Lisp_Object bar_window;
915 int part;
916 unsigned long time;
917 Lisp_Object x, y;
165e1749 918
46b657e2 919 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
5ca2ef64 920
99fe880d 921 if (new_f != 0)
a39f0477 922 XSETFRAME (window, new_f);
99fe880d
RS
923 else
924 window = selected_window;
b14db4d7 925#endif
88d4f6ec 926 window = selected_window;
99fe880d
RS
927 }
928 else if (CONSP (position))
929 {
930 Lisp_Object tem;
931 tem = Fcar (position);
b5bb2705 932 if (CONSP (tem))
99fe880d 933 window = Fcar (Fcdr (position));
80670155
RS
934 else
935 {
99fe880d
RS
936 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
937 window = Fcar (tem); /* POSN_WINDOW (tem) */
165e1749 938 }
165e1749 939 }
99fe880d
RS
940 else if (WINDOWP (position) || FRAMEP (position))
941 window = position;
a9be6839
RS
942 else
943 window = Qnil;
165e1749 944
99fe880d 945 /* Decode where to put the menu. */
165e1749 946
b5bb2705 947 if (FRAMEP (window))
99fe880d 948 f = XFRAME (window);
b5bb2705 949 else if (WINDOWP (window))
165e1749 950 {
99fe880d
RS
951 CHECK_LIVE_WINDOW (window, 0);
952 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
165e1749 953 }
99fe880d
RS
954 else
955 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
956 but I don't want to make one now. */
957 CHECK_WINDOW (window, 0);
165e1749 958
99fe880d
RS
959#ifndef USE_X_TOOLKIT
960 /* Display a menu with these alternatives
961 in the middle of frame F. */
962 {
963 Lisp_Object x, y, frame, newpos;
a39f0477
KH
964 XSETFRAME (frame, f);
965 XSETINT (x, x_pixel_width (f) / 2);
966 XSETINT (y, x_pixel_height (f) / 2);
99fe880d
RS
967 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
968
969 return Fx_popup_menu (newpos,
970 Fcons (Fcar (contents), Fcons (contents, Qnil)));
971 }
972#else
973 {
974 Lisp_Object title;
975 char *error_name;
976 Lisp_Object selection;
165e1749 977
99fe880d
RS
978 /* Decode the dialog items from what was specified. */
979 title = Fcar (contents);
980 CHECK_STRING (title, 1);
165e1749 981
99fe880d 982 list_of_panes (Fcons (contents, Qnil));
165e1749 983
99fe880d
RS
984 /* Display them in a dialog box. */
985 BLOCK_INPUT;
f8c2630d 986 selection = xdialog_show (f, 0, title, &error_name);
99fe880d 987 UNBLOCK_INPUT;
165e1749 988
99fe880d
RS
989 discard_menu_items ();
990
991 if (error_name) error (error_name);
992 return selection;
993 }
7464b131 994#endif
392d3f4b 995}
78589e07
RS
996\f
997#ifdef USE_X_TOOLKIT
18686d47 998
4dedbfe0 999/* Loop in Xt until the menu pulldown or dialog popup has been
f56e7ad2
RS
1000 popped down (deactivated). This is used for x-popup-menu
1001 and x-popup-dialog; it is not used for the menu bar any more.
c98fcf4b 1002
2e2b8e22 1003 NOTE: All calls to popup_get_selection should be protected
c98fcf4b 1004 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
aa669def 1005
4dedbfe0 1006void
2e2b8e22 1007popup_get_selection (initial_event, dpyinfo, id)
4dedbfe0 1008 XEvent *initial_event;
aa669def 1009 struct x_display_info *dpyinfo;
2e2b8e22 1010 LWLIB_ID id;
78589e07 1011{
4dedbfe0 1012 XEvent event;
78589e07 1013
aa669def
RS
1014 /* Define a queue to save up for later unreading
1015 all X events that don't pertain to the menu. */
1016 struct event_queue
1017 {
1018 XEvent event;
1019 struct event_queue *next;
1020 };
1021
1022 struct event_queue *queue = NULL;
1023 struct event_queue *queue_tmp;
1024
4dedbfe0
PR
1025 if (initial_event)
1026 event = *initial_event;
1027 else
1028 XtAppNextEvent (Xt_app_con, &event);
78589e07 1029
4dedbfe0 1030 while (1)
78589e07 1031 {
aa669def
RS
1032 /* Handle expose events for editor frames right away. */
1033 if (event.type == Expose)
1034 process_expose_from_menu (event);
10624005
KH
1035 /* Make sure we don't consider buttons grabbed after menu goes.
1036 And make sure to deactivate for any ButtonRelease,
1037 even if XtDispatchEvent doesn't do that. */
aa669def
RS
1038 else if (event.type == ButtonRelease
1039 && dpyinfo->display == event.xbutton.display)
10624005
KH
1040 {
1041 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1042 popup_activated_flag = 0;
c8b5aa3d
RS
1043#ifdef USE_MOTIF /* Pretending that the event came from a
1044 Btn1Down seems the only way to convince Motif to
1045 activate its callbacks; setting the XmNmenuPost
1046 isn't working. --marcus@sysc.pdx.edu. */
1047 event.xbutton.button = 1;
1048#endif
10624005 1049 }
2e2b8e22
KH
1050 /* If the user presses a key, deactivate the menu.
1051 The user is likely to do that if we get wedged. */
1052 else if (event.type == KeyPress
1053 && dpyinfo->display == event.xbutton.display)
1054 {
09155689
AS
1055 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1056 if (!IsModifierKey (keysym))
1057 {
1058 popup_activated_flag = 0;
1059 break;
1060 }
2e2b8e22 1061 }
d31d42cc
RS
1062 /* Button presses outside the menu also pop it down. */
1063 else if (event.type == ButtonPress
1064 && event.xany.display == dpyinfo->display
1065 && x_any_window_to_frame (dpyinfo, event.xany.window))
1066 {
1067 popup_activated_flag = 0;
1068 break;
1069 }
aa669def
RS
1070
1071 /* Queue all events not for this popup,
10624005 1072 except for Expose, which we've already handled, and ButtonRelease.
9572375b
KH
1073 Note that the X window is associated with the frame if this
1074 is a menu bar popup, but not if it's a dialog box. So we use
1075 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
aa669def 1076 if (event.type != Expose
10624005
KH
1077 && !(event.type == ButtonRelease
1078 && dpyinfo->display == event.xbutton.display)
aa669def 1079 && (event.xany.display != dpyinfo->display
9572375b 1080 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
aa669def 1081 {
aa669def
RS
1082 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1083
1084 if (queue_tmp != NULL)
1085 {
1086 queue_tmp->event = event;
1087 queue_tmp->next = queue;
1088 queue = queue_tmp;
1089 }
1090 }
9572375b
KH
1091 else
1092 XtDispatchEvent (&event);
aa669def
RS
1093
1094 if (!popup_activated ())
4dedbfe0
PR
1095 break;
1096 XtAppNextEvent (Xt_app_con, &event);
78589e07 1097 }
aa669def
RS
1098
1099 /* Unread any events that we got but did not handle. */
1100 while (queue != NULL)
1101 {
1102 queue_tmp = queue;
1103 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1104 queue = queue_tmp->next;
1105 free ((char *)queue_tmp);
1106 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1107 interrupt_input_pending = 1;
1108 }
78589e07
RS
1109}
1110
88766961
RS
1111/* Activate the menu bar of frame F.
1112 This is called from keyboard.c when it gets the
1113 menu_bar_activate_event out of the Emacs event queue.
1114
1115 To activate the menu bar, we use the X button-press event
ac78b144 1116 that was saved in saved_menu_event.
88766961
RS
1117 That makes the toolkit do its thing.
1118
1119 But first we recompute the menu bar contents (the whole tree).
1120
1121 The reason for saving the button event until here, instead of
1122 passing it to the toolkit right away, is that we can safely
1123 execute Lisp code. */
1124
dfcf069d 1125void
88766961
RS
1126x_activate_menubar (f)
1127 FRAME_PTR f;
1128{
ac78b144 1129 if (!f->output_data.x->saved_menu_event->type)
88766961
RS
1130 return;
1131
a9be6839 1132 set_frame_menubar (f, 0, 1);
88766961 1133 BLOCK_INPUT;
ac78b144 1134 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
88766961 1135 UNBLOCK_INPUT;
a9be6839 1136#ifdef USE_MOTIF
745c34fb
RS
1137 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1138 pending_menu_activation = 1;
a9be6839 1139#endif
745c34fb 1140
88766961 1141 /* Ignore this if we get it a second time. */
ac78b144 1142 f->output_data.x->saved_menu_event->type = 0;
88766961
RS
1143}
1144
c98fcf4b 1145/* Detect if a dialog or menu has been posted. */
aa669def 1146
4dedbfe0
PR
1147int
1148popup_activated ()
1149{
1150 return popup_activated_flag;
1151}
1152
4dedbfe0
PR
1153/* This callback is invoked when the user selects a menubar cascade
1154 pushbutton, but before the pulldown menu is posted. */
78589e07
RS
1155
1156static void
4dedbfe0 1157popup_activate_callback (widget, id, client_data)
78589e07
RS
1158 Widget widget;
1159 LWLIB_ID id;
1160 XtPointer client_data;
1161{
7555d825
GM
1162#ifdef USE_MOTIF
1163 ++popup_activated_flag;
1164#else
4dedbfe0 1165 popup_activated_flag = 1;
7555d825
GM
1166#endif
1167}
1168
1169/* This callback is invoked when a dialog or menu is finished being
1170 used and has been unposted. */
1171
1172static void
1173popup_deactivate_callback (widget, id, client_data)
1174 Widget widget;
1175 LWLIB_ID id;
1176 XtPointer client_data;
1177{
1178#ifdef USE_MOTIF
1179 --popup_activated_flag;
1180#else
1181 popup_activated_flag = 0;
1182#endif
78589e07
RS
1183}
1184
850df50b
GM
1185/* Lwlib callback called when menu items are highlighted/unhighlighted
1186 while moving the mouse over them. WIDGET is the menu bar or menu
1187 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1188 the widget_value structure for the menu item, or null in case of
1189 unhighlighting. */
1190
1191void
1192menu_highlight_callback (widget, id, call_data)
1193 Widget widget;
1194 LWLIB_ID id;
1195 void *call_data;
1196{
1197 widget_value *wv = (widget_value *) call_data;
1198 struct frame *f;
1199 Lisp_Object frame, help;
850df50b 1200
9cd50434
GM
1201 help = wv && wv->help ? build_string (wv->help) : Qnil;
1202
850df50b
GM
1203 /* Determine the frame for the help event. */
1204 f = menubar_id_to_frame (id);
1205 if (f)
9cd50434
GM
1206 {
1207 XSETFRAME (frame, f);
1208 kbd_buffer_store_help_event (frame, help);
1209 }
850df50b
GM
1210 else
1211 {
1212 /* WIDGET is the popup menu. It's parent is the frame's
1213 widget. See which frame that is. */
1214 Widget frame_widget = XtParent (widget);
1215 Lisp_Object tail;
1216
1217 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1218 {
1219 frame = XCAR (tail);
1220 if (GC_FRAMEP (frame)
1221 && (f = XFRAME (frame),
1222 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1223 break;
1224 }
850df50b 1225
9cd50434
GM
1226 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1227 }
850df50b
GM
1228}
1229
4dedbfe0
PR
1230/* This callback is called from the menu bar pulldown menu
1231 when the user makes a selection.
1232 Figure out what the user chose
1233 and put the appropriate events into the keyboard buffer. */
1234
78589e07 1235static void
4dedbfe0 1236menubar_selection_callback (widget, id, client_data)
78589e07
RS
1237 Widget widget;
1238 LWLIB_ID id;
1239 XtPointer client_data;
1240{
c63f6952 1241 Lisp_Object prefix, entry;
88766961 1242 FRAME_PTR f = menubar_id_to_frame (id);
4dedbfe0
PR
1243 Lisp_Object vector;
1244 Lisp_Object *subprefix_stack;
1245 int submenu_depth = 0;
1246 int i;
1247
1248 if (!f)
1249 return;
1250 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1251 vector = f->menu_bar_vector;
1252 prefix = Qnil;
1253 i = 0;
1254 while (i < f->menu_bar_items_used)
1255 {
4dedbfe0
PR
1256 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1257 {
1258 subprefix_stack[submenu_depth++] = prefix;
1259 prefix = entry;
1260 i++;
1261 }
1262 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1263 {
1264 prefix = subprefix_stack[--submenu_depth];
1265 i++;
1266 }
1267 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1268 {
4cb35c39 1269 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
4dedbfe0
PR
1270 i += MENU_ITEMS_PANE_LENGTH;
1271 }
1272 else
1273 {
4cb35c39 1274 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
01d5e892
RS
1275 /* The EMACS_INT cast avoids a warning. There's no problem
1276 as long as pointers have enough bits to hold small integers. */
1277 if ((int) (EMACS_INT) client_data == i)
4dedbfe0
PR
1278 {
1279 int j;
1280 struct input_event buf;
4cb35c39 1281 Lisp_Object frame;
4dedbfe0 1282
4cb35c39 1283 XSETFRAME (frame, f);
9979315d
GM
1284 buf.kind = MENU_BAR_EVENT;
1285 buf.frame_or_window = frame;
1286 buf.arg = frame;
4dedbfe0
PR
1287 kbd_buffer_store_event (&buf);
1288
1289 for (j = 0; j < submenu_depth; j++)
1290 if (!NILP (subprefix_stack[j]))
1291 {
9979315d
GM
1292 buf.kind = MENU_BAR_EVENT;
1293 buf.frame_or_window = frame;
1294 buf.arg = subprefix_stack[j];
4dedbfe0
PR
1295 kbd_buffer_store_event (&buf);
1296 }
1297
1298 if (!NILP (prefix))
1299 {
9979315d
GM
1300 buf.kind = MENU_BAR_EVENT;
1301 buf.frame_or_window = frame;
1302 buf.arg = prefix;
4dedbfe0
PR
1303 kbd_buffer_store_event (&buf);
1304 }
1305
9979315d
GM
1306 buf.kind = MENU_BAR_EVENT;
1307 buf.frame_or_window = frame;
1308 buf.arg = entry;
4dedbfe0
PR
1309 kbd_buffer_store_event (&buf);
1310
1311 return;
1312 }
1313 i += MENU_ITEMS_ITEM_LENGTH;
1314 }
1315 }
18686d47
RS
1316}
1317
f9655c60
RS
1318/* Allocate a widget_value, blocking input. */
1319
1320widget_value *
1321xmalloc_widget_value ()
1322{
1323 widget_value *value;
1324
1325 BLOCK_INPUT;
1326 value = malloc_widget_value ();
1327 UNBLOCK_INPUT;
1328
1329 return value;
1330}
4dedbfe0
PR
1331
1332/* This recursively calls free_widget_value on the tree of widgets.
18686d47 1333 It must free all data that was malloc'ed for these widget_values.
78589e07
RS
1334 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1335 must be left alone. */
1336
18686d47
RS
1337void
1338free_menubar_widget_value_tree (wv)
1339 widget_value *wv;
1340{
1341 if (! wv) return;
18686d47
RS
1342
1343 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1344
1345 if (wv->contents && (wv->contents != (widget_value*)1))
1346 {
1347 free_menubar_widget_value_tree (wv->contents);
1348 wv->contents = (widget_value *) 0xDEADBEEF;
1349 }
1350 if (wv->next)
1351 {
1352 free_menubar_widget_value_tree (wv->next);
1353 wv->next = (widget_value *) 0xDEADBEEF;
1354 }
1355 BLOCK_INPUT;
1356 free_widget_value (wv);
1357 UNBLOCK_INPUT;
1358}
4dedbfe0
PR
1359\f
1360/* Return a tree of widget_value structures for a menu bar item
1361 whose event type is ITEM_KEY (with string ITEM_NAME)
1362 and whose contents come from the list of keymaps MAPS. */
1363
1364static widget_value *
1365single_submenu (item_key, item_name, maps)
1366 Lisp_Object item_key, item_name, maps;
1367{
1368 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1369 int i;
1370 int submenu_depth = 0;
1371 Lisp_Object length;
1372 int len;
1373 Lisp_Object *mapvec;
1374 widget_value **submenu_stack;
4dedbfe0 1375 int previous_items = menu_items_used;
71dca3e3 1376 int top_level_items = 0;
4dedbfe0
PR
1377
1378 length = Flength (maps);
1379 len = XINT (length);
1380
1381 /* Convert the list MAPS into a vector MAPVEC. */
1382 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1383 for (i = 0; i < len; i++)
1384 {
1385 mapvec[i] = Fcar (maps);
1386 maps = Fcdr (maps);
1387 }
1388
1389 menu_items_n_panes = 0;
1390
1391 /* Loop over the given keymaps, making a pane for each map.
1392 But don't make a pane that is empty--ignore that map instead. */
1393 for (i = 0; i < len; i++)
71dca3e3 1394 {
846e8c10
RS
1395 if (SYMBOLP (mapvec[i])
1396 || (CONSP (mapvec[i])
1397 && NILP (Fkeymapp (mapvec[i]))))
71dca3e3 1398 {
846e8c10
RS
1399 /* Here we have a command at top level in the menu bar
1400 as opposed to a submenu. */
71dca3e3
RS
1401 top_level_items = 1;
1402 push_menu_pane (Qnil, Qnil);
850df50b
GM
1403 push_menu_item (item_name, Qt, item_key, mapvec[i],
1404 Qnil, Qnil, Qnil, Qnil);
71dca3e3
RS
1405 }
1406 else
98381e42 1407 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
71dca3e3 1408 }
4dedbfe0
PR
1409
1410 /* Create a tree of widget_value objects
1411 representing the panes and their items. */
1412
1413 submenu_stack
1414 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
f7fab165 1415 wv = xmalloc_widget_value ();
4dedbfe0
PR
1416 wv->name = "menu";
1417 wv->value = 0;
1418 wv->enabled = 1;
3427a3db 1419 wv->button_type = BUTTON_TYPE_NONE;
4dedbfe0
PR
1420 first_wv = wv;
1421 save_wv = 0;
71dca3e3 1422 prev_wv = 0;
4dedbfe0
PR
1423
1424 /* Loop over all panes and items made during this call
1425 and construct a tree of widget_value objects.
1426 Ignore the panes and items made by previous calls to
1427 single_submenu, even though those are also in menu_items. */
1428 i = previous_items;
1429 while (i < menu_items_used)
1430 {
1431 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1432 {
1433 submenu_stack[submenu_depth++] = save_wv;
1434 save_wv = prev_wv;
1435 prev_wv = 0;
1436 i++;
1437 }
1438 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1439 {
1440 prev_wv = save_wv;
1441 save_wv = submenu_stack[--submenu_depth];
1442 i++;
1443 }
1444 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1445 && submenu_depth != 0)
1446 i += MENU_ITEMS_PANE_LENGTH;
1447 /* Ignore a nil in the item list.
1448 It's meaningful only for dialog boxes. */
1449 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1450 i += 1;
1451 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1452 {
1453 /* Create a new pane. */
1454 Lisp_Object pane_name, prefix;
1455 char *pane_string;
1456 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1457 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
703dc2a8
KH
1458#ifndef HAVE_MULTILINGUAL_MENU
1459 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1460 pane_name = string_make_unibyte (pane_name);
1461#endif
4dedbfe0
PR
1462 pane_string = (NILP (pane_name)
1463 ? "" : (char *) XSTRING (pane_name)->data);
1464 /* If there is just one top-level pane, put all its items directly
1465 under the top-level menu. */
1466 if (menu_items_n_panes == 1)
1467 pane_string = "";
1468
1469 /* If the pane has a meaningful name,
1470 make the pane a top-level menu item
1471 with its items as a submenu beneath it. */
1472 if (strcmp (pane_string, ""))
1473 {
f7fab165 1474 wv = xmalloc_widget_value ();
4dedbfe0
PR
1475 if (save_wv)
1476 save_wv->next = wv;
1477 else
1478 first_wv->contents = wv;
1479 wv->name = pane_string;
ffcb5a51
RS
1480 /* Ignore the @ that means "separate pane".
1481 This is a kludge, but this isn't worth more time. */
1482 if (!NILP (prefix) && wv->name[0] == '@')
4dedbfe0
PR
1483 wv->name++;
1484 wv->value = 0;
1485 wv->enabled = 1;
3427a3db 1486 wv->button_type = BUTTON_TYPE_NONE;
4dedbfe0
PR
1487 }
1488 save_wv = wv;
1489 prev_wv = 0;
1490 i += MENU_ITEMS_PANE_LENGTH;
1491 }
1492 else
1493 {
1494 /* Create a new item within current pane. */
3427a3db 1495 Lisp_Object item_name, enable, descrip, def, type, selected;
850df50b
GM
1496 Lisp_Object help;
1497
4dedbfe0
PR
1498 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1499 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1500 descrip
1501 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
a352a815 1502 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
3427a3db
GM
1503 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1504 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
850df50b 1505 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3427a3db 1506
703dc2a8 1507#ifndef HAVE_MULTILINGUAL_MENU
3427a3db
GM
1508 if (STRING_MULTIBYTE (item_name))
1509 item_name = string_make_unibyte (item_name);
1510 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1511 descrip = string_make_unibyte (descrip);
703dc2a8 1512#endif
3427a3db 1513
f7fab165 1514 wv = xmalloc_widget_value ();
4dedbfe0
PR
1515 if (prev_wv)
1516 prev_wv->next = wv;
71dca3e3 1517 else
4dedbfe0 1518 save_wv->contents = wv;
71dca3e3 1519
4dedbfe0
PR
1520 wv->name = (char *) XSTRING (item_name)->data;
1521 if (!NILP (descrip))
1522 wv->key = (char *) XSTRING (descrip)->data;
1523 wv->value = 0;
01d5e892
RS
1524 /* The EMACS_INT cast avoids a warning. There's no problem
1525 as long as pointers have enough bits to hold small integers. */
1526 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
4dedbfe0 1527 wv->enabled = !NILP (enable);
3427a3db
GM
1528
1529 if (NILP (type))
1530 wv->button_type = BUTTON_TYPE_NONE;
1531 else if (EQ (type, QCradio))
1532 wv->button_type = BUTTON_TYPE_RADIO;
1533 else if (EQ (type, QCtoggle))
1534 wv->button_type = BUTTON_TYPE_TOGGLE;
1535 else
1536 abort ();
1537
1538 wv->selected = !NILP (selected);
850df50b
GM
1539 if (STRINGP (help))
1540 wv->help = XSTRING (help)->data;
3427a3db 1541
4dedbfe0
PR
1542 prev_wv = wv;
1543
1544 i += MENU_ITEMS_ITEM_LENGTH;
1545 }
1546 }
1547
71dca3e3
RS
1548 /* If we have just one "menu item"
1549 that was originally a button, return it by itself. */
1550 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1551 {
1552 wv = first_wv->contents;
1553 free_widget_value (first_wv);
1554 return wv;
1555 }
1556
4dedbfe0
PR
1557 return first_wv;
1558}
1559\f
cffa74ea
FP
1560extern void EmacsFrameSetCharSize ();
1561
4bcdbab1
KH
1562/* Recompute all the widgets of frame F, when the menu bar
1563 has been changed. */
4dedbfe0 1564
18686d47 1565static void
6af6cbb5 1566update_frame_menubar (f)
18686d47
RS
1567 FRAME_PTR f;
1568{
7556890b 1569 struct x_output *x = f->output_data.x;
cffa74ea 1570 int columns, rows;
18686d47
RS
1571 int menubar_changed;
1572
4dedbfe0
PR
1573 /* We assume the menubar contents has changed if the global flag is set,
1574 or if the current buffer has changed, or if the menubar has never
1575 been updated before.
1576 */
18686d47
RS
1577 menubar_changed = (x->menubar_widget
1578 && !XtIsManaged (x->menubar_widget));
1579
1580 if (! (menubar_changed))
1581 return;
1582
1583 BLOCK_INPUT;
cffa74ea
FP
1584 /* Save the size of the frame because the pane widget doesn't accept to
1585 resize itself. So force it. */
1586 columns = f->width;
1587 rows = f->height;
1588
4dedbfe0
PR
1589 /* Do the voodoo which means "I'm changing lots of things, don't try to
1590 refigure sizes until I'm done." */
1591 lw_refigure_widget (x->column_widget, False);
cffa74ea 1592
18686d47
RS
1593 /* the order in which children are managed is the top to
1594 bottom order in which they are displayed in the paned window.
1595 First, remove the text-area widget.
1596 */
1597 XtUnmanageChild (x->edit_widget);
1598
1599 /* remove the menubar that is there now, and put up the menubar that
1600 should be there.
1601 */
1602 if (menubar_changed)
1603 {
1604 XtManageChild (x->menubar_widget);
1605 XtMapWidget (x->menubar_widget);
1606 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1607 }
1608
c98fcf4b 1609 /* Re-manage the text-area widget, and then thrash the sizes. */
18686d47 1610 XtManageChild (x->edit_widget);
bf82bce4 1611 x_set_menu_resources_from_menu_face (f, x->menubar_widget);
4dedbfe0 1612 lw_refigure_widget (x->column_widget, True);
cffa74ea
FP
1613
1614 /* Force the pane widget to resize itself with the right values. */
1615 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
18686d47
RS
1616 UNBLOCK_INPUT;
1617}
1618
4bcdbab1
KH
1619/* Set the contents of the menubar widgets of frame F.
1620 The argument FIRST_TIME is currently ignored;
1621 it is set the first time this is called, from initialize_frame_menubar. */
1622
18686d47 1623void
88766961 1624set_frame_menubar (f, first_time, deep_p)
18686d47 1625 FRAME_PTR f;
706aa2f2 1626 int first_time;
88766961 1627 int deep_p;
18686d47 1628{
7556890b 1629 Widget menubar_widget = f->output_data.x->menubar_widget;
faa935b6 1630 Lisp_Object items;
4d19cb8e 1631 widget_value *wv, *first_wv, *prev_wv = 0;
5b3557df 1632 int i;
88766961 1633 LWLIB_ID id;
18686d47 1634
bfc524bc
RS
1635 XSETFRAME (Vmenu_updating_frame, f);
1636
7556890b
RS
1637 if (f->output_data.x->id == 0)
1638 f->output_data.x->id = next_menubar_widget_id++;
1639 id = f->output_data.x->id;
37a98547 1640
88766961
RS
1641 if (! menubar_widget)
1642 deep_p = 1;
745c34fb 1643 else if (pending_menu_activation && !deep_p)
a9be6839
RS
1644 deep_p = 1;
1645 /* Make the first call for any given frame always go deep. */
1646 else if (!f->output_data.x->saved_menu_event && !deep_p)
745c34fb
RS
1647 {
1648 deep_p = 1;
a9be6839
RS
1649 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1650 f->output_data.x->saved_menu_event->type = 0;
745c34fb 1651 }
18686d47 1652
f7fab165 1653 wv = xmalloc_widget_value ();
18686d47
RS
1654 wv->name = "menubar";
1655 wv->value = 0;
1656 wv->enabled = 1;
3427a3db 1657 wv->button_type = BUTTON_TYPE_NONE;
4d19cb8e 1658 first_wv = wv;
8d8a3494 1659
88766961 1660 if (deep_p)
18686d47 1661 {
88766961
RS
1662 /* Make a widget-value tree representing the entire menu trees. */
1663
1664 struct buffer *prev = current_buffer;
1665 Lisp_Object buffer;
1666 int specpdl_count = specpdl_ptr - specpdl;
1667 int previous_menu_items_used = f->menu_bar_items_used;
1668 Lisp_Object *previous_items
1669 = (Lisp_Object *) alloca (previous_menu_items_used
1670 * sizeof (Lisp_Object));
1671
0b1cf399
RS
1672 /* If we are making a new widget, its contents are empty,
1673 do always reinitialize them. */
1674 if (! menubar_widget)
1675 previous_menu_items_used = 0;
1676
88766961
RS
1677 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1678 specbind (Qinhibit_quit, Qt);
1679 /* Don't let the debugger step into this code
1680 because it is not reentrant. */
1681 specbind (Qdebug_on_next_call, Qnil);
1682
58226364 1683 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
88766961
RS
1684 if (NILP (Voverriding_local_map_menu_flag))
1685 {
1686 specbind (Qoverriding_terminal_local_map, Qnil);
1687 specbind (Qoverriding_local_map, Qnil);
1688 }
18686d47 1689
88766961 1690 set_buffer_internal_1 (XBUFFER (buffer));
18686d47 1691
88766961
RS
1692 /* Run the Lucid hook. */
1693 call1 (Vrun_hooks, Qactivate_menubar_hook);
1694 /* If it has changed current-menubar from previous value,
1695 really recompute the menubar from the value. */
1696 if (! NILP (Vlucid_menu_bar_dirty_flag))
1697 call0 (Qrecompute_lucid_menubar);
a57634d4 1698 safe_run_hooks (Qmenu_bar_update_hook);
88766961 1699 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
18686d47 1700
88766961 1701 items = FRAME_MENU_BAR_ITEMS (f);
8d8a3494 1702
88766961 1703 inhibit_garbage_collection ();
8d8a3494 1704
88766961
RS
1705 /* Save the frame's previous menu bar contents data. */
1706 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1707 previous_menu_items_used * sizeof (Lisp_Object));
8d8a3494 1708
88766961
RS
1709 /* Fill in the current menu bar contents. */
1710 menu_items = f->menu_bar_vector;
1711 menu_items_allocated = XVECTOR (menu_items)->size;
1712 init_menu_items ();
f04366cb 1713 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1714 {
1715 Lisp_Object key, string, maps;
1716
1717 key = XVECTOR (items)->contents[i];
1718 string = XVECTOR (items)->contents[i + 1];
1719 maps = XVECTOR (items)->contents[i + 2];
1720 if (NILP (string))
1721 break;
1722
1723 wv = single_submenu (key, string, maps);
1724 if (prev_wv)
1725 prev_wv->next = wv;
1726 else
1727 first_wv->contents = wv;
1728 /* Don't set wv->name here; GC during the loop might relocate it. */
1729 wv->enabled = 1;
3427a3db 1730 wv->button_type = BUTTON_TYPE_NONE;
88766961
RS
1731 prev_wv = wv;
1732 }
1733
1734 finish_menu_items ();
1735
1736 set_buffer_internal_1 (prev);
8d8a3494 1737 unbind_to (specpdl_count, Qnil);
8d8a3494 1738
88766961
RS
1739 /* If there has been no change in the Lisp-level contents
1740 of the menu bar, skip redisplaying it. Just exit. */
1741
1742 for (i = 0; i < previous_menu_items_used; i++)
1743 if (menu_items_used == i
99d3fac7 1744 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
88766961 1745 break;
62555c22 1746 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
88766961
RS
1747 {
1748 free_menubar_widget_value_tree (first_wv);
1749 menu_items = Qnil;
1750
1751 return;
1752 }
1753
1754 /* Now GC cannot happen during the lifetime of the widget_value,
1755 so it's safe to store data from a Lisp_String. */
1756 wv = first_wv->contents;
f04366cb 1757 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1758 {
1759 Lisp_Object string;
1760 string = XVECTOR (items)->contents[i + 1];
1761 if (NILP (string))
1762 break;
1763 wv->name = (char *) XSTRING (string)->data;
1764 wv = wv->next;
1765 }
1766
1767 f->menu_bar_vector = menu_items;
1768 f->menu_bar_items_used = menu_items_used;
1769 menu_items = Qnil;
4d19cb8e 1770 }
88766961
RS
1771 else
1772 {
1773 /* Make a widget-value tree containing
1774 just the top level menu bar strings. */
4d19cb8e 1775
88766961 1776 items = FRAME_MENU_BAR_ITEMS (f);
f04366cb 1777 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1778 {
1779 Lisp_Object string;
1780
1781 string = XVECTOR (items)->contents[i + 1];
1782 if (NILP (string))
1783 break;
1784
f7fab165 1785 wv = xmalloc_widget_value ();
88766961
RS
1786 wv->name = (char *) XSTRING (string)->data;
1787 wv->value = 0;
1788 wv->enabled = 1;
3427a3db 1789 wv->button_type = BUTTON_TYPE_NONE;
fe8fa62f
RS
1790 /* This prevents lwlib from assuming this
1791 menu item is really supposed to be empty. */
1792 /* The EMACS_INT cast avoids a warning.
1793 This value just has to be different from small integers. */
1794 wv->call_data = (void *) (EMACS_INT) (-1);
88766961
RS
1795
1796 if (prev_wv)
1797 prev_wv->next = wv;
1798 else
1799 first_wv->contents = wv;
1800 prev_wv = wv;
1801 }
62555c22
RS
1802
1803 /* Forget what we thought we knew about what is in the
1804 detailed contents of the menu bar menus.
1805 Changing the top level always destroys the contents. */
1806 f->menu_bar_items_used = 0;
88766961 1807 }
4dedbfe0 1808
88766961 1809 /* Create or update the menu bar widget. */
aa669def
RS
1810
1811 BLOCK_INPUT;
1812
18686d47 1813 if (menubar_widget)
4dedbfe0
PR
1814 {
1815 /* Disable resizing (done for Motif!) */
7556890b 1816 lw_allow_resizing (f->output_data.x->widget, False);
4dedbfe0
PR
1817
1818 /* The third arg is DEEP_P, which says to consider the entire
1819 menu trees we supply, rather than just the menu bar item names. */
88766961 1820 lw_modify_all_widgets (id, first_wv, deep_p);
4dedbfe0 1821
c98fcf4b 1822 /* Re-enable the edit widget to resize. */
7556890b 1823 lw_allow_resizing (f->output_data.x->widget, True);
4dedbfe0 1824 }
18686d47
RS
1825 else
1826 {
88766961 1827 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
7556890b 1828 f->output_data.x->column_widget,
4dedbfe0
PR
1829 0,
1830 popup_activate_callback,
1831 menubar_selection_callback,
850df50b
GM
1832 popup_deactivate_callback,
1833 menu_highlight_callback);
7556890b 1834 f->output_data.x->menubar_widget = menubar_widget;
18686d47 1835 }
1d1c1567
KH
1836
1837 {
1838 int menubar_size
7556890b
RS
1839 = (f->output_data.x->menubar_widget
1840 ? (f->output_data.x->menubar_widget->core.height
1841 + f->output_data.x->menubar_widget->core.border_width)
1d1c1567
KH
1842 : 0);
1843
f42aa681
RS
1844#if 0 /* Experimentally, we now get the right results
1845 for -geometry -0-0 without this. 24 Aug 96, rms. */
5ee80bd3 1846#ifdef USE_LUCID
1d1c1567
KH
1847 if (FRAME_EXTERNAL_MENU_BAR (f))
1848 {
1849 Dimension ibw = 0;
7556890b 1850 XtVaGetValues (f->output_data.x->column_widget,
1d1c1567
KH
1851 XtNinternalBorderWidth, &ibw, NULL);
1852 menubar_size += ibw;
1853 }
5ee80bd3 1854#endif /* USE_LUCID */
f42aa681 1855#endif /* 0 */
1d1c1567 1856
7556890b 1857 f->output_data.x->menubar_height = menubar_size;
1d1c1567 1858 }
18686d47
RS
1859
1860 free_menubar_widget_value_tree (first_wv);
4bcdbab1 1861 update_frame_menubar (f);
18686d47
RS
1862
1863 UNBLOCK_INPUT;
1864}
85f487d1 1865
8e6208c5 1866/* Called from Fx_create_frame to create the initial menubar of a frame
4dedbfe0
PR
1867 before it is mapped, so that the window is mapped with the menubar already
1868 there instead of us tacking it on later and thrashing the window after it
1869 is visible. */
1870
1871void
1872initialize_frame_menubar (f)
1873 FRAME_PTR f;
1874{
1875 /* This function is called before the first chance to redisplay
1876 the frame. It has to be, so the frame will have the right size. */
1877 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
88766961 1878 set_frame_menubar (f, 1, 1);
4dedbfe0
PR
1879}
1880
1881/* Get rid of the menu bar of frame F, and free its storage.
1882 This is used when deleting a frame, and when turning off the menu bar. */
1883
85f487d1
FP
1884void
1885free_frame_menubar (f)
1886 FRAME_PTR f;
1887{
1888 Widget menubar_widget;
85f487d1 1889
7556890b 1890 menubar_widget = f->output_data.x->menubar_widget;
a45bad2a
RS
1891
1892 f->output_data.x->menubar_height = 0;
85f487d1
FP
1893
1894 if (menubar_widget)
1895 {
1896 BLOCK_INPUT;
7556890b 1897 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
85f487d1
FP
1898 UNBLOCK_INPUT;
1899 }
1900}
78589e07 1901
78589e07
RS
1902#endif /* USE_X_TOOLKIT */
1903\f
1904/* xmenu_show actually displays a menu using the panes and items in menu_items
1905 and returns the value selected from it.
1906 There are two versions of xmenu_show, one for Xt and one for Xlib.
1907 Both assume input is blocked by the caller. */
1908
1909/* F is the frame the menu is for.
1910 X and Y are the frame-relative specified position,
1911 relative to the inside upper left corner of the frame F.
c8b5aa3d 1912 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
78589e07
RS
1913 KEYMAPS is 1 if this menu was specified with keymaps;
1914 in that case, we return a list containing the chosen item's value
1915 and perhaps also the pane's prefix.
1916 TITLE is the specified menu title.
1917 ERROR is a place to store an error message string in case of failure.
1918 (We return nil on failure, but the value doesn't actually matter.) */
18686d47
RS
1919
1920#ifdef USE_X_TOOLKIT
18686d47 1921
8ed87156 1922/* We need a unique id for each widget handled by the Lucid Widget
cc17e9bf
KH
1923 library.
1924
1925 For the main windows, and popup menus, we use this counter,
88766961 1926 which we increment each time after use. This starts from 1<<16.
cc17e9bf 1927
88766961
RS
1928 For menu bars, we use numbers starting at 0, counted in
1929 next_menubar_widget_id. */
8ed87156 1930LWLIB_ID widget_id_tick;
165e1749 1931
4dedbfe0 1932static Lisp_Object *volatile menu_item_selection;
4dedbfe0
PR
1933
1934static void
1935popup_selection_callback (widget, id, client_data)
1936 Widget widget;
1937 LWLIB_ID id;
1938 XtPointer client_data;
1939{
1940 menu_item_selection = (Lisp_Object *) client_data;
1941}
1942
78589e07 1943static Lisp_Object
673a6211 1944xmenu_show (f, x, y, for_click, keymaps, title, error)
18686d47 1945 FRAME_PTR f;
18686d47
RS
1946 int x;
1947 int y;
9685a93f 1948 int for_click;
78589e07
RS
1949 int keymaps;
1950 Lisp_Object title;
1951 char **error;
18686d47 1952{
78589e07 1953 int i;
cc17e9bf 1954 LWLIB_ID menu_id;
18686d47 1955 Widget menu;
ffcb5a51 1956 Arg av[2];
60f312e2 1957 int ac = 0;
78589e07 1958 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
101bb4a5
RS
1959 widget_value **submenu_stack
1960 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1961 Lisp_Object *subprefix_stack
1962 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1963 int submenu_depth = 0;
ffcb5a51 1964 XButtonPressedEvent dummy;
4e8d3549 1965
78c8278d
RS
1966 int first_pane;
1967
78589e07
RS
1968 *error = NULL;
1969
742f715d
KH
1970 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1971 {
1972 *error = "Empty menu";
1973 return Qnil;
1974 }
63c414df 1975
78589e07
RS
1976 /* Create a tree of widget_value objects
1977 representing the panes and their items. */
f7fab165 1978 wv = xmalloc_widget_value ();
78589e07
RS
1979 wv->name = "menu";
1980 wv->value = 0;
1981 wv->enabled = 1;
3427a3db 1982 wv->button_type = BUTTON_TYPE_NONE;
78589e07 1983 first_wv = wv;
78c8278d 1984 first_pane = 1;
78589e07
RS
1985
1986 /* Loop over all panes and items, filling in the tree. */
1987 i = 0;
1988 while (i < menu_items_used)
1989 {
101bb4a5
RS
1990 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1991 {
1992 submenu_stack[submenu_depth++] = save_wv;
1993 save_wv = prev_wv;
1994 prev_wv = 0;
78c8278d 1995 first_pane = 1;
101bb4a5
RS
1996 i++;
1997 }
1998 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1999 {
2000 prev_wv = save_wv;
2001 save_wv = submenu_stack[--submenu_depth];
78c8278d 2002 first_pane = 0;
101bb4a5
RS
2003 i++;
2004 }
2005 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2006 && submenu_depth != 0)
2007 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
2008 /* Ignore a nil in the item list.
2009 It's meaningful only for dialog boxes. */
2010 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2011 i += 1;
101bb4a5 2012 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2013 {
2014 /* Create a new pane. */
2015 Lisp_Object pane_name, prefix;
2016 char *pane_string;
2017 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2018 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
703dc2a8
KH
2019#ifndef HAVE_MULTILINGUAL_MENU
2020 if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name))
2021 pane_name = string_make_unibyte (pane_name);
2022#endif
78589e07
RS
2023 pane_string = (NILP (pane_name)
2024 ? "" : (char *) XSTRING (pane_name)->data);
101bb4a5 2025 /* If there is just one top-level pane, put all its items directly
78589e07
RS
2026 under the top-level menu. */
2027 if (menu_items_n_panes == 1)
2028 pane_string = "";
2029
2030 /* If the pane has a meaningful name,
2031 make the pane a top-level menu item
2032 with its items as a submenu beneath it. */
78c8278d 2033 if (!keymaps && strcmp (pane_string, ""))
78589e07 2034 {
f7fab165 2035 wv = xmalloc_widget_value ();
78589e07
RS
2036 if (save_wv)
2037 save_wv->next = wv;
2038 else
2039 first_wv->contents = wv;
2040 wv->name = pane_string;
2041 if (keymaps && !NILP (prefix))
2042 wv->name++;
2043 wv->value = 0;
2044 wv->enabled = 1;
3427a3db 2045 wv->button_type = BUTTON_TYPE_NONE;
78c8278d
RS
2046 save_wv = wv;
2047 prev_wv = 0;
78589e07 2048 }
78c8278d
RS
2049 else if (first_pane)
2050 {
2051 save_wv = wv;
2052 prev_wv = 0;
2053 }
2054 first_pane = 0;
78589e07
RS
2055 i += MENU_ITEMS_PANE_LENGTH;
2056 }
2057 else
2058 {
2059 /* Create a new item within current pane. */
9cd50434 2060 Lisp_Object item_name, enable, descrip, def, type, selected, help;
78589e07
RS
2061 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2062 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2063 descrip
2064 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
a352a815 2065 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
3427a3db
GM
2066 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
2067 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
9cd50434 2068 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3427a3db 2069
703dc2a8 2070#ifndef HAVE_MULTILINGUAL_MENU
3427a3db
GM
2071 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2072 item_name = string_make_unibyte (item_name);
2073 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2074 item_name = string_make_unibyte (descrip);
703dc2a8 2075#endif
3427a3db 2076
f7fab165 2077 wv = xmalloc_widget_value ();
78589e07
RS
2078 if (prev_wv)
2079 prev_wv->next = wv;
2080 else
2081 save_wv->contents = wv;
4e8d3549 2082 wv->name = (char *) XSTRING (item_name)->data;
78589e07 2083 if (!NILP (descrip))
4e8d3549 2084 wv->key = (char *) XSTRING (descrip)->data;
78589e07 2085 wv->value = 0;
a352a815
RS
2086 /* If this item has a null value,
2087 make the call_data null so that it won't display a box
2088 when the mouse is on it. */
2089 wv->call_data
2090 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
78589e07 2091 wv->enabled = !NILP (enable);
3427a3db
GM
2092
2093 if (NILP (type))
2094 wv->button_type = BUTTON_TYPE_NONE;
2095 else if (EQ (type, QCtoggle))
2096 wv->button_type = BUTTON_TYPE_TOGGLE;
2097 else if (EQ (type, QCradio))
2098 wv->button_type = BUTTON_TYPE_RADIO;
2099 else
2100 abort ();
2101
2102 wv->selected = !NILP (selected);
9cd50434
GM
2103 if (STRINGP (help))
2104 wv->help = XSTRING (help)->data;
3427a3db 2105
78589e07
RS
2106 prev_wv = wv;
2107
2108 i += MENU_ITEMS_ITEM_LENGTH;
2109 }
2110 }
2111
c98fcf4b 2112 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
2113 if (!NILP (title))
2114 {
f7fab165
RS
2115 widget_value *wv_title = xmalloc_widget_value ();
2116 widget_value *wv_sep1 = xmalloc_widget_value ();
2117 widget_value *wv_sep2 = xmalloc_widget_value ();
4dedbfe0
PR
2118
2119 wv_sep2->name = "--";
2120 wv_sep2->next = first_wv->contents;
2121
2122 wv_sep1->name = "--";
2123 wv_sep1->next = wv_sep2;
2124
703dc2a8
KH
2125#ifndef HAVE_MULTILINGUAL_MENU
2126 if (STRING_MULTIBYTE (title))
2127 title = string_make_unibyte (title);
2128#endif
4dedbfe0
PR
2129 wv_title->name = (char *) XSTRING (title)->data;
2130 wv_title->enabled = True;
3427a3db 2131 wv_title->button_type = BUTTON_TYPE_NONE;
4dedbfe0
PR
2132 wv_title->next = wv_sep1;
2133 first_wv->contents = wv_title;
2134 }
2135
78589e07 2136 /* Actually create the menu. */
cc17e9bf 2137 menu_id = widget_id_tick++;
78589e07 2138 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
7556890b 2139 f->output_data.x->widget, 1, 0,
4dedbfe0 2140 popup_selection_callback,
850df50b
GM
2141 popup_deactivate_callback,
2142 menu_highlight_callback);
60f312e2 2143
ffcb5a51
RS
2144 /* Adjust coordinates to relative to the outer (window manager) window. */
2145 {
2146 Window child;
2147 int win_x = 0, win_y = 0;
2148
2149 /* Find the position of the outside upper-left corner of
2150 the inner window, with respect to the outer window. */
2151 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2152 {
2153 BLOCK_INPUT;
2154 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2155
2156 /* From-window, to-window. */
2157 f->output_data.x->window_desc,
2158 f->output_data.x->parent_desc,
2159
2160 /* From-position, to-position. */
2161 0, 0, &win_x, &win_y,
2162
2163 /* Child of window. */
2164 &child);
2165 UNBLOCK_INPUT;
2166 x += win_x;
2167 y += win_y;
2168 }
2169 }
2170
2171 /* Adjust coordinates to be root-window-relative. */
2172 x += f->output_data.x->left_pos;
2173 y += f->output_data.x->top_pos;
2174
2175 dummy.type = ButtonPress;
2176 dummy.serial = 0;
2177 dummy.send_event = 0;
2178 dummy.display = FRAME_X_DISPLAY (f);
2179 dummy.time = CurrentTime;
ffcb5a51
RS
2180 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2181 dummy.window = dummy.root;
2182 dummy.subwindow = dummy.root;
2183 dummy.x_root = x;
2184 dummy.y_root = y;
2185 dummy.x = x;
2186 dummy.y = y;
c8b5aa3d
RS
2187 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2188 dummy.button = 0;
2189 for (i = 0; i < 5; i++)
2190 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2191 dummy.button = i;
ffcb5a51 2192
60f312e2
RS
2193 /* Don't allow any geometry request from the user. */
2194 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2195 XtSetValues (menu, av, ac);
2196
78589e07
RS
2197 /* Free the widget_value objects we used to specify the contents. */
2198 free_menubar_widget_value_tree (first_wv);
2199
2200 /* No selection has been chosen yet. */
2201 menu_item_selection = 0;
2202
78589e07 2203 /* Display the menu. */
a52445bd 2204 lw_popup_menu (menu, (XEvent *) &dummy);
ac441528 2205 x_set_menu_resources_from_menu_face (f, menu);
4dedbfe0 2206 popup_activated_flag = 1;
18686d47 2207
78589e07 2208 /* Process events that apply to the menu. */
2e2b8e22 2209 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
78589e07 2210
3427a3db
GM
2211#ifdef LESSTIF_VERSION
2212 /* Nov 1998: For an unknown reason a button grab remains active
2213 after the popup menu has gone. */
2214 XUngrabButton (XtDisplay (f->output_data.x->widget),
2215 AnyButton, AnyModifier,
2216 XtWindow (f->output_data.x->widget));
2217 XUngrabButton (XtDisplay (f->output_data.x->edit_widget),
2218 AnyButton, AnyModifier,
2219 XtWindow (f->output_data.x->edit_widget));
2220#endif /* LESSTIF_VERSION */
2221
a9c90b7c
RS
2222 /* fp turned off the following statement and wrote a comment
2223 that it is unnecessary--that the menu has already disappeared.
21af8a68
KH
2224 Nowadays the menu disappears ok, all right, but
2225 we need to delete the widgets or multiple ones will pile up. */
78589e07 2226 lw_destroy_all_widgets (menu_id);
18686d47 2227
78589e07
RS
2228 /* Find the selected item, and its pane, to return
2229 the proper value. */
2230 if (menu_item_selection != 0)
2231 {
c63f6952 2232 Lisp_Object prefix, entry;
78589e07
RS
2233
2234 prefix = Qnil;
2235 i = 0;
2236 while (i < menu_items_used)
2237 {
101bb4a5
RS
2238 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2239 {
2240 subprefix_stack[submenu_depth++] = prefix;
2241 prefix = entry;
2242 i++;
2243 }
2244 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2245 {
2246 prefix = subprefix_stack[--submenu_depth];
2247 i++;
2248 }
2249 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2250 {
2251 prefix
2252 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2253 i += MENU_ITEMS_PANE_LENGTH;
2254 }
d31d42cc
RS
2255 /* Ignore a nil in the item list.
2256 It's meaningful only for dialog boxes. */
2257 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2258 i += 1;
78589e07
RS
2259 else
2260 {
2261 entry
2262 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2263 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2264 {
2265 if (keymaps != 0)
2266 {
101bb4a5
RS
2267 int j;
2268
78589e07
RS
2269 entry = Fcons (entry, Qnil);
2270 if (!NILP (prefix))
2271 entry = Fcons (prefix, entry);
101bb4a5 2272 for (j = submenu_depth - 1; j >= 0; j--)
e48087b7 2273 if (!NILP (subprefix_stack[j]))
5964e450 2274 entry = Fcons (subprefix_stack[j], entry);
78589e07
RS
2275 }
2276 return entry;
2277 }
2278 i += MENU_ITEMS_ITEM_LENGTH;
2279 }
2280 }
2281 }
2282
2283 return Qnil;
18686d47 2284}
4dedbfe0
PR
2285\f
2286static void
2287dialog_selection_callback (widget, id, client_data)
2288 Widget widget;
2289 LWLIB_ID id;
2290 XtPointer client_data;
2291{
01d5e892
RS
2292 /* The EMACS_INT cast avoids a warning. There's no problem
2293 as long as pointers have enough bits to hold small integers. */
2294 if ((int) (EMACS_INT) client_data != -1)
4dedbfe0
PR
2295 menu_item_selection = (Lisp_Object *) client_data;
2296 BLOCK_INPUT;
2297 lw_destroy_all_widgets (id);
2298 UNBLOCK_INPUT;
9572375b 2299 popup_activated_flag = 0;
4dedbfe0 2300}
18686d47 2301
165e1749
FP
2302static char * button_names [] = {
2303 "button1", "button2", "button3", "button4", "button5",
2304 "button6", "button7", "button8", "button9", "button10" };
2305
2306static Lisp_Object
673a6211 2307xdialog_show (f, keymaps, title, error)
165e1749 2308 FRAME_PTR f;
165e1749
FP
2309 int keymaps;
2310 Lisp_Object title;
2311 char **error;
2312{
2313 int i, nb_buttons=0;
cc17e9bf 2314 LWLIB_ID dialog_id;
165e1749 2315 Widget menu;
80670155 2316 char dialog_name[6];
165e1749 2317
faa935b6 2318 widget_value *wv, *first_wv = 0, *prev_wv = 0;
165e1749 2319
fcaa7665
RS
2320 /* Number of elements seen so far, before boundary. */
2321 int left_count = 0;
2322 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2323 int boundary_seen = 0;
2324
165e1749
FP
2325 *error = NULL;
2326
80670155
RS
2327 if (menu_items_n_panes > 1)
2328 {
2329 *error = "Multiple panes in dialog box";
2330 return Qnil;
2331 }
2332
165e1749
FP
2333 /* Create a tree of widget_value objects
2334 representing the text label and buttons. */
2335 {
2336 Lisp_Object pane_name, prefix;
2337 char *pane_string;
2338 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2339 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2340 pane_string = (NILP (pane_name)
2341 ? "" : (char *) XSTRING (pane_name)->data);
f7fab165 2342 prev_wv = xmalloc_widget_value ();
165e1749
FP
2343 prev_wv->value = pane_string;
2344 if (keymaps && !NILP (prefix))
2345 prev_wv->name++;
2346 prev_wv->enabled = 1;
2347 prev_wv->name = "message";
2348 first_wv = prev_wv;
2349
2350 /* Loop over all panes and items, filling in the tree. */
2351 i = MENU_ITEMS_PANE_LENGTH;
2352 while (i < menu_items_used)
2353 {
2354
2355 /* Create a new item within current pane. */
2356 Lisp_Object item_name, enable, descrip;
2357 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2358 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2359 descrip
2360 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2361
80670155
RS
2362 if (NILP (item_name))
2363 {
2364 free_menubar_widget_value_tree (first_wv);
2365 *error = "Submenu in dialog items";
2366 return Qnil;
2367 }
fcaa7665
RS
2368 if (EQ (item_name, Qquote))
2369 {
2370 /* This is the boundary between left-side elts
2371 and right-side elts. Stop incrementing right_count. */
2372 boundary_seen = 1;
2373 i++;
2374 continue;
2375 }
86e71abf 2376 if (nb_buttons >= 9)
80670155
RS
2377 {
2378 free_menubar_widget_value_tree (first_wv);
2379 *error = "Too many dialog items";
2380 return Qnil;
2381 }
2382
f7fab165 2383 wv = xmalloc_widget_value ();
165e1749 2384 prev_wv->next = wv;
80670155 2385 wv->name = (char *) button_names[nb_buttons];
165e1749 2386 if (!NILP (descrip))
4e8d3549
RS
2387 wv->key = (char *) XSTRING (descrip)->data;
2388 wv->value = (char *) XSTRING (item_name)->data;
165e1749
FP
2389 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2390 wv->enabled = !NILP (enable);
2391 prev_wv = wv;
2392
fcaa7665
RS
2393 if (! boundary_seen)
2394 left_count++;
2395
165e1749
FP
2396 nb_buttons++;
2397 i += MENU_ITEMS_ITEM_LENGTH;
2398 }
2399
fcaa7665
RS
2400 /* If the boundary was not specified,
2401 by default put half on the left and half on the right. */
2402 if (! boundary_seen)
2403 left_count = nb_buttons - nb_buttons / 2;
2404
f7fab165 2405 wv = xmalloc_widget_value ();
80670155
RS
2406 wv->name = dialog_name;
2407
2408 /* Dialog boxes use a really stupid name encoding
2409 which specifies how many buttons to use
2410 and how many buttons are on the right.
2411 The Q means something also. */
2412 dialog_name[0] = 'Q';
2413 dialog_name[1] = '0' + nb_buttons;
2414 dialog_name[2] = 'B';
2415 dialog_name[3] = 'R';
fcaa7665
RS
2416 /* Number of buttons to put on the right. */
2417 dialog_name[4] = '0' + nb_buttons - left_count;
80670155 2418 dialog_name[5] = 0;
165e1749
FP
2419 wv->contents = first_wv;
2420 first_wv = wv;
165e1749
FP
2421 }
2422
2423 /* Actually create the dialog. */
cc17e9bf 2424 dialog_id = widget_id_tick++;
165e1749 2425 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
7556890b 2426 f->output_data.x->widget, 1, 0,
850df50b 2427 dialog_selection_callback, 0, 0);
b5587215 2428 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
165e1749
FP
2429 /* Free the widget_value objects we used to specify the contents. */
2430 free_menubar_widget_value_tree (first_wv);
2431
2432 /* No selection has been chosen yet. */
2433 menu_item_selection = 0;
2434
165e1749
FP
2435 /* Display the menu. */
2436 lw_pop_up_all_widgets (dialog_id);
aa669def 2437 popup_activated_flag = 1;
165e1749
FP
2438
2439 /* Process events that apply to the menu. */
2e2b8e22 2440 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
165e1749 2441
21af8a68
KH
2442 lw_destroy_all_widgets (dialog_id);
2443
165e1749
FP
2444 /* Find the selected item, and its pane, to return
2445 the proper value. */
2446 if (menu_item_selection != 0)
2447 {
2448 Lisp_Object prefix;
2449
2450 prefix = Qnil;
2451 i = 0;
2452 while (i < menu_items_used)
2453 {
2454 Lisp_Object entry;
2455
2456 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2457 {
2458 prefix
2459 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2460 i += MENU_ITEMS_PANE_LENGTH;
2461 }
2462 else
2463 {
2464 entry
2465 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2466 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2467 {
2468 if (keymaps != 0)
2469 {
2470 entry = Fcons (entry, Qnil);
2471 if (!NILP (prefix))
2472 entry = Fcons (prefix, entry);
2473 }
2474 return entry;
2475 }
2476 i += MENU_ITEMS_ITEM_LENGTH;
2477 }
2478 }
2479 }
2480
2481 return Qnil;
2482}
ba461919 2483
18686d47 2484#else /* not USE_X_TOOLKIT */
78589e07 2485
3e703b25
GM
2486/* The frame of the last activated non-toolkit menu bar.
2487 Used to generate menu help events. */
2488
2489static struct frame *menu_help_frame;
2490
2491
62145073
GM
2492/* Show help HELP_STRING, or clear help if HELP_STRING is null.
2493
2494 PANE is the pane number, and ITEM is the menu item number in
2495 the menu (currently not used).
2496
2497 This cannot be done with generating a HELP_EVENT because
2498 XMenuActivate contains a loop that doesn't let Emacs process
2499 keyboard events. */
3e703b25
GM
2500
2501static void
62145073 2502menu_help_callback (help_string, pane, item)
3e703b25 2503 char *help_string;
62145073 2504 int pane, item;
3e703b25 2505{
62145073
GM
2506 extern Lisp_Object Qmenu_item;
2507 Lisp_Object *first_item;
2508 Lisp_Object pane_name;
2509 Lisp_Object menu_object;
2510
2511 first_item = XVECTOR (menu_items)->contents;
2512 if (EQ (first_item[0], Qt))
2513 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2514 else if (EQ (first_item[0], Qquote))
2515 /* This shouldn't happen, see xmenu_show. */
2516 pane_name = build_string ("");
2517 else
2518 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2519
2520 /* (menu-item MENU-NAME PANE-NUMBER) */
2521 menu_object = Fcons (Qmenu_item,
2522 Fcons (pane_name,
2523 Fcons (make_number (pane), Qnil)));
ba461919 2524 show_help_echo (help_string ? build_string (help_string) : Qnil,
62145073 2525 Qnil, menu_object, make_number (item), 1);
3e703b25 2526}
62145073 2527
3e703b25 2528
78589e07 2529static Lisp_Object
673a6211 2530xmenu_show (f, x, y, for_click, keymaps, title, error)
78589e07
RS
2531 FRAME_PTR f;
2532 int x, y;
9685a93f
RS
2533 int for_click;
2534 int keymaps;
78589e07
RS
2535 Lisp_Object title;
2536 char **error;
dcfdbac7 2537{
78589e07
RS
2538 Window root;
2539 XMenu *menu;
2540 int pane, selidx, lpane, status;
2541 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
2542 char *datap;
2543 int ulx, uly, width, height;
2544 int dispwidth, dispheight;
4e8d3549
RS
2545 int i, j;
2546 int maxwidth;
78589e07
RS
2547 int dummy_int;
2548 unsigned int dummy_uint;
088831f6 2549
07a675b7 2550 *error = 0;
78589e07
RS
2551 if (menu_items_n_panes == 0)
2552 return Qnil;
088831f6 2553
742f715d
KH
2554 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2555 {
2556 *error = "Empty menu";
2557 return Qnil;
2558 }
2559
78589e07 2560 /* Figure out which root window F is on. */
92280f67 2561 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
2562 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2563 &dummy_uint, &dummy_uint);
18686d47 2564
78589e07 2565 /* Make the menu on that window. */
92280f67 2566 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 2567 if (menu == NULL)
dcfdbac7
JB
2568 {
2569 *error = "Can't create menu";
78589e07 2570 return Qnil;
dcfdbac7 2571 }
78589e07 2572
87485d6f 2573#ifdef HAVE_X_WINDOWS
78589e07 2574 /* Adjust coordinates to relative to the outer (window manager) window. */
78589e07
RS
2575 {
2576 Window child;
2577 int win_x = 0, win_y = 0;
2578
2579 /* Find the position of the outside upper-left corner of
2580 the inner window, with respect to the outer window. */
7556890b 2581 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
78589e07
RS
2582 {
2583 BLOCK_INPUT;
92280f67 2584 XTranslateCoordinates (FRAME_X_DISPLAY (f),
78589e07
RS
2585
2586 /* From-window, to-window. */
7556890b
RS
2587 f->output_data.x->window_desc,
2588 f->output_data.x->parent_desc,
78589e07
RS
2589
2590 /* From-position, to-position. */
2591 0, 0, &win_x, &win_y,
2592
2593 /* Child of window. */
2594 &child);
2595 UNBLOCK_INPUT;
2596 x += win_x;
2597 y += win_y;
2598 }
2599 }
87485d6f 2600#endif /* HAVE_X_WINDOWS */
78589e07
RS
2601
2602 /* Adjust coordinates to be root-window-relative. */
7556890b
RS
2603 x += f->output_data.x->left_pos;
2604 y += f->output_data.x->top_pos;
18686d47 2605
78589e07
RS
2606 /* Create all the necessary panes and their items. */
2607 i = 0;
2608 while (i < menu_items_used)
dcfdbac7 2609 {
78589e07 2610 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
dcfdbac7 2611 {
78589e07
RS
2612 /* Create a new pane. */
2613 Lisp_Object pane_name, prefix;
2614 char *pane_string;
2615
2616 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2617 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2618 pane_string = (NILP (pane_name)
2619 ? "" : (char *) XSTRING (pane_name)->data);
2620 if (keymaps && !NILP (prefix))
2621 pane_string++;
2622
92280f67 2623 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
2624 if (lpane == XM_FAILURE)
2625 {
92280f67 2626 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
78589e07
RS
2627 *error = "Can't create pane";
2628 return Qnil;
2629 }
2630 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
2631
2632 /* Find the width of the widest item in this pane. */
2633 maxwidth = 0;
2634 j = i;
2635 while (j < menu_items_used)
2636 {
2637 Lisp_Object item;
2638 item = XVECTOR (menu_items)->contents[j];
2639 if (EQ (item, Qt))
2640 break;
2641 if (NILP (item))
2642 {
2643 j++;
2644 continue;
2645 }
fc932ac6 2646 width = STRING_BYTES (XSTRING (item));
4e8d3549
RS
2647 if (width > maxwidth)
2648 maxwidth = width;
2649
2650 j += MENU_ITEMS_ITEM_LENGTH;
2651 }
dcfdbac7 2652 }
fcaa7665
RS
2653 /* Ignore a nil in the item list.
2654 It's meaningful only for dialog boxes. */
2655 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2656 i += 1;
78589e07 2657 else
dcfdbac7 2658 {
78589e07 2659 /* Create a new item within current pane. */
3e703b25 2660 Lisp_Object item_name, enable, descrip, help;
4e8d3549 2661 unsigned char *item_data;
3e703b25 2662 char *help_string;
78589e07
RS
2663
2664 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2665 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2666 descrip
2667 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3e703b25
GM
2668 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2669 help_string = STRINGP (help) ? XSTRING (help)->data : NULL;
2670
78589e07 2671 if (!NILP (descrip))
4e8d3549 2672 {
fc932ac6 2673 int gap = maxwidth - STRING_BYTES (XSTRING (item_name));
4e8d3549
RS
2674#ifdef C_ALLOCA
2675 Lisp_Object spacer;
2676 spacer = Fmake_string (make_number (gap), make_number (' '));
2677 item_name = concat2 (item_name, spacer);
2678 item_name = concat2 (item_name, descrip);
2679 item_data = XSTRING (item_name)->data;
2680#else
2681 /* if alloca is fast, use that to make the space,
2682 to reduce gc needs. */
2683 item_data
2684 = (unsigned char *) alloca (maxwidth
fc932ac6 2685 + STRING_BYTES (XSTRING (descrip)) + 1);
4e8d3549 2686 bcopy (XSTRING (item_name)->data, item_data,
fc932ac6 2687 STRING_BYTES (XSTRING (item_name)));
4e8d3549
RS
2688 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2689 item_data[j] = ' ';
2690 bcopy (XSTRING (descrip)->data, item_data + j,
fc932ac6
RS
2691 STRING_BYTES (XSTRING (descrip)));
2692 item_data[j + STRING_BYTES (XSTRING (descrip))] = 0;
4e8d3549
RS
2693#endif
2694 }
2695 else
2696 item_data = XSTRING (item_name)->data;
78589e07 2697
92280f67
RS
2698 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2699 menu, lpane, 0, item_data,
3e703b25 2700 !NILP (enable), help_string)
dcfdbac7
JB
2701 == XM_FAILURE)
2702 {
92280f67 2703 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
dcfdbac7 2704 *error = "Can't add selection to menu";
78589e07 2705 return Qnil;
dcfdbac7 2706 }
78589e07 2707 i += MENU_ITEMS_ITEM_LENGTH;
dcfdbac7
JB
2708 }
2709 }
4e8d3549 2710
78589e07 2711 /* All set and ready to fly. */
92280f67
RS
2712 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2713 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
f1847de3 2714 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
92280f67 2715 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
f1847de3 2716 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
78589e07
RS
2717 x = min (x, dispwidth);
2718 y = min (y, dispheight);
2719 x = max (x, 1);
2720 y = max (y, 1);
92280f67 2721 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
2722 &ulx, &uly, &width, &height);
2723 if (ulx+width > dispwidth)
2724 {
78589e07 2725 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
2726 ulx = dispwidth - width;
2727 }
2728 if (uly+height > dispheight)
2729 {
78589e07 2730 y -= (uly + height) - dispheight;
dcfdbac7
JB
2731 uly = dispheight - height;
2732 }
78589e07
RS
2733 if (ulx < 0) x -= ulx;
2734 if (uly < 0) y -= uly;
121e4555
KH
2735
2736 XMenuSetAEQ (menu, TRUE);
78589e07
RS
2737 XMenuSetFreeze (menu, TRUE);
2738 pane = selidx = 0;
3e703b25
GM
2739
2740 /* Help display under X won't work because XMenuActivate contains
2741 a loop that doesn't give Emacs a chance to process it. */
2742 menu_help_frame = f;
92280f67 2743 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3e703b25
GM
2744 x, y, ButtonReleaseMask, &datap,
2745 menu_help_callback);
a352a815
RS
2746
2747
f1df80a8 2748#ifdef HAVE_X_WINDOWS
a352a815
RS
2749 /* Assume the mouse has moved out of the X window.
2750 If it has actually moved in, we will get an EnterNotify. */
29e460bd 2751 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
f1df80a8 2752#endif
a352a815 2753
dcfdbac7
JB
2754 switch (status)
2755 {
2756 case XM_SUCCESS:
2757#ifdef XDEBUG
2758 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2759#endif
fa6d54d9 2760
78589e07
RS
2761 /* Find the item number SELIDX in pane number PANE. */
2762 i = 0;
2763 while (i < menu_items_used)
fa6d54d9 2764 {
78589e07 2765 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
088831f6 2766 {
78589e07
RS
2767 if (pane == 0)
2768 pane_prefix
2769 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2770 pane--;
2771 i += MENU_ITEMS_PANE_LENGTH;
088831f6 2772 }
78589e07 2773 else
ab6ee1a0 2774 {
78589e07 2775 if (pane == -1)
ab6ee1a0 2776 {
78589e07 2777 if (selidx == 0)
ab6ee1a0 2778 {
78589e07
RS
2779 entry
2780 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2781 if (keymaps != 0)
ab6ee1a0 2782 {
78589e07
RS
2783 entry = Fcons (entry, Qnil);
2784 if (!NILP (pane_prefix))
2785 entry = Fcons (pane_prefix, entry);
ab6ee1a0 2786 }
78589e07 2787 break;
ab6ee1a0 2788 }
78589e07 2789 selidx--;
ab6ee1a0 2790 }
78589e07 2791 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
2792 }
2793 }
78589e07 2794 break;
dcfdbac7 2795
78589e07 2796 case XM_FAILURE:
78589e07
RS
2797 *error = "Can't activate menu";
2798 case XM_IA_SELECT:
2799 case XM_NO_SELECT:
2800 entry = Qnil;
2801 break;
dcfdbac7 2802 }
92280f67 2803 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
a5285df3 2804
87485d6f 2805#ifdef HAVE_X_WINDOWS
a5285df3
RS
2806 /* State that no mouse buttons are now held.
2807 (The oldXMenu code doesn't track this info for us.)
2808 That is not necessarily true, but the fiction leads to reasonable
2809 results, and it is a pain to ask which are actually held now. */
e9a79fb2 2810 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
87485d6f 2811#endif
a5285df3 2812
78589e07 2813 return entry;
dcfdbac7 2814}
4dedbfe0 2815
78589e07 2816#endif /* not USE_X_TOOLKIT */
1e659e4c
RS
2817
2818#endif /* HAVE_MENUS */
088831f6 2819\f
dfcf069d 2820void
78589e07 2821syms_of_xmenu ()
dcfdbac7 2822{
78589e07
RS
2823 staticpro (&menu_items);
2824 menu_items = Qnil;
dcfdbac7 2825
0314aacb
RS
2826 Qdebug_on_next_call = intern ("debug-on-next-call");
2827 staticpro (&Qdebug_on_next_call);
2828
bfc524bc
RS
2829 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2830 "Frame for which we are updating a menu.\n\
2831The enable predicate for a menu command should check this variable.");
2832 Vmenu_updating_frame = Qnil;
2833
8ed87156 2834#ifdef USE_X_TOOLKIT
4dedbfe0 2835 widget_id_tick = (1<<16);
88766961 2836 next_menubar_widget_id = 1;
8ed87156
RS
2837#endif
2838
78589e07 2839 defsubr (&Sx_popup_menu);
1e659e4c 2840#ifdef HAVE_MENUS
165e1749 2841 defsubr (&Sx_popup_dialog);
1e659e4c 2842#endif
dcfdbac7 2843}