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