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