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