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