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