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