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