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