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