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