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