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