(restore_menu_items, save_menu_items): New fns.
[bpt/emacs.git] / src / xmenu.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 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 for (i = 0; i < previous_menu_items_used; i++)
2184 if (menu_items_used == i
2185 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
2186 break;
2187 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
2188 {
2189 free_menubar_widget_value_tree (first_wv);
2190 discard_menu_items ();
2191 unbind_to (specpdl_count, Qnil);
2192 return;
2193 }
2194
2195 f->menu_bar_vector = menu_items;
2196 f->menu_bar_items_used = menu_items_used;
2197
2198 unbind_to (specpdl_count, Qnil);
2199
2200 /* Now GC cannot happen during the lifetime of the widget_value,
2201 so it's safe to store data from a Lisp_String. */
2202 wv = first_wv->contents;
2203 for (i = 0; i < XVECTOR (items)->size; i += 4)
2204 {
2205 Lisp_Object string;
2206 string = XVECTOR (items)->contents[i + 1];
2207 if (NILP (string))
2208 break;
2209 wv->name = (char *) SDATA (string);
2210 update_submenu_strings (wv->contents);
2211 wv = wv->next;
2212 }
2213
2214 }
2215 else
2216 {
2217 /* Make a widget-value tree containing
2218 just the top level menu bar strings. */
2219
2220 wv = xmalloc_widget_value ();
2221 wv->name = "menubar";
2222 wv->value = 0;
2223 wv->enabled = 1;
2224 wv->button_type = BUTTON_TYPE_NONE;
2225 wv->help = Qnil;
2226 first_wv = wv;
2227
2228 items = FRAME_MENU_BAR_ITEMS (f);
2229 for (i = 0; i < XVECTOR (items)->size; i += 4)
2230 {
2231 Lisp_Object string;
2232
2233 string = XVECTOR (items)->contents[i + 1];
2234 if (NILP (string))
2235 break;
2236
2237 wv = xmalloc_widget_value ();
2238 wv->name = (char *) SDATA (string);
2239 wv->value = 0;
2240 wv->enabled = 1;
2241 wv->button_type = BUTTON_TYPE_NONE;
2242 wv->help = Qnil;
2243 /* This prevents lwlib from assuming this
2244 menu item is really supposed to be empty. */
2245 /* The EMACS_INT cast avoids a warning.
2246 This value just has to be different from small integers. */
2247 wv->call_data = (void *) (EMACS_INT) (-1);
2248
2249 if (prev_wv)
2250 prev_wv->next = wv;
2251 else
2252 first_wv->contents = wv;
2253 prev_wv = wv;
2254 }
2255
2256 /* Forget what we thought we knew about what is in the
2257 detailed contents of the menu bar menus.
2258 Changing the top level always destroys the contents. */
2259 f->menu_bar_items_used = 0;
2260 }
2261
2262 /* Create or update the menu bar widget. */
2263
2264 BLOCK_INPUT;
2265
2266 #ifdef USE_GTK
2267 xg_crazy_callback_abort = 1;
2268 if (menubar_widget)
2269 {
2270 /* The fourth arg is DEEP_P, which says to consider the entire
2271 menu trees we supply, rather than just the menu bar item names. */
2272 xg_modify_menubar_widgets (menubar_widget,
2273 f,
2274 first_wv,
2275 deep_p,
2276 G_CALLBACK (menubar_selection_callback),
2277 G_CALLBACK (popup_deactivate_callback),
2278 G_CALLBACK (menu_highlight_callback));
2279 }
2280 else
2281 {
2282 GtkWidget *wvbox = f->output_data.x->vbox_widget;
2283
2284 menubar_widget
2285 = xg_create_widget ("menubar", "menubar", f, first_wv,
2286 G_CALLBACK (menubar_selection_callback),
2287 G_CALLBACK (popup_deactivate_callback),
2288 G_CALLBACK (menu_highlight_callback));
2289
2290 f->output_data.x->menubar_widget = menubar_widget;
2291 }
2292
2293
2294 #else /* not USE_GTK */
2295 if (menubar_widget)
2296 {
2297 /* Disable resizing (done for Motif!) */
2298 lw_allow_resizing (f->output_data.x->widget, False);
2299
2300 /* The third arg is DEEP_P, which says to consider the entire
2301 menu trees we supply, rather than just the menu bar item names. */
2302 lw_modify_all_widgets (id, first_wv, deep_p);
2303
2304 /* Re-enable the edit widget to resize. */
2305 lw_allow_resizing (f->output_data.x->widget, True);
2306 }
2307 else
2308 {
2309 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
2310 XtTranslations override = XtParseTranslationTable (menuOverride);
2311
2312 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
2313 f->output_data.x->column_widget,
2314 0,
2315 popup_activate_callback,
2316 menubar_selection_callback,
2317 popup_deactivate_callback,
2318 menu_highlight_callback);
2319 f->output_data.x->menubar_widget = menubar_widget;
2320
2321 /* Make menu pop down on C-g. */
2322 XtOverrideTranslations (menubar_widget, override);
2323 }
2324
2325 {
2326 int menubar_size
2327 = (f->output_data.x->menubar_widget
2328 ? (f->output_data.x->menubar_widget->core.height
2329 + f->output_data.x->menubar_widget->core.border_width)
2330 : 0);
2331
2332 #if 0 /* Experimentally, we now get the right results
2333 for -geometry -0-0 without this. 24 Aug 96, rms. */
2334 #ifdef USE_LUCID
2335 if (FRAME_EXTERNAL_MENU_BAR (f))
2336 {
2337 Dimension ibw = 0;
2338 XtVaGetValues (f->output_data.x->column_widget,
2339 XtNinternalBorderWidth, &ibw, NULL);
2340 menubar_size += ibw;
2341 }
2342 #endif /* USE_LUCID */
2343 #endif /* 0 */
2344
2345 f->output_data.x->menubar_height = menubar_size;
2346 }
2347 #endif /* not USE_GTK */
2348
2349 free_menubar_widget_value_tree (first_wv);
2350 update_frame_menubar (f);
2351
2352 #ifdef USE_GTK
2353 xg_crazy_callback_abort = 0;
2354 #endif
2355
2356 UNBLOCK_INPUT;
2357 }
2358
2359 /* Called from Fx_create_frame to create the initial menubar of a frame
2360 before it is mapped, so that the window is mapped with the menubar already
2361 there instead of us tacking it on later and thrashing the window after it
2362 is visible. */
2363
2364 void
2365 initialize_frame_menubar (f)
2366 FRAME_PTR f;
2367 {
2368 /* This function is called before the first chance to redisplay
2369 the frame. It has to be, so the frame will have the right size. */
2370 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2371 set_frame_menubar (f, 1, 1);
2372 }
2373
2374
2375 /* Get rid of the menu bar of frame F, and free its storage.
2376 This is used when deleting a frame, and when turning off the menu bar.
2377 For GTK this function is in gtkutil.c. */
2378
2379 #ifndef USE_GTK
2380 void
2381 free_frame_menubar (f)
2382 FRAME_PTR f;
2383 {
2384 Widget menubar_widget;
2385
2386 menubar_widget = f->output_data.x->menubar_widget;
2387
2388 f->output_data.x->menubar_height = 0;
2389
2390 if (menubar_widget)
2391 {
2392 #ifdef USE_MOTIF
2393 /* Removing the menu bar magically changes the shell widget's x
2394 and y position of (0, 0) which, when the menu bar is turned
2395 on again, leads to pull-down menuss appearing in strange
2396 positions near the upper-left corner of the display. This
2397 happens only with some window managers like twm and ctwm,
2398 but not with other like Motif's mwm or kwm, because the
2399 latter generate ConfigureNotify events when the menu bar
2400 is switched off, which fixes the shell position. */
2401 Position x0, y0, x1, y1;
2402 #endif
2403
2404 BLOCK_INPUT;
2405
2406 #ifdef USE_MOTIF
2407 if (f->output_data.x->widget)
2408 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
2409 #endif
2410
2411 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
2412 f->output_data.x->menubar_widget = NULL;
2413
2414 #ifdef USE_MOTIF
2415 if (f->output_data.x->widget)
2416 {
2417 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
2418 if (x1 == 0 && y1 == 0)
2419 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
2420 }
2421 #endif
2422
2423 UNBLOCK_INPUT;
2424 }
2425 }
2426 #endif /* not USE_GTK */
2427
2428 #endif /* USE_X_TOOLKIT || USE_GTK */
2429 \f
2430 /* xmenu_show actually displays a menu using the panes and items in menu_items
2431 and returns the value selected from it.
2432 There are two versions of xmenu_show, one for Xt and one for Xlib.
2433 Both assume input is blocked by the caller. */
2434
2435 /* F is the frame the menu is for.
2436 X and Y are the frame-relative specified position,
2437 relative to the inside upper left corner of the frame F.
2438 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
2439 KEYMAPS is 1 if this menu was specified with keymaps;
2440 in that case, we return a list containing the chosen item's value
2441 and perhaps also the pane's prefix.
2442 TITLE is the specified menu title.
2443 ERROR is a place to store an error message string in case of failure.
2444 (We return nil on failure, but the value doesn't actually matter.) */
2445
2446 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
2447
2448 /* The item selected in the popup menu. */
2449 static Lisp_Object *volatile menu_item_selection;
2450
2451 #ifdef USE_GTK
2452
2453 /* Used when position a popup menu. See menu_position_func and
2454 create_and_show_popup_menu below. */
2455 struct next_popup_x_y
2456 {
2457 FRAME_PTR f;
2458 int x;
2459 int y;
2460 };
2461
2462 /* The menu position function to use if we are not putting a popup
2463 menu where the pointer is.
2464 MENU is the menu to pop up.
2465 X and Y shall on exit contain x/y where the menu shall pop up.
2466 PUSH_IN is not documented in the GTK manual.
2467 USER_DATA is any data passed in when calling gtk_menu_popup.
2468 Here it points to a struct next_popup_x_y where the coordinates
2469 to store in *X and *Y are as well as the frame for the popup.
2470
2471 Here only X and Y are used. */
2472 static void
2473 menu_position_func (menu, x, y, push_in, user_data)
2474 GtkMenu *menu;
2475 gint *x;
2476 gint *y;
2477 gboolean *push_in;
2478 gpointer user_data;
2479 {
2480 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
2481 GtkRequisition req;
2482 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
2483 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
2484
2485 *x = data->x;
2486 *y = data->y;
2487
2488 /* Check if there is room for the menu. If not, adjust x/y so that
2489 the menu is fully visible. */
2490 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2491 if (data->x + req.width > disp_width)
2492 *x -= data->x + req.width - disp_width;
2493 if (data->y + req.height > disp_height)
2494 *y -= data->y + req.height - disp_height;
2495 }
2496
2497 static void
2498 popup_selection_callback (widget, client_data)
2499 GtkWidget *widget;
2500 gpointer client_data;
2501 {
2502 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
2503
2504 if (xg_crazy_callback_abort) return;
2505 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
2506 }
2507
2508 static Lisp_Object
2509 pop_down_menu (arg)
2510 Lisp_Object arg;
2511 {
2512 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
2513
2514 popup_activated_flag = 0;
2515 BLOCK_INPUT;
2516 gtk_widget_destroy (GTK_WIDGET (p->pointer));
2517 UNBLOCK_INPUT;
2518 return Qnil;
2519 }
2520
2521 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2522 menu pops down.
2523 menu_item_selection will be set to the selection. */
2524 static void
2525 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2526 FRAME_PTR f;
2527 widget_value *first_wv;
2528 int x;
2529 int y;
2530 int for_click;
2531 {
2532 int i;
2533 GtkWidget *menu;
2534 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
2535 struct next_popup_x_y popup_x_y;
2536 int specpdl_count = SPECPDL_INDEX ();
2537
2538 xg_crazy_callback_abort = 1;
2539 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
2540 G_CALLBACK (popup_selection_callback),
2541 G_CALLBACK (popup_deactivate_callback),
2542 G_CALLBACK (menu_highlight_callback));
2543 xg_crazy_callback_abort = 0;
2544
2545 if (! for_click)
2546 {
2547 /* Not invoked by a click. pop up at x/y. */
2548 pos_func = menu_position_func;
2549
2550 /* Adjust coordinates to be root-window-relative. */
2551 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2552 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2553
2554 popup_x_y.x = x;
2555 popup_x_y.y = y;
2556 popup_x_y.f = f;
2557
2558 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
2559 }
2560 else
2561 {
2562 for (i = 0; i < 5; i++)
2563 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2564 break;
2565 }
2566
2567 /* Display the menu. */
2568 gtk_widget_show_all (menu);
2569 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
2570
2571 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
2572
2573 if (GTK_WIDGET_MAPPED (menu))
2574 {
2575 /* Set this to one. popup_widget_loop increases it by one, so it becomes
2576 two. show_help_echo uses this to detect popup menus. */
2577 popup_activated_flag = 1;
2578 /* Process events that apply to the menu. */
2579 popup_widget_loop (1, menu);
2580 }
2581
2582 unbind_to (specpdl_count, Qnil);
2583
2584 /* Must reset this manually because the button release event is not passed
2585 to Emacs event loop. */
2586 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2587 }
2588
2589 #else /* not USE_GTK */
2590
2591 /* We need a unique id for each widget handled by the Lucid Widget
2592 library.
2593
2594 For the main windows, and popup menus, we use this counter,
2595 which we increment each time after use. This starts from 1<<16.
2596
2597 For menu bars, we use numbers starting at 0, counted in
2598 next_menubar_widget_id. */
2599 LWLIB_ID widget_id_tick;
2600
2601 static void
2602 popup_selection_callback (widget, id, client_data)
2603 Widget widget;
2604 LWLIB_ID id;
2605 XtPointer client_data;
2606 {
2607 menu_item_selection = (Lisp_Object *) client_data;
2608 }
2609
2610 /* ARG is the LWLIB ID of the dialog box, represented
2611 as a Lisp object as (HIGHPART . LOWPART). */
2612
2613 static Lisp_Object
2614 pop_down_menu (arg)
2615 Lisp_Object arg;
2616 {
2617 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
2618 | XINT (XCDR (arg)));
2619
2620 BLOCK_INPUT;
2621 lw_destroy_all_widgets (id);
2622 UNBLOCK_INPUT;
2623 popup_activated_flag = 0;
2624
2625 return Qnil;
2626 }
2627
2628 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
2629 menu pops down.
2630 menu_item_selection will be set to the selection. */
2631 static void
2632 create_and_show_popup_menu (f, first_wv, x, y, for_click)
2633 FRAME_PTR f;
2634 widget_value *first_wv;
2635 int x;
2636 int y;
2637 int for_click;
2638 {
2639 int i;
2640 Arg av[2];
2641 int ac = 0;
2642 XButtonPressedEvent dummy;
2643 LWLIB_ID menu_id;
2644 Widget menu;
2645
2646 menu_id = widget_id_tick++;
2647 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
2648 f->output_data.x->widget, 1, 0,
2649 popup_selection_callback,
2650 popup_deactivate_callback,
2651 menu_highlight_callback);
2652
2653 dummy.type = ButtonPress;
2654 dummy.serial = 0;
2655 dummy.send_event = 0;
2656 dummy.display = FRAME_X_DISPLAY (f);
2657 dummy.time = CurrentTime;
2658 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
2659 dummy.window = dummy.root;
2660 dummy.subwindow = dummy.root;
2661 dummy.x = x;
2662 dummy.y = y;
2663
2664 /* Adjust coordinates to be root-window-relative. */
2665 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2666 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2667
2668 dummy.x_root = x;
2669 dummy.y_root = y;
2670
2671 dummy.state = 0;
2672 dummy.button = 0;
2673 for (i = 0; i < 5; i++)
2674 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
2675 dummy.button = i;
2676
2677 /* Don't allow any geometry request from the user. */
2678 XtSetArg (av[ac], XtNgeometry, 0); ac++;
2679 XtSetValues (menu, av, ac);
2680
2681 /* Display the menu. */
2682 lw_popup_menu (menu, (XEvent *) &dummy);
2683 popup_activated_flag = 1;
2684
2685 {
2686 int fact = 4 * sizeof (LWLIB_ID);
2687 int specpdl_count = SPECPDL_INDEX ();
2688 record_unwind_protect (pop_down_menu,
2689 Fcons (make_number (menu_id >> (fact)),
2690 make_number (menu_id & ~(-1 << (fact)))));
2691
2692 /* Process events that apply to the menu. */
2693 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
2694
2695 unbind_to (specpdl_count, Qnil);
2696 }
2697 }
2698
2699 #endif /* not USE_GTK */
2700
2701 static Lisp_Object
2702 xmenu_show (f, x, y, for_click, keymaps, title, error)
2703 FRAME_PTR f;
2704 int x;
2705 int y;
2706 int for_click;
2707 int keymaps;
2708 Lisp_Object title;
2709 char **error;
2710 {
2711 int i;
2712 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2713 widget_value **submenu_stack
2714 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
2715 Lisp_Object *subprefix_stack
2716 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
2717 int submenu_depth = 0;
2718
2719 int first_pane;
2720
2721 *error = NULL;
2722
2723 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2724 {
2725 *error = "Empty menu";
2726 return Qnil;
2727 }
2728
2729 /* Create a tree of widget_value objects
2730 representing the panes and their items. */
2731 wv = xmalloc_widget_value ();
2732 wv->name = "menu";
2733 wv->value = 0;
2734 wv->enabled = 1;
2735 wv->button_type = BUTTON_TYPE_NONE;
2736 wv->help =Qnil;
2737 first_wv = wv;
2738 first_pane = 1;
2739
2740 /* Loop over all panes and items, filling in the tree. */
2741 i = 0;
2742 while (i < menu_items_used)
2743 {
2744 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2745 {
2746 submenu_stack[submenu_depth++] = save_wv;
2747 save_wv = prev_wv;
2748 prev_wv = 0;
2749 first_pane = 1;
2750 i++;
2751 }
2752 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2753 {
2754 prev_wv = save_wv;
2755 save_wv = submenu_stack[--submenu_depth];
2756 first_pane = 0;
2757 i++;
2758 }
2759 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
2760 && submenu_depth != 0)
2761 i += MENU_ITEMS_PANE_LENGTH;
2762 /* Ignore a nil in the item list.
2763 It's meaningful only for dialog boxes. */
2764 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2765 i += 1;
2766 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2767 {
2768 /* Create a new pane. */
2769 Lisp_Object pane_name, prefix;
2770 char *pane_string;
2771
2772 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2773 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2774
2775 #ifndef HAVE_MULTILINGUAL_MENU
2776 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2777 {
2778 pane_name = ENCODE_MENU_STRING (pane_name);
2779 AREF (menu_items, i + MENU_ITEMS_PANE_NAME) = pane_name;
2780 }
2781 #endif
2782 pane_string = (NILP (pane_name)
2783 ? "" : (char *) SDATA (pane_name));
2784 /* If there is just one top-level pane, put all its items directly
2785 under the top-level menu. */
2786 if (menu_items_n_panes == 1)
2787 pane_string = "";
2788
2789 /* If the pane has a meaningful name,
2790 make the pane a top-level menu item
2791 with its items as a submenu beneath it. */
2792 if (!keymaps && strcmp (pane_string, ""))
2793 {
2794 wv = xmalloc_widget_value ();
2795 if (save_wv)
2796 save_wv->next = wv;
2797 else
2798 first_wv->contents = wv;
2799 wv->name = pane_string;
2800 if (keymaps && !NILP (prefix))
2801 wv->name++;
2802 wv->value = 0;
2803 wv->enabled = 1;
2804 wv->button_type = BUTTON_TYPE_NONE;
2805 wv->help = Qnil;
2806 save_wv = wv;
2807 prev_wv = 0;
2808 }
2809 else if (first_pane)
2810 {
2811 save_wv = wv;
2812 prev_wv = 0;
2813 }
2814 first_pane = 0;
2815 i += MENU_ITEMS_PANE_LENGTH;
2816 }
2817 else
2818 {
2819 /* Create a new item within current pane. */
2820 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2821 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2822 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2823 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2824 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2825 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2826 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2827 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2828
2829 #ifndef HAVE_MULTILINGUAL_MENU
2830 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2831 {
2832 item_name = ENCODE_MENU_STRING (item_name);
2833 AREF (menu_items, i + MENU_ITEMS_ITEM_NAME) = item_name;
2834 }
2835
2836 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2837 {
2838 descrip = ENCODE_MENU_STRING (descrip);
2839 AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY) = descrip;
2840 }
2841 #endif /* not HAVE_MULTILINGUAL_MENU */
2842
2843 wv = xmalloc_widget_value ();
2844 if (prev_wv)
2845 prev_wv->next = wv;
2846 else
2847 save_wv->contents = wv;
2848 wv->name = (char *) SDATA (item_name);
2849 if (!NILP (descrip))
2850 wv->key = (char *) SDATA (descrip);
2851 wv->value = 0;
2852 /* If this item has a null value,
2853 make the call_data null so that it won't display a box
2854 when the mouse is on it. */
2855 wv->call_data
2856 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2857 wv->enabled = !NILP (enable);
2858
2859 if (NILP (type))
2860 wv->button_type = BUTTON_TYPE_NONE;
2861 else if (EQ (type, QCtoggle))
2862 wv->button_type = BUTTON_TYPE_TOGGLE;
2863 else if (EQ (type, QCradio))
2864 wv->button_type = BUTTON_TYPE_RADIO;
2865 else
2866 abort ();
2867
2868 wv->selected = !NILP (selected);
2869
2870 if (! STRINGP (help))
2871 help = Qnil;
2872
2873 wv->help = help;
2874
2875 prev_wv = wv;
2876
2877 i += MENU_ITEMS_ITEM_LENGTH;
2878 }
2879 }
2880
2881 /* Deal with the title, if it is non-nil. */
2882 if (!NILP (title))
2883 {
2884 widget_value *wv_title = xmalloc_widget_value ();
2885 widget_value *wv_sep1 = xmalloc_widget_value ();
2886 widget_value *wv_sep2 = xmalloc_widget_value ();
2887
2888 wv_sep2->name = "--";
2889 wv_sep2->next = first_wv->contents;
2890 wv_sep2->help = Qnil;
2891
2892 wv_sep1->name = "--";
2893 wv_sep1->next = wv_sep2;
2894 wv_sep1->help = Qnil;
2895
2896 #ifndef HAVE_MULTILINGUAL_MENU
2897 if (STRING_MULTIBYTE (title))
2898 title = ENCODE_MENU_STRING (title);
2899 #endif
2900
2901 wv_title->name = (char *) SDATA (title);
2902 wv_title->enabled = TRUE;
2903 wv_title->button_type = BUTTON_TYPE_NONE;
2904 wv_title->next = wv_sep1;
2905 wv_title->help = Qnil;
2906 first_wv->contents = wv_title;
2907 }
2908
2909 /* No selection has been chosen yet. */
2910 menu_item_selection = 0;
2911
2912 /* Actually create and show the menu until popped down. */
2913 create_and_show_popup_menu (f, first_wv, x, y, for_click);
2914
2915 /* Free the widget_value objects we used to specify the contents. */
2916 free_menubar_widget_value_tree (first_wv);
2917
2918 /* Find the selected item, and its pane, to return
2919 the proper value. */
2920 if (menu_item_selection != 0)
2921 {
2922 Lisp_Object prefix, entry;
2923
2924 prefix = entry = Qnil;
2925 i = 0;
2926 while (i < menu_items_used)
2927 {
2928 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2929 {
2930 subprefix_stack[submenu_depth++] = prefix;
2931 prefix = entry;
2932 i++;
2933 }
2934 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2935 {
2936 prefix = subprefix_stack[--submenu_depth];
2937 i++;
2938 }
2939 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2940 {
2941 prefix
2942 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2943 i += MENU_ITEMS_PANE_LENGTH;
2944 }
2945 /* Ignore a nil in the item list.
2946 It's meaningful only for dialog boxes. */
2947 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2948 i += 1;
2949 else
2950 {
2951 entry
2952 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2953 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2954 {
2955 if (keymaps != 0)
2956 {
2957 int j;
2958
2959 entry = Fcons (entry, Qnil);
2960 if (!NILP (prefix))
2961 entry = Fcons (prefix, entry);
2962 for (j = submenu_depth - 1; j >= 0; j--)
2963 if (!NILP (subprefix_stack[j]))
2964 entry = Fcons (subprefix_stack[j], entry);
2965 }
2966 return entry;
2967 }
2968 i += MENU_ITEMS_ITEM_LENGTH;
2969 }
2970 }
2971 }
2972 else if (!for_click)
2973 /* Make "Cancel" equivalent to C-g. */
2974 Fsignal (Qquit, Qnil);
2975
2976 return Qnil;
2977 }
2978 \f
2979 #ifdef USE_GTK
2980 static void
2981 dialog_selection_callback (widget, client_data)
2982 GtkWidget *widget;
2983 gpointer client_data;
2984 {
2985 /* The EMACS_INT cast avoids a warning. There's no problem
2986 as long as pointers have enough bits to hold small integers. */
2987 if ((int) (EMACS_INT) client_data != -1)
2988 menu_item_selection = (Lisp_Object *) client_data;
2989
2990 popup_activated_flag = 0;
2991 }
2992
2993 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2994 dialog pops down.
2995 menu_item_selection will be set to the selection. */
2996 static void
2997 create_and_show_dialog (f, first_wv)
2998 FRAME_PTR f;
2999 widget_value *first_wv;
3000 {
3001 GtkWidget *menu;
3002
3003 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
3004 G_CALLBACK (dialog_selection_callback),
3005 G_CALLBACK (popup_deactivate_callback),
3006 0);
3007
3008 if (menu)
3009 {
3010 int specpdl_count = SPECPDL_INDEX ();
3011 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
3012
3013 /* Display the menu. */
3014 gtk_widget_show_all (menu);
3015
3016 /* Process events that apply to the menu. */
3017 popup_widget_loop (1, menu);
3018
3019 unbind_to (specpdl_count, Qnil);
3020 }
3021 }
3022
3023 #else /* not USE_GTK */
3024 static void
3025 dialog_selection_callback (widget, id, client_data)
3026 Widget widget;
3027 LWLIB_ID id;
3028 XtPointer client_data;
3029 {
3030 /* The EMACS_INT cast avoids a warning. There's no problem
3031 as long as pointers have enough bits to hold small integers. */
3032 if ((int) (EMACS_INT) client_data != -1)
3033 menu_item_selection = (Lisp_Object *) client_data;
3034
3035 BLOCK_INPUT;
3036 lw_destroy_all_widgets (id);
3037 UNBLOCK_INPUT;
3038 popup_activated_flag = 0;
3039 }
3040
3041
3042 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
3043 dialog pops down.
3044 menu_item_selection will be set to the selection. */
3045 static void
3046 create_and_show_dialog (f, first_wv)
3047 FRAME_PTR f;
3048 widget_value *first_wv;
3049 {
3050 LWLIB_ID dialog_id;
3051
3052 dialog_id = widget_id_tick++;
3053 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
3054 f->output_data.x->widget, 1, 0,
3055 dialog_selection_callback, 0, 0);
3056 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
3057
3058 /* Display the dialog box. */
3059 lw_pop_up_all_widgets (dialog_id);
3060 popup_activated_flag = 1;
3061
3062 /* Process events that apply to the dialog box.
3063 Also handle timers. */
3064 {
3065 int count = SPECPDL_INDEX ();
3066 int fact = 4 * sizeof (LWLIB_ID);
3067
3068 /* xdialog_show_unwind is responsible for popping the dialog box down. */
3069 record_unwind_protect (pop_down_menu,
3070 Fcons (make_number (dialog_id >> (fact)),
3071 make_number (dialog_id & ~(-1 << (fact)))));
3072
3073 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
3074 dialog_id, 1);
3075
3076 unbind_to (count, Qnil);
3077 }
3078 }
3079
3080 #endif /* not USE_GTK */
3081
3082 static char * button_names [] = {
3083 "button1", "button2", "button3", "button4", "button5",
3084 "button6", "button7", "button8", "button9", "button10" };
3085
3086 static Lisp_Object
3087 xdialog_show (f, keymaps, title, header, error_name)
3088 FRAME_PTR f;
3089 int keymaps;
3090 Lisp_Object title, header;
3091 char **error_name;
3092 {
3093 int i, nb_buttons=0;
3094 char dialog_name[6];
3095
3096 widget_value *wv, *first_wv = 0, *prev_wv = 0;
3097
3098 /* Number of elements seen so far, before boundary. */
3099 int left_count = 0;
3100 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
3101 int boundary_seen = 0;
3102
3103 *error_name = NULL;
3104
3105 if (menu_items_n_panes > 1)
3106 {
3107 *error_name = "Multiple panes in dialog box";
3108 return Qnil;
3109 }
3110
3111 /* Create a tree of widget_value objects
3112 representing the text label and buttons. */
3113 {
3114 Lisp_Object pane_name, prefix;
3115 char *pane_string;
3116 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
3117 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
3118 pane_string = (NILP (pane_name)
3119 ? "" : (char *) SDATA (pane_name));
3120 prev_wv = xmalloc_widget_value ();
3121 prev_wv->value = pane_string;
3122 if (keymaps && !NILP (prefix))
3123 prev_wv->name++;
3124 prev_wv->enabled = 1;
3125 prev_wv->name = "message";
3126 prev_wv->help = Qnil;
3127 first_wv = prev_wv;
3128
3129 /* Loop over all panes and items, filling in the tree. */
3130 i = MENU_ITEMS_PANE_LENGTH;
3131 while (i < menu_items_used)
3132 {
3133
3134 /* Create a new item within current pane. */
3135 Lisp_Object item_name, enable, descrip;
3136 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3137 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3138 descrip
3139 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3140
3141 if (NILP (item_name))
3142 {
3143 free_menubar_widget_value_tree (first_wv);
3144 *error_name = "Submenu in dialog items";
3145 return Qnil;
3146 }
3147 if (EQ (item_name, Qquote))
3148 {
3149 /* This is the boundary between left-side elts
3150 and right-side elts. Stop incrementing right_count. */
3151 boundary_seen = 1;
3152 i++;
3153 continue;
3154 }
3155 if (nb_buttons >= 9)
3156 {
3157 free_menubar_widget_value_tree (first_wv);
3158 *error_name = "Too many dialog items";
3159 return Qnil;
3160 }
3161
3162 wv = xmalloc_widget_value ();
3163 prev_wv->next = wv;
3164 wv->name = (char *) button_names[nb_buttons];
3165 if (!NILP (descrip))
3166 wv->key = (char *) SDATA (descrip);
3167 wv->value = (char *) SDATA (item_name);
3168 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
3169 wv->enabled = !NILP (enable);
3170 wv->help = Qnil;
3171 prev_wv = wv;
3172
3173 if (! boundary_seen)
3174 left_count++;
3175
3176 nb_buttons++;
3177 i += MENU_ITEMS_ITEM_LENGTH;
3178 }
3179
3180 /* If the boundary was not specified,
3181 by default put half on the left and half on the right. */
3182 if (! boundary_seen)
3183 left_count = nb_buttons - nb_buttons / 2;
3184
3185 wv = xmalloc_widget_value ();
3186 wv->name = dialog_name;
3187 wv->help = Qnil;
3188
3189 /* Frame title: 'Q' = Question, 'I' = Information.
3190 Can also have 'E' = Error if, one day, we want
3191 a popup for errors. */
3192 if (NILP(header))
3193 dialog_name[0] = 'Q';
3194 else
3195 dialog_name[0] = 'I';
3196
3197 /* Dialog boxes use a really stupid name encoding
3198 which specifies how many buttons to use
3199 and how many buttons are on the right. */
3200 dialog_name[1] = '0' + nb_buttons;
3201 dialog_name[2] = 'B';
3202 dialog_name[3] = 'R';
3203 /* Number of buttons to put on the right. */
3204 dialog_name[4] = '0' + nb_buttons - left_count;
3205 dialog_name[5] = 0;
3206 wv->contents = first_wv;
3207 first_wv = wv;
3208 }
3209
3210 /* No selection has been chosen yet. */
3211 menu_item_selection = 0;
3212
3213 /* Actually create and show the dialog. */
3214 create_and_show_dialog (f, first_wv);
3215
3216 /* Free the widget_value objects we used to specify the contents. */
3217 free_menubar_widget_value_tree (first_wv);
3218
3219 /* Find the selected item, and its pane, to return
3220 the proper value. */
3221 if (menu_item_selection != 0)
3222 {
3223 Lisp_Object prefix;
3224
3225 prefix = Qnil;
3226 i = 0;
3227 while (i < menu_items_used)
3228 {
3229 Lisp_Object entry;
3230
3231 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3232 {
3233 prefix
3234 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3235 i += MENU_ITEMS_PANE_LENGTH;
3236 }
3237 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3238 {
3239 /* This is the boundary between left-side elts and
3240 right-side elts. */
3241 ++i;
3242 }
3243 else
3244 {
3245 entry
3246 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3247 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
3248 {
3249 if (keymaps != 0)
3250 {
3251 entry = Fcons (entry, Qnil);
3252 if (!NILP (prefix))
3253 entry = Fcons (prefix, entry);
3254 }
3255 return entry;
3256 }
3257 i += MENU_ITEMS_ITEM_LENGTH;
3258 }
3259 }
3260 }
3261 else
3262 /* Make "Cancel" equivalent to C-g. */
3263 Fsignal (Qquit, Qnil);
3264
3265 return Qnil;
3266 }
3267
3268 #else /* not USE_X_TOOLKIT && not USE_GTK */
3269
3270 /* The frame of the last activated non-toolkit menu bar.
3271 Used to generate menu help events. */
3272
3273 static struct frame *menu_help_frame;
3274
3275
3276 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
3277
3278 PANE is the pane number, and ITEM is the menu item number in
3279 the menu (currently not used).
3280
3281 This cannot be done with generating a HELP_EVENT because
3282 XMenuActivate contains a loop that doesn't let Emacs process
3283 keyboard events. */
3284
3285 static void
3286 menu_help_callback (help_string, pane, item)
3287 char *help_string;
3288 int pane, item;
3289 {
3290 extern Lisp_Object Qmenu_item;
3291 Lisp_Object *first_item;
3292 Lisp_Object pane_name;
3293 Lisp_Object menu_object;
3294
3295 first_item = XVECTOR (menu_items)->contents;
3296 if (EQ (first_item[0], Qt))
3297 pane_name = first_item[MENU_ITEMS_PANE_NAME];
3298 else if (EQ (first_item[0], Qquote))
3299 /* This shouldn't happen, see xmenu_show. */
3300 pane_name = empty_string;
3301 else
3302 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
3303
3304 /* (menu-item MENU-NAME PANE-NUMBER) */
3305 menu_object = Fcons (Qmenu_item,
3306 Fcons (pane_name,
3307 Fcons (make_number (pane), Qnil)));
3308 show_help_echo (help_string ? build_string (help_string) : Qnil,
3309 Qnil, menu_object, make_number (item), 1);
3310 }
3311
3312 static Lisp_Object
3313 pop_down_menu (arg)
3314 Lisp_Object arg;
3315 {
3316 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
3317 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
3318
3319 FRAME_PTR f = p1->pointer;
3320 XMenu *menu = p2->pointer;
3321
3322 BLOCK_INPUT;
3323 #ifndef MSDOS
3324 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
3325 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
3326 #endif
3327 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3328
3329 #ifdef HAVE_X_WINDOWS
3330 /* Assume the mouse has moved out of the X window.
3331 If it has actually moved in, we will get an EnterNotify. */
3332 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
3333
3334 /* State that no mouse buttons are now held.
3335 (The oldXMenu code doesn't track this info for us.)
3336 That is not necessarily true, but the fiction leads to reasonable
3337 results, and it is a pain to ask which are actually held now. */
3338 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
3339
3340 #endif /* HAVE_X_WINDOWS */
3341
3342 UNBLOCK_INPUT;
3343
3344 return Qnil;
3345 }
3346
3347
3348 static Lisp_Object
3349 xmenu_show (f, x, y, for_click, keymaps, title, error)
3350 FRAME_PTR f;
3351 int x, y;
3352 int for_click;
3353 int keymaps;
3354 Lisp_Object title;
3355 char **error;
3356 {
3357 Window root;
3358 XMenu *menu;
3359 int pane, selidx, lpane, status;
3360 Lisp_Object entry, pane_prefix;
3361 char *datap;
3362 int ulx, uly, width, height;
3363 int dispwidth, dispheight;
3364 int i, j, lines, maxlines;
3365 int maxwidth;
3366 int dummy_int;
3367 unsigned int dummy_uint;
3368 int specpdl_count = SPECPDL_INDEX ();
3369
3370 *error = 0;
3371 if (menu_items_n_panes == 0)
3372 return Qnil;
3373
3374 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
3375 {
3376 *error = "Empty menu";
3377 return Qnil;
3378 }
3379
3380 /* Figure out which root window F is on. */
3381 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
3382 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
3383 &dummy_uint, &dummy_uint);
3384
3385 /* Make the menu on that window. */
3386 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
3387 if (menu == NULL)
3388 {
3389 *error = "Can't create menu";
3390 return Qnil;
3391 }
3392
3393 /* Don't GC while we prepare and show the menu,
3394 because we give the oldxmenu library pointers to the
3395 contents of strings. */
3396 inhibit_garbage_collection ();
3397
3398 #ifdef HAVE_X_WINDOWS
3399 /* Adjust coordinates to relative to the outer (window manager) window. */
3400 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
3401 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
3402 #endif /* HAVE_X_WINDOWS */
3403
3404 /* Adjust coordinates to be root-window-relative. */
3405 x += f->left_pos;
3406 y += f->top_pos;
3407
3408 /* Create all the necessary panes and their items. */
3409 maxlines = lines = i = 0;
3410 while (i < menu_items_used)
3411 {
3412 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3413 {
3414 /* Create a new pane. */
3415 Lisp_Object pane_name, prefix;
3416 char *pane_string;
3417
3418 maxlines = max (maxlines, lines);
3419 lines = 0;
3420 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
3421 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3422 pane_string = (NILP (pane_name)
3423 ? "" : (char *) SDATA (pane_name));
3424 if (keymaps && !NILP (prefix))
3425 pane_string++;
3426
3427 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
3428 if (lpane == XM_FAILURE)
3429 {
3430 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3431 *error = "Can't create pane";
3432 return Qnil;
3433 }
3434 i += MENU_ITEMS_PANE_LENGTH;
3435
3436 /* Find the width of the widest item in this pane. */
3437 maxwidth = 0;
3438 j = i;
3439 while (j < menu_items_used)
3440 {
3441 Lisp_Object item;
3442 item = XVECTOR (menu_items)->contents[j];
3443 if (EQ (item, Qt))
3444 break;
3445 if (NILP (item))
3446 {
3447 j++;
3448 continue;
3449 }
3450 width = SBYTES (item);
3451 if (width > maxwidth)
3452 maxwidth = width;
3453
3454 j += MENU_ITEMS_ITEM_LENGTH;
3455 }
3456 }
3457 /* Ignore a nil in the item list.
3458 It's meaningful only for dialog boxes. */
3459 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
3460 i += 1;
3461 else
3462 {
3463 /* Create a new item within current pane. */
3464 Lisp_Object item_name, enable, descrip, help;
3465 unsigned char *item_data;
3466 char *help_string;
3467
3468 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
3469 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
3470 descrip
3471 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
3472 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
3473 help_string = STRINGP (help) ? SDATA (help) : NULL;
3474
3475 if (!NILP (descrip))
3476 {
3477 int gap = maxwidth - SBYTES (item_name);
3478 #ifdef C_ALLOCA
3479 Lisp_Object spacer;
3480 spacer = Fmake_string (make_number (gap), make_number (' '));
3481 item_name = concat2 (item_name, spacer);
3482 item_name = concat2 (item_name, descrip);
3483 item_data = SDATA (item_name);
3484 #else
3485 /* if alloca is fast, use that to make the space,
3486 to reduce gc needs. */
3487 item_data
3488 = (unsigned char *) alloca (maxwidth
3489 + SBYTES (descrip) + 1);
3490 bcopy (SDATA (item_name), item_data,
3491 SBYTES (item_name));
3492 for (j = SCHARS (item_name); j < maxwidth; j++)
3493 item_data[j] = ' ';
3494 bcopy (SDATA (descrip), item_data + j,
3495 SBYTES (descrip));
3496 item_data[j + SBYTES (descrip)] = 0;
3497 #endif
3498 }
3499 else
3500 item_data = SDATA (item_name);
3501
3502 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
3503 menu, lpane, 0, item_data,
3504 !NILP (enable), help_string)
3505 == XM_FAILURE)
3506 {
3507 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
3508 *error = "Can't add selection to menu";
3509 return Qnil;
3510 }
3511 i += MENU_ITEMS_ITEM_LENGTH;
3512 lines++;
3513 }
3514 }
3515
3516 maxlines = max (maxlines, lines);
3517
3518 /* All set and ready to fly. */
3519 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
3520 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3521 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
3522 x = min (x, dispwidth);
3523 y = min (y, dispheight);
3524 x = max (x, 1);
3525 y = max (y, 1);
3526 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
3527 &ulx, &uly, &width, &height);
3528 if (ulx+width > dispwidth)
3529 {
3530 x -= (ulx + width) - dispwidth;
3531 ulx = dispwidth - width;
3532 }
3533 if (uly+height > dispheight)
3534 {
3535 y -= (uly + height) - dispheight;
3536 uly = dispheight - height;
3537 }
3538 if (ulx < 0) x -= ulx;
3539 if (uly < 0) y -= uly;
3540
3541 if (! for_click)
3542 {
3543 /* If position was not given by a mouse click, adjust so upper left
3544 corner of the menu as a whole ends up at given coordinates. This
3545 is what x-popup-menu says in its documentation. */
3546 x += width/2;
3547 y += 1.5*height/(maxlines+2);
3548 }
3549
3550 XMenuSetAEQ (menu, TRUE);
3551 XMenuSetFreeze (menu, TRUE);
3552 pane = selidx = 0;
3553
3554 #ifndef MSDOS
3555 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
3556 #endif
3557
3558 record_unwind_protect (pop_down_menu,
3559 Fcons (make_save_value (f, 0),
3560 make_save_value (menu, 0)));
3561
3562 /* Help display under X won't work because XMenuActivate contains
3563 a loop that doesn't give Emacs a chance to process it. */
3564 menu_help_frame = f;
3565 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
3566 x, y, ButtonReleaseMask, &datap,
3567 menu_help_callback);
3568
3569 switch (status)
3570 {
3571 case XM_SUCCESS:
3572 #ifdef XDEBUG
3573 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
3574 #endif
3575
3576 /* Find the item number SELIDX in pane number PANE. */
3577 i = 0;
3578 while (i < menu_items_used)
3579 {
3580 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
3581 {
3582 if (pane == 0)
3583 pane_prefix
3584 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
3585 pane--;
3586 i += MENU_ITEMS_PANE_LENGTH;
3587 }
3588 else
3589 {
3590 if (pane == -1)
3591 {
3592 if (selidx == 0)
3593 {
3594 entry
3595 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
3596 if (keymaps != 0)
3597 {
3598 entry = Fcons (entry, Qnil);
3599 if (!NILP (pane_prefix))
3600 entry = Fcons (pane_prefix, entry);
3601 }
3602 break;
3603 }
3604 selidx--;
3605 }
3606 i += MENU_ITEMS_ITEM_LENGTH;
3607 }
3608 }
3609 break;
3610
3611 case XM_FAILURE:
3612 *error = "Can't activate menu";
3613 case XM_IA_SELECT:
3614 entry = Qnil;
3615 break;
3616 case XM_NO_SELECT:
3617 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
3618 the menu was invoked with a mouse event as POSITION). */
3619 if (! for_click)
3620 Fsignal (Qquit, Qnil);
3621 entry = Qnil;
3622 break;
3623 }
3624
3625 unbind_to (specpdl_count, Qnil);
3626
3627 return entry;
3628 }
3629
3630 #endif /* not USE_X_TOOLKIT */
3631
3632 #endif /* HAVE_MENUS */
3633 \f
3634 void
3635 syms_of_xmenu ()
3636 {
3637 staticpro (&menu_items);
3638 menu_items = Qnil;
3639 menu_items_inuse = Qnil;
3640
3641 Qdebug_on_next_call = intern ("debug-on-next-call");
3642 staticpro (&Qdebug_on_next_call);
3643
3644 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
3645 doc: /* Frame for which we are updating a menu.
3646 The enable predicate for a menu command should check this variable. */);
3647 Vmenu_updating_frame = Qnil;
3648
3649 #ifdef USE_X_TOOLKIT
3650 widget_id_tick = (1<<16);
3651 next_menubar_widget_id = 1;
3652 #endif
3653
3654 defsubr (&Sx_popup_menu);
3655 #ifdef HAVE_MENUS
3656 defsubr (&Sx_popup_dialog);
3657 #endif
3658 }
3659
3660 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
3661 (do not change this comment) */