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