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