(Fwrite_region): Fix previous change.
[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\
a7ebd443
RS
766If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
767Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
768\n\
78589e07
RS
769You can also use a list of keymaps as MENU.\n\
770 Then each keymap makes a separate pane.\n\
771When MENU is a keymap or a list of keymaps, the return value\n\
772is a list of events.\n\n\
a7ebd443 773\n\
78589e07
RS
774Alternatively, you can specify a menu of multiple panes\n\
775 with a list of the form (TITLE PANE1 PANE2...),\n\
776where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
777Each ITEM is normally a cons cell (STRING . VALUE);\n\
778but a string can appear as an item--that makes a nonselectable line\n\
779in the menu.\n\
780With this form of menu, the return value is VALUE from the chosen item.\n\
781\n\
782If POSITION is nil, don't display the menu at all, just precalculate the\n\
783cached information about equivalent key sequences.")
784 (position, menu)
785 Lisp_Object position, menu;
786{
787 int number_of_panes, panes;
18686d47 788 Lisp_Object keymap, tem;
78589e07
RS
789 int xpos, ypos;
790 Lisp_Object title;
791 char *error_name;
792 Lisp_Object selection;
18686d47 793 int i, j;
78589e07
RS
794 FRAME_PTR f;
795 Lisp_Object x, y, window;
796 int keymaps = 0;
9685a93f 797 int for_click = 0;
78589e07
RS
798 struct gcpro gcpro1;
799
1e659e4c 800#ifdef HAVE_MENUS
78589e07
RS
801 if (! NILP (position))
802 {
101bb4a5
RS
803 check_x ();
804
78589e07 805 /* Decode the first argument: find the window and the coordinates. */
9572375b
KH
806 if (EQ (position, Qt)
807 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
78589e07
RS
808 {
809 /* Use the mouse's current position. */
ac8f8f7d 810 FRAME_PTR new_f = selected_frame;
78589e07
RS
811 Lisp_Object bar_window;
812 int part;
813 unsigned long time;
814
b137c582 815 if (mouse_position_hook)
46b657e2
RS
816 (*mouse_position_hook) (&new_f, 1, &bar_window,
817 &part, &x, &y, &time);
5ca2ef64 818 if (new_f != 0)
a39f0477 819 XSETFRAME (window, new_f);
5ca2ef64
RS
820 else
821 {
822 window = selected_window;
33b43fa6
KH
823 XSETFASTINT (x, 0);
824 XSETFASTINT (y, 0);
5ca2ef64 825 }
78589e07
RS
826 }
827 else
828 {
829 tem = Fcar (position);
b5bb2705 830 if (CONSP (tem))
78589e07
RS
831 {
832 window = Fcar (Fcdr (position));
833 x = Fcar (tem);
834 y = Fcar (Fcdr (tem));
835 }
836 else
837 {
9685a93f 838 for_click = 1;
78589e07
RS
839 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
840 window = Fcar (tem); /* POSN_WINDOW (tem) */
841 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
842 x = Fcar (tem);
843 y = Fcdr (tem);
78589e07
RS
844 }
845 }
846
847 CHECK_NUMBER (x, 0);
848 CHECK_NUMBER (y, 0);
849
850 /* Decode where to put the menu. */
851
b5bb2705 852 if (FRAMEP (window))
78589e07
RS
853 {
854 f = XFRAME (window);
78589e07
RS
855 xpos = 0;
856 ypos = 0;
857 }
b5bb2705 858 else if (WINDOWP (window))
78589e07
RS
859 {
860 CHECK_LIVE_WINDOW (window, 0);
861 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
862
99d3fac7
KH
863 xpos = (FONT_WIDTH (f->output_data.x->font)
864 * XFASTINT (XWINDOW (window)->left));
865 ypos = (f->output_data.x->line_height
866 * XFASTINT (XWINDOW (window)->top));
78589e07
RS
867 }
868 else
869 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
870 but I don't want to make one now. */
871 CHECK_WINDOW (window, 0);
872
873 xpos += XINT (x);
874 ypos += XINT (y);
bfc524bc
RS
875
876 XSETFRAME (Vmenu_updating_frame, f);
78589e07 877 }
bfc524bc 878 Vmenu_updating_frame = Qnil;
1e659e4c 879#endif /* HAVE_MENUS */
78589e07
RS
880
881 title = Qnil;
882 GCPRO1 (title);
883
884 /* Decode the menu items from what was specified. */
18686d47
RS
885
886 keymap = Fkeymapp (menu);
887 tem = Qnil;
b5bb2705 888 if (CONSP (menu))
18686d47
RS
889 tem = Fkeymapp (Fcar (menu));
890 if (!NILP (keymap))
891 {
892 /* We were given a keymap. Extract menu info from the keymap. */
893 Lisp_Object prompt;
894 keymap = get_keymap (menu);
895
78589e07 896 /* Extract the detailed info to make one pane. */
101bb4a5 897 keymap_panes (&menu, 1, NILP (position));
78589e07 898
18686d47
RS
899 /* Search for a string appearing directly as an element of the keymap.
900 That string is the title of the menu. */
901 prompt = map_prompt (keymap);
4f9ad016
RS
902 if (NILP (title) && !NILP (prompt))
903 title = prompt;
18686d47 904
78589e07
RS
905 /* Make that be the pane title of the first pane. */
906 if (!NILP (prompt) && menu_items_n_panes >= 0)
907 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
908
909 keymaps = 1;
18686d47
RS
910 }
911 else if (!NILP (tem))
912 {
913 /* We were given a list of keymaps. */
18686d47
RS
914 int nmaps = XFASTINT (Flength (menu));
915 Lisp_Object *maps
916 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
917 int i;
78589e07
RS
918
919 title = Qnil;
18686d47
RS
920
921 /* The first keymap that has a prompt string
922 supplies the menu title. */
b5bb2705 923 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
18686d47 924 {
78589e07
RS
925 Lisp_Object prompt;
926
18686d47
RS
927 maps[i++] = keymap = get_keymap (Fcar (tem));
928
929 prompt = map_prompt (keymap);
78589e07
RS
930 if (NILP (title) && !NILP (prompt))
931 title = prompt;
18686d47
RS
932 }
933
934 /* Extract the detailed info to make one pane. */
101bb4a5 935 keymap_panes (maps, nmaps, NILP (position));
78589e07
RS
936
937 /* Make the title be the pane title of the first pane. */
938 if (!NILP (title) && menu_items_n_panes >= 0)
939 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
940
941 keymaps = 1;
18686d47
RS
942 }
943 else
944 {
945 /* We were given an old-fashioned menu. */
78589e07
RS
946 title = Fcar (menu);
947 CHECK_STRING (title, 1);
18686d47 948
78589e07 949 list_of_panes (Fcdr (menu));
18686d47 950
78589e07
RS
951 keymaps = 0;
952 }
18686d47 953
78589e07 954 if (NILP (position))
18686d47 955 {
78589e07
RS
956 discard_menu_items ();
957 UNGCPRO;
958 return Qnil;
18686d47
RS
959 }
960
1e659e4c 961#ifdef HAVE_MENUS
78589e07
RS
962 /* Display them in a menu. */
963 BLOCK_INPUT;
18686d47 964
673a6211 965 selection = xmenu_show (f, xpos, ypos, for_click,
78589e07
RS
966 keymaps, title, &error_name);
967 UNBLOCK_INPUT;
18686d47 968
78589e07 969 discard_menu_items ();
18686d47 970
78589e07 971 UNGCPRO;
1e659e4c 972#endif /* HAVE_MENUS */
18686d47 973
78589e07
RS
974 if (error_name) error (error_name);
975 return selection;
18686d47 976}
165e1749 977
1e659e4c
RS
978#ifdef HAVE_MENUS
979
540e52d1 980DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
165e1749 981 "Pop up a dialog box and return user's selection.\n\
99fe880d
RS
982POSITION specifies which frame to use.\n\
983This is normally a mouse button event or a window or frame.\n\
984If POSITION is t, it means to use the frame the mouse is on.\n\
985The dialog box appears in the middle of the specified frame.\n\
165e1749 986\n\
99fe880d
RS
987CONTENTS specifies the alternatives to display in the dialog box.\n\
988It is a list of the form (TITLE ITEM1 ITEM2...).\n\
989Each ITEM is a cons cell (STRING . VALUE).\n\
fcaa7665
RS
990The return value is VALUE from the chosen item.\n\n\
991An ITEM may also be just a string--that makes a nonselectable item.\n\
992An ITEM may also be nil--that means to put all preceding items\n\
993on the left of the dialog box and all following items on the right.\n\
994\(By default, approximately half appear on each side.)")
99fe880d
RS
995 (position, contents)
996 Lisp_Object position, contents;
165e1749 997{
165e1749 998 FRAME_PTR f;
99fe880d 999 Lisp_Object window;
165e1749
FP
1000
1001 check_x ();
1002
99fe880d 1003 /* Decode the first argument: find the window or frame to use. */
88d4f6ec
KH
1004 if (EQ (position, Qt)
1005 || (CONSP (position) && EQ (XCONS (position)->car, Qmenu_bar)))
165e1749 1006 {
b14db4d7 1007#if 0 /* Using the frame the mouse is on may not be right. */
99fe880d 1008 /* Use the mouse's current position. */
ac8f8f7d 1009 FRAME_PTR new_f = selected_frame;
99fe880d
RS
1010 Lisp_Object bar_window;
1011 int part;
1012 unsigned long time;
1013 Lisp_Object x, y;
165e1749 1014
46b657e2 1015 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
5ca2ef64 1016
99fe880d 1017 if (new_f != 0)
a39f0477 1018 XSETFRAME (window, new_f);
99fe880d
RS
1019 else
1020 window = selected_window;
b14db4d7 1021#endif
88d4f6ec 1022 window = selected_window;
99fe880d
RS
1023 }
1024 else if (CONSP (position))
1025 {
1026 Lisp_Object tem;
1027 tem = Fcar (position);
b5bb2705 1028 if (CONSP (tem))
99fe880d 1029 window = Fcar (Fcdr (position));
80670155
RS
1030 else
1031 {
99fe880d
RS
1032 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1033 window = Fcar (tem); /* POSN_WINDOW (tem) */
165e1749 1034 }
165e1749 1035 }
99fe880d
RS
1036 else if (WINDOWP (position) || FRAMEP (position))
1037 window = position;
a9be6839
RS
1038 else
1039 window = Qnil;
165e1749 1040
99fe880d 1041 /* Decode where to put the menu. */
165e1749 1042
b5bb2705 1043 if (FRAMEP (window))
99fe880d 1044 f = XFRAME (window);
b5bb2705 1045 else if (WINDOWP (window))
165e1749 1046 {
99fe880d
RS
1047 CHECK_LIVE_WINDOW (window, 0);
1048 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
165e1749 1049 }
99fe880d
RS
1050 else
1051 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1052 but I don't want to make one now. */
1053 CHECK_WINDOW (window, 0);
165e1749 1054
99fe880d
RS
1055#ifndef USE_X_TOOLKIT
1056 /* Display a menu with these alternatives
1057 in the middle of frame F. */
1058 {
1059 Lisp_Object x, y, frame, newpos;
a39f0477
KH
1060 XSETFRAME (frame, f);
1061 XSETINT (x, x_pixel_width (f) / 2);
1062 XSETINT (y, x_pixel_height (f) / 2);
99fe880d
RS
1063 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1064
1065 return Fx_popup_menu (newpos,
1066 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1067 }
1068#else
1069 {
1070 Lisp_Object title;
1071 char *error_name;
1072 Lisp_Object selection;
165e1749 1073
99fe880d
RS
1074 /* Decode the dialog items from what was specified. */
1075 title = Fcar (contents);
1076 CHECK_STRING (title, 1);
165e1749 1077
99fe880d 1078 list_of_panes (Fcons (contents, Qnil));
165e1749 1079
99fe880d
RS
1080 /* Display them in a dialog box. */
1081 BLOCK_INPUT;
f8c2630d 1082 selection = xdialog_show (f, 0, title, &error_name);
99fe880d 1083 UNBLOCK_INPUT;
165e1749 1084
99fe880d
RS
1085 discard_menu_items ();
1086
1087 if (error_name) error (error_name);
1088 return selection;
1089 }
7464b131 1090#endif
392d3f4b 1091}
78589e07
RS
1092\f
1093#ifdef USE_X_TOOLKIT
18686d47 1094
4dedbfe0 1095/* Loop in Xt until the menu pulldown or dialog popup has been
f56e7ad2
RS
1096 popped down (deactivated). This is used for x-popup-menu
1097 and x-popup-dialog; it is not used for the menu bar any more.
c98fcf4b 1098
2e2b8e22 1099 NOTE: All calls to popup_get_selection should be protected
c98fcf4b 1100 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
aa669def 1101
4dedbfe0 1102void
2e2b8e22 1103popup_get_selection (initial_event, dpyinfo, id)
4dedbfe0 1104 XEvent *initial_event;
aa669def 1105 struct x_display_info *dpyinfo;
2e2b8e22 1106 LWLIB_ID id;
78589e07 1107{
4dedbfe0 1108 XEvent event;
78589e07 1109
aa669def
RS
1110 /* Define a queue to save up for later unreading
1111 all X events that don't pertain to the menu. */
1112 struct event_queue
1113 {
1114 XEvent event;
1115 struct event_queue *next;
1116 };
1117
1118 struct event_queue *queue = NULL;
1119 struct event_queue *queue_tmp;
1120
4dedbfe0
PR
1121 if (initial_event)
1122 event = *initial_event;
1123 else
1124 XtAppNextEvent (Xt_app_con, &event);
78589e07 1125
4dedbfe0 1126 while (1)
78589e07 1127 {
aa669def
RS
1128 /* Handle expose events for editor frames right away. */
1129 if (event.type == Expose)
1130 process_expose_from_menu (event);
10624005
KH
1131 /* Make sure we don't consider buttons grabbed after menu goes.
1132 And make sure to deactivate for any ButtonRelease,
1133 even if XtDispatchEvent doesn't do that. */
aa669def
RS
1134 else if (event.type == ButtonRelease
1135 && dpyinfo->display == event.xbutton.display)
10624005
KH
1136 {
1137 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1138 popup_activated_flag = 0;
c8b5aa3d
RS
1139#ifdef USE_MOTIF /* Pretending that the event came from a
1140 Btn1Down seems the only way to convince Motif to
1141 activate its callbacks; setting the XmNmenuPost
1142 isn't working. --marcus@sysc.pdx.edu. */
1143 event.xbutton.button = 1;
1144#endif
10624005 1145 }
2e2b8e22
KH
1146 /* If the user presses a key, deactivate the menu.
1147 The user is likely to do that if we get wedged. */
1148 else if (event.type == KeyPress
1149 && dpyinfo->display == event.xbutton.display)
1150 {
2e2b8e22
KH
1151 popup_activated_flag = 0;
1152 break;
1153 }
d31d42cc
RS
1154 /* Button presses outside the menu also pop it down. */
1155 else if (event.type == ButtonPress
1156 && event.xany.display == dpyinfo->display
1157 && x_any_window_to_frame (dpyinfo, event.xany.window))
1158 {
1159 popup_activated_flag = 0;
1160 break;
1161 }
aa669def
RS
1162
1163 /* Queue all events not for this popup,
10624005 1164 except for Expose, which we've already handled, and ButtonRelease.
9572375b
KH
1165 Note that the X window is associated with the frame if this
1166 is a menu bar popup, but not if it's a dialog box. So we use
1167 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
aa669def 1168 if (event.type != Expose
10624005
KH
1169 && !(event.type == ButtonRelease
1170 && dpyinfo->display == event.xbutton.display)
aa669def 1171 && (event.xany.display != dpyinfo->display
9572375b 1172 || x_non_menubar_window_to_frame (dpyinfo, event.xany.window)))
aa669def 1173 {
aa669def
RS
1174 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1175
1176 if (queue_tmp != NULL)
1177 {
1178 queue_tmp->event = event;
1179 queue_tmp->next = queue;
1180 queue = queue_tmp;
1181 }
1182 }
9572375b
KH
1183 else
1184 XtDispatchEvent (&event);
aa669def
RS
1185
1186 if (!popup_activated ())
4dedbfe0
PR
1187 break;
1188 XtAppNextEvent (Xt_app_con, &event);
78589e07 1189 }
aa669def
RS
1190
1191 /* Unread any events that we got but did not handle. */
1192 while (queue != NULL)
1193 {
1194 queue_tmp = queue;
1195 XPutBackEvent (queue_tmp->event.xany.display, &queue_tmp->event);
1196 queue = queue_tmp->next;
1197 free ((char *)queue_tmp);
1198 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1199 interrupt_input_pending = 1;
1200 }
78589e07
RS
1201}
1202
88766961
RS
1203/* Activate the menu bar of frame F.
1204 This is called from keyboard.c when it gets the
1205 menu_bar_activate_event out of the Emacs event queue.
1206
1207 To activate the menu bar, we use the X button-press event
ac78b144 1208 that was saved in saved_menu_event.
88766961
RS
1209 That makes the toolkit do its thing.
1210
1211 But first we recompute the menu bar contents (the whole tree).
1212
1213 The reason for saving the button event until here, instead of
1214 passing it to the toolkit right away, is that we can safely
1215 execute Lisp code. */
1216
1217x_activate_menubar (f)
1218 FRAME_PTR f;
1219{
ac78b144 1220 if (!f->output_data.x->saved_menu_event->type)
88766961
RS
1221 return;
1222
a9be6839 1223 set_frame_menubar (f, 0, 1);
88766961 1224 BLOCK_INPUT;
ac78b144 1225 XtDispatchEvent ((XEvent *) f->output_data.x->saved_menu_event);
88766961 1226 UNBLOCK_INPUT;
a9be6839 1227#ifdef USE_MOTIF
745c34fb
RS
1228 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1229 pending_menu_activation = 1;
a9be6839 1230#endif
745c34fb 1231
88766961 1232 /* Ignore this if we get it a second time. */
ac78b144 1233 f->output_data.x->saved_menu_event->type = 0;
88766961
RS
1234}
1235
c98fcf4b 1236/* Detect if a dialog or menu has been posted. */
aa669def 1237
4dedbfe0
PR
1238int
1239popup_activated ()
1240{
1241 return popup_activated_flag;
1242}
1243
1244
1245/* This callback is invoked when the user selects a menubar cascade
1246 pushbutton, but before the pulldown menu is posted. */
78589e07
RS
1247
1248static void
4dedbfe0 1249popup_activate_callback (widget, id, client_data)
78589e07
RS
1250 Widget widget;
1251 LWLIB_ID id;
1252 XtPointer client_data;
1253{
4dedbfe0 1254 popup_activated_flag = 1;
78589e07
RS
1255}
1256
4dedbfe0
PR
1257/* This callback is called from the menu bar pulldown menu
1258 when the user makes a selection.
1259 Figure out what the user chose
1260 and put the appropriate events into the keyboard buffer. */
1261
78589e07 1262static void
4dedbfe0 1263menubar_selection_callback (widget, id, client_data)
78589e07
RS
1264 Widget widget;
1265 LWLIB_ID id;
1266 XtPointer client_data;
1267{
c63f6952 1268 Lisp_Object prefix, entry;
88766961 1269 FRAME_PTR f = menubar_id_to_frame (id);
4dedbfe0
PR
1270 Lisp_Object vector;
1271 Lisp_Object *subprefix_stack;
1272 int submenu_depth = 0;
1273 int i;
1274
1275 if (!f)
1276 return;
1277 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1278 vector = f->menu_bar_vector;
1279 prefix = Qnil;
1280 i = 0;
1281 while (i < f->menu_bar_items_used)
1282 {
4dedbfe0
PR
1283 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1284 {
1285 subprefix_stack[submenu_depth++] = prefix;
1286 prefix = entry;
1287 i++;
1288 }
1289 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1290 {
1291 prefix = subprefix_stack[--submenu_depth];
1292 i++;
1293 }
1294 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1295 {
4cb35c39 1296 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
4dedbfe0
PR
1297 i += MENU_ITEMS_PANE_LENGTH;
1298 }
1299 else
1300 {
4cb35c39 1301 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
01d5e892
RS
1302 /* The EMACS_INT cast avoids a warning. There's no problem
1303 as long as pointers have enough bits to hold small integers. */
1304 if ((int) (EMACS_INT) client_data == i)
4dedbfe0
PR
1305 {
1306 int j;
1307 struct input_event buf;
4cb35c39 1308 Lisp_Object frame;
4dedbfe0 1309
4cb35c39 1310 XSETFRAME (frame, f);
4dedbfe0 1311 buf.kind = menu_bar_event;
9572375b 1312 buf.frame_or_window = Fcons (frame, Fcons (Qmenu_bar, Qnil));
4dedbfe0
PR
1313 kbd_buffer_store_event (&buf);
1314
1315 for (j = 0; j < submenu_depth; j++)
1316 if (!NILP (subprefix_stack[j]))
1317 {
1318 buf.kind = menu_bar_event;
4cb35c39 1319 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
4dedbfe0
PR
1320 kbd_buffer_store_event (&buf);
1321 }
1322
1323 if (!NILP (prefix))
1324 {
1325 buf.kind = menu_bar_event;
4cb35c39 1326 buf.frame_or_window = Fcons (frame, prefix);
4dedbfe0
PR
1327 kbd_buffer_store_event (&buf);
1328 }
1329
1330 buf.kind = menu_bar_event;
4cb35c39 1331 buf.frame_or_window = Fcons (frame, entry);
4dedbfe0
PR
1332 kbd_buffer_store_event (&buf);
1333
1334 return;
1335 }
1336 i += MENU_ITEMS_ITEM_LENGTH;
1337 }
1338 }
18686d47
RS
1339}
1340
4dedbfe0 1341/* This callback is invoked when a dialog or menu is finished being
c98fcf4b 1342 used and has been unposted. */
4dedbfe0 1343
165e1749 1344static void
4dedbfe0 1345popup_deactivate_callback (widget, id, client_data)
165e1749
FP
1346 Widget widget;
1347 LWLIB_ID id;
1348 XtPointer client_data;
1349{
4dedbfe0 1350 popup_activated_flag = 0;
165e1749
FP
1351}
1352
f9655c60
RS
1353/* Allocate a widget_value, blocking input. */
1354
1355widget_value *
1356xmalloc_widget_value ()
1357{
1358 widget_value *value;
1359
1360 BLOCK_INPUT;
1361 value = malloc_widget_value ();
1362 UNBLOCK_INPUT;
1363
1364 return value;
1365}
4dedbfe0
PR
1366
1367/* This recursively calls free_widget_value on the tree of widgets.
18686d47 1368 It must free all data that was malloc'ed for these widget_values.
78589e07
RS
1369 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1370 must be left alone. */
1371
18686d47
RS
1372void
1373free_menubar_widget_value_tree (wv)
1374 widget_value *wv;
1375{
1376 if (! wv) return;
18686d47
RS
1377
1378 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1379
1380 if (wv->contents && (wv->contents != (widget_value*)1))
1381 {
1382 free_menubar_widget_value_tree (wv->contents);
1383 wv->contents = (widget_value *) 0xDEADBEEF;
1384 }
1385 if (wv->next)
1386 {
1387 free_menubar_widget_value_tree (wv->next);
1388 wv->next = (widget_value *) 0xDEADBEEF;
1389 }
1390 BLOCK_INPUT;
1391 free_widget_value (wv);
1392 UNBLOCK_INPUT;
1393}
4dedbfe0
PR
1394\f
1395/* Return a tree of widget_value structures for a menu bar item
1396 whose event type is ITEM_KEY (with string ITEM_NAME)
1397 and whose contents come from the list of keymaps MAPS. */
1398
1399static widget_value *
1400single_submenu (item_key, item_name, maps)
1401 Lisp_Object item_key, item_name, maps;
1402{
1403 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1404 int i;
1405 int submenu_depth = 0;
1406 Lisp_Object length;
1407 int len;
1408 Lisp_Object *mapvec;
1409 widget_value **submenu_stack;
1410 int mapno;
1411 int previous_items = menu_items_used;
71dca3e3 1412 int top_level_items = 0;
4dedbfe0
PR
1413
1414 length = Flength (maps);
1415 len = XINT (length);
1416
1417 /* Convert the list MAPS into a vector MAPVEC. */
1418 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1419 for (i = 0; i < len; i++)
1420 {
1421 mapvec[i] = Fcar (maps);
1422 maps = Fcdr (maps);
1423 }
1424
1425 menu_items_n_panes = 0;
1426
1427 /* Loop over the given keymaps, making a pane for each map.
1428 But don't make a pane that is empty--ignore that map instead. */
1429 for (i = 0; i < len; i++)
71dca3e3 1430 {
846e8c10
RS
1431 if (SYMBOLP (mapvec[i])
1432 || (CONSP (mapvec[i])
1433 && NILP (Fkeymapp (mapvec[i]))))
71dca3e3 1434 {
846e8c10
RS
1435 /* Here we have a command at top level in the menu bar
1436 as opposed to a submenu. */
71dca3e3
RS
1437 top_level_items = 1;
1438 push_menu_pane (Qnil, Qnil);
1439 push_menu_item (item_name, Qt, item_key, mapvec[i], Qnil);
1440 }
1441 else
98381e42 1442 single_keymap_panes (mapvec[i], item_name, item_key, 0, 10);
71dca3e3 1443 }
4dedbfe0
PR
1444
1445 /* Create a tree of widget_value objects
1446 representing the panes and their items. */
1447
1448 submenu_stack
1449 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
f7fab165 1450 wv = xmalloc_widget_value ();
4dedbfe0
PR
1451 wv->name = "menu";
1452 wv->value = 0;
1453 wv->enabled = 1;
1454 first_wv = wv;
1455 save_wv = 0;
71dca3e3 1456 prev_wv = 0;
4dedbfe0
PR
1457
1458 /* Loop over all panes and items made during this call
1459 and construct a tree of widget_value objects.
1460 Ignore the panes and items made by previous calls to
1461 single_submenu, even though those are also in menu_items. */
1462 i = previous_items;
1463 while (i < menu_items_used)
1464 {
1465 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1466 {
1467 submenu_stack[submenu_depth++] = save_wv;
1468 save_wv = prev_wv;
1469 prev_wv = 0;
1470 i++;
1471 }
1472 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1473 {
1474 prev_wv = save_wv;
1475 save_wv = submenu_stack[--submenu_depth];
1476 i++;
1477 }
1478 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1479 && submenu_depth != 0)
1480 i += MENU_ITEMS_PANE_LENGTH;
1481 /* Ignore a nil in the item list.
1482 It's meaningful only for dialog boxes. */
1483 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1484 i += 1;
1485 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1486 {
1487 /* Create a new pane. */
1488 Lisp_Object pane_name, prefix;
1489 char *pane_string;
1490 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1491 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1492 pane_string = (NILP (pane_name)
1493 ? "" : (char *) XSTRING (pane_name)->data);
1494 /* If there is just one top-level pane, put all its items directly
1495 under the top-level menu. */
1496 if (menu_items_n_panes == 1)
1497 pane_string = "";
1498
1499 /* If the pane has a meaningful name,
1500 make the pane a top-level menu item
1501 with its items as a submenu beneath it. */
1502 if (strcmp (pane_string, ""))
1503 {
f7fab165 1504 wv = xmalloc_widget_value ();
4dedbfe0
PR
1505 if (save_wv)
1506 save_wv->next = wv;
1507 else
1508 first_wv->contents = wv;
1509 wv->name = pane_string;
ffcb5a51
RS
1510 /* Ignore the @ that means "separate pane".
1511 This is a kludge, but this isn't worth more time. */
1512 if (!NILP (prefix) && wv->name[0] == '@')
4dedbfe0
PR
1513 wv->name++;
1514 wv->value = 0;
1515 wv->enabled = 1;
1516 }
1517 save_wv = wv;
1518 prev_wv = 0;
1519 i += MENU_ITEMS_PANE_LENGTH;
1520 }
1521 else
1522 {
1523 /* Create a new item within current pane. */
a352a815 1524 Lisp_Object item_name, enable, descrip, def;
4dedbfe0
PR
1525 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1526 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1527 descrip
1528 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
a352a815 1529 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
18686d47 1530
f7fab165 1531 wv = xmalloc_widget_value ();
4dedbfe0
PR
1532 if (prev_wv)
1533 prev_wv->next = wv;
71dca3e3 1534 else
4dedbfe0 1535 save_wv->contents = wv;
71dca3e3 1536
4dedbfe0
PR
1537 wv->name = (char *) XSTRING (item_name)->data;
1538 if (!NILP (descrip))
1539 wv->key = (char *) XSTRING (descrip)->data;
1540 wv->value = 0;
01d5e892
RS
1541 /* The EMACS_INT cast avoids a warning. There's no problem
1542 as long as pointers have enough bits to hold small integers. */
1543 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
4dedbfe0
PR
1544 wv->enabled = !NILP (enable);
1545 prev_wv = wv;
1546
1547 i += MENU_ITEMS_ITEM_LENGTH;
1548 }
1549 }
1550
71dca3e3
RS
1551 /* If we have just one "menu item"
1552 that was originally a button, return it by itself. */
1553 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1554 {
1555 wv = first_wv->contents;
1556 free_widget_value (first_wv);
1557 return wv;
1558 }
1559
4dedbfe0
PR
1560 return first_wv;
1561}
1562\f
cffa74ea
FP
1563extern void EmacsFrameSetCharSize ();
1564
4bcdbab1
KH
1565/* Recompute all the widgets of frame F, when the menu bar
1566 has been changed. */
4dedbfe0 1567
18686d47 1568static void
6af6cbb5 1569update_frame_menubar (f)
18686d47
RS
1570 FRAME_PTR f;
1571{
7556890b 1572 struct x_output *x = f->output_data.x;
cffa74ea 1573 int columns, rows;
18686d47
RS
1574 int menubar_changed;
1575
4dedbfe0
PR
1576 Dimension shell_height;
1577
1578 /* We assume the menubar contents has changed if the global flag is set,
1579 or if the current buffer has changed, or if the menubar has never
1580 been updated before.
1581 */
18686d47
RS
1582 menubar_changed = (x->menubar_widget
1583 && !XtIsManaged (x->menubar_widget));
1584
1585 if (! (menubar_changed))
1586 return;
1587
1588 BLOCK_INPUT;
cffa74ea
FP
1589 /* Save the size of the frame because the pane widget doesn't accept to
1590 resize itself. So force it. */
1591 columns = f->width;
1592 rows = f->height;
1593
4dedbfe0
PR
1594 /* Do the voodoo which means "I'm changing lots of things, don't try to
1595 refigure sizes until I'm done." */
1596 lw_refigure_widget (x->column_widget, False);
cffa74ea 1597
18686d47
RS
1598 /* the order in which children are managed is the top to
1599 bottom order in which they are displayed in the paned window.
1600 First, remove the text-area widget.
1601 */
1602 XtUnmanageChild (x->edit_widget);
1603
1604 /* remove the menubar that is there now, and put up the menubar that
1605 should be there.
1606 */
1607 if (menubar_changed)
1608 {
1609 XtManageChild (x->menubar_widget);
1610 XtMapWidget (x->menubar_widget);
1611 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1612 }
1613
c98fcf4b 1614 /* Re-manage the text-area widget, and then thrash the sizes. */
18686d47 1615 XtManageChild (x->edit_widget);
4dedbfe0 1616 lw_refigure_widget (x->column_widget, True);
cffa74ea
FP
1617
1618 /* Force the pane widget to resize itself with the right values. */
1619 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1620
18686d47
RS
1621 UNBLOCK_INPUT;
1622}
1623
4bcdbab1
KH
1624/* Set the contents of the menubar widgets of frame F.
1625 The argument FIRST_TIME is currently ignored;
1626 it is set the first time this is called, from initialize_frame_menubar. */
1627
18686d47 1628void
88766961 1629set_frame_menubar (f, first_time, deep_p)
18686d47 1630 FRAME_PTR f;
706aa2f2 1631 int first_time;
88766961 1632 int deep_p;
18686d47 1633{
7556890b 1634 Widget menubar_widget = f->output_data.x->menubar_widget;
bd3a4da2 1635 Lisp_Object tail, items, frame;
4d19cb8e 1636 widget_value *wv, *first_wv, *prev_wv = 0;
5b3557df 1637 int i;
88766961 1638 LWLIB_ID id;
18686d47 1639
bfc524bc
RS
1640 XSETFRAME (Vmenu_updating_frame, f);
1641
7556890b
RS
1642 if (f->output_data.x->id == 0)
1643 f->output_data.x->id = next_menubar_widget_id++;
1644 id = f->output_data.x->id;
37a98547 1645
88766961
RS
1646 if (! menubar_widget)
1647 deep_p = 1;
745c34fb 1648 else if (pending_menu_activation && !deep_p)
a9be6839
RS
1649 deep_p = 1;
1650 /* Make the first call for any given frame always go deep. */
1651 else if (!f->output_data.x->saved_menu_event && !deep_p)
745c34fb
RS
1652 {
1653 deep_p = 1;
a9be6839
RS
1654 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1655 f->output_data.x->saved_menu_event->type = 0;
745c34fb 1656 }
18686d47 1657
f7fab165 1658 wv = xmalloc_widget_value ();
18686d47
RS
1659 wv->name = "menubar";
1660 wv->value = 0;
1661 wv->enabled = 1;
4d19cb8e 1662 first_wv = wv;
8d8a3494 1663
88766961 1664 if (deep_p)
18686d47 1665 {
88766961
RS
1666 /* Make a widget-value tree representing the entire menu trees. */
1667
1668 struct buffer *prev = current_buffer;
1669 Lisp_Object buffer;
1670 int specpdl_count = specpdl_ptr - specpdl;
1671 int previous_menu_items_used = f->menu_bar_items_used;
1672 Lisp_Object *previous_items
1673 = (Lisp_Object *) alloca (previous_menu_items_used
1674 * sizeof (Lisp_Object));
1675
0b1cf399
RS
1676 /* If we are making a new widget, its contents are empty,
1677 do always reinitialize them. */
1678 if (! menubar_widget)
1679 previous_menu_items_used = 0;
1680
88766961
RS
1681 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
1682 specbind (Qinhibit_quit, Qt);
1683 /* Don't let the debugger step into this code
1684 because it is not reentrant. */
1685 specbind (Qdebug_on_next_call, Qnil);
1686
7935c738 1687 record_unwind_protect (Fstore_match_data, Fmatch_data (Qnil, Qnil));
88766961
RS
1688 if (NILP (Voverriding_local_map_menu_flag))
1689 {
1690 specbind (Qoverriding_terminal_local_map, Qnil);
1691 specbind (Qoverriding_local_map, Qnil);
1692 }
18686d47 1693
88766961 1694 set_buffer_internal_1 (XBUFFER (buffer));
18686d47 1695
88766961
RS
1696 /* Run the Lucid hook. */
1697 call1 (Vrun_hooks, Qactivate_menubar_hook);
1698 /* If it has changed current-menubar from previous value,
1699 really recompute the menubar from the value. */
1700 if (! NILP (Vlucid_menu_bar_dirty_flag))
1701 call0 (Qrecompute_lucid_menubar);
a57634d4 1702 safe_run_hooks (Qmenu_bar_update_hook);
88766961 1703 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
18686d47 1704
88766961 1705 items = FRAME_MENU_BAR_ITEMS (f);
8d8a3494 1706
88766961 1707 inhibit_garbage_collection ();
8d8a3494 1708
88766961
RS
1709 /* Save the frame's previous menu bar contents data. */
1710 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1711 previous_menu_items_used * sizeof (Lisp_Object));
8d8a3494 1712
88766961
RS
1713 /* Fill in the current menu bar contents. */
1714 menu_items = f->menu_bar_vector;
1715 menu_items_allocated = XVECTOR (menu_items)->size;
1716 init_menu_items ();
f04366cb 1717 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1718 {
1719 Lisp_Object key, string, maps;
1720
1721 key = XVECTOR (items)->contents[i];
1722 string = XVECTOR (items)->contents[i + 1];
1723 maps = XVECTOR (items)->contents[i + 2];
1724 if (NILP (string))
1725 break;
1726
1727 wv = single_submenu (key, string, maps);
1728 if (prev_wv)
1729 prev_wv->next = wv;
1730 else
1731 first_wv->contents = wv;
1732 /* Don't set wv->name here; GC during the loop might relocate it. */
1733 wv->enabled = 1;
1734 prev_wv = wv;
1735 }
1736
1737 finish_menu_items ();
1738
1739 set_buffer_internal_1 (prev);
8d8a3494 1740 unbind_to (specpdl_count, Qnil);
8d8a3494 1741
88766961
RS
1742 /* If there has been no change in the Lisp-level contents
1743 of the menu bar, skip redisplaying it. Just exit. */
1744
1745 for (i = 0; i < previous_menu_items_used; i++)
1746 if (menu_items_used == i
99d3fac7 1747 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
88766961 1748 break;
62555c22 1749 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
88766961
RS
1750 {
1751 free_menubar_widget_value_tree (first_wv);
1752 menu_items = Qnil;
1753
1754 return;
1755 }
1756
1757 /* Now GC cannot happen during the lifetime of the widget_value,
1758 so it's safe to store data from a Lisp_String. */
1759 wv = first_wv->contents;
f04366cb 1760 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1761 {
1762 Lisp_Object string;
1763 string = XVECTOR (items)->contents[i + 1];
1764 if (NILP (string))
1765 break;
1766 wv->name = (char *) XSTRING (string)->data;
1767 wv = wv->next;
1768 }
1769
1770 f->menu_bar_vector = menu_items;
1771 f->menu_bar_items_used = menu_items_used;
1772 menu_items = Qnil;
4d19cb8e 1773 }
88766961
RS
1774 else
1775 {
1776 /* Make a widget-value tree containing
1777 just the top level menu bar strings. */
4d19cb8e 1778
88766961 1779 items = FRAME_MENU_BAR_ITEMS (f);
f04366cb 1780 for (i = 0; i < XVECTOR (items)->size; i += 4)
88766961
RS
1781 {
1782 Lisp_Object string;
1783
1784 string = XVECTOR (items)->contents[i + 1];
1785 if (NILP (string))
1786 break;
1787
f7fab165 1788 wv = xmalloc_widget_value ();
88766961
RS
1789 wv->name = (char *) XSTRING (string)->data;
1790 wv->value = 0;
1791 wv->enabled = 1;
fe8fa62f
RS
1792 /* This prevents lwlib from assuming this
1793 menu item is really supposed to be empty. */
1794 /* The EMACS_INT cast avoids a warning.
1795 This value just has to be different from small integers. */
1796 wv->call_data = (void *) (EMACS_INT) (-1);
88766961
RS
1797
1798 if (prev_wv)
1799 prev_wv->next = wv;
1800 else
1801 first_wv->contents = wv;
1802 prev_wv = wv;
1803 }
62555c22
RS
1804
1805 /* Forget what we thought we knew about what is in the
1806 detailed contents of the menu bar menus.
1807 Changing the top level always destroys the contents. */
1808 f->menu_bar_items_used = 0;
88766961 1809 }
4dedbfe0 1810
88766961 1811 /* Create or update the menu bar widget. */
aa669def
RS
1812
1813 BLOCK_INPUT;
1814
18686d47 1815 if (menubar_widget)
4dedbfe0
PR
1816 {
1817 /* Disable resizing (done for Motif!) */
7556890b 1818 lw_allow_resizing (f->output_data.x->widget, False);
4dedbfe0
PR
1819
1820 /* The third arg is DEEP_P, which says to consider the entire
1821 menu trees we supply, rather than just the menu bar item names. */
88766961 1822 lw_modify_all_widgets (id, first_wv, deep_p);
4dedbfe0 1823
c98fcf4b 1824 /* Re-enable the edit widget to resize. */
7556890b 1825 lw_allow_resizing (f->output_data.x->widget, True);
4dedbfe0 1826 }
18686d47
RS
1827 else
1828 {
88766961 1829 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
7556890b 1830 f->output_data.x->column_widget,
4dedbfe0
PR
1831 0,
1832 popup_activate_callback,
1833 menubar_selection_callback,
1834 popup_deactivate_callback);
7556890b 1835 f->output_data.x->menubar_widget = menubar_widget;
18686d47 1836 }
1d1c1567
KH
1837
1838 {
1839 int menubar_size
7556890b
RS
1840 = (f->output_data.x->menubar_widget
1841 ? (f->output_data.x->menubar_widget->core.height
1842 + f->output_data.x->menubar_widget->core.border_width)
1d1c1567
KH
1843 : 0);
1844
f42aa681
RS
1845#if 0 /* Experimentally, we now get the right results
1846 for -geometry -0-0 without this. 24 Aug 96, rms. */
5ee80bd3 1847#ifdef USE_LUCID
1d1c1567
KH
1848 if (FRAME_EXTERNAL_MENU_BAR (f))
1849 {
1850 Dimension ibw = 0;
7556890b 1851 XtVaGetValues (f->output_data.x->column_widget,
1d1c1567
KH
1852 XtNinternalBorderWidth, &ibw, NULL);
1853 menubar_size += ibw;
1854 }
5ee80bd3 1855#endif /* USE_LUCID */
f42aa681 1856#endif /* 0 */
1d1c1567 1857
7556890b 1858 f->output_data.x->menubar_height = menubar_size;
1d1c1567 1859 }
18686d47
RS
1860
1861 free_menubar_widget_value_tree (first_wv);
1862
4bcdbab1 1863 update_frame_menubar (f);
18686d47
RS
1864
1865 UNBLOCK_INPUT;
1866}
85f487d1 1867
8e6208c5 1868/* Called from Fx_create_frame to create the initial menubar of a frame
4dedbfe0
PR
1869 before it is mapped, so that the window is mapped with the menubar already
1870 there instead of us tacking it on later and thrashing the window after it
1871 is visible. */
1872
1873void
1874initialize_frame_menubar (f)
1875 FRAME_PTR f;
1876{
1877 /* This function is called before the first chance to redisplay
1878 the frame. It has to be, so the frame will have the right size. */
1879 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
88766961 1880 set_frame_menubar (f, 1, 1);
4dedbfe0
PR
1881}
1882
1883/* Get rid of the menu bar of frame F, and free its storage.
1884 This is used when deleting a frame, and when turning off the menu bar. */
1885
85f487d1
FP
1886void
1887free_frame_menubar (f)
1888 FRAME_PTR f;
1889{
1890 Widget menubar_widget;
1891 int id;
1892
7556890b 1893 menubar_widget = f->output_data.x->menubar_widget;
85f487d1
FP
1894
1895 if (menubar_widget)
1896 {
1897 BLOCK_INPUT;
7556890b 1898 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
85f487d1
FP
1899 UNBLOCK_INPUT;
1900 }
1901}
78589e07 1902
78589e07
RS
1903#endif /* USE_X_TOOLKIT */
1904\f
1905/* xmenu_show actually displays a menu using the panes and items in menu_items
1906 and returns the value selected from it.
1907 There are two versions of xmenu_show, one for Xt and one for Xlib.
1908 Both assume input is blocked by the caller. */
1909
1910/* F is the frame the menu is for.
1911 X and Y are the frame-relative specified position,
1912 relative to the inside upper left corner of the frame F.
c8b5aa3d 1913 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
78589e07
RS
1914 KEYMAPS is 1 if this menu was specified with keymaps;
1915 in that case, we return a list containing the chosen item's value
1916 and perhaps also the pane's prefix.
1917 TITLE is the specified menu title.
1918 ERROR is a place to store an error message string in case of failure.
1919 (We return nil on failure, but the value doesn't actually matter.) */
18686d47
RS
1920
1921#ifdef USE_X_TOOLKIT
18686d47 1922
8ed87156 1923/* We need a unique id for each widget handled by the Lucid Widget
cc17e9bf
KH
1924 library.
1925
1926 For the main windows, and popup menus, we use this counter,
88766961 1927 which we increment each time after use. This starts from 1<<16.
cc17e9bf 1928
88766961
RS
1929 For menu bars, we use numbers starting at 0, counted in
1930 next_menubar_widget_id. */
8ed87156 1931LWLIB_ID widget_id_tick;
165e1749 1932
4dedbfe0
PR
1933#ifdef __STDC__
1934static Lisp_Object *volatile menu_item_selection;
1935#else
1936static Lisp_Object *menu_item_selection;
1937#endif
1938
1939static void
1940popup_selection_callback (widget, id, client_data)
1941 Widget widget;
1942 LWLIB_ID id;
1943 XtPointer client_data;
1944{
1945 menu_item_selection = (Lisp_Object *) client_data;
1946}
1947
78589e07 1948static Lisp_Object
673a6211 1949xmenu_show (f, x, y, for_click, keymaps, title, error)
18686d47 1950 FRAME_PTR f;
18686d47
RS
1951 int x;
1952 int y;
9685a93f 1953 int for_click;
78589e07
RS
1954 int keymaps;
1955 Lisp_Object title;
1956 char **error;
18686d47 1957{
78589e07 1958 int i;
cc17e9bf 1959 LWLIB_ID menu_id;
18686d47 1960 Widget menu;
ffcb5a51 1961 Arg av[2];
60f312e2 1962 int ac = 0;
78589e07 1963 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
101bb4a5
RS
1964 widget_value **submenu_stack
1965 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1966 Lisp_Object *subprefix_stack
1967 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1968 int submenu_depth = 0;
ffcb5a51 1969 XButtonPressedEvent dummy;
4e8d3549 1970
78c8278d 1971 int first_pane;
a51b963c 1972 int next_release_must_exit = 0;
78c8278d 1973
78589e07
RS
1974 *error = NULL;
1975
742f715d
KH
1976 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1977 {
1978 *error = "Empty menu";
1979 return Qnil;
1980 }
63c414df 1981
78589e07
RS
1982 /* Create a tree of widget_value objects
1983 representing the panes and their items. */
f7fab165 1984 wv = xmalloc_widget_value ();
78589e07
RS
1985 wv->name = "menu";
1986 wv->value = 0;
1987 wv->enabled = 1;
1988 first_wv = wv;
78c8278d 1989 first_pane = 1;
78589e07
RS
1990
1991 /* Loop over all panes and items, filling in the tree. */
1992 i = 0;
1993 while (i < menu_items_used)
1994 {
101bb4a5
RS
1995 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1996 {
1997 submenu_stack[submenu_depth++] = save_wv;
1998 save_wv = prev_wv;
1999 prev_wv = 0;
78c8278d 2000 first_pane = 1;
101bb4a5
RS
2001 i++;
2002 }
2003 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2004 {
2005 prev_wv = save_wv;
2006 save_wv = submenu_stack[--submenu_depth];
78c8278d 2007 first_pane = 0;
101bb4a5
RS
2008 i++;
2009 }
2010 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2011 && submenu_depth != 0)
2012 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
2013 /* Ignore a nil in the item list.
2014 It's meaningful only for dialog boxes. */
2015 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2016 i += 1;
101bb4a5 2017 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2018 {
2019 /* Create a new pane. */
2020 Lisp_Object pane_name, prefix;
2021 char *pane_string;
2022 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2023 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2024 pane_string = (NILP (pane_name)
2025 ? "" : (char *) XSTRING (pane_name)->data);
101bb4a5 2026 /* If there is just one top-level pane, put all its items directly
78589e07
RS
2027 under the top-level menu. */
2028 if (menu_items_n_panes == 1)
2029 pane_string = "";
2030
2031 /* If the pane has a meaningful name,
2032 make the pane a top-level menu item
2033 with its items as a submenu beneath it. */
78c8278d 2034 if (!keymaps && strcmp (pane_string, ""))
78589e07 2035 {
f7fab165 2036 wv = xmalloc_widget_value ();
78589e07
RS
2037 if (save_wv)
2038 save_wv->next = wv;
2039 else
2040 first_wv->contents = wv;
2041 wv->name = pane_string;
2042 if (keymaps && !NILP (prefix))
2043 wv->name++;
2044 wv->value = 0;
2045 wv->enabled = 1;
78c8278d
RS
2046 save_wv = wv;
2047 prev_wv = 0;
78589e07 2048 }
78c8278d
RS
2049 else if (first_pane)
2050 {
2051 save_wv = wv;
2052 prev_wv = 0;
2053 }
2054 first_pane = 0;
78589e07
RS
2055 i += MENU_ITEMS_PANE_LENGTH;
2056 }
2057 else
2058 {
2059 /* Create a new item within current pane. */
a352a815 2060 Lisp_Object item_name, enable, descrip, def;
78589e07
RS
2061 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2062 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2063 descrip
2064 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
a352a815 2065 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
78589e07 2066
f7fab165 2067 wv = xmalloc_widget_value ();
78589e07
RS
2068 if (prev_wv)
2069 prev_wv->next = wv;
2070 else
2071 save_wv->contents = wv;
4e8d3549 2072 wv->name = (char *) XSTRING (item_name)->data;
78589e07 2073 if (!NILP (descrip))
4e8d3549 2074 wv->key = (char *) XSTRING (descrip)->data;
78589e07 2075 wv->value = 0;
a352a815
RS
2076 /* If this item has a null value,
2077 make the call_data null so that it won't display a box
2078 when the mouse is on it. */
2079 wv->call_data
2080 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
78589e07
RS
2081 wv->enabled = !NILP (enable);
2082 prev_wv = wv;
2083
2084 i += MENU_ITEMS_ITEM_LENGTH;
2085 }
2086 }
2087
c98fcf4b 2088 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
2089 if (!NILP (title))
2090 {
f7fab165
RS
2091 widget_value *wv_title = xmalloc_widget_value ();
2092 widget_value *wv_sep1 = xmalloc_widget_value ();
2093 widget_value *wv_sep2 = xmalloc_widget_value ();
4dedbfe0
PR
2094
2095 wv_sep2->name = "--";
2096 wv_sep2->next = first_wv->contents;
2097
2098 wv_sep1->name = "--";
2099 wv_sep1->next = wv_sep2;
2100
2101 wv_title->name = (char *) XSTRING (title)->data;
2102 wv_title->enabled = True;
2103 wv_title->next = wv_sep1;
2104 first_wv->contents = wv_title;
2105 }
2106
78589e07 2107 /* Actually create the menu. */
cc17e9bf 2108 menu_id = widget_id_tick++;
78589e07 2109 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
7556890b 2110 f->output_data.x->widget, 1, 0,
4dedbfe0
PR
2111 popup_selection_callback,
2112 popup_deactivate_callback);
60f312e2 2113
ffcb5a51
RS
2114 /* Adjust coordinates to relative to the outer (window manager) window. */
2115 {
2116 Window child;
2117 int win_x = 0, win_y = 0;
2118
2119 /* Find the position of the outside upper-left corner of
2120 the inner window, with respect to the outer window. */
2121 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2122 {
2123 BLOCK_INPUT;
2124 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2125
2126 /* From-window, to-window. */
2127 f->output_data.x->window_desc,
2128 f->output_data.x->parent_desc,
2129
2130 /* From-position, to-position. */
2131 0, 0, &win_x, &win_y,
2132
2133 /* Child of window. */
2134 &child);
2135 UNBLOCK_INPUT;
2136 x += win_x;
2137 y += win_y;
2138 }
2139 }
2140
2141 /* Adjust coordinates to be root-window-relative. */
2142 x += f->output_data.x->left_pos;
2143 y += f->output_data.x->top_pos;
2144
2145 dummy.type = ButtonPress;
2146 dummy.serial = 0;
2147 dummy.send_event = 0;
2148 dummy.display = FRAME_X_DISPLAY (f);
2149 dummy.time = CurrentTime;
ffcb5a51
RS
2150 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2151 dummy.window = dummy.root;
2152 dummy.subwindow = dummy.root;
2153 dummy.x_root = x;
2154 dummy.y_root = y;
2155 dummy.x = x;
2156 dummy.y = y;
c8b5aa3d
RS
2157 dummy.state = (FRAME_X_DISPLAY_INFO (f)->grabbed >> 1) * Button1Mask;
2158 dummy.button = 0;
2159 for (i = 0; i < 5; i++)
2160 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2161 dummy.button = i;
ffcb5a51 2162
60f312e2
RS
2163 /* Don't allow any geometry request from the user. */
2164 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2165 XtSetValues (menu, av, ac);
2166
78589e07
RS
2167 /* Free the widget_value objects we used to specify the contents. */
2168 free_menubar_widget_value_tree (first_wv);
2169
2170 /* No selection has been chosen yet. */
2171 menu_item_selection = 0;
2172
78589e07 2173 /* Display the menu. */
ffcb5a51 2174 lw_popup_menu (menu, &dummy);
4dedbfe0 2175 popup_activated_flag = 1;
18686d47 2176
78589e07 2177 /* Process events that apply to the menu. */
2e2b8e22 2178 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id);
78589e07 2179
a9c90b7c
RS
2180 /* fp turned off the following statement and wrote a comment
2181 that it is unnecessary--that the menu has already disappeared.
21af8a68
KH
2182 Nowadays the menu disappears ok, all right, but
2183 we need to delete the widgets or multiple ones will pile up. */
78589e07 2184 lw_destroy_all_widgets (menu_id);
18686d47 2185
78589e07
RS
2186 /* Find the selected item, and its pane, to return
2187 the proper value. */
2188 if (menu_item_selection != 0)
2189 {
c63f6952 2190 Lisp_Object prefix, entry;
78589e07
RS
2191
2192 prefix = Qnil;
2193 i = 0;
2194 while (i < menu_items_used)
2195 {
101bb4a5
RS
2196 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2197 {
2198 subprefix_stack[submenu_depth++] = prefix;
2199 prefix = entry;
2200 i++;
2201 }
2202 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2203 {
2204 prefix = subprefix_stack[--submenu_depth];
2205 i++;
2206 }
2207 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
78589e07
RS
2208 {
2209 prefix
2210 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2211 i += MENU_ITEMS_PANE_LENGTH;
2212 }
d31d42cc
RS
2213 /* Ignore a nil in the item list.
2214 It's meaningful only for dialog boxes. */
2215 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2216 i += 1;
78589e07
RS
2217 else
2218 {
2219 entry
2220 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2221 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2222 {
2223 if (keymaps != 0)
2224 {
101bb4a5
RS
2225 int j;
2226
78589e07
RS
2227 entry = Fcons (entry, Qnil);
2228 if (!NILP (prefix))
2229 entry = Fcons (prefix, entry);
101bb4a5 2230 for (j = submenu_depth - 1; j >= 0; j--)
e48087b7 2231 if (!NILP (subprefix_stack[j]))
5964e450 2232 entry = Fcons (subprefix_stack[j], entry);
78589e07
RS
2233 }
2234 return entry;
2235 }
2236 i += MENU_ITEMS_ITEM_LENGTH;
2237 }
2238 }
2239 }
2240
2241 return Qnil;
18686d47 2242}
4dedbfe0
PR
2243\f
2244static void
2245dialog_selection_callback (widget, id, client_data)
2246 Widget widget;
2247 LWLIB_ID id;
2248 XtPointer client_data;
2249{
01d5e892
RS
2250 /* The EMACS_INT cast avoids a warning. There's no problem
2251 as long as pointers have enough bits to hold small integers. */
2252 if ((int) (EMACS_INT) client_data != -1)
4dedbfe0
PR
2253 menu_item_selection = (Lisp_Object *) client_data;
2254 BLOCK_INPUT;
2255 lw_destroy_all_widgets (id);
2256 UNBLOCK_INPUT;
9572375b 2257 popup_activated_flag = 0;
4dedbfe0 2258}
18686d47 2259
165e1749
FP
2260static char * button_names [] = {
2261 "button1", "button2", "button3", "button4", "button5",
2262 "button6", "button7", "button8", "button9", "button10" };
2263
2264static Lisp_Object
673a6211 2265xdialog_show (f, keymaps, title, error)
165e1749 2266 FRAME_PTR f;
165e1749
FP
2267 int keymaps;
2268 Lisp_Object title;
2269 char **error;
2270{
2271 int i, nb_buttons=0;
cc17e9bf 2272 LWLIB_ID dialog_id;
165e1749 2273 Widget menu;
80670155 2274 char dialog_name[6];
165e1749 2275
165e1749
FP
2276 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2277
fcaa7665
RS
2278 /* Number of elements seen so far, before boundary. */
2279 int left_count = 0;
2280 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2281 int boundary_seen = 0;
2282
165e1749
FP
2283 *error = NULL;
2284
80670155
RS
2285 if (menu_items_n_panes > 1)
2286 {
2287 *error = "Multiple panes in dialog box";
2288 return Qnil;
2289 }
2290
165e1749
FP
2291 /* Create a tree of widget_value objects
2292 representing the text label and buttons. */
2293 {
2294 Lisp_Object pane_name, prefix;
2295 char *pane_string;
2296 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2297 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2298 pane_string = (NILP (pane_name)
2299 ? "" : (char *) XSTRING (pane_name)->data);
f7fab165 2300 prev_wv = xmalloc_widget_value ();
165e1749
FP
2301 prev_wv->value = pane_string;
2302 if (keymaps && !NILP (prefix))
2303 prev_wv->name++;
2304 prev_wv->enabled = 1;
2305 prev_wv->name = "message";
2306 first_wv = prev_wv;
2307
2308 /* Loop over all panes and items, filling in the tree. */
2309 i = MENU_ITEMS_PANE_LENGTH;
2310 while (i < menu_items_used)
2311 {
2312
2313 /* Create a new item within current pane. */
2314 Lisp_Object item_name, enable, descrip;
2315 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2316 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2317 descrip
2318 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2319
80670155
RS
2320 if (NILP (item_name))
2321 {
2322 free_menubar_widget_value_tree (first_wv);
2323 *error = "Submenu in dialog items";
2324 return Qnil;
2325 }
fcaa7665
RS
2326 if (EQ (item_name, Qquote))
2327 {
2328 /* This is the boundary between left-side elts
2329 and right-side elts. Stop incrementing right_count. */
2330 boundary_seen = 1;
2331 i++;
2332 continue;
2333 }
86e71abf 2334 if (nb_buttons >= 9)
80670155
RS
2335 {
2336 free_menubar_widget_value_tree (first_wv);
2337 *error = "Too many dialog items";
2338 return Qnil;
2339 }
2340
f7fab165 2341 wv = xmalloc_widget_value ();
165e1749 2342 prev_wv->next = wv;
80670155 2343 wv->name = (char *) button_names[nb_buttons];
165e1749 2344 if (!NILP (descrip))
4e8d3549
RS
2345 wv->key = (char *) XSTRING (descrip)->data;
2346 wv->value = (char *) XSTRING (item_name)->data;
165e1749
FP
2347 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2348 wv->enabled = !NILP (enable);
2349 prev_wv = wv;
2350
fcaa7665
RS
2351 if (! boundary_seen)
2352 left_count++;
2353
165e1749
FP
2354 nb_buttons++;
2355 i += MENU_ITEMS_ITEM_LENGTH;
2356 }
2357
fcaa7665
RS
2358 /* If the boundary was not specified,
2359 by default put half on the left and half on the right. */
2360 if (! boundary_seen)
2361 left_count = nb_buttons - nb_buttons / 2;
2362
f7fab165 2363 wv = xmalloc_widget_value ();
80670155
RS
2364 wv->name = dialog_name;
2365
2366 /* Dialog boxes use a really stupid name encoding
2367 which specifies how many buttons to use
2368 and how many buttons are on the right.
2369 The Q means something also. */
2370 dialog_name[0] = 'Q';
2371 dialog_name[1] = '0' + nb_buttons;
2372 dialog_name[2] = 'B';
2373 dialog_name[3] = 'R';
fcaa7665
RS
2374 /* Number of buttons to put on the right. */
2375 dialog_name[4] = '0' + nb_buttons - left_count;
80670155 2376 dialog_name[5] = 0;
165e1749
FP
2377 wv->contents = first_wv;
2378 first_wv = wv;
165e1749
FP
2379 }
2380
2381 /* Actually create the dialog. */
cc17e9bf 2382 dialog_id = widget_id_tick++;
165e1749 2383 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
7556890b 2384 f->output_data.x->widget, 1, 0,
165e1749 2385 dialog_selection_callback, 0);
b5587215 2386 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
165e1749
FP
2387 /* Free the widget_value objects we used to specify the contents. */
2388 free_menubar_widget_value_tree (first_wv);
2389
2390 /* No selection has been chosen yet. */
2391 menu_item_selection = 0;
2392
165e1749
FP
2393 /* Display the menu. */
2394 lw_pop_up_all_widgets (dialog_id);
aa669def 2395 popup_activated_flag = 1;
165e1749
FP
2396
2397 /* Process events that apply to the menu. */
2e2b8e22 2398 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id);
165e1749 2399
21af8a68
KH
2400 lw_destroy_all_widgets (dialog_id);
2401
165e1749
FP
2402 /* Find the selected item, and its pane, to return
2403 the proper value. */
2404 if (menu_item_selection != 0)
2405 {
2406 Lisp_Object prefix;
2407
2408 prefix = Qnil;
2409 i = 0;
2410 while (i < menu_items_used)
2411 {
2412 Lisp_Object entry;
2413
2414 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2415 {
2416 prefix
2417 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2418 i += MENU_ITEMS_PANE_LENGTH;
2419 }
2420 else
2421 {
2422 entry
2423 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2424 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2425 {
2426 if (keymaps != 0)
2427 {
2428 entry = Fcons (entry, Qnil);
2429 if (!NILP (prefix))
2430 entry = Fcons (prefix, entry);
2431 }
2432 return entry;
2433 }
2434 i += MENU_ITEMS_ITEM_LENGTH;
2435 }
2436 }
2437 }
2438
2439 return Qnil;
2440}
18686d47 2441#else /* not USE_X_TOOLKIT */
78589e07
RS
2442
2443static Lisp_Object
673a6211 2444xmenu_show (f, x, y, for_click, keymaps, title, error)
78589e07
RS
2445 FRAME_PTR f;
2446 int x, y;
9685a93f
RS
2447 int for_click;
2448 int keymaps;
78589e07
RS
2449 Lisp_Object title;
2450 char **error;
dcfdbac7 2451{
78589e07
RS
2452 Window root;
2453 XMenu *menu;
2454 int pane, selidx, lpane, status;
2455 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
2456 char *datap;
2457 int ulx, uly, width, height;
2458 int dispwidth, dispheight;
4e8d3549
RS
2459 int i, j;
2460 int maxwidth;
78589e07
RS
2461 int dummy_int;
2462 unsigned int dummy_uint;
088831f6 2463
07a675b7 2464 *error = 0;
78589e07
RS
2465 if (menu_items_n_panes == 0)
2466 return Qnil;
088831f6 2467
742f715d
KH
2468 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2469 {
2470 *error = "Empty menu";
2471 return Qnil;
2472 }
2473
78589e07 2474 /* Figure out which root window F is on. */
92280f67 2475 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
2476 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2477 &dummy_uint, &dummy_uint);
18686d47 2478
78589e07 2479 /* Make the menu on that window. */
92280f67 2480 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 2481 if (menu == NULL)
dcfdbac7
JB
2482 {
2483 *error = "Can't create menu";
78589e07 2484 return Qnil;
dcfdbac7 2485 }
78589e07 2486
87485d6f 2487#ifdef HAVE_X_WINDOWS
78589e07 2488 /* Adjust coordinates to relative to the outer (window manager) window. */
78589e07
RS
2489 {
2490 Window child;
2491 int win_x = 0, win_y = 0;
2492
2493 /* Find the position of the outside upper-left corner of
2494 the inner window, with respect to the outer window. */
7556890b 2495 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
78589e07
RS
2496 {
2497 BLOCK_INPUT;
92280f67 2498 XTranslateCoordinates (FRAME_X_DISPLAY (f),
78589e07
RS
2499
2500 /* From-window, to-window. */
7556890b
RS
2501 f->output_data.x->window_desc,
2502 f->output_data.x->parent_desc,
78589e07
RS
2503
2504 /* From-position, to-position. */
2505 0, 0, &win_x, &win_y,
2506
2507 /* Child of window. */
2508 &child);
2509 UNBLOCK_INPUT;
2510 x += win_x;
2511 y += win_y;
2512 }
2513 }
87485d6f 2514#endif /* HAVE_X_WINDOWS */
78589e07
RS
2515
2516 /* Adjust coordinates to be root-window-relative. */
7556890b
RS
2517 x += f->output_data.x->left_pos;
2518 y += f->output_data.x->top_pos;
18686d47 2519
78589e07
RS
2520 /* Create all the necessary panes and their items. */
2521 i = 0;
2522 while (i < menu_items_used)
dcfdbac7 2523 {
78589e07 2524 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
dcfdbac7 2525 {
78589e07
RS
2526 /* Create a new pane. */
2527 Lisp_Object pane_name, prefix;
2528 char *pane_string;
2529
2530 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2531 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2532 pane_string = (NILP (pane_name)
2533 ? "" : (char *) XSTRING (pane_name)->data);
2534 if (keymaps && !NILP (prefix))
2535 pane_string++;
2536
92280f67 2537 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
2538 if (lpane == XM_FAILURE)
2539 {
92280f67 2540 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
78589e07
RS
2541 *error = "Can't create pane";
2542 return Qnil;
2543 }
2544 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
2545
2546 /* Find the width of the widest item in this pane. */
2547 maxwidth = 0;
2548 j = i;
2549 while (j < menu_items_used)
2550 {
2551 Lisp_Object item;
2552 item = XVECTOR (menu_items)->contents[j];
2553 if (EQ (item, Qt))
2554 break;
2555 if (NILP (item))
2556 {
2557 j++;
2558 continue;
2559 }
2560 width = XSTRING (item)->size;
2561 if (width > maxwidth)
2562 maxwidth = width;
2563
2564 j += MENU_ITEMS_ITEM_LENGTH;
2565 }
dcfdbac7 2566 }
fcaa7665
RS
2567 /* Ignore a nil in the item list.
2568 It's meaningful only for dialog boxes. */
2569 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2570 i += 1;
78589e07 2571 else
dcfdbac7 2572 {
78589e07
RS
2573 /* Create a new item within current pane. */
2574 Lisp_Object item_name, enable, descrip;
4e8d3549 2575 unsigned char *item_data;
78589e07
RS
2576
2577 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2578 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2579 descrip
2580 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2581 if (!NILP (descrip))
4e8d3549
RS
2582 {
2583 int gap = maxwidth - XSTRING (item_name)->size;
2584#ifdef C_ALLOCA
2585 Lisp_Object spacer;
2586 spacer = Fmake_string (make_number (gap), make_number (' '));
2587 item_name = concat2 (item_name, spacer);
2588 item_name = concat2 (item_name, descrip);
2589 item_data = XSTRING (item_name)->data;
2590#else
2591 /* if alloca is fast, use that to make the space,
2592 to reduce gc needs. */
2593 item_data
2594 = (unsigned char *) alloca (maxwidth
2595 + XSTRING (descrip)->size + 1);
2596 bcopy (XSTRING (item_name)->data, item_data,
2597 XSTRING (item_name)->size);
2598 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2599 item_data[j] = ' ';
2600 bcopy (XSTRING (descrip)->data, item_data + j,
2601 XSTRING (descrip)->size);
2602 item_data[j + XSTRING (descrip)->size] = 0;
2603#endif
2604 }
2605 else
2606 item_data = XSTRING (item_name)->data;
78589e07 2607
92280f67
RS
2608 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2609 menu, lpane, 0, item_data,
78589e07 2610 !NILP (enable))
dcfdbac7
JB
2611 == XM_FAILURE)
2612 {
92280f67 2613 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
dcfdbac7 2614 *error = "Can't add selection to menu";
78589e07 2615 return Qnil;
dcfdbac7 2616 }
78589e07 2617 i += MENU_ITEMS_ITEM_LENGTH;
dcfdbac7
JB
2618 }
2619 }
4e8d3549 2620
78589e07 2621 /* All set and ready to fly. */
92280f67
RS
2622 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2623 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
f1847de3 2624 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
92280f67 2625 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
f1847de3 2626 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
78589e07
RS
2627 x = min (x, dispwidth);
2628 y = min (y, dispheight);
2629 x = max (x, 1);
2630 y = max (y, 1);
92280f67 2631 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
2632 &ulx, &uly, &width, &height);
2633 if (ulx+width > dispwidth)
2634 {
78589e07 2635 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
2636 ulx = dispwidth - width;
2637 }
2638 if (uly+height > dispheight)
2639 {
78589e07 2640 y -= (uly + height) - dispheight;
dcfdbac7
JB
2641 uly = dispheight - height;
2642 }
78589e07
RS
2643 if (ulx < 0) x -= ulx;
2644 if (uly < 0) y -= uly;
121e4555
KH
2645
2646 XMenuSetAEQ (menu, TRUE);
78589e07
RS
2647 XMenuSetFreeze (menu, TRUE);
2648 pane = selidx = 0;
dcfdbac7 2649
92280f67 2650 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
78589e07 2651 x, y, ButtonReleaseMask, &datap);
a352a815
RS
2652
2653
f1df80a8 2654#ifdef HAVE_X_WINDOWS
a352a815
RS
2655 /* Assume the mouse has moved out of the X window.
2656 If it has actually moved in, we will get an EnterNotify. */
29e460bd 2657 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
f1df80a8 2658#endif
a352a815 2659
dcfdbac7
JB
2660 switch (status)
2661 {
2662 case XM_SUCCESS:
2663#ifdef XDEBUG
2664 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2665#endif
fa6d54d9 2666
78589e07
RS
2667 /* Find the item number SELIDX in pane number PANE. */
2668 i = 0;
2669 while (i < menu_items_used)
fa6d54d9 2670 {
78589e07 2671 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
088831f6 2672 {
78589e07
RS
2673 if (pane == 0)
2674 pane_prefix
2675 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2676 pane--;
2677 i += MENU_ITEMS_PANE_LENGTH;
088831f6 2678 }
78589e07 2679 else
ab6ee1a0 2680 {
78589e07 2681 if (pane == -1)
ab6ee1a0 2682 {
78589e07 2683 if (selidx == 0)
ab6ee1a0 2684 {
78589e07
RS
2685 entry
2686 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2687 if (keymaps != 0)
ab6ee1a0 2688 {
78589e07
RS
2689 entry = Fcons (entry, Qnil);
2690 if (!NILP (pane_prefix))
2691 entry = Fcons (pane_prefix, entry);
ab6ee1a0 2692 }
78589e07 2693 break;
ab6ee1a0 2694 }
78589e07 2695 selidx--;
ab6ee1a0 2696 }
78589e07 2697 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
2698 }
2699 }
78589e07 2700 break;
dcfdbac7 2701
78589e07 2702 case XM_FAILURE:
78589e07
RS
2703 *error = "Can't activate menu";
2704 case XM_IA_SELECT:
2705 case XM_NO_SELECT:
2706 entry = Qnil;
2707 break;
dcfdbac7 2708 }
92280f67 2709 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
a5285df3 2710
87485d6f 2711#ifdef HAVE_X_WINDOWS
a5285df3
RS
2712 /* State that no mouse buttons are now held.
2713 (The oldXMenu code doesn't track this info for us.)
2714 That is not necessarily true, but the fiction leads to reasonable
2715 results, and it is a pain to ask which are actually held now. */
e9a79fb2 2716 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
87485d6f 2717#endif
a5285df3 2718
78589e07 2719 return entry;
dcfdbac7 2720}
4dedbfe0 2721
78589e07 2722#endif /* not USE_X_TOOLKIT */
1e659e4c
RS
2723
2724#endif /* HAVE_MENUS */
088831f6 2725\f
78589e07 2726syms_of_xmenu ()
dcfdbac7 2727{
78589e07
RS
2728 staticpro (&menu_items);
2729 menu_items = Qnil;
dcfdbac7 2730
67385caa
RS
2731 Qmenu_alias = intern ("menu-alias");
2732 staticpro (&Qmenu_alias);
2733
0314aacb
RS
2734 Qdebug_on_next_call = intern ("debug-on-next-call");
2735 staticpro (&Qdebug_on_next_call);
2736
bfc524bc
RS
2737 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
2738 "Frame for which we are updating a menu.\n\
2739The enable predicate for a menu command should check this variable.");
2740 Vmenu_updating_frame = Qnil;
2741
8ed87156 2742#ifdef USE_X_TOOLKIT
4dedbfe0 2743 widget_id_tick = (1<<16);
88766961 2744 next_menubar_widget_id = 1;
8ed87156
RS
2745#endif
2746
78589e07 2747 defsubr (&Sx_popup_menu);
1e659e4c 2748#ifdef HAVE_MENUS
165e1749 2749 defsubr (&Sx_popup_dialog);
1e659e4c 2750#endif
dcfdbac7 2751}