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