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