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