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