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