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