*** empty log message ***
[bpt/emacs.git] / src / xmenu.c
CommitLineData
dcfdbac7 1/* X Communication module for terminals which understand the X protocol.
2b06561a 2 Copyright (C) 1986, 88, 93, 94, 96, 99, 2000 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"
02067692 41#include "keyboard.h"
7708e9bd 42#include "frame.h"
dcfdbac7 43#include "window.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;
6bbd7a29 694 int xpos = 0, ypos = 0;
78589e07
RS
695 Lisp_Object title;
696 char *error_name;
697 Lisp_Object selection;
6bbd7a29 698 struct frame *f = NULL;
78589e07
RS
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)
2b06561a
DL
711 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
712 || EQ (XCAR (position), Qtool_bar))))
78589e07
RS
713 {
714 /* Use the mouse's current position. */
b404828f 715 FRAME_PTR new_f = SELECTED_FRAME ();
78589e07 716 Lisp_Object bar_window;
dfcf069d 717 enum scroll_bar_part part;
78589e07
RS
718 unsigned long time;
719
b137c582 720 if (mouse_position_hook)
46b657e2
RS
721 (*mouse_position_hook) (&new_f, 1, &bar_window,
722 &part, &x, &y, &time);
5ca2ef64 723 if (new_f != 0)
a39f0477 724 XSETFRAME (window, new_f);
5ca2ef64
RS
725 else
726 {
727 window = selected_window;
33b43fa6
KH
728 XSETFASTINT (x, 0);
729 XSETFASTINT (y, 0);
5ca2ef64 730 }
78589e07
RS
731 }
732 else
733 {
734 tem = Fcar (position);
b5bb2705 735 if (CONSP (tem))
78589e07
RS
736 {
737 window = Fcar (Fcdr (position));
738 x = Fcar (tem);
739 y = Fcar (Fcdr (tem));
740 }
741 else
742 {
9685a93f 743 for_click = 1;
78589e07
RS
744 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
745 window = Fcar (tem); /* POSN_WINDOW (tem) */
746 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
747 x = Fcar (tem);
748 y = Fcdr (tem);
78589e07
RS
749 }
750 }
751
752 CHECK_NUMBER (x, 0);
753 CHECK_NUMBER (y, 0);
754
755 /* Decode where to put the menu. */
756
b5bb2705 757 if (FRAMEP (window))
78589e07
RS
758 {
759 f = XFRAME (window);
78589e07
RS
760 xpos = 0;
761 ypos = 0;
762 }
b5bb2705 763 else if (WINDOWP (window))
78589e07
RS
764 {
765 CHECK_LIVE_WINDOW (window, 0);
766 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
767
99d3fac7
KH
768 xpos = (FONT_WIDTH (f->output_data.x->font)
769 * XFASTINT (XWINDOW (window)->left));
770 ypos = (f->output_data.x->line_height
771 * XFASTINT (XWINDOW (window)->top));
78589e07
RS
772 }
773 else
774 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
775 but I don't want to make one now. */
776 CHECK_WINDOW (window, 0);
777
778 xpos += XINT (x);
779 ypos += XINT (y);
bfc524bc
RS
780
781 XSETFRAME (Vmenu_updating_frame, f);
78589e07 782 }
bfc524bc 783 Vmenu_updating_frame = Qnil;
1e659e4c 784#endif /* HAVE_MENUS */
78589e07
RS
785
786 title = Qnil;
787 GCPRO1 (title);
788
789 /* Decode the menu items from what was specified. */
18686d47 790
02067692
SM
791 keymap = get_keymap (menu, 0, 0);
792 if (CONSP (keymap))
18686d47
RS
793 {
794 /* We were given a keymap. Extract menu info from the keymap. */
795 Lisp_Object prompt;
18686d47 796
78589e07 797 /* Extract the detailed info to make one pane. */
101bb4a5 798 keymap_panes (&menu, 1, NILP (position));
78589e07 799
18686d47
RS
800 /* Search for a string appearing directly as an element of the keymap.
801 That string is the title of the menu. */
802 prompt = map_prompt (keymap);
4f9ad016
RS
803 if (NILP (title) && !NILP (prompt))
804 title = prompt;
18686d47 805
78589e07
RS
806 /* Make that be the pane title of the first pane. */
807 if (!NILP (prompt) && menu_items_n_panes >= 0)
808 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
809
810 keymaps = 1;
18686d47 811 }
02067692 812 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
18686d47
RS
813 {
814 /* We were given a list of keymaps. */
18686d47
RS
815 int nmaps = XFASTINT (Flength (menu));
816 Lisp_Object *maps
817 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
818 int i;
78589e07
RS
819
820 title = Qnil;
18686d47
RS
821
822 /* The first keymap that has a prompt string
823 supplies the menu title. */
b5bb2705 824 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
18686d47 825 {
78589e07
RS
826 Lisp_Object prompt;
827
02067692 828 maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0);
18686d47
RS
829
830 prompt = map_prompt (keymap);
78589e07
RS
831 if (NILP (title) && !NILP (prompt))
832 title = prompt;
18686d47
RS
833 }
834
835 /* Extract the detailed info to make one pane. */
101bb4a5 836 keymap_panes (maps, nmaps, NILP (position));
78589e07
RS
837
838 /* Make the title be the pane title of the first pane. */
839 if (!NILP (title) && menu_items_n_panes >= 0)
840 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
841
842 keymaps = 1;
18686d47
RS
843 }
844 else
845 {
846 /* We were given an old-fashioned menu. */
78589e07
RS
847 title = Fcar (menu);
848 CHECK_STRING (title, 1);
18686d47 849
78589e07 850 list_of_panes (Fcdr (menu));
18686d47 851
78589e07
RS
852 keymaps = 0;
853 }
18686d47 854
78589e07 855 if (NILP (position))
18686d47 856 {
78589e07
RS
857 discard_menu_items ();
858 UNGCPRO;
859 return Qnil;
18686d47
RS
860 }
861
1e659e4c 862#ifdef HAVE_MENUS
78589e07
RS
863 /* Display them in a menu. */
864 BLOCK_INPUT;
18686d47 865
673a6211 866 selection = xmenu_show (f, xpos, ypos, for_click,
78589e07
RS
867 keymaps, title, &error_name);
868 UNBLOCK_INPUT;
18686d47 869
78589e07 870 discard_menu_items ();
18686d47 871
78589e07 872 UNGCPRO;
1e659e4c 873#endif /* HAVE_MENUS */
18686d47 874
78589e07
RS
875 if (error_name) error (error_name);
876 return selection;
18686d47 877}
165e1749 878
1e659e4c
RS
879#ifdef HAVE_MENUS
880
540e52d1 881DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
165e1749 882 "Pop up a dialog box and return user's selection.\n\
99fe880d
RS
883POSITION specifies which frame to use.\n\
884This is normally a mouse button event or a window or frame.\n\
885If POSITION is t, it means to use the frame the mouse is on.\n\
886The dialog box appears in the middle of the specified frame.\n\
165e1749 887\n\
99fe880d
RS
888CONTENTS specifies the alternatives to display in the dialog box.\n\
889It is a list of the form (TITLE ITEM1 ITEM2...).\n\
890Each ITEM is a cons cell (STRING . VALUE).\n\
fcaa7665
RS
891The return value is VALUE from the chosen item.\n\n\
892An ITEM may also be just a string--that makes a nonselectable item.\n\
893An ITEM may also be nil--that means to put all preceding items\n\
894on the left of the dialog box and all following items on the right.\n\
895\(By default, approximately half appear on each side.)")
99fe880d
RS
896 (position, contents)
897 Lisp_Object position, contents;
165e1749 898{
6bbd7a29 899 struct frame * f = NULL;
99fe880d 900 Lisp_Object window;
165e1749
FP
901
902 check_x ();
903
99fe880d 904 /* Decode the first argument: find the window or frame to use. */
88d4f6ec 905 if (EQ (position, Qt)
2b06561a
DL
906 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
907 || EQ (XCAR (position), Qtool_bar))))
165e1749 908 {
b14db4d7 909#if 0 /* Using the frame the mouse is on may not be right. */
99fe880d 910 /* Use the mouse's current position. */
b404828f 911 FRAME_PTR new_f = SELECTED_FRAME ();
99fe880d
RS
912 Lisp_Object bar_window;
913 int part;
914 unsigned long time;
915 Lisp_Object x, y;
165e1749 916
46b657e2 917 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
5ca2ef64 918
99fe880d 919 if (new_f != 0)
a39f0477 920 XSETFRAME (window, new_f);
99fe880d
RS
921 else
922 window = selected_window;
b14db4d7 923#endif
88d4f6ec 924 window = selected_window;
99fe880d
RS
925 }
926 else if (CONSP (position))
927 {
928 Lisp_Object tem;
929 tem = Fcar (position);
b5bb2705 930 if (CONSP (tem))
99fe880d 931 window = Fcar (Fcdr (position));
80670155
RS
932 else
933 {
99fe880d
RS
934 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
935 window = Fcar (tem); /* POSN_WINDOW (tem) */
165e1749 936 }
165e1749 937 }
99fe880d
RS
938 else if (WINDOWP (position) || FRAMEP (position))
939 window = position;
a9be6839
RS
940 else
941 window = Qnil;
165e1749 942
99fe880d 943 /* Decode where to put the menu. */
165e1749 944
b5bb2705 945 if (FRAMEP (window))
99fe880d 946 f = XFRAME (window);
b5bb2705 947 else if (WINDOWP (window))
165e1749 948 {
99fe880d
RS
949 CHECK_LIVE_WINDOW (window, 0);
950 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
165e1749 951 }
99fe880d
RS
952 else
953 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
954 but I don't want to make one now. */
955 CHECK_WINDOW (window, 0);
165e1749 956
99fe880d
RS
957#ifndef USE_X_TOOLKIT
958 /* Display a menu with these alternatives
959 in the middle of frame F. */
960 {
961 Lisp_Object x, y, frame, newpos;
a39f0477
KH
962 XSETFRAME (frame, f);
963 XSETINT (x, x_pixel_width (f) / 2);
964 XSETINT (y, x_pixel_height (f) / 2);
99fe880d
RS
965 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
966
967 return Fx_popup_menu (newpos,
968 Fcons (Fcar (contents), Fcons (contents, Qnil)));
969 }
970#else
971 {
972 Lisp_Object title;
973 char *error_name;
974 Lisp_Object selection;
165e1749 975
99fe880d
RS
976 /* Decode the dialog items from what was specified. */
977 title = Fcar (contents);
978 CHECK_STRING (title, 1);
165e1749 979
99fe880d 980 list_of_panes (Fcons (contents, Qnil));
165e1749 981
99fe880d
RS
982 /* Display them in a dialog box. */
983 BLOCK_INPUT;
f8c2630d 984 selection = xdialog_show (f, 0, title, &error_name);
99fe880d 985 UNBLOCK_INPUT;
165e1749 986
99fe880d
RS
987 discard_menu_items ();
988
989 if (error_name) error (error_name);
990 return selection;
991 }
7464b131 992#endif
392d3f4b 993}
78589e07
RS
994\f
995#ifdef USE_X_TOOLKIT
18686d47 996
4dedbfe0 997/* Loop in Xt until the menu pulldown or dialog popup has been
f56e7ad2
RS
998 popped down (deactivated). This is used for x-popup-menu
999 and x-popup-dialog; it is not used for the menu bar any more.
c98fcf4b 1000
2e2b8e22 1001 NOTE: All calls to popup_get_selection should be protected
c98fcf4b 1002 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
aa669def 1003
4dedbfe0 1004void
2e2b8e22 1005popup_get_selection (initial_event, dpyinfo, id)
4dedbfe0 1006 XEvent *initial_event;
aa669def 1007 struct x_display_info *dpyinfo;
2e2b8e22 1008 LWLIB_ID id;
78589e07 1009{
4dedbfe0 1010 XEvent event;
78589e07 1011
aa669def
RS
1012 /* Define a queue to save up for later unreading
1013 all X events that don't pertain to the menu. */
1014 struct event_queue
1015 {
1016 XEvent event;
1017 struct event_queue *next;
1018 };
1019
1020 struct event_queue *queue = NULL;
1021 struct event_queue *queue_tmp;
1022
4dedbfe0
PR
1023 if (initial_event)
1024 event = *initial_event;
1025 else
1026 XtAppNextEvent (Xt_app_con, &event);
78589e07 1027
4dedbfe0 1028 while (1)
78589e07 1029 {
aa669def
RS
1030 /* Handle expose events for editor frames right away. */
1031 if (event.type == Expose)
1032 process_expose_from_menu (event);
10624005
KH
1033 /* Make sure we don't consider buttons grabbed after menu goes.
1034 And make sure to deactivate for any ButtonRelease,
1035 even if XtDispatchEvent doesn't do that. */
aa669def
RS
1036 else if (event.type == ButtonRelease
1037 && dpyinfo->display == event.xbutton.display)
10624005
KH
1038 {
1039 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1040 popup_activated_flag = 0;
c8b5aa3d
RS
1041#ifdef USE_MOTIF /* Pretending that the event came from a
1042 Btn1Down seems the only way to convince Motif to
1043 activate its callbacks; setting the XmNmenuPost
1044 isn't working. --marcus@sysc.pdx.edu. */
1045 event.xbutton.button = 1;
1046#endif
10624005 1047 }
2e2b8e22
KH
1048 /* If the user presses a key, deactivate the menu.
1049 The user is likely to do that if we get wedged. */
1050 else if (event.type == KeyPress
1051 && dpyinfo->display == event.xbutton.display)
1052 {
09155689
AS
1053 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1054 if (!IsModifierKey (keysym))
1055 {
1056 popup_activated_flag = 0;
1057 break;
1058 }
2e2b8e22 1059 }
d31d42cc
RS
1060 /* Button presses outside the menu also pop it down. */
1061 else if (event.type == ButtonPress
1062 && event.xany.display == dpyinfo->display
1063 && x_any_window_to_frame (dpyinfo, event.xany.window))
1064 {
1065 popup_activated_flag = 0;
1066 break;
1067 }
aa669def
RS
1068
1069 /* Queue all events not for this popup,
10624005 1070 except for Expose, which we've already handled, and ButtonRelease.
9572375b
KH
1071 Note that the X window is associated with the frame if this
1072 is a menu bar popup, but not if it's a dialog box. So we use
1073 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
aa669def 1074 if (event.type != Expose
10624005
KH
1075 && !(event.type == ButtonRelease
1076 && dpyinfo->display == event.xbutton.display)
aa669def 1077 && (event.xany.display != dpyinfo->display
9572375b 1078 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
aa669def 1079 {
aa669def
RS
1080 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1081
1082 if (queue_tmp != NULL)
1083 {
1084 queue_tmp->event = event;
1085 queue_tmp->next = queue;
1086 queue = queue_tmp;
1087 }
1088 }
9572375b
KH
1089 else
1090 XtDispatchEvent (&event);
aa669def
RS
1091
1092 if (!popup_activated ())
4dedbfe0
PR
1093 break;
1094 XtAppNextEvent (Xt_app_con, &event);
78589e07 1095 }
aa669def
RS
1096
1097 /* Unread any events that we got but did not handle. */
1098 while (queue != NULL)
1099 {
1100 queue_tmp = queue;
1101 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1102 queue = queue_tmp->next;
36cc2d38 1103 xfree ((char *)queue_tmp);
aa669def
RS
1104 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1105 interrupt_input_pending = 1;
1106 }
78589e07
RS
1107}
1108
88766961
RS
1109/* Activate the menu bar of frame F.
1110 This is called from keyboard.c when it gets the
1111 menu_bar_activate_event out of the Emacs event queue.
1112
1113 To activate the menu bar, we use the X button-press event
ac78b144 1114 that was saved in saved_menu_event.
88766961
RS
1115 That makes the toolkit do its thing.
1116
1117 But first we recompute the menu bar contents (the whole tree).
1118
1119 The reason for saving the button event until here, instead of
1120 passing it to the toolkit right away, is that we can safely
1121 execute Lisp code. */
1122
dfcf069d 1123void
88766961
RS
1124x_activate_menubar (f)
1125 FRAME_PTR f;
1126{
ac78b144 1127 if (!f->output_data.x->saved_menu_event->type)
88766961
RS
1128 return;
1129
a9be6839 1130 set_frame_menubar (f, 0, 1);
88766961 1131 BLOCK_INPUT;
ac78b144 1132 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
88766961 1133 UNBLOCK_INPUT;
a9be6839 1134#ifdef USE_MOTIF
745c34fb
RS
1135 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1136 pending_menu_activation = 1;
a9be6839 1137#endif
745c34fb 1138
88766961 1139 /* Ignore this if we get it a second time. */
ac78b144 1140 f->output_data.x->saved_menu_event->type = 0;
88766961
RS
1141}
1142
c98fcf4b 1143/* Detect if a dialog or menu has been posted. */
aa669def 1144
4dedbfe0
PR
1145int
1146popup_activated ()
1147{
1148 return popup_activated_flag;
1149}
1150
4dedbfe0
PR
1151/* This callback is invoked when the user selects a menubar cascade
1152 pushbutton, but before the pulldown menu is posted. */
78589e07
RS
1153
1154static void
4dedbfe0 1155popup_activate_callback (widget, id, client_data)
78589e07
RS
1156 Widget widget;
1157 LWLIB_ID id;
1158 XtPointer client_data;
1159{
7555d825
GM
1160#ifdef USE_MOTIF
1161 ++popup_activated_flag;
1162#else
4dedbfe0 1163 popup_activated_flag = 1;
7555d825
GM
1164#endif
1165}
1166
1167/* This callback is invoked when a dialog or menu is finished being
1168 used and has been unposted. */
1169
1170static void
1171popup_deactivate_callback (widget, id, client_data)
1172 Widget widget;
1173 LWLIB_ID id;
1174 XtPointer client_data;
1175{
1176#ifdef USE_MOTIF
1177 --popup_activated_flag;
1178#else
1179 popup_activated_flag = 0;
1180#endif
78589e07
RS
1181}
1182
850df50b
GM
1183/* Lwlib callback called when menu items are highlighted/unhighlighted
1184 while moving the mouse over them. WIDGET is the menu bar or menu
1185 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1186 the widget_value structure for the menu item, or null in case of
1187 unhighlighting. */
1188
1189void
1190menu_highlight_callback (widget, id, call_data)
1191 Widget widget;
1192 LWLIB_ID id;
1193 void *call_data;
1194{
1195 widget_value *wv = (widget_value *) call_data;
1196 struct frame *f;
1197 Lisp_Object frame, help;
850df50b 1198
9cd50434
GM
1199 help = wv && wv->help ? build_string (wv->help) : Qnil;
1200
850df50b
GM
1201 /* Determine the frame for the help event. */
1202 f = menubar_id_to_frame (id);
1203 if (f)
9cd50434
GM
1204 {
1205 XSETFRAME (frame, f);
1206 kbd_buffer_store_help_event (frame, help);
1207 }
850df50b
GM
1208 else
1209 {
1210 /* WIDGET is the popup menu. It's parent is the frame's
1211 widget. See which frame that is. */
1212 Widget frame_widget = XtParent (widget);
1213 Lisp_Object tail;
1214
1215 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1216 {
1217 frame = XCAR (tail);
1218 if (GC_FRAMEP (frame)
1219 && (f = XFRAME (frame),
1220 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1221 break;
1222 }
850df50b 1223
9cd50434
GM
1224 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1225 }
850df50b
GM
1226}
1227
4dedbfe0
PR
1228/* This callback is called from the menu bar pulldown menu
1229 when the user makes a selection.
1230 Figure out what the user chose
1231 and put the appropriate events into the keyboard buffer. */
1232
78589e07 1233static void
4dedbfe0 1234menubar_selection_callback (widget, id, client_data)
78589e07
RS
1235 Widget widget;
1236 LWLIB_ID id;
1237 XtPointer client_data;
1238{
c63f6952 1239 Lisp_Object prefix, entry;
88766961 1240 FRAME_PTR f = menubar_id_to_frame (id);
4dedbfe0
PR
1241 Lisp_Object vector;
1242 Lisp_Object *subprefix_stack;
1243 int submenu_depth = 0;
1244 int i;
1245
1246 if (!f)
1247 return;
6bbd7a29 1248 entry = Qnil;
4dedbfe0
PR
1249 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1250 vector = f->menu_bar_vector;
1251 prefix = Qnil;
1252 i = 0;
1253 while (i < f->menu_bar_items_used)
1254 {
4dedbfe0
PR
1255 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1256 {
1257 subprefix_stack[submenu_depth++] = prefix;
1258 prefix = entry;
1259 i++;
1260 }
1261 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1262 {
1263 prefix = subprefix_stack[--submenu_depth];
1264 i++;
1265 }
1266 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1267 {
4cb35c39 1268 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
4dedbfe0
PR
1269 i += MENU_ITEMS_PANE_LENGTH;
1270 }
1271 else
1272 {
4cb35c39 1273 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
01d5e892
RS
1274 /* The EMACS_INT cast avoids a warning. There's no problem
1275 as long as pointers have enough bits to hold small integers. */
1276 if ((int) (EMACS_INT) client_data == i)
4dedbfe0
PR
1277 {
1278 int j;
1279 struct input_event buf;
4cb35c39 1280 Lisp_Object frame;
4dedbfe0 1281
4cb35c39 1282 XSETFRAME (frame, f);
9979315d
GM
1283 buf.kind = MENU_BAR_EVENT;
1284 buf.frame_or_window = frame;
1285 buf.arg = frame;
4dedbfe0
PR
1286 kbd_buffer_store_event (&buf);
1287
1288 for (j = 0; j < submenu_depth; j++)
1289 if (!NILP (subprefix_stack[j]))
1290 {
9979315d
GM
1291 buf.kind = MENU_BAR_EVENT;
1292 buf.frame_or_window = frame;
1293 buf.arg = subprefix_stack[j];
4dedbfe0
PR
1294 kbd_buffer_store_event (&buf);
1295 }
1296
1297 if (!NILP (prefix))
1298 {
9979315d
GM
1299 buf.kind = MENU_BAR_EVENT;
1300 buf.frame_or_window = frame;
1301 buf.arg = prefix;
4dedbfe0
PR
1302 kbd_buffer_store_event (&buf);
1303 }
1304
9979315d
GM
1305 buf.kind = MENU_BAR_EVENT;
1306 buf.frame_or_window = frame;
1307 buf.arg = entry;
4dedbfe0
PR
1308 kbd_buffer_store_event (&buf);
1309
1310 return;
1311 }
1312 i += MENU_ITEMS_ITEM_LENGTH;
1313 }
1314 }
18686d47
RS
1315}
1316
f9655c60
RS
1317/* Allocate a widget_value, blocking input. */
1318
1319widget_value *
1320xmalloc_widget_value ()
1321{
1322 widget_value *value;
1323
1324 BLOCK_INPUT;
1325 value = malloc_widget_value ();
1326 UNBLOCK_INPUT;
1327
1328 return value;
1329}
4dedbfe0
PR
1330
1331/* This recursively calls free_widget_value on the tree of widgets.
18686d47 1332 It must free all data that was malloc'ed for these widget_values.
78589e07
RS
1333 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1334 must be left alone. */
1335
18686d47
RS
1336void
1337free_menubar_widget_value_tree (wv)
1338 widget_value *wv;
1339{
1340 if (! wv) return;
18686d47
RS
1341
1342 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1343
1344 if (wv->contents && (wv->contents != (widget_value*)1))
1345 {
1346 free_menubar_widget_value_tree (wv->contents);
1347 wv->contents = (widget_value *) 0xDEADBEEF;
1348 }
1349 if (wv->next)
1350 {
1351 free_menubar_widget_value_tree (wv->next);
1352 wv->next = (widget_value *) 0xDEADBEEF;
1353 }
1354 BLOCK_INPUT;
1355 free_widget_value (wv);
1356 UNBLOCK_INPUT;
1357}
4dedbfe0
PR
1358\f
1359/* Return a tree of widget_value structures for a menu bar item
1360 whose event type is ITEM_KEY (with string ITEM_NAME)
1361 and whose contents come from the list of keymaps MAPS. */
1362
1363static widget_value *
1364single_submenu (item_key, item_name, maps)
1365 Lisp_Object item_key, item_name, maps;
1366{
1367 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1368 int i;
1369 int submenu_depth = 0;
1370 Lisp_Object length;
1371 int len;
1372 Lisp_Object *mapvec;
1373 widget_value **submenu_stack;
4dedbfe0 1374 int previous_items = menu_items_used;
71dca3e3 1375 int top_level_items = 0;
4dedbfe0
PR
1376
1377 length = Flength (maps);
1378 len = XINT (length);
1379
1380 /* Convert the list MAPS into a vector MAPVEC. */
1381 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1382 for (i = 0; i < len; i++)
1383 {
1384 mapvec[i] = Fcar (maps);
1385 maps = Fcdr (maps);
1386 }
1387
1388 menu_items_n_panes = 0;
1389
1390 /* Loop over the given keymaps, making a pane for each map.
1391 But don't make a pane that is empty--ignore that map instead. */
1392 for (i = 0; i < len; i++)
71dca3e3 1393 {
846e8c10 1394 if (SYMBOLP (mapvec[i])
02067692 1395 || (CONSP (mapvec[i]) && !KEYMAPP (mapvec[i])))
71dca3e3 1396 {
846e8c10
RS
1397 /* Here we have a command at top level in the menu bar
1398 as opposed to a submenu. */
71dca3e3
RS
1399 top_level_items = 1;
1400 push_menu_pane (Qnil, Qnil);
850df50b
GM
1401 push_menu_item (item_name, Qt, item_key, mapvec[i],
1402 Qnil, Qnil, Qnil, Qnil);
71dca3e3
RS
1403 }
1404 else
98381e42 1405 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
71dca3e3 1406 }
4dedbfe0
PR
1407
1408 /* Create a tree of widget_value objects
1409 representing the panes and their items. */
1410
1411 submenu_stack
1412 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
f7fab165 1413 wv = xmalloc_widget_value ();
4dedbfe0
PR
1414 wv->name = "menu";
1415 wv->value = 0;
1416 wv->enabled = 1;
3427a3db 1417 wv->button_type = BUTTON_TYPE_NONE;
4dedbfe0
PR
1418 first_wv = wv;
1419 save_wv = 0;
71dca3e3 1420 prev_wv = 0;
4dedbfe0
PR
1421
1422 /* Loop over all panes and items made during this call
1423 and construct a tree of widget_value objects.
1424 Ignore the panes and items made by previous calls to
1425 single_submenu, even though those are also in menu_items. */
1426 i = previous_items;
1427 while (i < menu_items_used)
1428 {
1429 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1430 {
1431 submenu_stack[submenu_depth++] = save_wv;
1432 save_wv = prev_wv;
1433 prev_wv = 0;
1434 i++;
1435 }
1436 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1437 {
1438 prev_wv = save_wv;
1439 save_wv = submenu_stack[--submenu_depth];
1440 i++;
1441 }
1442 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1443 && submenu_depth != 0)
1444 i += MENU_ITEMS_PANE_LENGTH;
1445 /* Ignore a nil in the item list.
1446 It's meaningful only for dialog boxes. */
1447 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1448 i += 1;
1449 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1450 {
1451 /* Create a new pane. */
1452 Lisp_Object pane_name, prefix;
1453 char *pane_string;
1454 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1455 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
703dc2a8
KH
1456#ifndef HAVE_MULTILINGUAL_MENU
1457 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1458 pane_name = string_make_unibyte (pane_name);
1459#endif
4dedbfe0
PR
1460 pane_string = (NILP (pane_name)
1461 ? "" : (char *) XSTRING (pane_name)->data);
1462 /* If there is just one top-level pane, put all its items directly
1463 under the top-level menu. */
1464 if (menu_items_n_panes == 1)
1465 pane_string = "";
1466
1467 /* If the pane has a meaningful name,
1468 make the pane a top-level menu item
1469 with its items as a submenu beneath it. */
1470 if (strcmp (pane_string, ""))
1471 {
f7fab165 1472 wv = xmalloc_widget_value ();
4dedbfe0
PR
1473 if (save_wv)
1474 save_wv->next = wv;
1475 else
1476 first_wv->contents = wv;
1477 wv->name = pane_string;
ffcb5a51
RS
1478 /* Ignore the @ that means "separate pane".
1479 This is a kludge, but this isn't worth more time. */
1480 if (!NILP (prefix) && wv->name[0] == '@')
4dedbfe0
PR
1481 wv->name++;
1482 wv->value = 0;
1483 wv->enabled = 1;
3427a3db 1484 wv->button_type = BUTTON_TYPE_NONE;
4dedbfe0
PR
1485 }
1486 save_wv = wv;
1487 prev_wv = 0;
1488 i += MENU_ITEMS_PANE_LENGTH;
1489 }
1490 else
1491 {
1492 /* Create a new item within current pane. */
3427a3db 1493 Lisp_Object item_name, enable, descrip, def, type, selected;
850df50b
GM
1494 Lisp_Object help;
1495
4dedbfe0
PR
1496 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1497 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1498 descrip
1499 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
a352a815 1500 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
3427a3db
GM
1501 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
1502 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
850df50b 1503 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3427a3db 1504
703dc2a8 1505#ifndef HAVE_MULTILINGUAL_MENU
3427a3db
GM
1506 if (STRING_MULTIBYTE (item_name))
1507 item_name = string_make_unibyte (item_name);
1508 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1509 descrip = string_make_unibyte (descrip);
703dc2a8 1510#endif
3427a3db 1511
f7fab165 1512 wv = xmalloc_widget_value ();
4dedbfe0
PR
1513 if (prev_wv)
1514 prev_wv->next = wv;
71dca3e3 1515 else
4dedbfe0 1516 save_wv->contents = wv;
71dca3e3 1517
4dedbfe0
PR
1518 wv->name = (char *) XSTRING (item_name)->data;
1519 if (!NILP (descrip))
1520 wv->key = (char *) XSTRING (descrip)->data;
1521 wv->value = 0;
01d5e892
RS
1522 /* The EMACS_INT cast avoids a warning. There's no problem
1523 as long as pointers have enough bits to hold small integers. */
1524 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
4dedbfe0 1525 wv->enabled = !NILP (enable);
3427a3db
GM
1526
1527 if (NILP (type))
1528 wv->button_type = BUTTON_TYPE_NONE;
1529 else if (EQ (type, QCradio))
1530 wv->button_type = BUTTON_TYPE_RADIO;
1531 else if (EQ (type, QCtoggle))
1532 wv->button_type = BUTTON_TYPE_TOGGLE;
1533 else
1534 abort ();
1535
1536 wv->selected = !NILP (selected);
850df50b
GM
1537 if (STRINGP (help))
1538 wv->help = XSTRING (help)->data;
3427a3db 1539
4dedbfe0
PR
1540 prev_wv = wv;
1541
1542 i += MENU_ITEMS_ITEM_LENGTH;
1543 }
1544 }
1545
71dca3e3
RS
1546 /* If we have just one "menu item"
1547 that was originally a button, return it by itself. */
1548 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1549 {
1550 wv = first_wv->contents;
1551 free_widget_value (first_wv);
1552 return wv;
1553 }
1554
4dedbfe0
PR
1555 return first_wv;
1556}
1557\f
cffa74ea
FP
1558extern void EmacsFrameSetCharSize ();
1559
4bcdbab1
KH
1560/* Recompute all the widgets of frame F, when the menu bar
1561 has been changed. */
4dedbfe0 1562
18686d47 1563static void
6af6cbb5 1564update_frame_menubar (f)
18686d47
RS
1565 FRAME_PTR f;
1566{
7556890b 1567 struct x_output *x = f->output_data.x;
cffa74ea 1568 int columns, rows;
18686d47
RS
1569 int menubar_changed;
1570
4dedbfe0
PR
1571 /* We assume the menubar contents has changed if the global flag is set,
1572 or if the current buffer has changed, or if the menubar has never
1573 been updated before.
1574 */
18686d47
RS
1575 menubar_changed = (x->menubar_widget
1576 && !XtIsManaged (x->menubar_widget));
1577
1578 if (! (menubar_changed))
1579 return;
1580
1581 BLOCK_INPUT;
cffa74ea
FP
1582 /* Save the size of the frame because the pane widget doesn't accept to
1583 resize itself. So force it. */
1584 columns = f->width;
1585 rows = f->height;
1586
4dedbfe0
PR
1587 /* Do the voodoo which means "I'm changing lots of things, don't try to
1588 refigure sizes until I'm done." */
1589 lw_refigure_widget (x->column_widget, False);
cffa74ea 1590
18686d47
RS
1591 /* the order in which children are managed is the top to
1592 bottom order in which they are displayed in the paned window.
1593 First, remove the text-area widget.
1594 */
1595 XtUnmanageChild (x->edit_widget);
1596
1597 /* remove the menubar that is there now, and put up the menubar that
1598 should be there.
1599 */
1600 if (menubar_changed)
1601 {
1602 XtManageChild (x->menubar_widget);
1603 XtMapWidget (x->menubar_widget);
6a2bfc84 1604 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
18686d47
RS
1605 }
1606
c98fcf4b 1607 /* Re-manage the text-area widget, and then thrash the sizes. */
18686d47 1608 XtManageChild (x->edit_widget);
bf82bce4 1609 x_set_menu_resources_from_menu_face (f, x->menubar_widget);
4dedbfe0 1610 lw_refigure_widget (x->column_widget, True);
cffa74ea
FP
1611
1612 /* Force the pane widget to resize itself with the right values. */
1613 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
18686d47
RS
1614 UNBLOCK_INPUT;
1615}
1616
4bcdbab1
KH
1617/* Set the contents of the menubar widgets of frame F.
1618 The argument FIRST_TIME is currently ignored;
1619 it is set the first time this is called, from initialize_frame_menubar. */
1620
18686d47 1621void
88766961 1622set_frame_menubar (f, first_time, deep_p)
18686d47 1623 FRAME_PTR f;
706aa2f2 1624 int first_time;
88766961 1625 int deep_p;
18686d47 1626{
7556890b 1627 Widget menubar_widget = f->output_data.x->menubar_widget;
faa935b6 1628 Lisp_Object items;
4d19cb8e 1629 widget_value *wv, *first_wv, *prev_wv = 0;
5b3557df 1630 int i;
88766961 1631 LWLIB_ID id;
18686d47 1632
bfc524bc
RS
1633 XSETFRAME (Vmenu_updating_frame, f);
1634
7556890b
RS
1635 if (f->output_data.x->id == 0)
1636 f->output_data.x->id = next_menubar_widget_id++;
1637 id = f->output_data.x->id;
37a98547 1638
88766961
RS
1639 if (! menubar_widget)
1640 deep_p = 1;
745c34fb 1641 else if (pending_menu_activation && !deep_p)
a9be6839
RS
1642 deep_p = 1;
1643 /* Make the first call for any given frame always go deep. */
1644 else if (!f->output_data.x->saved_menu_event && !deep_p)
745c34fb
RS
1645 {
1646 deep_p = 1;
a9be6839
RS
1647 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1648 f->output_data.x->saved_menu_event->type = 0;
745c34fb 1649 }
18686d47 1650
f7fab165 1651 wv = xmalloc_widget_value ();
18686d47
RS
1652 wv->name = "menubar";
1653 wv->value = 0;
1654 wv->enabled = 1;
3427a3db 1655 wv->button_type = BUTTON_TYPE_NONE;
4d19cb8e 1656 first_wv = wv;
8d8a3494 1657
88766961 1658 if (deep_p)
18686d47 1659 {
88766961
RS
1660 /* Make a widget-value tree representing the entire menu trees. */
1661
1662 struct buffer *prev = current_buffer;
1663 Lisp_Object buffer;
1664 int specpdl_count = specpdl_ptr - specpdl;
1665 int previous_menu_items_used = f->menu_bar_items_used;
1666 Lisp_Object *previous_items
1667 = (Lisp_Object *) alloca (previous_menu_items_used
1668 * sizeof (Lisp_Object));
1669
0b1cf399
RS
1670 /* If we are making a new widget, its contents are empty,
1671 do always reinitialize them. */
1672 if (! menubar_widget)
1673 previous_menu_items_used = 0;
1674
88766961
RS
1675 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1676 specbind (Qinhibit_quit, Qt);
1677 /* Don't let the debugger step into this code
1678 because it is not reentrant. */
1679 specbind (Qdebug_on_next_call, Qnil);
1680
58226364 1681 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
88766961
RS
1682 if (NILP (Voverriding_local_map_menu_flag))
1683 {
1684 specbind (Qoverriding_terminal_local_map, Qnil);
1685 specbind (Qoverriding_local_map, Qnil);
1686 }
18686d47 1687
88766961 1688 set_buffer_internal_1 (XBUFFER (buffer));
18686d47 1689
88766961
RS
1690 /* Run the Lucid hook. */
1691 call1 (Vrun_hooks, Qactivate_menubar_hook);
1692 /* If it has changed current-menubar from previous value,
1693 really recompute the menubar from the value. */
1694 if (! NILP (Vlucid_menu_bar_dirty_flag))
1695 call0 (Qrecompute_lucid_menubar);
a57634d4 1696 safe_run_hooks (Qmenu_bar_update_hook);
88766961 1697 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
18686d47 1698
88766961 1699 items = FRAME_MENU_BAR_ITEMS (f);
8d8a3494 1700
88766961 1701 inhibit_garbage_collection ();
8d8a3494 1702
88766961
RS
1703 /* Save the frame's previous menu bar contents data. */
1704 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1705 previous_menu_items_used * sizeof (Lisp_Object));
8d8a3494 1706
88766961
RS
1707 /* Fill in the current menu bar contents. */
1708 menu_items = f->menu_bar_vector;
1709 menu_items_allocated = XVECTOR (menu_items)->size;
1710 init_menu_items ();
f04366cb 1711 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1712 {
1713 Lisp_Object key, string, maps;
1714
1715 key = XVECTOR (items)->contents[i];
1716 string = XVECTOR (items)->contents[i + 1];
1717 maps = XVECTOR (items)->contents[i + 2];
1718 if (NILP (string))
1719 break;
1720
1721 wv = single_submenu (key, string, maps);
1722 if (prev_wv)
1723 prev_wv->next = wv;
1724 else
1725 first_wv->contents = wv;
1726 /* Don't set wv->name here; GC during the loop might relocate it. */
1727 wv->enabled = 1;
3427a3db 1728 wv->button_type = BUTTON_TYPE_NONE;
88766961
RS
1729 prev_wv = wv;
1730 }
1731
1732 finish_menu_items ();
1733
1734 set_buffer_internal_1 (prev);
8d8a3494 1735 unbind_to (specpdl_count, Qnil);
8d8a3494 1736
88766961
RS
1737 /* If there has been no change in the Lisp-level contents
1738 of the menu bar, skip redisplaying it. Just exit. */
1739
1740 for (i = 0; i < previous_menu_items_used; i++)
1741 if (menu_items_used == i
99d3fac7 1742 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
88766961 1743 break;
62555c22 1744 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
88766961
RS
1745 {
1746 free_menubar_widget_value_tree (first_wv);
1747 menu_items = Qnil;
1748
1749 return;
1750 }
1751
1752 /* Now GC cannot happen during the lifetime of the widget_value,
1753 so it's safe to store data from a Lisp_String. */
1754 wv = first_wv->contents;
f04366cb 1755 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1756 {
1757 Lisp_Object string;
1758 string = XVECTOR (items)->contents[i + 1];
1759 if (NILP (string))
1760 break;
1761 wv->name = (char *) XSTRING (string)->data;
1762 wv = wv->next;
1763 }
1764
1765 f->menu_bar_vector = menu_items;
1766 f->menu_bar_items_used = menu_items_used;
1767 menu_items = Qnil;
4d19cb8e 1768 }
88766961
RS
1769 else
1770 {
1771 /* Make a widget-value tree containing
1772 just the top level menu bar strings. */
4d19cb8e 1773
88766961 1774 items = FRAME_MENU_BAR_ITEMS (f);
f04366cb 1775 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1776 {
1777 Lisp_Object string;
1778
1779 string = XVECTOR (items)->contents[i + 1];
1780 if (NILP (string))
1781 break;
1782
f7fab165 1783 wv = xmalloc_widget_value ();
88766961
RS
1784 wv->name = (char *) XSTRING (string)->data;
1785 wv->value = 0;
1786 wv->enabled = 1;
3427a3db 1787 wv->button_type = BUTTON_TYPE_NONE;
fe8fa62f
RS
1788 /* This prevents lwlib from assuming this
1789 menu item is really supposed to be empty. */
1790 /* The EMACS_INT cast avoids a warning.
1791 This value just has to be different from small integers. */
1792 wv->call_data = (void *) (EMACS_INT) (-1);
88766961
RS
1793
1794 if (prev_wv)
1795 prev_wv->next = wv;
1796 else
1797 first_wv->contents = wv;
1798 prev_wv = wv;
1799 }
62555c22
RS
1800
1801 /* Forget what we thought we knew about what is in the
1802 detailed contents of the menu bar menus.
1803 Changing the top level always destroys the contents. */
1804 f->menu_bar_items_used = 0;
88766961 1805 }
4dedbfe0 1806
88766961 1807 /* Create or update the menu bar widget. */
aa669def
RS
1808
1809 BLOCK_INPUT;
1810
18686d47 1811 if (menubar_widget)
4dedbfe0
PR
1812 {
1813 /* Disable resizing (done for Motif!) */
7556890b 1814 lw_allow_resizing (f->output_data.x->widget, False);
4dedbfe0
PR
1815
1816 /* The third arg is DEEP_P, which says to consider the entire
1817 menu trees we supply, rather than just the menu bar item names. */
88766961 1818 lw_modify_all_widgets (id, first_wv, deep_p);
4dedbfe0 1819
c98fcf4b 1820 /* Re-enable the edit widget to resize. */
7556890b 1821 lw_allow_resizing (f->output_data.x->widget, True);
4dedbfe0 1822 }
18686d47
RS
1823 else
1824 {
88766961 1825 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
7556890b 1826 f->output_data.x->column_widget,
4dedbfe0
PR
1827 0,
1828 popup_activate_callback,
1829 menubar_selection_callback,
850df50b
GM
1830 popup_deactivate_callback,
1831 menu_highlight_callback);
7556890b 1832 f->output_data.x->menubar_widget = menubar_widget;
18686d47 1833 }
1d1c1567
KH
1834
1835 {
1836 int menubar_size
7556890b
RS
1837 = (f->output_data.x->menubar_widget
1838 ? (f->output_data.x->menubar_widget->core.height
1839 + f->output_data.x->menubar_widget->core.border_width)
1d1c1567
KH
1840 : 0);
1841
f42aa681
RS
1842#if 0 /* Experimentally, we now get the right results
1843 for -geometry -0-0 without this. 24 Aug 96, rms. */
5ee80bd3 1844#ifdef USE_LUCID
1d1c1567
KH
1845 if (FRAME_EXTERNAL_MENU_BAR (f))
1846 {
1847 Dimension ibw = 0;
7556890b 1848 XtVaGetValues (f->output_data.x->column_widget,
1d1c1567
KH
1849 XtNinternalBorderWidth, &ibw, NULL);
1850 menubar_size += ibw;
1851 }
5ee80bd3 1852#endif /* USE_LUCID */
f42aa681 1853#endif /* 0 */
1d1c1567 1854
7556890b 1855 f->output_data.x->menubar_height = menubar_size;
1d1c1567 1856 }
18686d47
RS
1857
1858 free_menubar_widget_value_tree (first_wv);
4bcdbab1 1859 update_frame_menubar (f);
18686d47
RS
1860
1861 UNBLOCK_INPUT;
1862}
85f487d1 1863
8e6208c5 1864/* Called from Fx_create_frame to create the initial menubar of a frame
4dedbfe0
PR
1865 before it is mapped, so that the window is mapped with the menubar already
1866 there instead of us tacking it on later and thrashing the window after it
1867 is visible. */
1868
1869void
1870initialize_frame_menubar (f)
1871 FRAME_PTR f;
1872{
1873 /* This function is called before the first chance to redisplay
1874 the frame. It has to be, so the frame will have the right size. */
1875 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
88766961 1876 set_frame_menubar (f, 1, 1);
4dedbfe0
PR
1877}
1878
1879/* Get rid of the menu bar of frame F, and free its storage.
1880 This is used when deleting a frame, and when turning off the menu bar. */
1881
85f487d1
FP
1882void
1883free_frame_menubar (f)
1884 FRAME_PTR f;
1885{
1886 Widget menubar_widget;
85f487d1 1887
7556890b 1888 menubar_widget = f->output_data.x->menubar_widget;
a45bad2a
RS
1889
1890 f->output_data.x->menubar_height = 0;
85f487d1
FP
1891
1892 if (menubar_widget)
1893 {
1894 BLOCK_INPUT;
7556890b 1895 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
85f487d1
FP
1896 UNBLOCK_INPUT;
1897 }
1898}
78589e07 1899
78589e07
RS
1900#endif /* USE_X_TOOLKIT */
1901\f
1902/* xmenu_show actually displays a menu using the panes and items in menu_items
1903 and returns the value selected from it.
1904 There are two versions of xmenu_show, one for Xt and one for Xlib.
1905 Both assume input is blocked by the caller. */
1906
1907/* F is the frame the menu is for.
1908 X and Y are the frame-relative specified position,
1909 relative to the inside upper left corner of the frame F.
c8b5aa3d 1910 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
78589e07
RS
1911 KEYMAPS is 1 if this menu was specified with keymaps;
1912 in that case, we return a list containing the chosen item's value
1913 and perhaps also the pane's prefix.
1914 TITLE is the specified menu title.
1915 ERROR is a place to store an error message string in case of failure.
1916 (We return nil on failure, but the value doesn't actually matter.) */
18686d47
RS
1917
1918#ifdef USE_X_TOOLKIT
18686d47 1919
8ed87156 1920/* We need a unique id for each widget handled by the Lucid Widget
cc17e9bf
KH
1921 library.
1922
1923 For the main windows, and popup menus, we use this counter,
88766961 1924 which we increment each time after use. This starts from 1<<16.
cc17e9bf 1925
88766961
RS
1926 For menu bars, we use numbers starting at 0, counted in
1927 next_menubar_widget_id. */
8ed87156 1928LWLIB_ID widget_id_tick;
165e1749 1929
4dedbfe0 1930static Lisp_Object *volatile menu_item_selection;
4dedbfe0
PR
1931
1932static void
1933popup_selection_callback (widget, id, client_data)
1934 Widget widget;
1935 LWLIB_ID id;
1936 XtPointer client_data;
1937{
1938 menu_item_selection = (Lisp_Object *) client_data;
1939}
1940
78589e07 1941static Lisp_Object
673a6211 1942xmenu_show (f, x, y, for_click, keymaps, title, error)
18686d47 1943 FRAME_PTR f;
18686d47
RS
1944 int x;
1945 int y;
9685a93f 1946 int for_click;
78589e07
RS
1947 int keymaps;
1948 Lisp_Object title;
1949 char **error;
18686d47 1950{
78589e07 1951 int i;
cc17e9bf 1952 LWLIB_ID menu_id;
18686d47 1953 Widget menu;
ffcb5a51 1954 Arg av[2];
60f312e2 1955 int ac = 0;
78589e07 1956 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
101bb4a5
RS
1957 widget_value **submenu_stack
1958 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1959 Lisp_Object *subprefix_stack
1960 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1961 int submenu_depth = 0;
ffcb5a51 1962 XButtonPressedEvent dummy;
4e8d3549 1963
78c8278d
RS
1964 int first_pane;
1965
78589e07
RS
1966 *error = NULL;
1967
742f715d
KH
1968 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1969 {
1970 *error = "Empty menu";
1971 return Qnil;
1972 }
63c414df 1973
78589e07
RS
1974 /* Create a tree of widget_value objects
1975 representing the panes and their items. */
f7fab165 1976 wv = xmalloc_widget_value ();
78589e07
RS
1977 wv->name = "menu";
1978 wv->value = 0;
1979 wv->enabled = 1;
3427a3db 1980 wv->button_type = BUTTON_TYPE_NONE;
78589e07 1981 first_wv = wv;
78c8278d 1982 first_pane = 1;
78589e07
RS
1983
1984 /* Loop over all panes and items, filling in the tree. */
1985 i = 0;
1986 while (i < menu_items_used)
1987 {
101bb4a5
RS
1988 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1989 {
1990 submenu_stack[submenu_depth++] = save_wv;
1991 save_wv = prev_wv;
1992 prev_wv = 0;
78c8278d 1993 first_pane = 1;
101bb4a5
RS
1994 i++;
1995 }
1996 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1997 {
1998 prev_wv = save_wv;
1999 save_wv = submenu_stack[--submenu_depth];
78c8278d 2000 first_pane = 0;
101bb4a5
RS
2001 i++;
2002 }
2003 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2004 && submenu_depth != 0)
2005 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
2006 /* Ignore a nil in the item list.
2007 It's meaningful only for dialog boxes. */
2008 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2009 i += 1;
101bb4a5 2010 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2011 {
2012 /* Create a new pane. */
2013 Lisp_Object pane_name, prefix;
2014 char *pane_string;
2015 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2016 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
703dc2a8
KH
2017#ifndef HAVE_MULTILINGUAL_MENU
2018 if (!NILP (pane_name) && STRING_MULTIBYTE (pane_name))
2019 pane_name = string_make_unibyte (pane_name);
2020#endif
78589e07
RS
2021 pane_string = (NILP (pane_name)
2022 ? "" : (char *) XSTRING (pane_name)->data);
101bb4a5 2023 /* If there is just one top-level pane, put all its items directly
78589e07
RS
2024 under the top-level menu. */
2025 if (menu_items_n_panes == 1)
2026 pane_string = "";
2027
2028 /* If the pane has a meaningful name,
2029 make the pane a top-level menu item
2030 with its items as a submenu beneath it. */
78c8278d 2031 if (!keymaps && strcmp (pane_string, ""))
78589e07 2032 {
f7fab165 2033 wv = xmalloc_widget_value ();
78589e07
RS
2034 if (save_wv)
2035 save_wv->next = wv;
2036 else
2037 first_wv->contents = wv;
2038 wv->name = pane_string;
2039 if (keymaps && !NILP (prefix))
2040 wv->name++;
2041 wv->value = 0;
2042 wv->enabled = 1;
3427a3db 2043 wv->button_type = BUTTON_TYPE_NONE;
78c8278d
RS
2044 save_wv = wv;
2045 prev_wv = 0;
78589e07 2046 }
78c8278d
RS
2047 else if (first_pane)
2048 {
2049 save_wv = wv;
2050 prev_wv = 0;
2051 }
2052 first_pane = 0;
78589e07
RS
2053 i += MENU_ITEMS_PANE_LENGTH;
2054 }
2055 else
2056 {
2057 /* Create a new item within current pane. */
9cd50434 2058 Lisp_Object item_name, enable, descrip, def, type, selected, help;
78589e07
RS
2059 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2060 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2061 descrip
2062 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
a352a815 2063 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
3427a3db
GM
2064 type = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_TYPE];
2065 selected = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_SELECTED];
9cd50434 2066 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3427a3db 2067
703dc2a8 2068#ifndef HAVE_MULTILINGUAL_MENU
3427a3db
GM
2069 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2070 item_name = string_make_unibyte (item_name);
2071 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2072 item_name = string_make_unibyte (descrip);
703dc2a8 2073#endif
3427a3db 2074
f7fab165 2075 wv = xmalloc_widget_value ();
78589e07
RS
2076 if (prev_wv)
2077 prev_wv->next = wv;
2078 else
2079 save_wv->contents = wv;
4e8d3549 2080 wv->name = (char *) XSTRING (item_name)->data;
78589e07 2081 if (!NILP (descrip))
4e8d3549 2082 wv->key = (char *) XSTRING (descrip)->data;
78589e07 2083 wv->value = 0;
a352a815
RS
2084 /* If this item has a null value,
2085 make the call_data null so that it won't display a box
2086 when the mouse is on it. */
2087 wv->call_data
2088 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
78589e07 2089 wv->enabled = !NILP (enable);
3427a3db
GM
2090
2091 if (NILP (type))
2092 wv->button_type = BUTTON_TYPE_NONE;
2093 else if (EQ (type, QCtoggle))
2094 wv->button_type = BUTTON_TYPE_TOGGLE;
2095 else if (EQ (type, QCradio))
2096 wv->button_type = BUTTON_TYPE_RADIO;
2097 else
2098 abort ();
2099
2100 wv->selected = !NILP (selected);
9cd50434
GM
2101 if (STRINGP (help))
2102 wv->help = XSTRING (help)->data;
3427a3db 2103
78589e07
RS
2104 prev_wv = wv;
2105
2106 i += MENU_ITEMS_ITEM_LENGTH;
2107 }
2108 }
2109
c98fcf4b 2110 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
2111 if (!NILP (title))
2112 {
f7fab165
RS
2113 widget_value *wv_title = xmalloc_widget_value ();
2114 widget_value *wv_sep1 = xmalloc_widget_value ();
2115 widget_value *wv_sep2 = xmalloc_widget_value ();
4dedbfe0
PR
2116
2117 wv_sep2->name = "--";
2118 wv_sep2->next = first_wv->contents;
2119
2120 wv_sep1->name = "--";
2121 wv_sep1->next = wv_sep2;
2122
703dc2a8
KH
2123#ifndef HAVE_MULTILINGUAL_MENU
2124 if (STRING_MULTIBYTE (title))
2125 title = string_make_unibyte (title);
2126#endif
4dedbfe0
PR
2127 wv_title->name = (char *) XSTRING (title)->data;
2128 wv_title->enabled = True;
3427a3db 2129 wv_title->button_type = BUTTON_TYPE_NONE;
4dedbfe0
PR
2130 wv_title->next = wv_sep1;
2131 first_wv->contents = wv_title;
2132 }
2133
78589e07 2134 /* Actually create the menu. */
cc17e9bf 2135 menu_id = widget_id_tick++;
78589e07 2136 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
7556890b 2137 f->output_data.x->widget, 1, 0,
4dedbfe0 2138 popup_selection_callback,
850df50b
GM
2139 popup_deactivate_callback,
2140 menu_highlight_callback);
60f312e2 2141
ffcb5a51
RS
2142 /* Adjust coordinates to relative to the outer (window manager) window. */
2143 {
2144 Window child;
2145 int win_x = 0, win_y = 0;
2146
2147 /* Find the position of the outside upper-left corner of
2148 the inner window, with respect to the outer window. */
2149 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2150 {
2151 BLOCK_INPUT;
2152 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2153
2154 /* From-window, to-window. */
2155 f->output_data.x->window_desc,
2156 f->output_data.x->parent_desc,
2157
2158 /* From-position, to-position. */
2159 0, 0, &win_x, &win_y,
2160
2161 /* Child of window. */
2162 &child);
2163 UNBLOCK_INPUT;
2164 x += win_x;
2165 y += win_y;
2166 }
2167 }
2168
2169 /* Adjust coordinates to be root-window-relative. */
2170 x += f->output_data.x->left_pos;
2171 y += f->output_data.x->top_pos;
2172
2173 dummy.type = ButtonPress;
2174 dummy.serial = 0;
2175 dummy.send_event = 0;
2176 dummy.display = FRAME_X_DISPLAY (f);
2177 dummy.time = CurrentTime;
ffcb5a51
RS
2178 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2179 dummy.window = dummy.root;
2180 dummy.subwindow = dummy.root;
2181 dummy.x_root = x;
2182 dummy.y_root = y;
2183 dummy.x = x;
2184 dummy.y = y;
c8b5aa3d
RS
2185 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2186 dummy.button = 0;
2187 for (i = 0; i < 5; i++)
2188 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2189 dummy.button = i;
ffcb5a51 2190
60f312e2
RS
2191 /* Don't allow any geometry request from the user. */
2192 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2193 XtSetValues (menu, av, ac);
2194
78589e07
RS
2195 /* Free the widget_value objects we used to specify the contents. */
2196 free_menubar_widget_value_tree (first_wv);
2197
357460d0
MB
2198 /* Override any default settings with ones from the `menu' face. */
2199 x_set_menu_resources_from_menu_face (f, menu);
2200
78589e07
RS
2201 /* No selection has been chosen yet. */
2202 menu_item_selection = 0;
2203
78589e07 2204 /* Display the menu. */
a52445bd 2205 lw_popup_menu (menu, (XEvent *) &dummy);
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 2233
6bbd7a29 2234 prefix = entry = Qnil;
78589e07
RS
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 }
85996cfb
GM
2462 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2463 {
2464 /* This is the boundary between left-side elts and
2465 right-side elts. */
2466 ++i;
2467 }
165e1749
FP
2468 else
2469 {
2470 entry
2471 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2472 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2473 {
2474 if (keymaps != 0)
2475 {
2476 entry = Fcons (entry, Qnil);
2477 if (!NILP (prefix))
2478 entry = Fcons (prefix, entry);
2479 }
2480 return entry;
2481 }
2482 i += MENU_ITEMS_ITEM_LENGTH;
2483 }
2484 }
2485 }
2486
2487 return Qnil;
2488}
ba461919 2489
18686d47 2490#else /* not USE_X_TOOLKIT */
78589e07 2491
3e703b25
GM
2492/* The frame of the last activated non-toolkit menu bar.
2493 Used to generate menu help events. */
2494
2495static struct frame *menu_help_frame;
2496
2497
62145073
GM
2498/* Show help HELP_STRING, or clear help if HELP_STRING is null.
2499
2500 PANE is the pane number, and ITEM is the menu item number in
2501 the menu (currently not used).
2502
2503 This cannot be done with generating a HELP_EVENT because
2504 XMenuActivate contains a loop that doesn't let Emacs process
2505 keyboard events. */
3e703b25
GM
2506
2507static void
62145073 2508menu_help_callback (help_string, pane, item)
3e703b25 2509 char *help_string;
62145073 2510 int pane, item;
3e703b25 2511{
62145073
GM
2512 extern Lisp_Object Qmenu_item;
2513 Lisp_Object *first_item;
2514 Lisp_Object pane_name;
2515 Lisp_Object menu_object;
2516
2517 first_item = XVECTOR (menu_items)->contents;
2518 if (EQ (first_item[0], Qt))
2519 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2520 else if (EQ (first_item[0], Qquote))
2521 /* This shouldn't happen, see xmenu_show. */
2522 pane_name = build_string ("");
2523 else
2524 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2525
2526 /* (menu-item MENU-NAME PANE-NUMBER) */
2527 menu_object = Fcons (Qmenu_item,
2528 Fcons (pane_name,
2529 Fcons (make_number (pane), Qnil)));
ba461919 2530 show_help_echo (help_string ? build_string (help_string) : Qnil,
62145073 2531 Qnil, menu_object, make_number (item), 1);
3e703b25 2532}
62145073 2533
3e703b25 2534
78589e07 2535static Lisp_Object
673a6211 2536xmenu_show (f, x, y, for_click, keymaps, title, error)
78589e07
RS
2537 FRAME_PTR f;
2538 int x, y;
9685a93f
RS
2539 int for_click;
2540 int keymaps;
78589e07
RS
2541 Lisp_Object title;
2542 char **error;
dcfdbac7 2543{
78589e07
RS
2544 Window root;
2545 XMenu *menu;
2546 int pane, selidx, lpane, status;
2547 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
2548 char *datap;
2549 int ulx, uly, width, height;
2550 int dispwidth, dispheight;
4e8d3549
RS
2551 int i, j;
2552 int maxwidth;
78589e07
RS
2553 int dummy_int;
2554 unsigned int dummy_uint;
088831f6 2555
07a675b7 2556 *error = 0;
78589e07
RS
2557 if (menu_items_n_panes == 0)
2558 return Qnil;
088831f6 2559
742f715d
KH
2560 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2561 {
2562 *error = "Empty menu";
2563 return Qnil;
2564 }
2565
78589e07 2566 /* Figure out which root window F is on. */
92280f67 2567 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
2568 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2569 &dummy_uint, &dummy_uint);
18686d47 2570
78589e07 2571 /* Make the menu on that window. */
92280f67 2572 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 2573 if (menu == NULL)
dcfdbac7
JB
2574 {
2575 *error = "Can't create menu";
78589e07 2576 return Qnil;
dcfdbac7 2577 }
78589e07 2578
87485d6f 2579#ifdef HAVE_X_WINDOWS
78589e07 2580 /* Adjust coordinates to relative to the outer (window manager) window. */
78589e07
RS
2581 {
2582 Window child;
2583 int win_x = 0, win_y = 0;
2584
2585 /* Find the position of the outside upper-left corner of
2586 the inner window, with respect to the outer window. */
7556890b 2587 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
78589e07
RS
2588 {
2589 BLOCK_INPUT;
92280f67 2590 XTranslateCoordinates (FRAME_X_DISPLAY (f),
78589e07
RS
2591
2592 /* From-window, to-window. */
7556890b
RS
2593 f->output_data.x->window_desc,
2594 f->output_data.x->parent_desc,
78589e07
RS
2595
2596 /* From-position, to-position. */
2597 0, 0, &win_x, &win_y,
2598
2599 /* Child of window. */
2600 &child);
2601 UNBLOCK_INPUT;
2602 x += win_x;
2603 y += win_y;
2604 }
2605 }
87485d6f 2606#endif /* HAVE_X_WINDOWS */
78589e07
RS
2607
2608 /* Adjust coordinates to be root-window-relative. */
7556890b
RS
2609 x += f->output_data.x->left_pos;
2610 y += f->output_data.x->top_pos;
18686d47 2611
78589e07
RS
2612 /* Create all the necessary panes and their items. */
2613 i = 0;
2614 while (i < menu_items_used)
dcfdbac7 2615 {
78589e07 2616 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
dcfdbac7 2617 {
78589e07
RS
2618 /* Create a new pane. */
2619 Lisp_Object pane_name, prefix;
2620 char *pane_string;
2621
2622 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2623 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2624 pane_string = (NILP (pane_name)
2625 ? "" : (char *) XSTRING (pane_name)->data);
2626 if (keymaps && !NILP (prefix))
2627 pane_string++;
2628
92280f67 2629 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
2630 if (lpane == XM_FAILURE)
2631 {
92280f67 2632 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
78589e07
RS
2633 *error = "Can't create pane";
2634 return Qnil;
2635 }
2636 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
2637
2638 /* Find the width of the widest item in this pane. */
2639 maxwidth = 0;
2640 j = i;
2641 while (j < menu_items_used)
2642 {
2643 Lisp_Object item;
2644 item = XVECTOR (menu_items)->contents[j];
2645 if (EQ (item, Qt))
2646 break;
2647 if (NILP (item))
2648 {
2649 j++;
2650 continue;
2651 }
fc932ac6 2652 width = STRING_BYTES (XSTRING (item));
4e8d3549
RS
2653 if (width > maxwidth)
2654 maxwidth = width;
2655
2656 j += MENU_ITEMS_ITEM_LENGTH;
2657 }
dcfdbac7 2658 }
fcaa7665
RS
2659 /* Ignore a nil in the item list.
2660 It's meaningful only for dialog boxes. */
2661 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2662 i += 1;
78589e07 2663 else
dcfdbac7 2664 {
78589e07 2665 /* Create a new item within current pane. */
3e703b25 2666 Lisp_Object item_name, enable, descrip, help;
4e8d3549 2667 unsigned char *item_data;
3e703b25 2668 char *help_string;
78589e07
RS
2669
2670 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2671 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2672 descrip
2673 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3e703b25
GM
2674 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2675 help_string = STRINGP (help) ? XSTRING (help)->data : NULL;
2676
78589e07 2677 if (!NILP (descrip))
4e8d3549 2678 {
fc932ac6 2679 int gap = maxwidth - STRING_BYTES (XSTRING (item_name));
4e8d3549
RS
2680#ifdef C_ALLOCA
2681 Lisp_Object spacer;
2682 spacer = Fmake_string (make_number (gap), make_number (' '));
2683 item_name = concat2 (item_name, spacer);
2684 item_name = concat2 (item_name, descrip);
2685 item_data = XSTRING (item_name)->data;
2686#else
2687 /* if alloca is fast, use that to make the space,
2688 to reduce gc needs. */
2689 item_data
2690 = (unsigned char *) alloca (maxwidth
fc932ac6 2691 + STRING_BYTES (XSTRING (descrip)) + 1);
4e8d3549 2692 bcopy (XSTRING (item_name)->data, item_data,
fc932ac6 2693 STRING_BYTES (XSTRING (item_name)));
4e8d3549
RS
2694 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2695 item_data[j] = ' ';
2696 bcopy (XSTRING (descrip)->data, item_data + j,
fc932ac6
RS
2697 STRING_BYTES (XSTRING (descrip)));
2698 item_data[j + STRING_BYTES (XSTRING (descrip))] = 0;
4e8d3549
RS
2699#endif
2700 }
2701 else
2702 item_data = XSTRING (item_name)->data;
78589e07 2703
92280f67
RS
2704 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2705 menu, lpane, 0, item_data,
3e703b25 2706 !NILP (enable), help_string)
dcfdbac7
JB
2707 == XM_FAILURE)
2708 {
92280f67 2709 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
dcfdbac7 2710 *error = "Can't add selection to menu";
78589e07 2711 return Qnil;
dcfdbac7 2712 }
78589e07 2713 i += MENU_ITEMS_ITEM_LENGTH;
dcfdbac7
JB
2714 }
2715 }
4e8d3549 2716
78589e07 2717 /* All set and ready to fly. */
92280f67
RS
2718 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2719 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
f1847de3 2720 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
92280f67 2721 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
f1847de3 2722 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
78589e07
RS
2723 x = min (x, dispwidth);
2724 y = min (y, dispheight);
2725 x = max (x, 1);
2726 y = max (y, 1);
92280f67 2727 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
2728 &ulx, &uly, &width, &height);
2729 if (ulx+width > dispwidth)
2730 {
78589e07 2731 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
2732 ulx = dispwidth - width;
2733 }
2734 if (uly+height > dispheight)
2735 {
78589e07 2736 y -= (uly + height) - dispheight;
dcfdbac7
JB
2737 uly = dispheight - height;
2738 }
78589e07
RS
2739 if (ulx < 0) x -= ulx;
2740 if (uly < 0) y -= uly;
121e4555
KH
2741
2742 XMenuSetAEQ (menu, TRUE);
78589e07
RS
2743 XMenuSetFreeze (menu, TRUE);
2744 pane = selidx = 0;
3e703b25
GM
2745
2746 /* Help display under X won't work because XMenuActivate contains
2747 a loop that doesn't give Emacs a chance to process it. */
2748 menu_help_frame = f;
92280f67 2749 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3e703b25
GM
2750 x, y, ButtonReleaseMask, &datap,
2751 menu_help_callback);
a352a815
RS
2752
2753
f1df80a8 2754#ifdef HAVE_X_WINDOWS
a352a815
RS
2755 /* Assume the mouse has moved out of the X window.
2756 If it has actually moved in, we will get an EnterNotify. */
29e460bd 2757 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
f1df80a8 2758#endif
a352a815 2759
dcfdbac7
JB
2760 switch (status)
2761 {
2762 case XM_SUCCESS:
2763#ifdef XDEBUG
2764 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2765#endif
fa6d54d9 2766
78589e07
RS
2767 /* Find the item number SELIDX in pane number PANE. */
2768 i = 0;
2769 while (i < menu_items_used)
fa6d54d9 2770 {
78589e07 2771 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
088831f6 2772 {
78589e07
RS
2773 if (pane == 0)
2774 pane_prefix
2775 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2776 pane--;
2777 i += MENU_ITEMS_PANE_LENGTH;
088831f6 2778 }
78589e07 2779 else
ab6ee1a0 2780 {
78589e07 2781 if (pane == -1)
ab6ee1a0 2782 {
78589e07 2783 if (selidx == 0)
ab6ee1a0 2784 {
78589e07
RS
2785 entry
2786 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2787 if (keymaps != 0)
ab6ee1a0 2788 {
78589e07
RS
2789 entry = Fcons (entry, Qnil);
2790 if (!NILP (pane_prefix))
2791 entry = Fcons (pane_prefix, entry);
ab6ee1a0 2792 }
78589e07 2793 break;
ab6ee1a0 2794 }
78589e07 2795 selidx--;
ab6ee1a0 2796 }
78589e07 2797 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
2798 }
2799 }
78589e07 2800 break;
dcfdbac7 2801
78589e07 2802 case XM_FAILURE:
78589e07
RS
2803 *error = "Can't activate menu";
2804 case XM_IA_SELECT:
2805 case XM_NO_SELECT:
2806 entry = Qnil;
2807 break;
dcfdbac7 2808 }
92280f67 2809 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
a5285df3 2810
87485d6f 2811#ifdef HAVE_X_WINDOWS
a5285df3
RS
2812 /* State that no mouse buttons are now held.
2813 (The oldXMenu code doesn't track this info for us.)
2814 That is not necessarily true, but the fiction leads to reasonable
2815 results, and it is a pain to ask which are actually held now. */
e9a79fb2 2816 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
87485d6f 2817#endif
a5285df3 2818
78589e07 2819 return entry;
dcfdbac7 2820}
4dedbfe0 2821
78589e07 2822#endif /* not USE_X_TOOLKIT */
1e659e4c
RS
2823
2824#endif /* HAVE_MENUS */
088831f6 2825\f
dfcf069d 2826void
78589e07 2827syms_of_xmenu ()
dcfdbac7 2828{
78589e07
RS
2829 staticpro (&menu_items);
2830 menu_items = Qnil;
dcfdbac7 2831
0314aacb
RS
2832 Qdebug_on_next_call = intern ("debug-on-next-call");
2833 staticpro (&Qdebug_on_next_call);
2834
bfc524bc
RS
2835 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2836 "Frame for which we are updating a menu.\n\
2837The enable predicate for a menu command should check this variable.");
2838 Vmenu_updating_frame = Qnil;
2839
8ed87156 2840#ifdef USE_X_TOOLKIT
4dedbfe0 2841 widget_id_tick = (1<<16);
88766961 2842 next_menubar_widget_id = 1;
8ed87156
RS
2843#endif
2844
78589e07 2845 defsubr (&Sx_popup_menu);
1e659e4c 2846#ifdef HAVE_MENUS
165e1749 2847 defsubr (&Sx_popup_dialog);
1e659e4c 2848#endif
dcfdbac7 2849}