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