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