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