Fix compilation errors in previous commit.
[bpt/emacs.git] / src / xmenu.c
... / ...
CommitLineData
1/* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 Free Software Foundation, Inc.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
21
22/* X pop-up deck-of-cards menu facility for GNU Emacs.
23 *
24 * Written by Jon Arnold and Roman Budzianowski
25 * Mods and rewrite by Robert Krawitz
26 *
27 */
28
29/* Modified by Fred Pierresteguy on December 93
30 to make the popup menus and menubar use the Xt. */
31
32/* Rewritten for clarity and GC protection by rms in Feb 94. */
33
34#include <config.h>
35
36#if 0 /* Why was this included? And without syssignal.h? */
37/* On 4.3 this loses if it comes after xterm.h. */
38#include <signal.h>
39#endif
40
41#include <stdio.h>
42
43#include "lisp.h"
44#include "keyboard.h"
45#include "keymap.h"
46#include "frame.h"
47#include "termhooks.h"
48#include "window.h"
49#include "blockinput.h"
50#include "buffer.h"
51#include "charset.h"
52#include "coding.h"
53#include "sysselect.h"
54
55#ifdef MSDOS
56#include "msdos.h"
57#endif
58
59#ifdef HAVE_X_WINDOWS
60/* This may include sys/types.h, and that somehow loses
61 if this is not done before the other system files. */
62#include "xterm.h"
63#endif
64
65/* Load sys/types.h if not already loaded.
66 In some systems loading it twice is suicidal. */
67#ifndef makedev
68#include <sys/types.h>
69#endif
70
71#include "dispextern.h"
72
73#ifdef HAVE_X_WINDOWS
74/* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
75 code accepts the Emacs internal encoding. */
76#undef HAVE_MULTILINGUAL_MENU
77#ifdef USE_X_TOOLKIT
78#include "widget.h"
79#include <X11/Xlib.h>
80#include <X11/IntrinsicP.h>
81#include <X11/CoreP.h>
82#include <X11/StringDefs.h>
83#include <X11/Shell.h>
84#ifdef USE_LUCID
85#include <X11/Xaw/Paned.h>
86#endif /* USE_LUCID */
87#include "../lwlib/lwlib.h"
88#else /* not USE_X_TOOLKIT */
89#ifndef USE_GTK
90#include "../oldXMenu/XMenu.h"
91#endif
92#endif /* not USE_X_TOOLKIT */
93#endif /* HAVE_X_WINDOWS */
94
95#ifndef TRUE
96#define TRUE 1
97#define FALSE 0
98#endif /* no TRUE */
99
100Lisp_Object Vmenu_updating_frame;
101
102Lisp_Object Qdebug_on_next_call;
103
104extern Lisp_Object Qmenu_bar;
105
106extern Lisp_Object QCtoggle, QCradio;
107
108extern Lisp_Object Voverriding_local_map;
109extern Lisp_Object Voverriding_local_map_menu_flag;
110
111extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
112
113extern Lisp_Object Qmenu_bar_update_hook;
114
115#ifdef USE_X_TOOLKIT
116extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
117extern XtAppContext Xt_app_con;
118
119static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, Lisp_Object,
120 char **));
121static void popup_get_selection P_ ((XEvent *, struct x_display_info *,
122 LWLIB_ID, int));
123
124/* Define HAVE_BOXES if menus can handle radio and toggle buttons. */
125
126#define HAVE_BOXES 1
127#endif /* USE_X_TOOLKIT */
128
129#ifdef USE_GTK
130#include "gtkutil.h"
131#define HAVE_BOXES 1
132extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
133static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, Lisp_Object,
134 char **));
135#endif
136
137/* This is how to deal with multibyte text if HAVE_MULTILINGUAL_MENU
138 isn't defined. The use of HAVE_MULTILINGUAL_MENU could probably be
139 confined to an extended version of this with sections of code below
140 using it unconditionally. */
141#ifdef USE_GTK
142/* gtk just uses utf-8. */
143# define ENCODE_MENU_STRING(str) ENCODE_UTF_8 (str)
144#elif defined HAVE_X_I18N
145# define ENCODE_MENU_STRING(str) ENCODE_SYSTEM (str)
146#else
147# define ENCODE_MENU_STRING(str) string_make_unibyte (str)
148#endif
149
150static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
151 Lisp_Object, Lisp_Object, Lisp_Object,
152 Lisp_Object, Lisp_Object));
153static int update_frame_menubar P_ ((struct frame *));
154static Lisp_Object xmenu_show P_ ((struct frame *, int, int, int, int,
155 Lisp_Object, char **));
156static void keymap_panes P_ ((Lisp_Object *, int, int));
157static void single_keymap_panes P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
158 int, int));
159static void list_of_panes P_ ((Lisp_Object));
160static void list_of_items P_ ((Lisp_Object));
161
162\f
163/* This holds a Lisp vector that holds the results of decoding
164 the keymaps or alist-of-alists that specify a menu.
165
166 It describes the panes and items within the panes.
167
168 Each pane is described by 3 elements in the vector:
169 t, the pane name, the pane's prefix key.
170 Then follow the pane's items, with 5 elements per item:
171 the item string, the enable flag, the item's value,
172 the definition, and the equivalent keyboard key's description string.
173
174 In some cases, multiple levels of menus may be described.
175 A single vector slot containing nil indicates the start of a submenu.
176 A single vector slot containing lambda indicates the end of a submenu.
177 The submenu follows a menu item which is the way to reach the submenu.
178
179 A single vector slot containing quote indicates that the
180 following items should appear on the right of a dialog box.
181
182 Using a Lisp vector to hold this information while we decode it
183 takes care of protecting all the data from GC. */
184
185#define MENU_ITEMS_PANE_NAME 1
186#define MENU_ITEMS_PANE_PREFIX 2
187#define MENU_ITEMS_PANE_LENGTH 3
188
189enum menu_item_idx
190{
191 MENU_ITEMS_ITEM_NAME = 0,
192 MENU_ITEMS_ITEM_ENABLE,
193 MENU_ITEMS_ITEM_VALUE,
194 MENU_ITEMS_ITEM_EQUIV_KEY,
195 MENU_ITEMS_ITEM_DEFINITION,
196 MENU_ITEMS_ITEM_TYPE,
197 MENU_ITEMS_ITEM_SELECTED,
198 MENU_ITEMS_ITEM_HELP,
199 MENU_ITEMS_ITEM_LENGTH
200};
201
202static Lisp_Object menu_items;
203
204/* If non-nil, means that the global vars defined here are already in use.
205 Used to detect cases where we try to re-enter this non-reentrant code. */
206static Lisp_Object menu_items_inuse;
207
208/* Number of slots currently allocated in menu_items. */
209static int menu_items_allocated;
210
211/* This is the index in menu_items of the first empty slot. */
212static int menu_items_used;
213
214/* The number of panes currently recorded in menu_items,
215 excluding those within submenus. */
216static int menu_items_n_panes;
217
218/* Current depth within submenus. */
219static int menu_items_submenu_depth;
220
221/* Flag which when set indicates a dialog or menu has been posted by
222 Xt on behalf of one of the widget sets. */
223static int popup_activated_flag;
224
225static int next_menubar_widget_id;
226
227/* This is set nonzero after the user activates the menu bar, and set
228 to zero again after the menu bars are redisplayed by prepare_menu_bar.
229 While it is nonzero, all calls to set_frame_menubar go deep.
230
231 I don't understand why this is needed, but it does seem to be
232 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
233
234int pending_menu_activation;
235\f
236#ifdef USE_X_TOOLKIT
237
238/* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
239
240static struct frame *
241menubar_id_to_frame (id)
242 LWLIB_ID id;
243{
244 Lisp_Object tail, frame;
245 FRAME_PTR f;
246
247 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
248 {
249 frame = XCAR (tail);
250 if (!GC_FRAMEP (frame))
251 continue;
252 f = XFRAME (frame);
253 if (!FRAME_WINDOW_P (f))
254 continue;
255 if (f->output_data.x->id == id)
256 return f;
257 }
258 return 0;
259}
260
261#endif
262\f
263/* Initialize the menu_items structure if we haven't already done so.
264 Also mark it as currently empty. */
265
266static void
267init_menu_items ()
268{
269 if (NILP (menu_items))
270 {
271 menu_items_allocated = 60;
272 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
273 }
274
275 if (!NILP (menu_items_inuse))
276 error ("Trying to use a menu from within a menu-entry");
277 menu_items_inuse = Qt;
278 menu_items_used = 0;
279 menu_items_n_panes = 0;
280 menu_items_submenu_depth = 0;
281}
282
283/* Call at the end of generating the data in menu_items. */
284
285static void
286finish_menu_items ()
287{
288}
289
290static Lisp_Object
291unuse_menu_items (dummy)
292 Lisp_Object dummy;
293{
294 return menu_items_inuse = Qnil;
295}
296
297/* Call when finished using the data for the current menu
298 in menu_items. */
299
300static void
301discard_menu_items ()
302{
303 /* Free the structure if it is especially large.
304 Otherwise, hold on to it, to save time. */
305 if (menu_items_allocated > 200)
306 {
307 menu_items = Qnil;
308 menu_items_allocated = 0;
309 }
310 xassert (NILP (menu_items_inuse));
311}
312
313/* Make the menu_items vector twice as large. */
314
315static void
316grow_menu_items ()
317{
318 Lisp_Object old;
319 int old_size = menu_items_allocated;
320 old = menu_items;
321
322 menu_items_allocated *= 2;
323 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
324 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
325 old_size * sizeof (Lisp_Object));
326}
327
328/* Begin a submenu. */
329
330static void
331push_submenu_start ()
332{
333 if (menu_items_used + 1 > menu_items_allocated)
334 grow_menu_items ();
335
336 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
337 menu_items_submenu_depth++;
338}
339
340/* End a submenu. */
341
342static void
343push_submenu_end ()
344{
345 if (menu_items_used + 1 > menu_items_allocated)
346 grow_menu_items ();
347
348 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
349 menu_items_submenu_depth--;
350}
351
352/* Indicate boundary between left and right. */
353
354static void
355push_left_right_boundary ()
356{
357 if (menu_items_used + 1 > menu_items_allocated)
358 grow_menu_items ();
359
360 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
361}
362
363/* Start a new menu pane in menu_items.
364 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
365
366static void
367push_menu_pane (name, prefix_vec)
368 Lisp_Object name, prefix_vec;
369{
370 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
371 grow_menu_items ();
372
373 if (menu_items_submenu_depth == 0)
374 menu_items_n_panes++;
375 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
376 XVECTOR (menu_items)->contents[menu_items_used++] = name;
377 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
378}
379
380/* Push one menu item into the current pane. NAME is the string to
381 display. ENABLE if non-nil means this item can be selected. KEY
382 is the key generated by choosing this item, or nil if this item
383 doesn't really have a definition. DEF is the definition of this
384 item. EQUIV is the textual description of the keyboard equivalent
385 for this item (or nil if none). TYPE is the type of this menu
386 item, one of nil, `toggle' or `radio'. */
387
388static void
389push_menu_item (name, enable, key, def, equiv, type, selected, help)
390 Lisp_Object name, enable, key, def, equiv, type, selected, help;
391{
392 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
393 grow_menu_items ();
394
395 XVECTOR (menu_items)->contents[menu_items_used++] = name;
396 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
397 XVECTOR (menu_items)->contents[menu_items_used++] = key;
398 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
399 XVECTOR (menu_items)->contents[menu_items_used++] = def;
400 XVECTOR (menu_items)->contents[menu_items_used++] = type;
401 XVECTOR (menu_items)->contents[menu_items_used++] = selected;
402 XVECTOR (menu_items)->contents[menu_items_used++] = help;
403}
404\f
405/* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
406 and generate menu panes for them in menu_items.
407 If NOTREAL is nonzero,
408 don't bother really computing whether an item is enabled. */
409
410static void
411keymap_panes (keymaps, nmaps, notreal)
412 Lisp_Object *keymaps;
413 int nmaps;
414 int notreal;
415{
416 int mapno;
417
418 init_menu_items ();
419
420 /* Loop over the given keymaps, making a pane for each map.
421 But don't make a pane that is empty--ignore that map instead.
422 P is the number of panes we have made so far. */
423 for (mapno = 0; mapno < nmaps; mapno++)
424 single_keymap_panes (keymaps[mapno],
425 Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10);
426
427 finish_menu_items ();
428}
429
430/* Args passed between single_keymap_panes and single_menu_item. */
431struct skp
432 {
433 Lisp_Object pending_maps;
434 int maxdepth, notreal;
435 int notbuttons;
436 };
437
438static void single_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
439 void *));
440
441/* This is a recursive subroutine of keymap_panes.
442 It handles one keymap, KEYMAP.
443 The other arguments are passed along
444 or point to local variables of the previous function.
445 If NOTREAL is nonzero, only check for equivalent key bindings, don't
446 evaluate expressions in menu items and don't make any menu.
447
448 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
449
450static void
451single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth)
452 Lisp_Object keymap;
453 Lisp_Object pane_name;
454 Lisp_Object prefix;
455 int notreal;
456 int maxdepth;
457{
458 struct skp skp;
459 struct gcpro gcpro1;
460
461 skp.pending_maps = Qnil;
462 skp.maxdepth = maxdepth;
463 skp.notreal = notreal;
464 skp.notbuttons = 0;
465
466 if (maxdepth <= 0)
467 return;
468
469 push_menu_pane (pane_name, prefix);
470
471#ifndef HAVE_BOXES
472 /* Remember index for first item in this pane so we can go back and
473 add a prefix when (if) we see the first button. After that, notbuttons
474 is set to 0, to mark that we have seen a button and all non button
475 items need a prefix. */
476 skp.notbuttons = menu_items_used;
477#endif
478
479 GCPRO1 (skp.pending_maps);
480 map_keymap (keymap, single_menu_item, Qnil, &skp, 1);
481 UNGCPRO;
482
483 /* Process now any submenus which want to be panes at this level. */
484 while (CONSP (skp.pending_maps))
485 {
486 Lisp_Object elt, eltcdr, string;
487 elt = XCAR (skp.pending_maps);
488 eltcdr = XCDR (elt);
489 string = XCAR (eltcdr);
490 /* We no longer discard the @ from the beginning of the string here.
491 Instead, we do this in xmenu_show. */
492 single_keymap_panes (Fcar (elt), string,
493 XCDR (eltcdr), notreal, maxdepth - 1);
494 skp.pending_maps = XCDR (skp.pending_maps);
495 }
496}
497\f
498/* This is a subroutine of single_keymap_panes that handles one
499 keymap entry.
500 KEY is a key in a keymap and ITEM is its binding.
501 SKP->PENDING_MAPS_PTR is a list of keymaps waiting to be made into
502 separate panes.
503 If SKP->NOTREAL is nonzero, only check for equivalent key bindings, don't
504 evaluate expressions in menu items and don't make any menu.
505 If we encounter submenus deeper than SKP->MAXDEPTH levels, ignore them.
506 SKP->NOTBUTTONS is only used when simulating toggle boxes and radio
507 buttons. It keeps track of if we have seen a button in this menu or
508 not. */
509
510static void
511single_menu_item (key, item, dummy, skp_v)
512 Lisp_Object key, item, dummy;
513 void *skp_v;
514{
515 Lisp_Object map, item_string, enabled;
516 struct gcpro gcpro1, gcpro2;
517 int res;
518 struct skp *skp = skp_v;
519
520 /* Parse the menu item and leave the result in item_properties. */
521 GCPRO2 (key, item);
522 res = parse_menu_item (item, skp->notreal, 0);
523 UNGCPRO;
524 if (!res)
525 return; /* Not a menu item. */
526
527 map = XVECTOR (item_properties)->contents[ITEM_PROPERTY_MAP];
528
529 if (skp->notreal)
530 {
531 /* We don't want to make a menu, just traverse the keymaps to
532 precompute equivalent key bindings. */
533 if (!NILP (map))
534 single_keymap_panes (map, Qnil, key, 1, skp->maxdepth - 1);
535 return;
536 }
537
538 enabled = XVECTOR (item_properties)->contents[ITEM_PROPERTY_ENABLE];
539 item_string = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
540
541 if (!NILP (map) && SREF (item_string, 0) == '@')
542 {
543 if (!NILP (enabled))
544 /* An enabled separate pane. Remember this to handle it later. */
545 skp->pending_maps = Fcons (Fcons (map, Fcons (item_string, key)),
546 skp->pending_maps);
547 return;
548 }
549
550#ifndef HAVE_BOXES
551 /* Simulate radio buttons and toggle boxes by putting a prefix in
552 front of them. */
553 {
554 Lisp_Object prefix = Qnil;
555 Lisp_Object type = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
556 if (!NILP (type))
557 {
558 Lisp_Object selected
559 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
560
561 if (skp->notbuttons)
562 /* The first button. Line up previous items in this menu. */
563 {
564 int index = skp->notbuttons; /* Index for first item this menu. */
565 int submenu = 0;
566 Lisp_Object tem;
567 while (index < menu_items_used)
568 {
569 tem
570 = XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME];
571 if (NILP (tem))
572 {
573 index++;
574 submenu++; /* Skip sub menu. */
575 }
576 else if (EQ (tem, Qlambda))
577 {
578 index++;
579 submenu--; /* End sub menu. */
580 }
581 else if (EQ (tem, Qt))
582 index += 3; /* Skip new pane marker. */
583 else if (EQ (tem, Qquote))
584 index++; /* Skip a left, right divider. */
585 else
586 {
587 if (!submenu && SREF (tem, 0) != '\0'
588 && SREF (tem, 0) != '-')
589 XVECTOR (menu_items)->contents[index + MENU_ITEMS_ITEM_NAME]
590 = concat2 (build_string (" "), tem);
591 index += MENU_ITEMS_ITEM_LENGTH;
592 }
593 }
594 skp->notbuttons = 0;
595 }
596
597 /* Calculate prefix, if any, for this item. */
598 if (EQ (type, QCtoggle))
599 prefix = build_string (NILP (selected) ? "[ ] " : "[X] ");
600 else if (EQ (type, QCradio))
601 prefix = build_string (NILP (selected) ? "( ) " : "(*) ");
602 }
603 /* Not a button. If we have earlier buttons, then we need a prefix. */
604 else if (!skp->notbuttons && SREF (item_string, 0) != '\0'
605 && SREF (item_string, 0) != '-')
606 prefix = build_string (" ");
607
608 if (!NILP (prefix))
609 item_string = concat2 (prefix, item_string);
610 }
611#endif /* not HAVE_BOXES */
612
613#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
614 if (!NILP (map))
615 /* Indicate visually that this is a submenu. */
616 item_string = concat2 (item_string, build_string (" >"));
617#endif
618
619 push_menu_item (item_string, enabled, key,
620 XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF],
621 XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ],
622 XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE],
623 XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED],
624 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP]);
625
626#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
627 /* Display a submenu using the toolkit. */
628 if (! (NILP (map) || NILP (enabled)))
629 {
630 push_submenu_start ();
631 single_keymap_panes (map, Qnil, key, 0, skp->maxdepth - 1);
632 push_submenu_end ();
633 }
634#endif
635}
636\f
637/* Push all the panes and items of a menu described by the
638 alist-of-alists MENU.
639 This handles old-fashioned calls to x-popup-menu. */
640
641static void
642list_of_panes (menu)
643 Lisp_Object menu;
644{
645 Lisp_Object tail;
646
647 init_menu_items ();
648
649 for (tail = menu; CONSP (tail); tail = XCDR (tail))
650 {
651 Lisp_Object elt, pane_name, pane_data;
652 elt = XCAR (tail);
653 pane_name = Fcar (elt);
654 CHECK_STRING (pane_name);
655 push_menu_pane (ENCODE_MENU_STRING (pane_name), Qnil);
656 pane_data = Fcdr (elt);
657 CHECK_CONS (pane_data);
658 list_of_items (pane_data);
659 }
660
661 finish_menu_items ();
662}
663
664/* Push the items in a single pane defined by the alist PANE. */
665
666static void
667list_of_items (pane)
668 Lisp_Object pane;
669{
670 Lisp_Object tail, item, item1;
671
672 for (tail = pane; CONSP (tail); tail = XCDR (tail))
673 {
674 item = XCAR (tail);
675 if (STRINGP (item))
676 push_menu_item (ENCODE_MENU_STRING (item), Qnil, Qnil, Qt,
677 Qnil, Qnil, Qnil, Qnil);
678 else if (CONSP (item))
679 {
680 item1 = XCAR (item);
681 CHECK_STRING (item1);
682 push_menu_item (ENCODE_MENU_STRING (item1), Qt, XCDR (item),
683 Qt, Qnil, Qnil, Qnil, Qnil);
684 }
685 else
686 push_left_right_boundary ();
687
688 }
689}
690\f
691#ifdef HAVE_X_WINDOWS
692/* Return the mouse position in *X and *Y. The coordinates are window
693 relative for the edit window in frame F.
694 This is for Fx_popup_menu. The mouse_position_hook can not
695 be used for X, as it returns window relative coordinates
696 for the window where the mouse is in. This could be the menu bar,
697 the scroll bar or the edit window. Fx_popup_menu needs to be
698 sure it is the edit window. */
699static void
700mouse_position_for_popup (f, x, y)
701 FRAME_PTR f;
702 int *x;
703 int *y;
704{
705 Window root, dummy_window;
706 int dummy;
707
708 if (! FRAME_X_P (f))
709 abort ();
710
711 BLOCK_INPUT;
712
713 XQueryPointer (FRAME_X_DISPLAY (f),
714 DefaultRootWindow (FRAME_X_DISPLAY (f)),
715
716 /* The root window which contains the pointer. */
717 &root,
718
719 /* Window pointer is on, not used */
720 &dummy_window,
721
722 /* The position on that root window. */
723 x, y,
724
725 /* x/y in dummy_window coordinates, not used. */
726 &dummy, &dummy,
727
728 /* Modifier keys and pointer buttons, about which
729 we don't care. */
730 (unsigned int *) &dummy);
731
732 UNBLOCK_INPUT;
733
734 /* xmenu_show expects window coordinates, not root window
735 coordinates. Translate. */
736 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
737 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
738}
739
740#endif /* HAVE_X_WINDOWS */
741
742DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
743 doc: /* Pop up a deck-of-cards menu and return user's selection.
744POSITION is a position specification. This is either a mouse button event
745or a list ((XOFFSET YOFFSET) WINDOW)
746where XOFFSET and YOFFSET are positions in pixels from the top left
747corner of WINDOW. (WINDOW may be a window or a frame object.)
748This controls the position of the top left of the menu as a whole.
749If POSITION is t, it means to use the current mouse position.
750
751MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
752The menu items come from key bindings that have a menu string as well as
753a definition; actually, the "definition" in such a key binding looks like
754\(STRING . REAL-DEFINITION). To give the menu a title, put a string into
755the keymap as a top-level element.
756
757If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
758Otherwise, REAL-DEFINITION should be a valid key binding definition.
759
760You can also use a list of keymaps as MENU.
761 Then each keymap makes a separate pane.
762
763When MENU is a keymap or a list of keymaps, the return value is the
764list of events corresponding to the user's choice. Note that
765`x-popup-menu' does not actually execute the command bound to that
766sequence of events.
767
768Alternatively, you can specify a menu of multiple panes
769 with a list of the form (TITLE PANE1 PANE2...),
770where each pane is a list of form (TITLE ITEM1 ITEM2...).
771Each ITEM is normally a cons cell (STRING . VALUE);
772but a string can appear as an item--that makes a nonselectable line
773in the menu.
774With this form of menu, the return value is VALUE from the chosen item.
775
776If POSITION is nil, don't display the menu at all, just precalculate the
777cached information about equivalent key sequences.
778
779If the user gets rid of the menu without making a valid choice, for
780instance by clicking the mouse away from a valid choice or by typing
781keyboard input, then this normally results in a quit and
782`x-popup-menu' does not return. But if POSITION is a mouse button
783event (indicating that the user invoked the menu with the mouse) then
784no quit occurs and `x-popup-menu' returns nil. */)
785 (position, menu)
786 Lisp_Object position, menu;
787{
788 Lisp_Object keymap, tem;
789 int xpos = 0, ypos = 0;
790 Lisp_Object title;
791 char *error_name = NULL;
792 Lisp_Object selection;
793 FRAME_PTR f = NULL;
794 Lisp_Object x, y, window;
795 int keymaps = 0;
796 int for_click = 0;
797 int specpdl_count = SPECPDL_INDEX ();
798 struct gcpro gcpro1;
799
800#ifdef HAVE_MENUS
801 if (! NILP (position))
802 {
803 int get_current_pos_p = 0;
804 check_x ();
805
806 /* Decode the first argument: find the window and the coordinates. */
807 if (EQ (position, Qt)
808 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
809 || EQ (XCAR (position), Qtool_bar))))
810 {
811 get_current_pos_p = 1;
812 }
813 else
814 {
815 tem = Fcar (position);
816 if (CONSP (tem))
817 {
818 window = Fcar (Fcdr (position));
819 x = XCAR (tem);
820 y = Fcar (XCDR (tem));
821 }
822 else
823 {
824 for_click = 1;
825 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
826 window = Fcar (tem); /* POSN_WINDOW (tem) */
827 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
828 x = Fcar (tem);
829 y = Fcdr (tem);
830 }
831
832 /* If a click happens in an external tool bar or a detached
833 tool bar, x and y is NIL. In that case, use the current
834 mouse position. This happens for the help button in the
835 tool bar. Ideally popup-menu should pass NIL to
836 this function, but it doesn't. */
837 if (NILP (x) && NILP (y))
838 get_current_pos_p = 1;
839 }
840
841 if (get_current_pos_p)
842 {
843 /* Use the mouse's current position. */
844 FRAME_PTR new_f = SELECTED_FRAME ();
845#ifdef HAVE_X_WINDOWS
846 /* Can't use mouse_position_hook for X since it returns
847 coordinates relative to the window the mouse is in,
848 we need coordinates relative to the edit widget always. */
849 if (new_f != 0)
850 {
851 int cur_x, cur_y;
852
853 mouse_position_for_popup (new_f, &cur_x, &cur_y);
854 /* cur_x/y may be negative, so use make_number. */
855 x = make_number (cur_x);
856 y = make_number (cur_y);
857 }
858
859#else /* not HAVE_X_WINDOWS */
860 Lisp_Object bar_window;
861 enum scroll_bar_part part;
862 unsigned long time;
863
864 if (mouse_position_hook)
865 (*mouse_position_hook) (&new_f, 1, &bar_window,
866 &part, &x, &y, &time);
867#endif /* not HAVE_X_WINDOWS */
868
869 if (new_f != 0)
870 XSETFRAME (window, new_f);
871 else
872 {
873 window = selected_window;
874 XSETFASTINT (x, 0);
875 XSETFASTINT (y, 0);
876 }
877 }
878
879 CHECK_NUMBER (x);
880 CHECK_NUMBER (y);
881
882 /* Decode where to put the menu. */
883
884 if (FRAMEP (window))
885 {
886 f = XFRAME (window);
887 xpos = 0;
888 ypos = 0;
889 }
890 else if (WINDOWP (window))
891 {
892 CHECK_LIVE_WINDOW (window);
893 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
894
895 xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window));
896 ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window));
897 }
898 else
899 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
900 but I don't want to make one now. */
901 CHECK_WINDOW (window);
902
903 xpos += XINT (x);
904 ypos += XINT (y);
905
906 if (! FRAME_X_P (f))
907 error ("Can not put X menu on non-X terminal");
908 }
909 Vmenu_updating_frame = Qnil;
910#endif /* HAVE_MENUS */
911
912 record_unwind_protect (unuse_menu_items, Qnil);
913 title = Qnil;
914 GCPRO1 (title);
915
916 /* Decode the menu items from what was specified. */
917
918 keymap = get_keymap (menu, 0, 0);
919 if (CONSP (keymap))
920 {
921 /* We were given a keymap. Extract menu info from the keymap. */
922 Lisp_Object prompt;
923
924 /* Extract the detailed info to make one pane. */
925 keymap_panes (&menu, 1, NILP (position));
926
927 /* Search for a string appearing directly as an element of the keymap.
928 That string is the title of the menu. */
929 prompt = Fkeymap_prompt (keymap);
930 if (NILP (title) && !NILP (prompt))
931 title = prompt;
932
933 /* Make that be the pane title of the first pane. */
934 if (!NILP (prompt) && menu_items_n_panes >= 0)
935 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
936
937 keymaps = 1;
938 }
939 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
940 {
941 /* We were given a list of keymaps. */
942 int nmaps = XFASTINT (Flength (menu));
943 Lisp_Object *maps
944 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
945 int i;
946
947 title = Qnil;
948
949 /* The first keymap that has a prompt string
950 supplies the menu title. */
951 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
952 {
953 Lisp_Object prompt;
954
955 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
956
957 prompt = Fkeymap_prompt (keymap);
958 if (NILP (title) && !NILP (prompt))
959 title = prompt;
960 }
961
962 /* Extract the detailed info to make one pane. */
963 keymap_panes (maps, nmaps, NILP (position));
964
965 /* Make the title be the pane title of the first pane. */
966 if (!NILP (title) && menu_items_n_panes >= 0)
967 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
968
969 keymaps = 1;
970 }
971 else
972 {
973 /* We were given an old-fashioned menu. */
974 title = Fcar (menu);
975 CHECK_STRING (title);
976
977 list_of_panes (Fcdr (menu));
978
979 keymaps = 0;
980 }
981
982 unbind_to (specpdl_count, Qnil);
983
984 if (NILP (position))
985 {
986 discard_menu_items ();
987 UNGCPRO;
988 return Qnil;
989 }
990
991#ifdef HAVE_MENUS
992 /* Display them in a menu. */
993 BLOCK_INPUT;
994
995 selection = xmenu_show (f, xpos, ypos, for_click,
996 keymaps, title, &error_name);
997 UNBLOCK_INPUT;
998
999 discard_menu_items ();
1000
1001 UNGCPRO;
1002#endif /* HAVE_MENUS */
1003
1004 if (error_name) error (error_name);
1005 return selection;
1006}
1007
1008#ifdef HAVE_MENUS
1009
1010DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
1011 doc: /* Pop up a dialog box and return user's selection.
1012POSITION specifies which frame to use.
1013This is normally a mouse button event or a window or frame.
1014If POSITION is t, it means to use the frame the mouse is on.
1015The dialog box appears in the middle of the specified frame.
1016
1017CONTENTS specifies the alternatives to display in the dialog box.
1018It is a list of the form (DIALOG ITEM1 ITEM2...).
1019Each ITEM is a cons cell (STRING . VALUE).
1020The return value is VALUE from the chosen item.
1021
1022An ITEM may also be just a string--that makes a nonselectable item.
1023An ITEM may also be nil--that means to put all preceding items
1024on the left of the dialog box and all following items on the right.
1025\(By default, approximately half appear on each side.)
1026
1027If HEADER is non-nil, the frame title for the box is "Information",
1028otherwise it is "Question".
1029
1030If the user gets rid of the dialog box without making a valid choice,
1031for instance using the window manager, then this produces a quit and
1032`x-popup-dialog' does not return. */)
1033 (position, contents, header)
1034 Lisp_Object position, contents, header;
1035{
1036 FRAME_PTR f = NULL;
1037 Lisp_Object window;
1038
1039 check_x ();
1040
1041 /* Decode the first argument: find the window or frame to use. */
1042 if (EQ (position, Qt)
1043 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
1044 || EQ (XCAR (position), Qtool_bar))))
1045 {
1046#if 0 /* Using the frame the mouse is on may not be right. */
1047 /* Use the mouse's current position. */
1048 FRAME_PTR new_f = SELECTED_FRAME ();
1049 Lisp_Object bar_window;
1050 enum scroll_bar_part part;
1051 unsigned long time;
1052 Lisp_Object x, y;
1053
1054 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
1055
1056 if (new_f != 0)
1057 XSETFRAME (window, new_f);
1058 else
1059 window = selected_window;
1060#endif
1061 window = selected_window;
1062 }
1063 else if (CONSP (position))
1064 {
1065 Lisp_Object tem;
1066 tem = Fcar (position);
1067 if (CONSP (tem))
1068 window = Fcar (Fcdr (position));
1069 else
1070 {
1071 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
1072 window = Fcar (tem); /* POSN_WINDOW (tem) */
1073 }
1074 }
1075 else if (WINDOWP (position) || FRAMEP (position))
1076 window = position;
1077 else
1078 window = Qnil;
1079
1080 /* Decode where to put the menu. */
1081
1082 if (FRAMEP (window))
1083 f = XFRAME (window);
1084 else if (WINDOWP (window))
1085 {
1086 CHECK_LIVE_WINDOW (window);
1087 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1088 }
1089 else
1090 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
1091 but I don't want to make one now. */
1092 CHECK_WINDOW (window);
1093
1094 if (! FRAME_X_P (f))
1095 error ("Can not put X dialog on non-X terminal");
1096
1097#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1098 /* Display a menu with these alternatives
1099 in the middle of frame F. */
1100 {
1101 Lisp_Object x, y, frame, newpos;
1102 XSETFRAME (frame, f);
1103 XSETINT (x, x_pixel_width (f) / 2);
1104 XSETINT (y, x_pixel_height (f) / 2);
1105 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
1106
1107 return Fx_popup_menu (newpos,
1108 Fcons (Fcar (contents), Fcons (contents, Qnil)));
1109 }
1110#else
1111 {
1112 Lisp_Object title;
1113 char *error_name;
1114 Lisp_Object selection;
1115 int specpdl_count = SPECPDL_INDEX ();
1116
1117 /* Decode the dialog items from what was specified. */
1118 title = Fcar (contents);
1119 CHECK_STRING (title);
1120 record_unwind_protect (unuse_menu_items, Qnil);
1121
1122 if (NILP (Fcar (Fcdr (contents))))
1123 /* No buttons specified, add an "Ok" button so users can pop down
1124 the dialog. Also, the lesstif/motif version crashes if there are
1125 no buttons. */
1126 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
1127
1128 list_of_panes (Fcons (contents, Qnil));
1129
1130 /* Display them in a dialog box. */
1131 BLOCK_INPUT;
1132 selection = xdialog_show (f, 0, title, header, &error_name);
1133 UNBLOCK_INPUT;
1134
1135 unbind_to (specpdl_count, Qnil);
1136 discard_menu_items ();
1137
1138 if (error_name) error (error_name);
1139 return selection;
1140 }
1141#endif
1142}
1143
1144
1145#ifndef MSDOS
1146
1147/* Set menu_items_inuse so no other popup menu or dialog is created. */
1148
1149void
1150x_menu_set_in_use (in_use)
1151 int in_use;
1152{
1153 menu_items_inuse = in_use ? Qt : Qnil;
1154 popup_activated_flag = in_use;
1155}
1156
1157/* Wait for an X event to arrive or for a timer to expire. */
1158
1159void
1160x_menu_wait_for_event (void *data)
1161{
1162 extern EMACS_TIME timer_check P_ ((int));
1163
1164 /* Another way to do this is to register a timer callback, that can be
1165 done in GTK and Xt. But we have to do it like this when using only X
1166 anyway, and with callbacks we would have three variants for timer handling
1167 instead of the small ifdefs below. */
1168
1169 while (
1170#ifdef USE_X_TOOLKIT
1171 ! XtAppPending (Xt_app_con)
1172#elif defined USE_GTK
1173 ! gtk_events_pending ()
1174#else
1175 ! XPending ((Display*) data)
1176#endif
1177 )
1178 {
1179 EMACS_TIME next_time = timer_check (1);
1180 long secs = EMACS_SECS (next_time);
1181 long usecs = EMACS_USECS (next_time);
1182 SELECT_TYPE read_fds;
1183 struct x_display_info *dpyinfo;
1184 int n = 0;
1185
1186 FD_ZERO (&read_fds);
1187 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
1188 {
1189 int fd = ConnectionNumber (dpyinfo->display);
1190 FD_SET (fd, &read_fds);
1191 if (fd > n) n = fd;
1192 }
1193
1194 if (secs < 0 || (secs == 0 && usecs == 0))
1195 {
1196 /* Sometimes timer_check returns -1 (no timers) even if there are
1197 timers. So do a timeout anyway. */
1198 EMACS_SET_SECS (next_time, 1);
1199 EMACS_SET_USECS (next_time, 0);
1200 }
1201
1202 select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, &next_time);
1203 }
1204}
1205#endif /* ! MSDOS */
1206
1207\f
1208#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1209
1210#ifdef USE_X_TOOLKIT
1211
1212/* Loop in Xt until the menu pulldown or dialog popup has been
1213 popped down (deactivated). This is used for x-popup-menu
1214 and x-popup-dialog; it is not used for the menu bar.
1215
1216 NOTE: All calls to popup_get_selection should be protected
1217 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1218
1219static void
1220popup_get_selection (initial_event, dpyinfo, id, do_timers)
1221 XEvent *initial_event;
1222 struct x_display_info *dpyinfo;
1223 LWLIB_ID id;
1224 int do_timers;
1225{
1226 XEvent event;
1227
1228 while (popup_activated_flag)
1229 {
1230 if (initial_event)
1231 {
1232 event = *initial_event;
1233 initial_event = 0;
1234 }
1235 else
1236 {
1237 if (do_timers) x_menu_wait_for_event (0);
1238 XtAppNextEvent (Xt_app_con, &event);
1239 }
1240
1241 /* Make sure we don't consider buttons grabbed after menu goes.
1242 And make sure to deactivate for any ButtonRelease,
1243 even if XtDispatchEvent doesn't do that. */
1244 if (event.type == ButtonRelease
1245 && dpyinfo->display == event.xbutton.display)
1246 {
1247 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
1248#ifdef USE_MOTIF /* Pretending that the event came from a
1249 Btn1Down seems the only way to convince Motif to
1250 activate its callbacks; setting the XmNmenuPost
1251 isn't working. --marcus@sysc.pdx.edu. */
1252 event.xbutton.button = 1;
1253 /* Motif only pops down menus when no Ctrl, Alt or Mod
1254 key is pressed and the button is released. So reset key state
1255 so Motif thinks this is the case. */
1256 event.xbutton.state = 0;
1257#endif
1258 }
1259 /* Pop down on C-g and Escape. */
1260 else if (event.type == KeyPress
1261 && dpyinfo->display == event.xbutton.display)
1262 {
1263 KeySym keysym = XLookupKeysym (&event.xkey, 0);
1264
1265 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
1266 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
1267 popup_activated_flag = 0;
1268 }
1269
1270 x_dispatch_event (&event, event.xany.display);
1271 }
1272}
1273
1274#endif /* USE_X_TOOLKIT */
1275
1276#ifdef USE_GTK
1277/* Loop util popup_activated_flag is set to zero in a callback.
1278 Used for popup menus and dialogs. */
1279
1280static void
1281popup_widget_loop (do_timers, widget)
1282 int do_timers;
1283 GtkWidget *widget;
1284{
1285 ++popup_activated_flag;
1286
1287 /* Process events in the Gtk event loop until done. */
1288 while (popup_activated_flag)
1289 {
1290 if (do_timers) x_menu_wait_for_event (0);
1291 gtk_main_iteration ();
1292 }
1293}
1294#endif
1295
1296/* Activate the menu bar of frame F.
1297 This is called from keyboard.c when it gets the
1298 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
1299
1300 To activate the menu bar, we use the X button-press event
1301 that was saved in saved_menu_event.
1302 That makes the toolkit do its thing.
1303
1304 But first we recompute the menu bar contents (the whole tree).
1305
1306 The reason for saving the button event until here, instead of
1307 passing it to the toolkit right away, is that we can safely
1308 execute Lisp code. */
1309
1310void
1311x_activate_menubar (f)
1312 FRAME_PTR f;
1313{
1314 if (! FRAME_X_P (f))
1315 abort ();
1316
1317 if (!f->output_data.x->saved_menu_event->type)
1318 return;
1319
1320#ifdef USE_GTK
1321 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
1322 f->output_data.x->saved_menu_event->xany.window))
1323 return;
1324#endif
1325
1326 set_frame_menubar (f, 0, 1);
1327 BLOCK_INPUT;
1328#ifdef USE_GTK
1329 XPutBackEvent (f->output_data.x->display_info->display,
1330 f->output_data.x->saved_menu_event);
1331 popup_activated_flag = 1;
1332#else
1333 XtDispatchEvent (f->output_data.x->saved_menu_event);
1334#endif
1335 UNBLOCK_INPUT;
1336#ifdef USE_MOTIF
1337 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
1338 pending_menu_activation = 1;
1339#endif
1340
1341 /* Ignore this if we get it a second time. */
1342 f->output_data.x->saved_menu_event->type = 0;
1343}
1344
1345/* Detect if a dialog or menu has been posted. */
1346
1347int
1348popup_activated ()
1349{
1350 return popup_activated_flag;
1351}
1352
1353/* This callback is invoked when the user selects a menubar cascade
1354 pushbutton, but before the pulldown menu is posted. */
1355
1356#ifndef USE_GTK
1357static void
1358popup_activate_callback (widget, id, client_data)
1359 Widget widget;
1360 LWLIB_ID id;
1361 XtPointer client_data;
1362{
1363 popup_activated_flag = 1;
1364}
1365#endif
1366
1367/* This callback is invoked when a dialog or menu is finished being
1368 used and has been unposted. */
1369
1370#ifdef USE_GTK
1371static void
1372popup_deactivate_callback (widget, client_data)
1373 GtkWidget *widget;
1374 gpointer client_data;
1375{
1376 popup_activated_flag = 0;
1377}
1378#else
1379static void
1380popup_deactivate_callback (widget, id, client_data)
1381 Widget widget;
1382 LWLIB_ID id;
1383 XtPointer client_data;
1384{
1385 popup_activated_flag = 0;
1386}
1387#endif
1388
1389
1390/* Function that finds the frame for WIDGET and shows the HELP text
1391 for that widget.
1392 F is the frame if known, or NULL if not known. */
1393static void
1394show_help_event (f, widget, help)
1395 FRAME_PTR f;
1396 xt_or_gtk_widget widget;
1397 Lisp_Object help;
1398{
1399 Lisp_Object frame;
1400
1401 if (f)
1402 {
1403 XSETFRAME (frame, f);
1404 kbd_buffer_store_help_event (frame, help);
1405 }
1406 else
1407 {
1408#if 0 /* This code doesn't do anything useful. ++kfs */
1409 /* WIDGET is the popup menu. It's parent is the frame's
1410 widget. See which frame that is. */
1411 xt_or_gtk_widget frame_widget = XtParent (widget);
1412 Lisp_Object tail;
1413
1414 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1415 {
1416 frame = XCAR (tail);
1417 if (GC_FRAMEP (frame)
1418 && (f = XFRAME (frame),
1419 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1420 break;
1421 }
1422#endif
1423 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1424 }
1425}
1426
1427/* Callback called when menu items are highlighted/unhighlighted
1428 while moving the mouse over them. WIDGET is the menu bar or menu
1429 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
1430 the data structure for the menu item, or null in case of
1431 unhighlighting. */
1432
1433#ifdef USE_GTK
1434void
1435menu_highlight_callback (widget, call_data)
1436 GtkWidget *widget;
1437 gpointer call_data;
1438{
1439 xg_menu_item_cb_data *cb_data;
1440 Lisp_Object help;
1441
1442 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
1443 XG_ITEM_DATA);
1444 if (! cb_data) return;
1445
1446 help = call_data ? cb_data->help : Qnil;
1447
1448 /* If popup_activated_flag is greater than 1 we are in a popup menu.
1449 Don't show help for them, they won't appear before the
1450 popup is popped down. */
1451 if (popup_activated_flag <= 1)
1452 show_help_event (cb_data->cl_data->f, widget, help);
1453}
1454#else
1455void
1456menu_highlight_callback (widget, id, call_data)
1457 Widget widget;
1458 LWLIB_ID id;
1459 void *call_data;
1460{
1461 struct frame *f;
1462 Lisp_Object help;
1463
1464 widget_value *wv = (widget_value *) call_data;
1465
1466 help = wv ? wv->help : Qnil;
1467
1468 /* Determine the frame for the help event. */
1469 f = menubar_id_to_frame (id);
1470
1471 show_help_event (f, widget, help);
1472}
1473#endif
1474
1475/* Find the menu selection and store it in the keyboard buffer.
1476 F is the frame the menu is on.
1477 MENU_BAR_ITEMS_USED is the length of VECTOR.
1478 VECTOR is an array of menu events for the whole menu. */
1479
1480static void
1481find_and_call_menu_selection (f, menu_bar_items_used, vector, client_data)
1482 FRAME_PTR f;
1483 int menu_bar_items_used;
1484 Lisp_Object vector;
1485 void *client_data;
1486{
1487 Lisp_Object prefix, entry;
1488 Lisp_Object *subprefix_stack;
1489 int submenu_depth = 0;
1490 int i;
1491
1492 entry = Qnil;
1493 subprefix_stack = (Lisp_Object *) alloca (menu_bar_items_used * sizeof (Lisp_Object));
1494 prefix = Qnil;
1495 i = 0;
1496
1497 while (i < menu_bar_items_used)
1498 {
1499 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1500 {
1501 subprefix_stack[submenu_depth++] = prefix;
1502 prefix = entry;
1503 i++;
1504 }
1505 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1506 {
1507 prefix = subprefix_stack[--submenu_depth];
1508 i++;
1509 }
1510 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1511 {
1512 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1513 i += MENU_ITEMS_PANE_LENGTH;
1514 }
1515 else
1516 {
1517 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1518 /* The EMACS_INT cast avoids a warning. There's no problem
1519 as long as pointers have enough bits to hold small integers. */
1520 if ((int) (EMACS_INT) client_data == i)
1521 {
1522 int j;
1523 struct input_event buf;
1524 Lisp_Object frame;
1525 EVENT_INIT (buf);
1526
1527 XSETFRAME (frame, f);
1528 buf.kind = MENU_BAR_EVENT;
1529 buf.frame_or_window = frame;
1530 buf.arg = frame;
1531 kbd_buffer_store_event (&buf);
1532
1533 for (j = 0; j < submenu_depth; j++)
1534 if (!NILP (subprefix_stack[j]))
1535 {
1536 buf.kind = MENU_BAR_EVENT;
1537 buf.frame_or_window = frame;
1538 buf.arg = subprefix_stack[j];
1539 kbd_buffer_store_event (&buf);
1540 }
1541
1542 if (!NILP (prefix))
1543 {
1544 buf.kind = MENU_BAR_EVENT;
1545 buf.frame_or_window = frame;
1546 buf.arg = prefix;
1547 kbd_buffer_store_event (&buf);
1548 }
1549
1550 buf.kind = MENU_BAR_EVENT;
1551 buf.frame_or_window = frame;
1552 buf.arg = entry;
1553 kbd_buffer_store_event (&buf);
1554
1555 return;
1556 }
1557 i += MENU_ITEMS_ITEM_LENGTH;
1558 }
1559 }
1560}
1561
1562
1563#ifdef USE_GTK
1564/* Gtk calls callbacks just because we tell it what item should be
1565 selected in a radio group. If this variable is set to a non-zero
1566 value, we are creating menus and don't want callbacks right now.
1567*/
1568static int xg_crazy_callback_abort;
1569
1570/* This callback is called from the menu bar pulldown menu
1571 when the user makes a selection.
1572 Figure out what the user chose
1573 and put the appropriate events into the keyboard buffer. */
1574static void
1575menubar_selection_callback (widget, client_data)
1576 GtkWidget *widget;
1577 gpointer client_data;
1578{
1579 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1580
1581 if (xg_crazy_callback_abort)
1582 return;
1583
1584 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
1585 return;
1586
1587 /* For a group of radio buttons, GTK calls the selection callback first
1588 for the item that was active before the selection and then for the one that
1589 is active after the selection. For C-h k this means we get the help on
1590 the deselected item and then the selected item is executed. Prevent that
1591 by ignoring the non-active item. */
1592 if (GTK_IS_RADIO_MENU_ITEM (widget)
1593 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
1594 return;
1595
1596 /* When a menu is popped down, X generates a focus event (i.e. focus
1597 goes back to the frame below the menu). Since GTK buffers events,
1598 we force it out here before the menu selection event. Otherwise
1599 sit-for will exit at once if the focus event follows the menu selection
1600 event. */
1601
1602 BLOCK_INPUT;
1603 while (gtk_events_pending ())
1604 gtk_main_iteration ();
1605 UNBLOCK_INPUT;
1606
1607 find_and_call_menu_selection (cb_data->cl_data->f,
1608 cb_data->cl_data->menu_bar_items_used,
1609 cb_data->cl_data->menu_bar_vector,
1610 cb_data->call_data);
1611}
1612
1613#else /* not USE_GTK */
1614
1615/* This callback is called from the menu bar pulldown menu
1616 when the user makes a selection.
1617 Figure out what the user chose
1618 and put the appropriate events into the keyboard buffer. */
1619static void
1620menubar_selection_callback (widget, id, client_data)
1621 Widget widget;
1622 LWLIB_ID id;
1623 XtPointer client_data;
1624{
1625 FRAME_PTR f;
1626
1627 f = menubar_id_to_frame (id);
1628 if (!f)
1629 return;
1630 find_and_call_menu_selection (f, f->menu_bar_items_used,
1631 f->menu_bar_vector, client_data);
1632}
1633#endif /* not USE_GTK */
1634
1635/* Allocate a widget_value, blocking input. */
1636
1637widget_value *
1638xmalloc_widget_value ()
1639{
1640 widget_value *value;
1641
1642 BLOCK_INPUT;
1643 value = malloc_widget_value ();
1644 UNBLOCK_INPUT;
1645
1646 return value;
1647}
1648
1649/* This recursively calls free_widget_value on the tree of widgets.
1650 It must free all data that was malloc'ed for these widget_values.
1651 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1652 must be left alone. */
1653
1654void
1655free_menubar_widget_value_tree (wv)
1656 widget_value *wv;
1657{
1658 if (! wv) return;
1659
1660 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1661
1662 if (wv->contents && (wv->contents != (widget_value*)1))
1663 {
1664 free_menubar_widget_value_tree (wv->contents);
1665 wv->contents = (widget_value *) 0xDEADBEEF;
1666 }
1667 if (wv->next)
1668 {
1669 free_menubar_widget_value_tree (wv->next);
1670 wv->next = (widget_value *) 0xDEADBEEF;
1671 }
1672 BLOCK_INPUT;
1673 free_widget_value (wv);
1674 UNBLOCK_INPUT;
1675}
1676\f
1677/* Set up data in menu_items for a menu bar item
1678 whose event type is ITEM_KEY (with string ITEM_NAME)
1679 and whose contents come from the list of keymaps MAPS. */
1680
1681static int
1682parse_single_submenu (item_key, item_name, maps)
1683 Lisp_Object item_key, item_name, maps;
1684{
1685 Lisp_Object length;
1686 int len;
1687 Lisp_Object *mapvec;
1688 int i;
1689 int top_level_items = 0;
1690
1691 length = Flength (maps);
1692 len = XINT (length);
1693
1694 /* Convert the list MAPS into a vector MAPVEC. */
1695 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1696 for (i = 0; i < len; i++)
1697 {
1698 mapvec[i] = Fcar (maps);
1699 maps = Fcdr (maps);
1700 }
1701
1702 /* Loop over the given keymaps, making a pane for each map.
1703 But don't make a pane that is empty--ignore that map instead. */
1704 for (i = 0; i < len; i++)
1705 {
1706 if (!KEYMAPP (mapvec[i]))
1707 {
1708 /* Here we have a command at top level in the menu bar
1709 as opposed to a submenu. */
1710 top_level_items = 1;
1711 push_menu_pane (Qnil, Qnil);
1712 push_menu_item (item_name, Qt, item_key, mapvec[i],
1713 Qnil, Qnil, Qnil, Qnil);
1714 }
1715 else
1716 {
1717 Lisp_Object prompt;
1718 prompt = Fkeymap_prompt (mapvec[i]);
1719 single_keymap_panes (mapvec[i],
1720 !NILP (prompt) ? prompt : item_name,
1721 item_key, 0, 10);
1722 }
1723 }
1724
1725 return top_level_items;
1726}
1727
1728/* Create a tree of widget_value objects
1729 representing the panes and items
1730 in menu_items starting at index START, up to index END. */
1731
1732static widget_value *
1733digest_single_submenu (start, end, top_level_items)
1734 int start, end, top_level_items;
1735{
1736 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1737 int i;
1738 int submenu_depth = 0;
1739 widget_value **submenu_stack;
1740
1741 submenu_stack
1742 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1743 wv = xmalloc_widget_value ();
1744 wv->name = "menu";
1745 wv->value = 0;
1746 wv->enabled = 1;
1747 wv->button_type = BUTTON_TYPE_NONE;
1748 wv->help = Qnil;
1749 first_wv = wv;
1750 save_wv = 0;
1751 prev_wv = 0;
1752
1753 /* Loop over all panes and items made by the preceding call
1754 to parse_single_submenu and construct a tree of widget_value objects.
1755 Ignore the panes and items used by previous calls to
1756 digest_single_submenu, even though those are also in menu_items. */
1757 i = start;
1758 while (i < end)
1759 {
1760 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1761 {
1762 submenu_stack[submenu_depth++] = save_wv;
1763 save_wv = prev_wv;
1764 prev_wv = 0;
1765 i++;
1766 }
1767 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1768 {
1769 prev_wv = save_wv;
1770 save_wv = submenu_stack[--submenu_depth];
1771 i++;
1772 }
1773 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1774 && submenu_depth != 0)
1775 i += MENU_ITEMS_PANE_LENGTH;
1776 /* Ignore a nil in the item list.
1777 It's meaningful only for dialog boxes. */
1778 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1779 i += 1;
1780 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1781 {
1782 /* Create a new pane. */
1783 Lisp_Object pane_name, prefix;
1784 char *pane_string;
1785
1786 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1787 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1788
1789#ifndef HAVE_MULTILINGUAL_MENU
1790 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1791 {
1792 pane_name = ENCODE_MENU_STRING (pane_name);
1793 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
1794 }
1795#endif
1796 pane_string = (NILP (pane_name)
1797 ? "" : (char *) SDATA (pane_name));
1798 /* If there is just one top-level pane, put all its items directly
1799 under the top-level menu. */
1800 if (menu_items_n_panes == 1)
1801 pane_string = "";
1802
1803 /* If the pane has a meaningful name,
1804 make the pane a top-level menu item
1805 with its items as a submenu beneath it. */
1806 if (strcmp (pane_string, ""))
1807 {
1808 wv = xmalloc_widget_value ();
1809 if (save_wv)
1810 save_wv->next = wv;
1811 else
1812 first_wv->contents = wv;
1813 wv->lname = pane_name;
1814 /* Set value to 1 so update_submenu_strings can handle '@' */
1815 wv->value = (char *)1;
1816 wv->enabled = 1;
1817 wv->button_type = BUTTON_TYPE_NONE;
1818 wv->help = Qnil;
1819 }
1820 save_wv = wv;
1821 prev_wv = 0;
1822 i += MENU_ITEMS_PANE_LENGTH;
1823 }
1824 else
1825 {
1826 /* Create a new item within current pane. */
1827 Lisp_Object item_name, enable, descrip, def, type, selected;
1828 Lisp_Object help;
1829
1830 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1831 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1832 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1833 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1834 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1835 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1836 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1837
1838#ifndef HAVE_MULTILINGUAL_MENU
1839 if (STRING_MULTIBYTE (item_name))
1840 {
1841 item_name = ENCODE_MENU_STRING (item_name);
1842 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
1843 }
1844
1845 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1846 {
1847 descrip = ENCODE_MENU_STRING (descrip);
1848 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
1849 }
1850#endif /* not HAVE_MULTILINGUAL_MENU */
1851
1852 wv = xmalloc_widget_value ();
1853 if (prev_wv)
1854 prev_wv->next = wv;
1855 else
1856 save_wv->contents = wv;
1857
1858 wv->lname = item_name;
1859 if (!NILP (descrip))
1860 wv->lkey = descrip;
1861 wv->value = 0;
1862 /* The EMACS_INT cast avoids a warning. There's no problem
1863 as long as pointers have enough bits to hold small integers. */
1864 wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0);
1865 wv->enabled = !NILP (enable);
1866
1867 if (NILP (type))
1868 wv->button_type = BUTTON_TYPE_NONE;
1869 else if (EQ (type, QCradio))
1870 wv->button_type = BUTTON_TYPE_RADIO;
1871 else if (EQ (type, QCtoggle))
1872 wv->button_type = BUTTON_TYPE_TOGGLE;
1873 else
1874 abort ();
1875
1876 wv->selected = !NILP (selected);
1877 if (! STRINGP (help))
1878 help = Qnil;
1879
1880 wv->help = help;
1881
1882 prev_wv = wv;
1883
1884 i += MENU_ITEMS_ITEM_LENGTH;
1885 }
1886 }
1887
1888 /* If we have just one "menu item"
1889 that was originally a button, return it by itself. */
1890 if (top_level_items && first_wv->contents && first_wv->contents->next == 0)
1891 {
1892 wv = first_wv->contents;
1893 free_widget_value (first_wv);
1894 return wv;
1895 }
1896
1897 return first_wv;
1898}
1899
1900/* Walk through the widget_value tree starting at FIRST_WV and update
1901 the char * pointers from the corresponding lisp values.
1902 We do this after building the whole tree, since GC may happen while the
1903 tree is constructed, and small strings are relocated. So we must wait
1904 until no GC can happen before storing pointers into lisp values. */
1905static void
1906update_submenu_strings (first_wv)
1907 widget_value *first_wv;
1908{
1909 widget_value *wv;
1910
1911 for (wv = first_wv; wv; wv = wv->next)
1912 {
1913 if (STRINGP (wv->lname))
1914 {
1915 wv->name = (char *) SDATA (wv->lname);
1916
1917 /* Ignore the @ that means "separate pane".
1918 This is a kludge, but this isn't worth more time. */
1919 if (wv->value == (char *)1)
1920 {
1921 if (wv->name[0] == '@')
1922 wv->name++;
1923 wv->value = 0;
1924 }
1925 }
1926
1927 if (STRINGP (wv->lkey))
1928 wv->key = (char *) SDATA (wv->lkey);
1929
1930 if (wv->contents)
1931 update_submenu_strings (wv->contents);
1932 }
1933}
1934
1935\f
1936/* Recompute all the widgets of frame F, when the menu bar has been
1937 changed. Value is non-zero if widgets were updated. */
1938
1939static int
1940update_frame_menubar (f)
1941 FRAME_PTR f;
1942{
1943#ifdef USE_GTK
1944 return xg_update_frame_menubar (f);
1945#else
1946 struct x_output *x;
1947 int columns, rows;
1948
1949 if (! FRAME_X_P (f))
1950 abort ();
1951
1952 x = f->output_data.x;
1953
1954 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
1955 return 0;
1956
1957 BLOCK_INPUT;
1958 /* Save the size of the frame because the pane widget doesn't accept
1959 to resize itself. So force it. */
1960 columns = FRAME_COLS (f);
1961 rows = FRAME_LINES (f);
1962
1963 /* Do the voodoo which means "I'm changing lots of things, don't try
1964 to refigure sizes until I'm done." */
1965 lw_refigure_widget (x->column_widget, False);
1966
1967 /* The order in which children are managed is the top to bottom
1968 order in which they are displayed in the paned window. First,
1969 remove the text-area widget. */
1970 XtUnmanageChild (x->edit_widget);
1971
1972 /* Remove the menubar that is there now, and put up the menubar that
1973 should be there. */
1974 XtManageChild (x->menubar_widget);
1975 XtMapWidget (x->menubar_widget);
1976 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
1977
1978 /* Re-manage the text-area widget, and then thrash the sizes. */
1979 XtManageChild (x->edit_widget);
1980 lw_refigure_widget (x->column_widget, True);
1981
1982 /* Force the pane widget to resize itself with the right values. */
1983 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1984 UNBLOCK_INPUT;
1985#endif
1986 return 1;
1987}
1988
1989/* Set the contents of the menubar widgets of frame F.
1990 The argument FIRST_TIME is currently ignored;
1991 it is set the first time this is called, from initialize_frame_menubar. */
1992
1993void
1994set_frame_menubar (f, first_time, deep_p)
1995 FRAME_PTR f;
1996 int first_time;
1997 int deep_p;
1998{
1999 xt_or_gtk_widget menubar_widget;
2000#ifdef USE_X_TOOLKIT
2001 LWLIB_ID id;
2002#endif
2003 Lisp_Object items;
2004 widget_value *wv, *first_wv, *prev_wv = 0;
2005 int i, last_i = 0;
2006 int *submenu_start, *submenu_end;
2007 int *submenu_top_level_items, *submenu_n_panes;
2008
2009 if (! FRAME_X_P (f))
2010 abort ();
2011
2012 menubar_widget = f->output_data.x->menubar_widget;
2013
2014 XSETFRAME (Vmenu_updating_frame, f);
2015
2016#ifdef USE_X_TOOLKIT
2017 if (f->output_data.x->id == 0)
2018 f->output_data.x->id = next_menubar_widget_id++;
2019 id = f->output_data.x->id;
2020#endif
2021
2022 if (! menubar_widget)
2023 deep_p = 1;
2024 else if (pending_menu_activation && !deep_p)
2025 deep_p = 1;
2026 /* Make the first call for any given frame always go deep. */
2027 else if (!f->output_data.x->saved_menu_event && !deep_p)
2028 {
2029 deep_p = 1;
2030 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
2031 f->output_data.x->saved_menu_event->type = 0;
2032 }
2033
2034#ifdef USE_GTK
2035 /* If we have detached menus, we must update deep so detached menus
2036 also gets updated. */
2037 deep_p = deep_p || xg_have_tear_offs ();
2038#endif
2039
2040 if (deep_p)
2041 {
2042 /* Make a widget-value tree representing the entire menu trees. */
2043
2044 struct buffer *prev = current_buffer;
2045 Lisp_Object buffer;
2046 int specpdl_count = SPECPDL_INDEX ();
2047 int previous_menu_items_used = f->menu_bar_items_used;
2048 Lisp_Object *previous_items
2049 = (Lisp_Object *) alloca (previous_menu_items_used
2050 * sizeof (Lisp_Object));
2051
2052 /* If we are making a new widget, its contents are empty,
2053 do always reinitialize them. */
2054 if (! menubar_widget)
2055 previous_menu_items_used = 0;
2056
2057 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
2058 specbind (Qinhibit_quit, Qt);
2059 /* Don't let the debugger step into this code
2060 because it is not reentrant. */
2061 specbind (Qdebug_on_next_call, Qnil);
2062
2063 record_unwind_save_match_data ();
2064 record_unwind_protect (unuse_menu_items, Qnil);
2065 if (NILP (Voverriding_local_map_menu_flag))
2066 {
2067 specbind (Qoverriding_terminal_local_map, Qnil);
2068 specbind (Qoverriding_local_map, Qnil);
2069 }
2070
2071 set_buffer_internal_1 (XBUFFER (buffer));
2072
2073 /* Run the Lucid hook. */
2074 safe_run_hooks (Qactivate_menubar_hook);
2075
2076 /* If it has changed current-menubar from previous value,
2077 really recompute the menubar from the value. */
2078 if (! NILP (Vlucid_menu_bar_dirty_flag))
2079 call0 (Qrecompute_lucid_menubar);
2080 safe_run_hooks (Qmenu_bar_update_hook);
2081 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2082
2083 items = FRAME_MENU_BAR_ITEMS (f);
2084
2085 /* Save the frame's previous menu bar contents data. */
2086 if (previous_menu_items_used)
2087 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
2088 previous_menu_items_used * sizeof (Lisp_Object));
2089
2090 /* Fill in menu_items with the current menu bar contents.
2091 This can evaluate Lisp code. */
2092 menu_items = f->menu_bar_vector;
2093 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
2094 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2095 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2096 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
2097 submenu_top_level_items
2098 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
2099 init_menu_items ();
2100 for (i = 0; i < XVECTOR (items)->size; i += 4)
2101 {
2102 Lisp_Object key, string, maps;
2103
2104 last_i = i;
2105
2106 key = XVECTOR (items)->contents[i];
2107 string = XVECTOR (items)->contents[i + 1];
2108 maps = XVECTOR (items)->contents[i + 2];
2109 if (NILP (string))
2110 break;
2111
2112 submenu_start[i] = menu_items_used;
2113
2114 menu_items_n_panes = 0;
2115 submenu_top_level_items[i]
2116 = parse_single_submenu (key, string, maps);
2117 submenu_n_panes[i] = menu_items_n_panes;
2118
2119 submenu_end[i] = menu_items_used;
2120 }
2121
2122 finish_menu_items ();
2123
2124 /* Convert menu_items into widget_value trees
2125 to display the menu. This cannot evaluate Lisp code. */
2126
2127 wv = xmalloc_widget_value ();
2128 wv->name = "menubar";
2129 wv->value = 0;
2130 wv->enabled = 1;
2131 wv->button_type = BUTTON_TYPE_NONE;
2132 wv->help = Qnil;
2133 first_wv = wv;
2134
2135 for (i = 0; i < last_i; i += 4)
2136 {
2137 menu_items_n_panes = submenu_n_panes[i];
2138 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
2139 submenu_top_level_items[i]);
2140 if (prev_wv)
2141 prev_wv->next = wv;
2142 else
2143 first_wv->contents = wv;
2144 /* Don't set wv->name here; GC during the loop might relocate it. */
2145 wv->enabled = 1;
2146 wv->button_type = BUTTON_TYPE_NONE;
2147 prev_wv = wv;
2148 }
2149
2150 set_buffer_internal_1 (prev);
2151 unbind_to (specpdl_count, Qnil);
2152
2153 /* If there has been no change in the Lisp-level contents
2154 of the menu bar, skip redisplaying it. Just exit. */
2155
2156 for (i = 0; i < previous_menu_items_used; i++)
2157 if (menu_items_used == i
2158 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
2159 break;
2160 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
2161 {
2162 free_menubar_widget_value_tree (first_wv);
2163 discard_menu_items ();
2164
2165 return;
2166 }
2167
2168 /* Now GC cannot happen during the lifetime of the widget_value,
2169 so it's safe to store data from a Lisp_String. */
2170 wv = first_wv->contents;
2171 for (i = 0; i < XVECTOR (items)->size; i += 4)
2172 {
2173 Lisp_Object string;
2174 string = XVECTOR (items)->contents[i + 1];
2175 if (NILP (string))
2176 break;
2177 wv->name = (char *) SDATA (string);
2178 update_submenu_strings (wv->contents);
2179 wv = wv->next;
2180 }
2181
2182 f->menu_bar_vector = menu_items;
2183 f->menu_bar_items_used = menu_items_used;
2184 discard_menu_items ();
2185 }
2186 else
2187 {
2188 /* Make a widget-value tree containing
2189 just the top level menu bar strings. */
2190
2191 wv = xmalloc_widget_value ();
2192 wv->name = "menubar";
2193 wv->value = 0;
2194 wv->enabled = 1;
2195 wv->button_type = BUTTON_TYPE_NONE;
2196 wv->help = Qnil;
2197 first_wv = wv;
2198
2199 items = FRAME_MENU_BAR_ITEMS (f);
2200 for (i = 0; i < XVECTOR (items)->size; i += 4)
2201 {
2202 Lisp_Object string;
2203
2204 string = XVECTOR (items)->contents[i + 1];
2205 if (NILP (string))
2206 break;
2207
2208 wv = xmalloc_widget_value ();
2209 wv->name = (char *) SDATA (string);
2210 wv->value = 0;
2211 wv->enabled = 1;
2212 wv->button_type = BUTTON_TYPE_NONE;
2213 wv->help = Qnil;
2214 /* This prevents lwlib from assuming this
2215 menu item is really supposed to be empty. */
2216 /* The EMACS_INT cast avoids a warning.
2217 This value just has to be different from small integers. */
2218 wv->call_data = (void *) (EMACS_INT) (-1);
2219
2220 if (prev_wv)
2221 prev_wv->next = wv;
2222 else
2223 first_wv->contents = wv;
2224 prev_wv = wv;
2225 }
2226
2227 /* Forget what we thought we knew about what is in the
2228 detailed contents of the menu bar menus.
2229 Changing the top level always destroys the contents. */
2230 f->menu_bar_items_used = 0;
2231 }
2232
2233 /* Create or update the menu bar widget. */
2234
2235 BLOCK_INPUT;
2236
2237#ifdef USE_GTK
2238 xg_crazy_callback_abort = 1;
2239 if (menubar_widget)
2240 {
2241 /* The fourth arg is DEEP_P, which says to consider the entire
2242 menu trees we supply, rather than just the menu bar item names. */
2243 xg_modify_menubar_widgets (menubar_widget,
2244 f,
2245 first_wv,
2246 deep_p,
2247 G_CALLBACK (menubar_selection_callback),
2248 G_CALLBACK (popup_deactivate_callback),
2249 G_CALLBACK (menu_highlight_callback));
2250 }
2251 else
2252 {
2253 GtkWidget *wvbox = f->output_data.x->vbox_widget;
2254
2255 menubar_widget
2256 = xg_create_widget ("menubar", "menubar", f, first_wv,
2257 G_CALLBACK (menubar_selection_callback),
2258 G_CALLBACK (popup_deactivate_callback),
2259 G_CALLBACK (menu_highlight_callback));
2260
2261 f->output_data.x->menubar_widget = menubar_widget;
2262 }
2263
2264
2265#else /* not USE_GTK */
2266 if (menubar_widget)
2267 {
2268 /* Disable resizing (done for Motif!) */
2269 lw_allow_resizing (f->output_data.x->widget, False);
2270
2271 /* The third arg is DEEP_P, which says to consider the entire
2272 menu trees we supply, rather than just the menu bar item names. */
2273 lw_modify_all_widgets (id, first_wv, deep_p);
2274
2275 /* Re-enable the edit widget to resize. */
2276 lw_allow_resizing (f->output_data.x->widget, True);
2277 }
2278 else
2279 {
2280 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2281 XtTranslations override = XtParseTranslationTable (menuOverride);
2282
2283 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
2284 f->output_data.x->column_widget,
2285 0,
2286 popup_activate_callback,
2287 menubar_selection_callback,
2288 popup_deactivate_callback,
2289 menu_highlight_callback);
2290 f->output_data.x->menubar_widget = menubar_widget;
2291
2292 /* Make menu pop down on C-g. */
2293 XtOverrideTranslations (menubar_widget, override);
2294 }
2295
2296 {
2297 int menubar_size
2298 = (f->output_data.x->menubar_widget
2299 ? (f->output_data.x->menubar_widget->core.height
2300 + f->output_data.x->menubar_widget->core.border_width)
2301 : 0);
2302
2303#if 0 /* Experimentally, we now get the right results
2304 for -geometry -0-0 without this. 24 Aug 96, rms. */
2305#ifdef USE_LUCID
2306 if (FRAME_EXTERNAL_MENU_BAR (f))
2307 {
2308 Dimension ibw = 0;
2309 XtVaGetValues (f->output_data.x->column_widget,
2310 XtNinternalBorderWidth, &ibw, NULL);
2311 menubar_size += ibw;
2312 }
2313#endif /* USE_LUCID */
2314#endif /* 0 */
2315
2316 f->output_data.x->menubar_height = menubar_size;
2317 }
2318#endif /* not USE_GTK */
2319
2320 free_menubar_widget_value_tree (first_wv);
2321 update_frame_menubar (f);
2322
2323#ifdef USE_GTK
2324 xg_crazy_callback_abort = 0;
2325#endif
2326
2327 UNBLOCK_INPUT;
2328}
2329
2330/* Called from Fx_create_frame to create the initial menubar of a frame
2331 before it is mapped, so that the window is mapped with the menubar already
2332 there instead of us tacking it on later and thrashing the window after it
2333 is visible. */
2334
2335void
2336initialize_frame_menubar (f)
2337 FRAME_PTR f;
2338{
2339 /* This function is called before the first chance to redisplay
2340 the frame. It has to be, so the frame will have the right size. */
2341 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2342 set_frame_menubar (f, 1, 1);
2343}
2344
2345
2346/* Get rid of the menu bar of frame F, and free its storage.
2347 This is used when deleting a frame, and when turning off the menu bar.
2348 For GTK this function is in gtkutil.c. */
2349
2350#ifndef USE_GTK
2351void
2352free_frame_menubar (f)
2353 FRAME_PTR f;
2354{
2355 Widget menubar_widget;
2356
2357 if (! FRAME_X_P (f))
2358 abort ();
2359
2360 menubar_widget = f->output_data.x->menubar_widget;
2361
2362 f->output_data.x->menubar_height = 0;
2363
2364 if (menubar_widget)
2365 {
2366#ifdef USE_MOTIF
2367 /* Removing the menu bar magically changes the shell widget's x
2368 and y position of (0, 0) which, when the menu bar is turned
2369 on again, leads to pull-down menuss appearing in strange
2370 positions near the upper-left corner of the display. This
2371 happens only with some window managers like twm and ctwm,
2372 but not with other like Motif's mwm or kwm, because the
2373 latter generate ConfigureNotify events when the menu bar
2374 is switched off, which fixes the shell position. */
2375 Position x0, y0, x1, y1;
2376#endif
2377
2378 BLOCK_INPUT;
2379
2380#ifdef USE_MOTIF
2381 if (f->output_data.x->widget)
2382 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
2383#endif
2384
2385 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
2386 f->output_data.x->menubar_widget = NULL;
2387
2388#ifdef USE_MOTIF
2389 if (f->output_data.x->widget)
2390 {
2391 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2392 if (x1 == 0 && y1 == 0)
2393 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2394 }
2395#endif
2396
2397 UNBLOCK_INPUT;
2398 }
2399}
2400#endif /* not USE_GTK */
2401
2402#endif /* USE_X_TOOLKIT || USE_GTK */
2403\f
2404/* xmenu_show actually displays a menu using the panes and items in menu_items
2405 and returns the value selected from it.
2406 There are two versions of xmenu_show, one for Xt and one for Xlib.
2407 Both assume input is blocked by the caller. */
2408
2409/* F is the frame the menu is for.
2410 X and Y are the frame-relative specified position,
2411 relative to the inside upper left corner of the frame F.
2412 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2413 KEYMAPS is 1 if this menu was specified with keymaps;
2414 in that case, we return a list containing the chosen item's value
2415 and perhaps also the pane's prefix.
2416 TITLE is the specified menu title.
2417 ERROR is a place to store an error message string in case of failure.
2418 (We return nil on failure, but the value doesn't actually matter.) */
2419
2420#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2421
2422/* The item selected in the popup menu. */
2423static Lisp_Object *volatile menu_item_selection;
2424
2425#ifdef USE_GTK
2426
2427/* Used when position a popup menu. See menu_position_func and
2428 create_and_show_popup_menu below. */
2429struct next_popup_x_y
2430{
2431 FRAME_PTR f;
2432 int x;
2433 int y;
2434};
2435
2436/* The menu position function to use if we are not putting a popup
2437 menu where the pointer is.
2438 MENU is the menu to pop up.
2439 X and Y shall on exit contain x/y where the menu shall pop up.
2440 PUSH_IN is not documented in the GTK manual.
2441 USER_DATA is any data passed in when calling gtk_menu_popup.
2442 Here it points to a struct next_popup_x_y where the coordinates
2443 to store in *X and *Y are as well as the frame for the popup.
2444
2445 Here only X and Y are used. */
2446static void
2447menu_position_func (menu, x, y, push_in, user_data)
2448 GtkMenu *menu;
2449 gint *x;
2450 gint *y;
2451 gboolean *push_in;
2452 gpointer user_data;
2453{
2454 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2455 GtkRequisition req;
2456 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2457 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
2458
2459 *x = data->x;
2460 *y = data->y;
2461
2462 /* Check if there is room for the menu. If not, adjust x/y so that
2463 the menu is fully visible. */
2464 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2465 if (data->x + req.width > disp_width)
2466 *x -= data->x + req.width - disp_width;
2467 if (data->y + req.height > disp_height)
2468 *y -= data->y + req.height - disp_height;
2469}
2470
2471static void
2472popup_selection_callback (widget, client_data)
2473 GtkWidget *widget;
2474 gpointer client_data;
2475{
2476 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2477
2478 if (xg_crazy_callback_abort) return;
2479 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2480}
2481
2482static Lisp_Object
2483pop_down_menu (arg)
2484 Lisp_Object arg;
2485{
2486 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
2487
2488 popup_activated_flag = 0;
2489 BLOCK_INPUT;
2490 gtk_widget_destroy (GTK_WIDGET (p->pointer));
2491 UNBLOCK_INPUT;
2492 return Qnil;
2493}
2494
2495/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2496 menu pops down.
2497 menu_item_selection will be set to the selection. */
2498static void
2499create_and_show_popup_menu (f, first_wv, x, y, for_click)
2500 FRAME_PTR f;
2501 widget_value *first_wv;
2502 int x;
2503 int y;
2504 int for_click;
2505{
2506 int i;
2507 GtkWidget *menu;
2508 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2509 struct next_popup_x_y popup_x_y;
2510 int specpdl_count = SPECPDL_INDEX ();
2511
2512 if (! FRAME_X_P (f))
2513 abort ();
2514
2515 xg_crazy_callback_abort = 1;
2516 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2517 G_CALLBACK (popup_selection_callback),
2518 G_CALLBACK (popup_deactivate_callback),
2519 G_CALLBACK (menu_highlight_callback));
2520 xg_crazy_callback_abort = 0;
2521
2522 if (! for_click)
2523 {
2524 /* Not invoked by a click. pop up at x/y. */
2525 pos_func = menu_position_func;
2526
2527 /* Adjust coordinates to be root-window-relative. */
2528 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2529 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2530
2531 popup_x_y.x = x;
2532 popup_x_y.y = y;
2533 popup_x_y.f = f;
2534
2535 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2536 }
2537 else
2538 {
2539 for (i = 0; i < 5; i++)
2540 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2541 break;
2542 }
2543
2544 /* Display the menu. */
2545 gtk_widget_show_all (menu);
2546 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
2547
2548 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
2549
2550 if (GTK_WIDGET_MAPPED (menu))
2551 {
2552 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2553 two. show_help_echo uses this to detect popup menus. */
2554 popup_activated_flag = 1;
2555 /* Process events that apply to the menu. */
2556 popup_widget_loop (1, menu);
2557 }
2558
2559 unbind_to (specpdl_count, Qnil);
2560
2561 /* Must reset this manually because the button release event is not passed
2562 to Emacs event loop. */
2563 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2564}
2565
2566#else /* not USE_GTK */
2567
2568/* We need a unique id for each widget handled by the Lucid Widget
2569 library.
2570
2571 For the main windows, and popup menus, we use this counter,
2572 which we increment each time after use. This starts from 1<<16.
2573
2574 For menu bars, we use numbers starting at 0, counted in
2575 next_menubar_widget_id. */
2576LWLIB_ID widget_id_tick;
2577
2578static void
2579popup_selection_callback (widget, id, client_data)
2580 Widget widget;
2581 LWLIB_ID id;
2582 XtPointer client_data;
2583{
2584 menu_item_selection = (Lisp_Object *) client_data;
2585}
2586
2587/* ARG is the LWLIB ID of the dialog box, represented
2588 as a Lisp object as (HIGHPART . LOWPART). */
2589
2590static Lisp_Object
2591pop_down_menu (arg)
2592 Lisp_Object arg;
2593{
2594 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2595 | XINT (XCDR (arg)));
2596
2597 BLOCK_INPUT;
2598 lw_destroy_all_widgets (id);
2599 UNBLOCK_INPUT;
2600 popup_activated_flag = 0;
2601
2602 return Qnil;
2603}
2604
2605/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2606 menu pops down.
2607 menu_item_selection will be set to the selection. */
2608static void
2609create_and_show_popup_menu (f, first_wv, x, y, for_click)
2610 FRAME_PTR f;
2611 widget_value *first_wv;
2612 int x;
2613 int y;
2614 int for_click;
2615{
2616 int i;
2617 Arg av[2];
2618 int ac = 0;
2619 XButtonPressedEvent dummy;
2620 LWLIB_ID menu_id;
2621 Widget menu;
2622
2623 if (! FRAME_X_P (f))
2624 abort ();
2625
2626 menu_id = widget_id_tick++;
2627 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2628 f->output_data.x->widget, 1, 0,
2629 popup_selection_callback,
2630 popup_deactivate_callback,
2631 menu_highlight_callback);
2632
2633 dummy.type = ButtonPress;
2634 dummy.serial = 0;
2635 dummy.send_event = 0;
2636 dummy.display = FRAME_X_DISPLAY (f);
2637 dummy.time = CurrentTime;
2638 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2639 dummy.window = dummy.root;
2640 dummy.subwindow = dummy.root;
2641 dummy.x = x;
2642 dummy.y = y;
2643
2644 /* Adjust coordinates to be root-window-relative. */
2645 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2646 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2647
2648 dummy.x_root = x;
2649 dummy.y_root = y;
2650
2651 dummy.state = 0;
2652 dummy.button = 0;
2653 for (i = 0; i < 5; i++)
2654 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2655 dummy.button = i;
2656
2657 /* Don't allow any geometry request from the user. */
2658 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2659 XtSetValues (menu, av, ac);
2660
2661 /* Display the menu. */
2662 lw_popup_menu (menu, (XEvent *) &dummy);
2663 popup_activated_flag = 1;
2664
2665 {
2666 int fact = 4 * sizeof (LWLIB_ID);
2667 int specpdl_count = SPECPDL_INDEX ();
2668 record_unwind_protect (pop_down_menu,
2669 Fcons (make_number (menu_id >> (fact)),
2670 make_number (menu_id & ~(-1 << (fact)))));
2671
2672 /* Process events that apply to the menu. */
2673 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
2674
2675 unbind_to (specpdl_count, Qnil);
2676 }
2677}
2678
2679#endif /* not USE_GTK */
2680
2681static Lisp_Object
2682xmenu_show (f, x, y, for_click, keymaps, title, error)
2683 FRAME_PTR f;
2684 int x;
2685 int y;
2686 int for_click;
2687 int keymaps;
2688 Lisp_Object title;
2689 char **error;
2690{
2691 int i;
2692 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2693 widget_value **submenu_stack
2694 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2695 Lisp_Object *subprefix_stack
2696 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2697 int submenu_depth = 0;
2698
2699 int first_pane;
2700
2701 if (! FRAME_X_P (f))
2702 abort ();
2703
2704 *error = NULL;
2705
2706 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2707 {
2708 *error = "Empty menu";
2709 return Qnil;
2710 }
2711
2712 /* Create a tree of widget_value objects
2713 representing the panes and their items. */
2714 wv = xmalloc_widget_value ();
2715 wv->name = "menu";
2716 wv->value = 0;
2717 wv->enabled = 1;
2718 wv->button_type = BUTTON_TYPE_NONE;
2719 wv->help =Qnil;
2720 first_wv = wv;
2721 first_pane = 1;
2722
2723 /* Loop over all panes and items, filling in the tree. */
2724 i = 0;
2725 while (i < menu_items_used)
2726 {
2727 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2728 {
2729 submenu_stack[submenu_depth++] = save_wv;
2730 save_wv = prev_wv;
2731 prev_wv = 0;
2732 first_pane = 1;
2733 i++;
2734 }
2735 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2736 {
2737 prev_wv = save_wv;
2738 save_wv = submenu_stack[--submenu_depth];
2739 first_pane = 0;
2740 i++;
2741 }
2742 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2743 && submenu_depth != 0)
2744 i += MENU_ITEMS_PANE_LENGTH;
2745 /* Ignore a nil in the item list.
2746 It's meaningful only for dialog boxes. */
2747 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2748 i += 1;
2749 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2750 {
2751 /* Create a new pane. */
2752 Lisp_Object pane_name, prefix;
2753 char *pane_string;
2754
2755 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2756 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2757
2758#ifndef HAVE_MULTILINGUAL_MENU
2759 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2760 {
2761 pane_name = ENCODE_MENU_STRING (pane_name);
2762 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2763 }
2764#endif
2765 pane_string = (NILP (pane_name)
2766 ? "" : (char *) SDATA (pane_name));
2767 /* If there is just one top-level pane, put all its items directly
2768 under the top-level menu. */
2769 if (menu_items_n_panes == 1)
2770 pane_string = "";
2771
2772 /* If the pane has a meaningful name,
2773 make the pane a top-level menu item
2774 with its items as a submenu beneath it. */
2775 if (!keymaps && strcmp (pane_string, ""))
2776 {
2777 wv = xmalloc_widget_value ();
2778 if (save_wv)
2779 save_wv->next = wv;
2780 else
2781 first_wv->contents = wv;
2782 wv->name = pane_string;
2783 if (keymaps && !NILP (prefix))
2784 wv->name++;
2785 wv->value = 0;
2786 wv->enabled = 1;
2787 wv->button_type = BUTTON_TYPE_NONE;
2788 wv->help = Qnil;
2789 save_wv = wv;
2790 prev_wv = 0;
2791 }
2792 else if (first_pane)
2793 {
2794 save_wv = wv;
2795 prev_wv = 0;
2796 }
2797 first_pane = 0;
2798 i += MENU_ITEMS_PANE_LENGTH;
2799 }
2800 else
2801 {
2802 /* Create a new item within current pane. */
2803 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2804 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2805 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2806 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2807 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2808 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2809 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2810 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2811
2812#ifndef HAVE_MULTILINGUAL_MENU
2813 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2814 {
2815 item_name = ENCODE_MENU_STRING (item_name);
2816 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2817 }
2818
2819 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2820 {
2821 descrip = ENCODE_MENU_STRING (descrip);
2822 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2823 }
2824#endif /* not HAVE_MULTILINGUAL_MENU */
2825
2826 wv = xmalloc_widget_value ();
2827 if (prev_wv)
2828 prev_wv->next = wv;
2829 else
2830 save_wv->contents = wv;
2831 wv->name = (char *) SDATA (item_name);
2832 if (!NILP (descrip))
2833 wv->key = (char *) SDATA (descrip);
2834 wv->value = 0;
2835 /* If this item has a null value,
2836 make the call_data null so that it won't display a box
2837 when the mouse is on it. */
2838 wv->call_data
2839 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2840 wv->enabled = !NILP (enable);
2841
2842 if (NILP (type))
2843 wv->button_type = BUTTON_TYPE_NONE;
2844 else if (EQ (type, QCtoggle))
2845 wv->button_type = BUTTON_TYPE_TOGGLE;
2846 else if (EQ (type, QCradio))
2847 wv->button_type = BUTTON_TYPE_RADIO;
2848 else
2849 abort ();
2850
2851 wv->selected = !NILP (selected);
2852
2853 if (! STRINGP (help))
2854 help = Qnil;
2855
2856 wv->help = help;
2857
2858 prev_wv = wv;
2859
2860 i += MENU_ITEMS_ITEM_LENGTH;
2861 }
2862 }
2863
2864 /* Deal with the title, if it is non-nil. */
2865 if (!NILP (title))
2866 {
2867 widget_value *wv_title = xmalloc_widget_value ();
2868 widget_value *wv_sep1 = xmalloc_widget_value ();
2869 widget_value *wv_sep2 = xmalloc_widget_value ();
2870
2871 wv_sep2->name = "--";
2872 wv_sep2->next = first_wv->contents;
2873 wv_sep2->help = Qnil;
2874
2875 wv_sep1->name = "--";
2876 wv_sep1->next = wv_sep2;
2877 wv_sep1->help = Qnil;
2878
2879#ifndef HAVE_MULTILINGUAL_MENU
2880 if (STRING_MULTIBYTE (title))
2881 title = ENCODE_MENU_STRING (title);
2882#endif
2883
2884 wv_title->name = (char *) SDATA (title);
2885 wv_title->enabled = TRUE;
2886 wv_title->button_type = BUTTON_TYPE_NONE;
2887 wv_title->next = wv_sep1;
2888 wv_title->help = Qnil;
2889 first_wv->contents = wv_title;
2890 }
2891
2892 /* No selection has been chosen yet. */
2893 menu_item_selection = 0;
2894
2895 /* Actually create and show the menu until popped down. */
2896 create_and_show_popup_menu (f, first_wv, x, y, for_click);
2897
2898 /* Free the widget_value objects we used to specify the contents. */
2899 free_menubar_widget_value_tree (first_wv);
2900
2901 /* Find the selected item, and its pane, to return
2902 the proper value. */
2903 if (menu_item_selection != 0)
2904 {
2905 Lisp_Object prefix, entry;
2906
2907 prefix = entry = Qnil;
2908 i = 0;
2909 while (i < menu_items_used)
2910 {
2911 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2912 {
2913 subprefix_stack[submenu_depth++] = prefix;
2914 prefix = entry;
2915 i++;
2916 }
2917 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2918 {
2919 prefix = subprefix_stack[--submenu_depth];
2920 i++;
2921 }
2922 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2923 {
2924 prefix
2925 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2926 i += MENU_ITEMS_PANE_LENGTH;
2927 }
2928 /* Ignore a nil in the item list.
2929 It's meaningful only for dialog boxes. */
2930 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2931 i += 1;
2932 else
2933 {
2934 entry
2935 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2936 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2937 {
2938 if (keymaps != 0)
2939 {
2940 int j;
2941
2942 entry = Fcons (entry, Qnil);
2943 if (!NILP (prefix))
2944 entry = Fcons (prefix, entry);
2945 for (j = submenu_depth - 1; j >= 0; j--)
2946 if (!NILP (subprefix_stack[j]))
2947 entry = Fcons (subprefix_stack[j], entry);
2948 }
2949 return entry;
2950 }
2951 i += MENU_ITEMS_ITEM_LENGTH;
2952 }
2953 }
2954 }
2955 else if (!for_click)
2956 /* Make "Cancel" equivalent to C-g. */
2957 Fsignal (Qquit, Qnil);
2958
2959 return Qnil;
2960}
2961\f
2962#ifdef USE_GTK
2963static void
2964dialog_selection_callback (widget, client_data)
2965 GtkWidget *widget;
2966 gpointer client_data;
2967{
2968 /* The EMACS_INT cast avoids a warning. There's no problem
2969 as long as pointers have enough bits to hold small integers. */
2970 if ((int) (EMACS_INT) client_data != -1)
2971 menu_item_selection = (Lisp_Object *) client_data;
2972
2973 popup_activated_flag = 0;
2974}
2975
2976/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2977 dialog pops down.
2978 menu_item_selection will be set to the selection. */
2979static void
2980create_and_show_dialog (f, first_wv)
2981 FRAME_PTR f;
2982 widget_value *first_wv;
2983{
2984 GtkWidget *menu;
2985
2986 if (! FRAME_X_P (f))
2987 abort ();
2988
2989 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
2990 G_CALLBACK (dialog_selection_callback),
2991 G_CALLBACK (popup_deactivate_callback),
2992 0);
2993
2994 if (menu)
2995 {
2996 int specpdl_count = SPECPDL_INDEX ();
2997 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
2998
2999 /* Display the menu. */
3000 gtk_widget_show_all (menu);
3001
3002 /* Process events that apply to the menu. */
3003 popup_widget_loop (1, menu);
3004
3005 unbind_to (specpdl_count, Qnil);
3006 }
3007}
3008
3009#else /* not USE_GTK */
3010static void
3011dialog_selection_callback (widget, id, client_data)
3012 Widget widget;
3013 LWLIB_ID id;
3014 XtPointer client_data;
3015{
3016 /* The EMACS_INT cast avoids a warning. There's no problem
3017 as long as pointers have enough bits to hold small integers. */
3018 if ((int) (EMACS_INT) client_data != -1)
3019 menu_item_selection = (Lisp_Object *) client_data;
3020
3021 BLOCK_INPUT;
3022 lw_destroy_all_widgets (id);
3023 UNBLOCK_INPUT;
3024 popup_activated_flag = 0;
3025}
3026
3027
3028/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3029 dialog pops down.
3030 menu_item_selection will be set to the selection. */
3031static void
3032create_and_show_dialog (f, first_wv)
3033 FRAME_PTR f;
3034 widget_value *first_wv;
3035{
3036 LWLIB_ID dialog_id;
3037
3038 if (!FRAME_X_P (f))
3039 abort();
3040
3041 dialog_id = widget_id_tick++;
3042 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
3043 f->output_data.x->widget, 1, 0,
3044 dialog_selection_callback, 0, 0);
3045 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
3046
3047 /* Display the dialog box. */
3048 lw_pop_up_all_widgets (dialog_id);
3049 popup_activated_flag = 1;
3050
3051 /* Process events that apply to the dialog box.
3052 Also handle timers. */
3053 {
3054 int count = SPECPDL_INDEX ();
3055 int fact = 4 * sizeof (LWLIB_ID);
3056
3057 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3058 record_unwind_protect (pop_down_menu,
3059 Fcons (make_number (dialog_id >> (fact)),
3060 make_number (dialog_id & ~(-1 << (fact)))));
3061
3062 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
3063 dialog_id, 1);
3064
3065 unbind_to (count, Qnil);
3066 }
3067}
3068
3069#endif /* not USE_GTK */
3070
3071static char * button_names [] = {
3072 "button1", "button2", "button3", "button4", "button5",
3073 "button6", "button7", "button8", "button9", "button10" };
3074
3075static Lisp_Object
3076xdialog_show (f, keymaps, title, header, error_name)
3077 FRAME_PTR f;
3078 int keymaps;
3079 Lisp_Object title, header;
3080 char **error_name;
3081{
3082 int i, nb_buttons=0;
3083 char dialog_name[6];
3084
3085 widget_value *wv, *first_wv = 0, *prev_wv = 0;
3086
3087 /* Number of elements seen so far, before boundary. */
3088 int left_count = 0;
3089 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3090 int boundary_seen = 0;
3091
3092 if (! FRAME_X_P (f))
3093 abort ();
3094
3095 *error_name = NULL;
3096
3097 if (menu_items_n_panes > 1)
3098 {
3099 *error_name = "Multiple panes in dialog box";
3100 return Qnil;
3101 }
3102
3103 /* Create a tree of widget_value objects
3104 representing the text label and buttons. */
3105 {
3106 Lisp_Object pane_name, prefix;
3107 char *pane_string;
3108 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
3109 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
3110 pane_string = (NILP (pane_name)
3111 ? "" : (char *) SDATA (pane_name));
3112 prev_wv = xmalloc_widget_value ();
3113 prev_wv->value = pane_string;
3114 if (keymaps && !NILP (prefix))
3115 prev_wv->name++;
3116 prev_wv->enabled = 1;
3117 prev_wv->name = "message";
3118 prev_wv->help = Qnil;
3119 first_wv = prev_wv;
3120
3121 /* Loop over all panes and items, filling in the tree. */
3122 i = MENU_ITEMS_PANE_LENGTH;
3123 while (i < menu_items_used)
3124 {
3125
3126 /* Create a new item within current pane. */
3127 Lisp_Object item_name, enable, descrip;
3128 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3129 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3130 descrip
3131 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3132
3133 if (NILP (item_name))
3134 {
3135 free_menubar_widget_value_tree (first_wv);
3136 *error_name = "Submenu in dialog items";
3137 return Qnil;
3138 }
3139 if (EQ (item_name, Qquote))
3140 {
3141 /* This is the boundary between left-side elts
3142 and right-side elts. Stop incrementing right_count. */
3143 boundary_seen = 1;
3144 i++;
3145 continue;
3146 }
3147 if (nb_buttons >= 9)
3148 {
3149 free_menubar_widget_value_tree (first_wv);
3150 *error_name = "Too many dialog items";
3151 return Qnil;
3152 }
3153
3154 wv = xmalloc_widget_value ();
3155 prev_wv->next = wv;
3156 wv->name = (char *) button_names[nb_buttons];
3157 if (!NILP (descrip))
3158 wv->key = (char *) SDATA (descrip);
3159 wv->value = (char *) SDATA (item_name);
3160 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
3161 wv->enabled = !NILP (enable);
3162 wv->help = Qnil;
3163 prev_wv = wv;
3164
3165 if (! boundary_seen)
3166 left_count++;
3167
3168 nb_buttons++;
3169 i += MENU_ITEMS_ITEM_LENGTH;
3170 }
3171
3172 /* If the boundary was not specified,
3173 by default put half on the left and half on the right. */
3174 if (! boundary_seen)
3175 left_count = nb_buttons - nb_buttons / 2;
3176
3177 wv = xmalloc_widget_value ();
3178 wv->name = dialog_name;
3179 wv->help = Qnil;
3180
3181 /* Frame title: 'Q' = Question, 'I' = Information.
3182 Can also have 'E' = Error if, one day, we want
3183 a popup for errors. */
3184 if (NILP(header))
3185 dialog_name[0] = 'Q';
3186 else
3187 dialog_name[0] = 'I';
3188
3189 /* Dialog boxes use a really stupid name encoding
3190 which specifies how many buttons to use
3191 and how many buttons are on the right. */
3192 dialog_name[1] = '0' + nb_buttons;
3193 dialog_name[2] = 'B';
3194 dialog_name[3] = 'R';
3195 /* Number of buttons to put on the right. */
3196 dialog_name[4] = '0' + nb_buttons - left_count;
3197 dialog_name[5] = 0;
3198 wv->contents = first_wv;
3199 first_wv = wv;
3200 }
3201
3202 /* No selection has been chosen yet. */
3203 menu_item_selection = 0;
3204
3205 /* Actually create and show the dialog. */
3206 create_and_show_dialog (f, first_wv);
3207
3208 /* Free the widget_value objects we used to specify the contents. */
3209 free_menubar_widget_value_tree (first_wv);
3210
3211 /* Find the selected item, and its pane, to return
3212 the proper value. */
3213 if (menu_item_selection != 0)
3214 {
3215 Lisp_Object prefix;
3216
3217 prefix = Qnil;
3218 i = 0;
3219 while (i < menu_items_used)
3220 {
3221 Lisp_Object entry;
3222
3223 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3224 {
3225 prefix
3226 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3227 i += MENU_ITEMS_PANE_LENGTH;
3228 }
3229 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3230 {
3231 /* This is the boundary between left-side elts and
3232 right-side elts. */
3233 ++i;
3234 }
3235 else
3236 {
3237 entry
3238 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3239 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3240 {
3241 if (keymaps != 0)
3242 {
3243 entry = Fcons (entry, Qnil);
3244 if (!NILP (prefix))
3245 entry = Fcons (prefix, entry);
3246 }
3247 return entry;
3248 }
3249 i += MENU_ITEMS_ITEM_LENGTH;
3250 }
3251 }
3252 }
3253 else
3254 /* Make "Cancel" equivalent to C-g. */
3255 Fsignal (Qquit, Qnil);
3256
3257 return Qnil;
3258}
3259
3260#else /* not USE_X_TOOLKIT && not USE_GTK */
3261
3262/* The frame of the last activated non-toolkit menu bar.
3263 Used to generate menu help events. */
3264
3265static struct frame *menu_help_frame;
3266
3267
3268/* Show help HELP_STRING, or clear help if HELP_STRING is null.
3269
3270 PANE is the pane number, and ITEM is the menu item number in
3271 the menu (currently not used).
3272
3273 This cannot be done with generating a HELP_EVENT because
3274 XMenuActivate contains a loop that doesn't let Emacs process
3275 keyboard events. */
3276
3277static void
3278menu_help_callback (help_string, pane, item)
3279 char *help_string;
3280 int pane, item;
3281{
3282 extern Lisp_Object Qmenu_item;
3283 Lisp_Object *first_item;
3284 Lisp_Object pane_name;
3285 Lisp_Object menu_object;
3286
3287 first_item = XVECTOR (menu_items)->contents;
3288 if (EQ (first_item[0], Qt))
3289 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3290 else if (EQ (first_item[0], Qquote))
3291 /* This shouldn't happen, see xmenu_show. */
3292 pane_name = empty_string;
3293 else
3294 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
3295
3296 /* (menu-item MENU-NAME PANE-NUMBER) */
3297 menu_object = Fcons (Qmenu_item,
3298 Fcons (pane_name,
3299 Fcons (make_number (pane), Qnil)));
3300 show_help_echo (help_string ? build_string (help_string) : Qnil,
3301 Qnil, menu_object, make_number (item), 1);
3302}
3303
3304static Lisp_Object
3305pop_down_menu (arg)
3306 Lisp_Object arg;
3307{
3308 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
3309 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
3310
3311 FRAME_PTR f = p1->pointer;
3312 XMenu *menu = p2->pointer;
3313
3314 BLOCK_INPUT;
3315#ifndef MSDOS
3316 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
3317 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
3318#endif
3319 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3320
3321#ifdef HAVE_X_WINDOWS
3322 /* Assume the mouse has moved out of the X window.
3323 If it has actually moved in, we will get an EnterNotify. */
3324 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
3325
3326 /* State that no mouse buttons are now held.
3327 (The oldXMenu code doesn't track this info for us.)
3328 That is not necessarily true, but the fiction leads to reasonable
3329 results, and it is a pain to ask which are actually held now. */
3330 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
3331
3332#endif /* HAVE_X_WINDOWS */
3333
3334 UNBLOCK_INPUT;
3335
3336 return Qnil;
3337}
3338
3339
3340static Lisp_Object
3341xmenu_show (f, x, y, for_click, keymaps, title, error)
3342 FRAME_PTR f;
3343 int x, y;
3344 int for_click;
3345 int keymaps;
3346 Lisp_Object title;
3347 char **error;
3348{
3349 Window root;
3350 XMenu *menu;
3351 int pane, selidx, lpane, status;
3352 Lisp_Object entry, pane_prefix;
3353 char *datap;
3354 int ulx, uly, width, height;
3355 int dispwidth, dispheight;
3356 int i, j, lines, maxlines;
3357 int maxwidth;
3358 int dummy_int;
3359 unsigned int dummy_uint;
3360 int specpdl_count = SPECPDL_INDEX ();
3361
3362 if (! FRAME_X_P (f))
3363 abort ();
3364
3365 *error = 0;
3366 if (menu_items_n_panes == 0)
3367 return Qnil;
3368
3369 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3370 {
3371 *error = "Empty menu";
3372 return Qnil;
3373 }
3374
3375 /* Figure out which root window F is on. */
3376 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
3377 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3378 &dummy_uint, &dummy_uint);
3379
3380 /* Make the menu on that window. */
3381 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
3382 if (menu == NULL)
3383 {
3384 *error = "Can't create menu";
3385 return Qnil;
3386 }
3387
3388#ifdef HAVE_X_WINDOWS
3389 /* Adjust coordinates to relative to the outer (window manager) window. */
3390 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
3391 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
3392#endif /* HAVE_X_WINDOWS */
3393
3394 /* Adjust coordinates to be root-window-relative. */
3395 x += f->left_pos;
3396 y += f->top_pos;
3397
3398 /* Create all the necessary panes and their items. */
3399 maxlines = lines = i = 0;
3400 while (i < menu_items_used)
3401 {
3402 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3403 {
3404 /* Create a new pane. */
3405 Lisp_Object pane_name, prefix;
3406 char *pane_string;
3407
3408 maxlines = max (maxlines, lines);
3409 lines = 0;
3410 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3411 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3412 pane_string = (NILP (pane_name)
3413 ? "" : (char *) SDATA (pane_name));
3414 if (keymaps && !NILP (prefix))
3415 pane_string++;
3416
3417 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
3418 if (lpane == XM_FAILURE)
3419 {
3420 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3421 *error = "Can't create pane";
3422 return Qnil;
3423 }
3424 i += MENU_ITEMS_PANE_LENGTH;
3425
3426 /* Find the width of the widest item in this pane. */
3427 maxwidth = 0;
3428 j = i;
3429 while (j < menu_items_used)
3430 {
3431 Lisp_Object item;
3432 item = XVECTOR (menu_items)->contents[j];
3433 if (EQ (item, Qt))
3434 break;
3435 if (NILP (item))
3436 {
3437 j++;
3438 continue;
3439 }
3440 width = SBYTES (item);
3441 if (width > maxwidth)
3442 maxwidth = width;
3443
3444 j += MENU_ITEMS_ITEM_LENGTH;
3445 }
3446 }
3447 /* Ignore a nil in the item list.
3448 It's meaningful only for dialog boxes. */
3449 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3450 i += 1;
3451 else
3452 {
3453 /* Create a new item within current pane. */
3454 Lisp_Object item_name, enable, descrip, help;
3455 unsigned char *item_data;
3456 char *help_string;
3457
3458 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3459 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3460 descrip
3461 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3462 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3463 help_string = STRINGP (help) ? SDATA (help) : NULL;
3464
3465 if (!NILP (descrip))
3466 {
3467 int gap = maxwidth - SBYTES (item_name);
3468#ifdef C_ALLOCA
3469 Lisp_Object spacer;
3470 spacer = Fmake_string (make_number (gap), make_number (' '));
3471 item_name = concat2 (item_name, spacer);
3472 item_name = concat2 (item_name, descrip);
3473 item_data = SDATA (item_name);
3474#else
3475 /* if alloca is fast, use that to make the space,
3476 to reduce gc needs. */
3477 item_data
3478 = (unsigned char *) alloca (maxwidth
3479 + SBYTES (descrip) + 1);
3480 bcopy (SDATA (item_name), item_data,
3481 SBYTES (item_name));
3482 for (j = SCHARS (item_name); j < maxwidth; j++)
3483 item_data[j] = ' ';
3484 bcopy (SDATA (descrip), item_data + j,
3485 SBYTES (descrip));
3486 item_data[j + SBYTES (descrip)] = 0;
3487#endif
3488 }
3489 else
3490 item_data = SDATA (item_name);
3491
3492 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3493 menu, lpane, 0, item_data,
3494 !NILP (enable), help_string)
3495 == XM_FAILURE)
3496 {
3497 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3498 *error = "Can't add selection to menu";
3499 return Qnil;
3500 }
3501 i += MENU_ITEMS_ITEM_LENGTH;
3502 lines++;
3503 }
3504 }
3505
3506 maxlines = max (maxlines, lines);
3507
3508 /* All set and ready to fly. */
3509 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
3510 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3511 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3512 x = min (x, dispwidth);
3513 y = min (y, dispheight);
3514 x = max (x, 1);
3515 y = max (y, 1);
3516 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
3517 &ulx, &uly, &width, &height);
3518 if (ulx+width > dispwidth)
3519 {
3520 x -= (ulx + width) - dispwidth;
3521 ulx = dispwidth - width;
3522 }
3523 if (uly+height > dispheight)
3524 {
3525 y -= (uly + height) - dispheight;
3526 uly = dispheight - height;
3527 }
3528 if (ulx < 0) x -= ulx;
3529 if (uly < 0) y -= uly;
3530
3531 if (! for_click)
3532 {
3533 /* If position was not given by a mouse click, adjust so upper left
3534 corner of the menu as a whole ends up at given coordinates. This
3535 is what x-popup-menu says in its documentation. */
3536 x += width/2;
3537 y += 1.5*height/(maxlines+2);
3538 }
3539
3540 XMenuSetAEQ (menu, TRUE);
3541 XMenuSetFreeze (menu, TRUE);
3542 pane = selidx = 0;
3543
3544#ifndef MSDOS
3545 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
3546#endif
3547
3548 record_unwind_protect (pop_down_menu,
3549 Fcons (make_save_value (f, 0),
3550 make_save_value (menu, 0)));
3551
3552 /* Help display under X won't work because XMenuActivate contains
3553 a loop that doesn't give Emacs a chance to process it. */
3554 menu_help_frame = f;
3555 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3556 x, y, ButtonReleaseMask, &datap,
3557 menu_help_callback);
3558
3559 switch (status)
3560 {
3561 case XM_SUCCESS:
3562#ifdef XDEBUG
3563 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3564#endif
3565
3566 /* Find the item number SELIDX in pane number PANE. */
3567 i = 0;
3568 while (i < menu_items_used)
3569 {
3570 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3571 {
3572 if (pane == 0)
3573 pane_prefix
3574 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3575 pane--;
3576 i += MENU_ITEMS_PANE_LENGTH;
3577 }
3578 else
3579 {
3580 if (pane == -1)
3581 {
3582 if (selidx == 0)
3583 {
3584 entry
3585 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3586 if (keymaps != 0)
3587 {
3588 entry = Fcons (entry, Qnil);
3589 if (!NILP (pane_prefix))
3590 entry = Fcons (pane_prefix, entry);
3591 }
3592 break;
3593 }
3594 selidx--;
3595 }
3596 i += MENU_ITEMS_ITEM_LENGTH;
3597 }
3598 }
3599 break;
3600
3601 case XM_FAILURE:
3602 *error = "Can't activate menu";
3603 case XM_IA_SELECT:
3604 entry = Qnil;
3605 break;
3606 case XM_NO_SELECT:
3607 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3608 the menu was invoked with a mouse event as POSITION). */
3609 if (! for_click)
3610 Fsignal (Qquit, Qnil);
3611 entry = Qnil;
3612 break;
3613 }
3614
3615 unbind_to (specpdl_count, Qnil);
3616
3617 return entry;
3618}
3619
3620#endif /* not USE_X_TOOLKIT */
3621
3622#endif /* HAVE_MENUS */
3623\f
3624void
3625syms_of_xmenu ()
3626{
3627 staticpro (&menu_items);
3628 menu_items = Qnil;
3629 menu_items_inuse = Qnil;
3630
3631 Qdebug_on_next_call = intern ("debug-on-next-call");
3632 staticpro (&Qdebug_on_next_call);
3633
3634 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3635 doc: /* Frame for which we are updating a menu.
3636The enable predicate for a menu command should check this variable. */);
3637 Vmenu_updating_frame = Qnil;
3638
3639#ifdef USE_X_TOOLKIT
3640 widget_id_tick = (1<<16);
3641 next_menubar_widget_id = 1;
3642#endif
3643
3644 defsubr (&Sx_popup_menu);
3645#ifdef HAVE_MENUS
3646 defsubr (&Sx_popup_dialog);
3647#endif
3648}
3649
3650/* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3651 (do not change this comment) */