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