(menu_item_equiv_key): Delete the code that rejected
[bpt/emacs.git] / src / xmenu.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
21 *
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
24 *
25 */
26
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
29
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
31
32 /* On 4.3 this loses if it comes after xterm.h. */
33 #include <signal.h>
34 #include <config.h>
35
36 #include <stdio.h>
37 #include "lisp.h"
38 #include "termhooks.h"
39 #include "frame.h"
40 #include "window.h"
41 #include "keyboard.h"
42 #include "blockinput.h"
43 #include "puresize.h"
44
45 #ifdef MSDOS
46 #include "msdos.h"
47 #endif
48
49 #ifdef HAVE_X_WINDOWS
50 /* This may include sys/types.h, and that somehow loses
51 if this is not done before the other system files. */
52 #include "xterm.h"
53 #endif
54
55 /* Load sys/types.h if not already loaded.
56 In some systems loading it twice is suicidal. */
57 #ifndef makedev
58 #include <sys/types.h>
59 #endif
60
61 #include "dispextern.h"
62
63 #ifdef HAVE_X_WINDOWS
64 #ifdef USE_X_TOOLKIT
65 #include <X11/Xlib.h>
66 #include <X11/IntrinsicP.h>
67 #include <X11/CoreP.h>
68 #include <X11/StringDefs.h>
69 #include <X11/Shell.h>
70 #include "../lwlib/lwlib.h"
71 #else /* not USE_X_TOOLKIT */
72 #include "../oldXMenu/XMenu.h"
73 #endif /* not USE_X_TOOLKIT */
74 #endif /* HAVE_X_WINDOWS */
75
76 #define min(x,y) (((x) < (y)) ? (x) : (y))
77 #define max(x,y) (((x) > (y)) ? (x) : (y))
78
79 #ifndef TRUE
80 #define TRUE 1
81 #define FALSE 0
82 #endif /* no TRUE */
83
84 extern Lisp_Object Qmenu_enable;
85 extern Lisp_Object Qmenu_bar;
86 extern Lisp_Object Qmouse_click, Qevent_kind;
87
88 #ifdef USE_X_TOOLKIT
89 extern void process_expose_from_menu ();
90 extern XtAppContext Xt_app_con;
91
92 static Lisp_Object xdialog_show ();
93 void popup_get_selection ();
94 #endif
95
96 static Lisp_Object xmenu_show ();
97 static void keymap_panes ();
98 static void single_keymap_panes ();
99 static void list_of_panes ();
100 static void list_of_items ();
101 \f
102 /* This holds a Lisp vector that holds the results of decoding
103 the keymaps or alist-of-alists that specify a menu.
104
105 It describes the panes and items within the panes.
106
107 Each pane is described by 3 elements in the vector:
108 t, the pane name, the pane's prefix key.
109 Then follow the pane's items, with 5 elements per item:
110 the item string, the enable flag, the item's value,
111 the definition, and the equivalent keyboard key's description string.
112
113 In some cases, multiple levels of menus may be described.
114 A single vector slot containing nil indicates the start of a submenu.
115 A single vector slot containing lambda indicates the end of a submenu.
116 The submenu follows a menu item which is the way to reach the submenu.
117
118 A single vector slot containing quote indicates that the
119 following items should appear on the right of a dialog box.
120
121 Using a Lisp vector to hold this information while we decode it
122 takes care of protecting all the data from GC. */
123
124 #define MENU_ITEMS_PANE_NAME 1
125 #define MENU_ITEMS_PANE_PREFIX 2
126 #define MENU_ITEMS_PANE_LENGTH 3
127
128 #define MENU_ITEMS_ITEM_NAME 0
129 #define MENU_ITEMS_ITEM_ENABLE 1
130 #define MENU_ITEMS_ITEM_VALUE 2
131 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
132 #define MENU_ITEMS_ITEM_DEFINITION 4
133 #define MENU_ITEMS_ITEM_LENGTH 5
134
135 static Lisp_Object menu_items;
136
137 /* Number of slots currently allocated in menu_items. */
138 static int menu_items_allocated;
139
140 /* This is the index in menu_items of the first empty slot. */
141 static int menu_items_used;
142
143 /* The number of panes currently recorded in menu_items,
144 excluding those within submenus. */
145 static int menu_items_n_panes;
146
147 /* Current depth within submenus. */
148 static int menu_items_submenu_depth;
149
150 /* Flag which when set indicates a dialog or menu has been posted by
151 Xt on behalf of one of the widget sets. */
152 static int popup_activated_flag;
153
154 \f
155 /* Initialize the menu_items structure if we haven't already done so.
156 Also mark it as currently empty. */
157
158 static void
159 init_menu_items ()
160 {
161 if (NILP (menu_items))
162 {
163 menu_items_allocated = 60;
164 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
165 }
166
167 menu_items_used = 0;
168 menu_items_n_panes = 0;
169 menu_items_submenu_depth = 0;
170 }
171
172 /* Call at the end of generating the data in menu_items.
173 This fills in the number of items in the last pane. */
174
175 static void
176 finish_menu_items ()
177 {
178 }
179
180 /* Call when finished using the data for the current menu
181 in menu_items. */
182
183 static void
184 discard_menu_items ()
185 {
186 /* Free the structure if it is especially large.
187 Otherwise, hold on to it, to save time. */
188 if (menu_items_allocated > 200)
189 {
190 menu_items = Qnil;
191 menu_items_allocated = 0;
192 }
193 }
194
195 /* Make the menu_items vector twice as large. */
196
197 static void
198 grow_menu_items ()
199 {
200 Lisp_Object old;
201 int old_size = menu_items_allocated;
202 old = menu_items;
203
204 menu_items_allocated *= 2;
205 menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil);
206 bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents,
207 old_size * sizeof (Lisp_Object));
208 }
209
210 /* Begin a submenu. */
211
212 static void
213 push_submenu_start ()
214 {
215 if (menu_items_used + 1 > menu_items_allocated)
216 grow_menu_items ();
217
218 XVECTOR (menu_items)->contents[menu_items_used++] = Qnil;
219 menu_items_submenu_depth++;
220 }
221
222 /* End a submenu. */
223
224 static void
225 push_submenu_end ()
226 {
227 if (menu_items_used + 1 > menu_items_allocated)
228 grow_menu_items ();
229
230 XVECTOR (menu_items)->contents[menu_items_used++] = Qlambda;
231 menu_items_submenu_depth--;
232 }
233
234 /* Indicate boundary between left and right. */
235
236 static void
237 push_left_right_boundary ()
238 {
239 if (menu_items_used + 1 > menu_items_allocated)
240 grow_menu_items ();
241
242 XVECTOR (menu_items)->contents[menu_items_used++] = Qquote;
243 }
244
245 /* Start a new menu pane in menu_items..
246 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
247
248 static void
249 push_menu_pane (name, prefix_vec)
250 Lisp_Object name, prefix_vec;
251 {
252 if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated)
253 grow_menu_items ();
254
255 if (menu_items_submenu_depth == 0)
256 menu_items_n_panes++;
257 XVECTOR (menu_items)->contents[menu_items_used++] = Qt;
258 XVECTOR (menu_items)->contents[menu_items_used++] = name;
259 XVECTOR (menu_items)->contents[menu_items_used++] = prefix_vec;
260 }
261
262 /* Push one menu item into the current pane.
263 NAME is the string to display. ENABLE if non-nil means
264 this item can be selected. KEY is the key generated by
265 choosing this item, or nil if this item doesn't really have a definition.
266 DEF is the definition of this item.
267 EQUIV is the textual description of the keyboard equivalent for
268 this item (or nil if none). */
269
270 static void
271 push_menu_item (name, enable, key, def, equiv)
272 Lisp_Object name, enable, key, def, equiv;
273 {
274 if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated)
275 grow_menu_items ();
276
277 XVECTOR (menu_items)->contents[menu_items_used++] = name;
278 XVECTOR (menu_items)->contents[menu_items_used++] = enable;
279 XVECTOR (menu_items)->contents[menu_items_used++] = key;
280 XVECTOR (menu_items)->contents[menu_items_used++] = equiv;
281 XVECTOR (menu_items)->contents[menu_items_used++] = def;
282 }
283 \f
284 /* Figure out the current keyboard equivalent of a menu item ITEM1.
285 The item string for menu display should be ITEM_STRING.
286 Store the equivalent keyboard key sequence's
287 textual description into *DESCRIP_PTR.
288 Also cache them in the item itself.
289 Return the real definition to execute. */
290
291 static Lisp_Object
292 menu_item_equiv_key (item_string, item1, descrip_ptr)
293 Lisp_Object item_string;
294 Lisp_Object item1;
295 Lisp_Object *descrip_ptr;
296 {
297 /* This is the real definition--the function to run. */
298 Lisp_Object def;
299 /* This is the sublist that records cached equiv key data
300 so we can save time. */
301 Lisp_Object cachelist;
302 /* These are the saved equivalent keyboard key sequence
303 and its key-description. */
304 Lisp_Object savedkey, descrip;
305 Lisp_Object def1;
306 int changed = 0;
307 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
308
309 /* If a help string follows the item string, skip it. */
310 if (CONSP (XCONS (item1)->cdr)
311 && STRINGP (XCONS (XCONS (item1)->cdr)->car))
312 item1 = XCONS (item1)->cdr;
313
314 def = Fcdr (item1);
315
316 /* Get out the saved equivalent-keyboard-key info. */
317 cachelist = savedkey = descrip = Qnil;
318 if (CONSP (def) && CONSP (XCONS (def)->car)
319 && (NILP (XCONS (XCONS (def)->car)->car)
320 || VECTORP (XCONS (XCONS (def)->car)->car)))
321 {
322 cachelist = XCONS (def)->car;
323 def = XCONS (def)->cdr;
324 savedkey = XCONS (cachelist)->car;
325 descrip = XCONS (cachelist)->cdr;
326 }
327
328 GCPRO4 (def, def1, savedkey, descrip);
329
330 /* Is it still valid? */
331 def1 = Qnil;
332 if (!NILP (savedkey))
333 def1 = Fkey_binding (savedkey, Qnil);
334 /* If not, update it. */
335 if (! EQ (def1, def)
336 /* If the command is an alias for another
337 (such as easymenu.el and lmenu.el set it up),
338 check if the original command matches the cached command. */
339 && !(SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function)
340 && EQ (def1, XSYMBOL (def)->function))
341 /* If something had no key binding before, don't recheck it--
342 doing that takes too much time and makes menus too slow. */
343 && !(!NILP (cachelist) && NILP (savedkey)))
344 {
345 changed = 1;
346 descrip = Qnil;
347 /* If the command is an alias for another
348 (such as easymenu.el and lmenu.el set it up),
349 see if the original command name has equivalent keys. */
350 if (SYMBOLP (def) && SYMBOLP (XSYMBOL (def)->function))
351 savedkey = Fwhere_is_internal (XSYMBOL (def)->function,
352 Qnil, Qt, Qnil);
353 else
354 /* Otherwise look up the specified command itself.
355 We don't try both, because that makes easymenu menus slow. */
356 savedkey = Fwhere_is_internal (def, Qnil, Qt, Qnil);
357
358 if (!NILP (savedkey))
359 {
360 descrip = Fkey_description (savedkey);
361 descrip = concat2 (make_string (" (", 3), descrip);
362 descrip = concat2 (descrip, make_string (")", 1));
363 }
364 }
365
366 /* Cache the data we just got in a sublist of the menu binding. */
367 if (NILP (cachelist))
368 {
369 CHECK_IMPURE (item1);
370 XCONS (item1)->cdr = Fcons (Fcons (savedkey, descrip), def);
371 }
372 else if (changed)
373 {
374 XCONS (cachelist)->car = savedkey;
375 XCONS (cachelist)->cdr = descrip;
376 }
377
378 UNGCPRO;
379 *descrip_ptr = descrip;
380 return def;
381 }
382
383 /* This is used as the handler when calling internal_condition_case_1. */
384
385 static Lisp_Object
386 menu_item_enabled_p_1 (arg)
387 Lisp_Object arg;
388 {
389 return Qnil;
390 }
391
392 /* Return non-nil if the command DEF is enabled when used as a menu item.
393 This is based on looking for a menu-enable property.
394 If NOTREAL is set, don't bother really computing this. */
395
396 static Lisp_Object
397 menu_item_enabled_p (def, notreal)
398 Lisp_Object def;
399 int notreal;
400 {
401 Lisp_Object enabled, tem;
402
403 enabled = Qt;
404 if (notreal)
405 return enabled;
406 if (SYMBOLP (def))
407 {
408 /* No property, or nil, means enable.
409 Otherwise, enable if value is not nil. */
410 tem = Fget (def, Qmenu_enable);
411 if (!NILP (tem))
412 /* (condition-case nil (eval tem)
413 (error nil)) */
414 enabled = internal_condition_case_1 (Feval, tem, Qerror,
415 menu_item_enabled_p_1);
416 }
417 return enabled;
418 }
419 \f
420 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
421 and generate menu panes for them in menu_items.
422 If NOTREAL is nonzero,
423 don't bother really computing whether an item is enabled. */
424
425 static void
426 keymap_panes (keymaps, nmaps, notreal)
427 Lisp_Object *keymaps;
428 int nmaps;
429 int notreal;
430 {
431 int mapno;
432
433 init_menu_items ();
434
435 /* Loop over the given keymaps, making a pane for each map.
436 But don't make a pane that is empty--ignore that map instead.
437 P is the number of panes we have made so far. */
438 for (mapno = 0; mapno < nmaps; mapno++)
439 single_keymap_panes (keymaps[mapno], Qnil, Qnil, notreal);
440
441 finish_menu_items ();
442 }
443
444 /* This is a recursive subroutine of keymap_panes.
445 It handles one keymap, KEYMAP.
446 The other arguments are passed along
447 or point to local variables of the previous function.
448 If NOTREAL is nonzero,
449 don't bother really computing whether an item is enabled. */
450
451 static void
452 single_keymap_panes (keymap, pane_name, prefix, notreal)
453 Lisp_Object keymap;
454 Lisp_Object pane_name;
455 Lisp_Object prefix;
456 int notreal;
457 {
458 Lisp_Object pending_maps;
459 Lisp_Object tail, item, item1, item_string, table;
460 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
461
462 pending_maps = Qnil;
463
464 push_menu_pane (pane_name, prefix);
465
466 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
467 {
468 /* Look at each key binding, and if it has a menu string,
469 make a menu item from it. */
470 item = XCONS (tail)->car;
471 if (CONSP (item))
472 {
473 item1 = XCONS (item)->cdr;
474 if (CONSP (item1))
475 {
476 item_string = XCONS (item1)->car;
477 if (STRINGP (item_string))
478 {
479 /* This is the real definition--the function to run. */
480 Lisp_Object def;
481 /* These are the saved equivalent keyboard key sequence
482 and its key-description. */
483 Lisp_Object descrip;
484 Lisp_Object tem, enabled;
485
486 /* GCPRO because ...enabled_p will call eval
487 and ..._equiv_key may autoload something.
488 Protecting KEYMAP preserves everything we use;
489 aside from that, must protect whatever might be
490 a string. Since there's no GCPRO5, we refetch
491 item_string instead of protecting it. */
492 descrip = def = Qnil;
493 GCPRO4 (keymap, pending_maps, def, descrip);
494
495 def = menu_item_equiv_key (item_string, item1, &descrip);
496 enabled = menu_item_enabled_p (def, notreal);
497
498 UNGCPRO;
499
500 item_string = XCONS (item1)->car;
501
502 tem = Fkeymapp (def);
503 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
504 pending_maps = Fcons (Fcons (def, Fcons (item_string, XCONS (item)->car)),
505 pending_maps);
506 else
507 {
508 Lisp_Object submap;
509 GCPRO4 (keymap, pending_maps, descrip, item_string);
510 submap = get_keymap_1 (def, 0, 1);
511 UNGCPRO;
512 #ifndef USE_X_TOOLKIT
513 /* Indicate visually that this is a submenu. */
514 if (!NILP (submap))
515 item_string = concat2 (item_string,
516 build_string (" >"));
517 #endif
518 /* If definition is nil, pass nil as the key. */
519 push_menu_item (item_string, enabled,
520 XCONS (item)->car, def,
521 descrip);
522 #ifdef USE_X_TOOLKIT
523 /* Display a submenu using the toolkit. */
524 if (! NILP (submap))
525 {
526 push_submenu_start ();
527 single_keymap_panes (submap, Qnil,
528 XCONS (item)->car, notreal);
529 push_submenu_end ();
530 }
531 #endif
532 }
533 }
534 }
535 }
536 else if (VECTORP (item))
537 {
538 /* Loop over the char values represented in the vector. */
539 int len = XVECTOR (item)->size;
540 int c;
541 for (c = 0; c < len; c++)
542 {
543 Lisp_Object character;
544 XSETFASTINT (character, c);
545 item1 = XVECTOR (item)->contents[c];
546 if (CONSP (item1))
547 {
548 item_string = XCONS (item1)->car;
549 if (STRINGP (item_string))
550 {
551 Lisp_Object def;
552
553 /* These are the saved equivalent keyboard key sequence
554 and its key-description. */
555 Lisp_Object descrip;
556 Lisp_Object tem, enabled;
557
558 /* GCPRO because ...enabled_p will call eval
559 and ..._equiv_key may autoload something.
560 Protecting KEYMAP preserves everything we use;
561 aside from that, must protect whatever might be
562 a string. Since there's no GCPRO5, we refetch
563 item_string instead of protecting it. */
564 GCPRO4 (keymap, pending_maps, def, descrip);
565 descrip = def = Qnil;
566
567 def = menu_item_equiv_key (item_string, item1, &descrip);
568 enabled = menu_item_enabled_p (def, notreal);
569
570 UNGCPRO;
571
572 item_string = XCONS (item1)->car;
573
574 tem = Fkeymapp (def);
575 if (XSTRING (item_string)->data[0] == '@' && !NILP (tem))
576 pending_maps = Fcons (Fcons (def, Fcons (item_string, character)),
577 pending_maps);
578 else
579 {
580 Lisp_Object submap;
581 GCPRO4 (keymap, pending_maps, descrip, item_string);
582 submap = get_keymap_1 (def, 0, 1);
583 UNGCPRO;
584 #ifndef USE_X_TOOLKIT
585 if (!NILP (submap))
586 item_string = concat2 (item_string,
587 build_string (" >"));
588 #endif
589 /* If definition is nil, pass nil as the key. */
590 push_menu_item (item_string, enabled, character,
591 def, descrip);
592 #ifdef USE_X_TOOLKIT
593 if (! NILP (submap))
594 {
595 push_submenu_start ();
596 single_keymap_panes (submap, Qnil,
597 character, notreal);
598 push_submenu_end ();
599 }
600 #endif
601 }
602 }
603 }
604 }
605 }
606 }
607
608 /* Process now any submenus which want to be panes at this level. */
609 while (!NILP (pending_maps))
610 {
611 Lisp_Object elt, eltcdr, string;
612 elt = Fcar (pending_maps);
613 eltcdr = XCONS (elt)->cdr;
614 string = XCONS (eltcdr)->car;
615 /* We no longer discard the @ from the beginning of the string here.
616 Instead, we do this in xmenu_show. */
617 single_keymap_panes (Fcar (elt), string,
618 XCONS (eltcdr)->cdr, notreal);
619 pending_maps = Fcdr (pending_maps);
620 }
621 }
622 \f
623 /* Push all the panes and items of a menu decsribed by the
624 alist-of-alists MENU.
625 This handles old-fashioned calls to x-popup-menu. */
626
627 static void
628 list_of_panes (menu)
629 Lisp_Object menu;
630 {
631 Lisp_Object tail;
632
633 init_menu_items ();
634
635 for (tail = menu; !NILP (tail); tail = Fcdr (tail))
636 {
637 Lisp_Object elt, pane_name, pane_data;
638 elt = Fcar (tail);
639 pane_name = Fcar (elt);
640 CHECK_STRING (pane_name, 0);
641 push_menu_pane (pane_name, Qnil);
642 pane_data = Fcdr (elt);
643 CHECK_CONS (pane_data, 0);
644 list_of_items (pane_data);
645 }
646
647 finish_menu_items ();
648 }
649
650 /* Push the items in a single pane defined by the alist PANE. */
651
652 static void
653 list_of_items (pane)
654 Lisp_Object pane;
655 {
656 Lisp_Object tail, item, item1;
657
658 for (tail = pane; !NILP (tail); tail = Fcdr (tail))
659 {
660 item = Fcar (tail);
661 if (STRINGP (item))
662 push_menu_item (item, Qnil, Qnil, Qt, Qnil);
663 else if (NILP (item))
664 push_left_right_boundary ();
665 else
666 {
667 CHECK_CONS (item, 0);
668 item1 = Fcar (item);
669 CHECK_STRING (item1, 1);
670 push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil);
671 }
672 }
673 }
674 \f
675 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
676 "Pop up a deck-of-cards menu and return user's selection.\n\
677 POSITION is a position specification. This is either a mouse button event\n\
678 or a list ((XOFFSET YOFFSET) WINDOW)\n\
679 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
680 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
681 This controls the position of the center of the first line\n\
682 in the first pane of the menu, not the top left of the menu as a whole.\n\
683 If POSITION is t, it means to use the current mouse position.\n\
684 \n\
685 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
686 The menu items come from key bindings that have a menu string as well as\n\
687 a definition; actually, the \"definition\" in such a key binding looks like\n\
688 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
689 the keymap as a top-level element.\n\n\
690 You can also use a list of keymaps as MENU.\n\
691 Then each keymap makes a separate pane.\n\
692 When MENU is a keymap or a list of keymaps, the return value\n\
693 is a list of events.\n\n\
694 Alternatively, you can specify a menu of multiple panes\n\
695 with a list of the form (TITLE PANE1 PANE2...),\n\
696 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
697 Each ITEM is normally a cons cell (STRING . VALUE);\n\
698 but a string can appear as an item--that makes a nonselectable line\n\
699 in the menu.\n\
700 With this form of menu, the return value is VALUE from the chosen item.\n\
701 \n\
702 If POSITION is nil, don't display the menu at all, just precalculate the\n\
703 cached information about equivalent key sequences.")
704 (position, menu)
705 Lisp_Object position, menu;
706 {
707 int number_of_panes, panes;
708 Lisp_Object keymap, tem;
709 int xpos, ypos;
710 Lisp_Object title;
711 char *error_name;
712 Lisp_Object selection;
713 int i, j;
714 FRAME_PTR f;
715 Lisp_Object x, y, window;
716 int keymaps = 0;
717 int menubarp = 0;
718 int for_click = 0;
719 struct gcpro gcpro1;
720
721 if (! NILP (position))
722 {
723 check_x ();
724
725 /* Decode the first argument: find the window and the coordinates. */
726 if (EQ (position, Qt))
727 {
728 /* Use the mouse's current position. */
729 FRAME_PTR new_f = 0;
730 Lisp_Object bar_window;
731 int part;
732 unsigned long time;
733
734 if (mouse_position_hook)
735 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
736 if (new_f != 0)
737 XSETFRAME (window, new_f);
738 else
739 {
740 window = selected_window;
741 XSETFASTINT (x, 0);
742 XSETFASTINT (y, 0);
743 }
744 }
745 else
746 {
747 tem = Fcar (position);
748 if (CONSP (tem))
749 {
750 window = Fcar (Fcdr (position));
751 x = Fcar (tem);
752 y = Fcar (Fcdr (tem));
753 }
754 else
755 {
756 for_click = 1;
757 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
758 window = Fcar (tem); /* POSN_WINDOW (tem) */
759 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
760 x = Fcar (tem);
761 y = Fcdr (tem);
762
763 /* Determine whether this menu is handling a menu bar click. */
764 tem = Fcar (Fcdr (Fcar (Fcdr (position))));
765 if (CONSP (tem) && EQ (Fcar (tem), Qmenu_bar))
766 menubarp = 1;
767 }
768 }
769
770 CHECK_NUMBER (x, 0);
771 CHECK_NUMBER (y, 0);
772
773 /* Decode where to put the menu. */
774
775 if (FRAMEP (window))
776 {
777 f = XFRAME (window);
778 xpos = 0;
779 ypos = 0;
780 }
781 else if (WINDOWP (window))
782 {
783 CHECK_LIVE_WINDOW (window, 0);
784 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
785
786 xpos = (FONT_WIDTH (f->display.x->font) * XWINDOW (window)->left);
787 ypos = (f->display.x->line_height * XWINDOW (window)->top);
788 }
789 else
790 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
791 but I don't want to make one now. */
792 CHECK_WINDOW (window, 0);
793
794 xpos += XINT (x);
795 ypos += XINT (y);
796 }
797
798 title = Qnil;
799 GCPRO1 (title);
800
801 /* Decode the menu items from what was specified. */
802
803 keymap = Fkeymapp (menu);
804 tem = Qnil;
805 if (CONSP (menu))
806 tem = Fkeymapp (Fcar (menu));
807 if (!NILP (keymap))
808 {
809 /* We were given a keymap. Extract menu info from the keymap. */
810 Lisp_Object prompt;
811 keymap = get_keymap (menu);
812
813 /* Extract the detailed info to make one pane. */
814 keymap_panes (&menu, 1, NILP (position));
815
816 /* Search for a string appearing directly as an element of the keymap.
817 That string is the title of the menu. */
818 prompt = map_prompt (keymap);
819
820 /* Make that be the pane title of the first pane. */
821 if (!NILP (prompt) && menu_items_n_panes >= 0)
822 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = prompt;
823
824 keymaps = 1;
825 }
826 else if (!NILP (tem))
827 {
828 /* We were given a list of keymaps. */
829 int nmaps = XFASTINT (Flength (menu));
830 Lisp_Object *maps
831 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
832 int i;
833
834 title = Qnil;
835
836 /* The first keymap that has a prompt string
837 supplies the menu title. */
838 for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem))
839 {
840 Lisp_Object prompt;
841
842 maps[i++] = keymap = get_keymap (Fcar (tem));
843
844 prompt = map_prompt (keymap);
845 if (NILP (title) && !NILP (prompt))
846 title = prompt;
847 }
848
849 /* Extract the detailed info to make one pane. */
850 keymap_panes (maps, nmaps, NILP (position));
851
852 /* Make the title be the pane title of the first pane. */
853 if (!NILP (title) && menu_items_n_panes >= 0)
854 XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME] = title;
855
856 keymaps = 1;
857 }
858 else
859 {
860 /* We were given an old-fashioned menu. */
861 title = Fcar (menu);
862 CHECK_STRING (title, 1);
863
864 list_of_panes (Fcdr (menu));
865
866 keymaps = 0;
867 }
868
869 if (NILP (position))
870 {
871 discard_menu_items ();
872 UNGCPRO;
873 return Qnil;
874 }
875
876 /* Display them in a menu. */
877 BLOCK_INPUT;
878
879 selection = xmenu_show (f, xpos, ypos, menubarp, for_click,
880 keymaps, title, &error_name);
881 UNBLOCK_INPUT;
882
883 discard_menu_items ();
884
885 UNGCPRO;
886
887 if (error_name) error (error_name);
888 return selection;
889 }
890
891 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0,
892 "Pop up a dialog box and return user's selection.\n\
893 POSITION specifies which frame to use.\n\
894 This is normally a mouse button event or a window or frame.\n\
895 If POSITION is t, it means to use the frame the mouse is on.\n\
896 The dialog box appears in the middle of the specified frame.\n\
897 \n\
898 CONTENTS specifies the alternatives to display in the dialog box.\n\
899 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
900 Each ITEM is a cons cell (STRING . VALUE).\n\
901 The return value is VALUE from the chosen item.\n\n\
902 An ITEM may also be just a string--that makes a nonselectable item.\n\
903 An ITEM may also be nil--that means to put all preceding items\n\
904 on the left of the dialog box and all following items on the right.\n\
905 \(By default, approximately half appear on each side.)")
906 (position, contents)
907 Lisp_Object position, contents;
908 {
909 FRAME_PTR f;
910 Lisp_Object window;
911
912 check_x ();
913
914 /* Decode the first argument: find the window or frame to use. */
915 if (EQ (position, Qt))
916 {
917 #if 0 /* Using the frame the mouse is on may not be right. */
918 /* Use the mouse's current position. */
919 FRAME_PTR new_f = 0;
920 Lisp_Object bar_window;
921 int part;
922 unsigned long time;
923 Lisp_Object x, y;
924
925 (*mouse_position_hook) (&new_f, &bar_window, &part, &x, &y, &time);
926
927 if (new_f != 0)
928 XSETFRAME (window, new_f);
929 else
930 window = selected_window;
931 #endif
932 /* Decode the first argument: find the window and the coordinates. */
933 if (EQ (position, Qt))
934 window = selected_window;
935 }
936 else if (CONSP (position))
937 {
938 Lisp_Object tem;
939 tem = Fcar (position);
940 if (CONSP (tem))
941 window = Fcar (Fcdr (position));
942 else
943 {
944 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
945 window = Fcar (tem); /* POSN_WINDOW (tem) */
946 }
947 }
948 else if (WINDOWP (position) || FRAMEP (position))
949 window = position;
950
951 /* Decode where to put the menu. */
952
953 if (FRAMEP (window))
954 f = XFRAME (window);
955 else if (WINDOWP (window))
956 {
957 CHECK_LIVE_WINDOW (window, 0);
958 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
959 }
960 else
961 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
962 but I don't want to make one now. */
963 CHECK_WINDOW (window, 0);
964
965 #ifndef USE_X_TOOLKIT
966 /* Display a menu with these alternatives
967 in the middle of frame F. */
968 {
969 Lisp_Object x, y, frame, newpos;
970 XSETFRAME (frame, f);
971 XSETINT (x, x_pixel_width (f) / 2);
972 XSETINT (y, x_pixel_height (f) / 2);
973 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
974
975 return Fx_popup_menu (newpos,
976 Fcons (Fcar (contents), Fcons (contents, Qnil)));
977 }
978 #else
979 {
980 Lisp_Object title;
981 char *error_name;
982 Lisp_Object selection;
983
984 /* Decode the dialog items from what was specified. */
985 title = Fcar (contents);
986 CHECK_STRING (title, 1);
987
988 list_of_panes (Fcons (contents, Qnil));
989
990 /* Display them in a dialog box. */
991 BLOCK_INPUT;
992 selection = xdialog_show (f, 0, 0, title, &error_name);
993 UNBLOCK_INPUT;
994
995 discard_menu_items ();
996
997 if (error_name) error (error_name);
998 return selection;
999 }
1000 #endif
1001 }
1002 \f
1003 #ifdef USE_X_TOOLKIT
1004
1005 /* Loop in Xt until the menu pulldown or dialog popup has been
1006 popped down (deactivated).
1007
1008 NOTE: All calls to popup_get_selection() should be protected
1009 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1010 void
1011 popup_get_selection (initial_event)
1012 XEvent *initial_event;
1013 {
1014 XEvent event;
1015
1016 if (initial_event)
1017 event = *initial_event;
1018 else
1019 XtAppNextEvent (Xt_app_con, &event);
1020
1021 while (1)
1022 {
1023 XtDispatchEvent (&event);
1024 if (!popup_activated())
1025 break;
1026 XtAppNextEvent (Xt_app_con, &event);
1027 }
1028 }
1029
1030 /* Detect if a dialog or menu has been posted. */
1031 int
1032 popup_activated ()
1033 {
1034 return popup_activated_flag;
1035 }
1036
1037
1038 /* This callback is invoked when the user selects a menubar cascade
1039 pushbutton, but before the pulldown menu is posted. */
1040
1041 static void
1042 popup_activate_callback (widget, id, client_data)
1043 Widget widget;
1044 LWLIB_ID id;
1045 XtPointer client_data;
1046 {
1047 popup_activated_flag = 1;
1048 }
1049
1050 /* This callback is called from the menu bar pulldown menu
1051 when the user makes a selection.
1052 Figure out what the user chose
1053 and put the appropriate events into the keyboard buffer. */
1054
1055 static void
1056 menubar_selection_callback (widget, id, client_data)
1057 Widget widget;
1058 LWLIB_ID id;
1059 XtPointer client_data;
1060 {
1061 Lisp_Object prefix;
1062 FRAME_PTR f = (FRAME_PTR) id;
1063 Lisp_Object vector;
1064 Lisp_Object *subprefix_stack;
1065 int submenu_depth = 0;
1066 int i;
1067
1068 if (!f)
1069 return;
1070 subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object));
1071 vector = f->menu_bar_vector;
1072 prefix = Qnil;
1073 i = 0;
1074 while (i < f->menu_bar_items_used)
1075 {
1076 Lisp_Object entry;
1077
1078 if (EQ (XVECTOR (vector)->contents[i], Qnil))
1079 {
1080 subprefix_stack[submenu_depth++] = prefix;
1081 prefix = entry;
1082 i++;
1083 }
1084 else if (EQ (XVECTOR (vector)->contents[i], Qlambda))
1085 {
1086 prefix = subprefix_stack[--submenu_depth];
1087 i++;
1088 }
1089 else if (EQ (XVECTOR (vector)->contents[i], Qt))
1090 {
1091 prefix = XVECTOR (vector)->contents[i + MENU_ITEMS_PANE_PREFIX];
1092 i += MENU_ITEMS_PANE_LENGTH;
1093 }
1094 else
1095 {
1096 entry = XVECTOR (vector)->contents[i + MENU_ITEMS_ITEM_VALUE];
1097 if ((int) client_data == i)
1098 {
1099 int j;
1100 struct input_event buf;
1101 Lisp_Object frame;
1102
1103 XSETFRAME (frame, f);
1104 buf.kind = menu_bar_event;
1105 buf.frame_or_window = Fcons (frame, Qmenu_bar);
1106 kbd_buffer_store_event (&buf);
1107
1108 for (j = 0; j < submenu_depth; j++)
1109 if (!NILP (subprefix_stack[j]))
1110 {
1111 buf.kind = menu_bar_event;
1112 buf.frame_or_window = Fcons (frame, subprefix_stack[j]);
1113 kbd_buffer_store_event (&buf);
1114 }
1115
1116 if (!NILP (prefix))
1117 {
1118 buf.kind = menu_bar_event;
1119 buf.frame_or_window = Fcons (frame, prefix);
1120 kbd_buffer_store_event (&buf);
1121 }
1122
1123 buf.kind = menu_bar_event;
1124 buf.frame_or_window = Fcons (frame, entry);
1125 kbd_buffer_store_event (&buf);
1126
1127 return;
1128 }
1129 i += MENU_ITEMS_ITEM_LENGTH;
1130 }
1131 }
1132 }
1133
1134 /* This callback is invoked when a dialog or menu is finished being
1135 used and has been unposted. */
1136
1137 static void
1138 popup_deactivate_callback (widget, id, client_data)
1139 Widget widget;
1140 LWLIB_ID id;
1141 XtPointer client_data;
1142 {
1143 popup_activated_flag = 0;
1144 }
1145
1146
1147 /* This recursively calls free_widget_value on the tree of widgets.
1148 It must free all data that was malloc'ed for these widget_values.
1149 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1150 must be left alone. */
1151
1152 void
1153 free_menubar_widget_value_tree (wv)
1154 widget_value *wv;
1155 {
1156 if (! wv) return;
1157
1158 wv->name = wv->value = wv->key = (char *) 0xDEADBEEF;
1159
1160 if (wv->contents && (wv->contents != (widget_value*)1))
1161 {
1162 free_menubar_widget_value_tree (wv->contents);
1163 wv->contents = (widget_value *) 0xDEADBEEF;
1164 }
1165 if (wv->next)
1166 {
1167 free_menubar_widget_value_tree (wv->next);
1168 wv->next = (widget_value *) 0xDEADBEEF;
1169 }
1170 BLOCK_INPUT;
1171 free_widget_value (wv);
1172 UNBLOCK_INPUT;
1173 }
1174 \f
1175 /* Return a tree of widget_value structures for a menu bar item
1176 whose event type is ITEM_KEY (with string ITEM_NAME)
1177 and whose contents come from the list of keymaps MAPS. */
1178
1179 static widget_value *
1180 single_submenu (item_key, item_name, maps)
1181 Lisp_Object item_key, item_name, maps;
1182 {
1183 widget_value *wv, *prev_wv, *save_wv, *first_wv;
1184 int i;
1185 int submenu_depth = 0;
1186 Lisp_Object length;
1187 int len;
1188 Lisp_Object *mapvec;
1189 widget_value **submenu_stack;
1190 int mapno;
1191 int previous_items = menu_items_used;
1192
1193 length = Flength (maps);
1194 len = XINT (length);
1195
1196 /* Convert the list MAPS into a vector MAPVEC. */
1197 mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1198 for (i = 0; i < len; i++)
1199 {
1200 mapvec[i] = Fcar (maps);
1201 maps = Fcdr (maps);
1202 }
1203
1204 menu_items_n_panes = 0;
1205
1206 /* Loop over the given keymaps, making a pane for each map.
1207 But don't make a pane that is empty--ignore that map instead. */
1208 for (i = 0; i < len; i++)
1209 single_keymap_panes (mapvec[i], item_name, item_key, 0);
1210
1211 /* Create a tree of widget_value objects
1212 representing the panes and their items. */
1213
1214 submenu_stack
1215 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1216 wv = malloc_widget_value ();
1217 wv->name = "menu";
1218 wv->value = 0;
1219 wv->enabled = 1;
1220 first_wv = wv;
1221 save_wv = 0;
1222
1223 /* Loop over all panes and items made during this call
1224 and construct a tree of widget_value objects.
1225 Ignore the panes and items made by previous calls to
1226 single_submenu, even though those are also in menu_items. */
1227 i = previous_items;
1228 while (i < menu_items_used)
1229 {
1230 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1231 {
1232 submenu_stack[submenu_depth++] = save_wv;
1233 save_wv = prev_wv;
1234 prev_wv = 0;
1235 i++;
1236 }
1237 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1238 {
1239 prev_wv = save_wv;
1240 save_wv = submenu_stack[--submenu_depth];
1241 i++;
1242 }
1243 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1244 && submenu_depth != 0)
1245 i += MENU_ITEMS_PANE_LENGTH;
1246 /* Ignore a nil in the item list.
1247 It's meaningful only for dialog boxes. */
1248 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1249 i += 1;
1250 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1251 {
1252 /* Create a new pane. */
1253 Lisp_Object pane_name, prefix;
1254 char *pane_string;
1255 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1256 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1257 pane_string = (NILP (pane_name)
1258 ? "" : (char *) XSTRING (pane_name)->data);
1259 /* If there is just one top-level pane, put all its items directly
1260 under the top-level menu. */
1261 if (menu_items_n_panes == 1)
1262 pane_string = "";
1263
1264 /* If the pane has a meaningful name,
1265 make the pane a top-level menu item
1266 with its items as a submenu beneath it. */
1267 if (strcmp (pane_string, ""))
1268 {
1269 wv = malloc_widget_value ();
1270 if (save_wv)
1271 save_wv->next = wv;
1272 else
1273 first_wv->contents = wv;
1274 wv->name = pane_string;
1275 if (!NILP (prefix))
1276 wv->name++;
1277 wv->value = 0;
1278 wv->enabled = 1;
1279 }
1280 save_wv = wv;
1281 prev_wv = 0;
1282 i += MENU_ITEMS_PANE_LENGTH;
1283 }
1284 else
1285 {
1286 /* Create a new item within current pane. */
1287 Lisp_Object item_name, enable, descrip, def;
1288 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1289 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1290 descrip
1291 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1292 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1293
1294 wv = malloc_widget_value ();
1295 if (prev_wv)
1296 prev_wv->next = wv;
1297 else
1298 save_wv->contents = wv;
1299 wv->name = (char *) XSTRING (item_name)->data;
1300 if (!NILP (descrip))
1301 wv->key = (char *) XSTRING (descrip)->data;
1302 wv->value = 0;
1303 wv->call_data = (!NILP (def) ? (void *) i : 0);
1304 wv->enabled = !NILP (enable);
1305 prev_wv = wv;
1306
1307 i += MENU_ITEMS_ITEM_LENGTH;
1308 }
1309 }
1310
1311 return first_wv;
1312 }
1313 \f
1314 extern void EmacsFrameSetCharSize ();
1315
1316 /* Recompute the menu bar of frame F. */
1317
1318 static void
1319 update_frame_menubar (f)
1320 FRAME_PTR f;
1321 {
1322 struct x_display *x = f->display.x;
1323 int columns, rows;
1324 int menubar_changed;
1325
1326 Dimension shell_height;
1327
1328 /* We assume the menubar contents has changed if the global flag is set,
1329 or if the current buffer has changed, or if the menubar has never
1330 been updated before.
1331 */
1332 menubar_changed = (x->menubar_widget
1333 && !XtIsManaged (x->menubar_widget));
1334
1335 if (! (menubar_changed))
1336 return;
1337
1338 BLOCK_INPUT;
1339 /* Save the size of the frame because the pane widget doesn't accept to
1340 resize itself. So force it. */
1341 columns = f->width;
1342 rows = f->height;
1343
1344 /* Do the voodoo which means "I'm changing lots of things, don't try to
1345 refigure sizes until I'm done." */
1346 lw_refigure_widget (x->column_widget, False);
1347
1348 /* the order in which children are managed is the top to
1349 bottom order in which they are displayed in the paned window.
1350 First, remove the text-area widget.
1351 */
1352 XtUnmanageChild (x->edit_widget);
1353
1354 /* remove the menubar that is there now, and put up the menubar that
1355 should be there.
1356 */
1357 if (menubar_changed)
1358 {
1359 XtManageChild (x->menubar_widget);
1360 XtMapWidget (x->menubar_widget);
1361 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, 0);
1362 }
1363
1364 /* Re-manage the text-area widget, and then thrash the sizes. */
1365 XtManageChild (x->edit_widget);
1366 lw_refigure_widget (x->column_widget, True);
1367
1368 /* Force the pane widget to resize itself with the right values. */
1369 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1370
1371 UNBLOCK_INPUT;
1372 }
1373
1374 void
1375 set_frame_menubar (f, first_time)
1376 FRAME_PTR f;
1377 int first_time;
1378 {
1379 Widget menubar_widget = f->display.x->menubar_widget;
1380 int id = (int) f;
1381 Lisp_Object tail, items;
1382 widget_value *wv, *first_wv, *prev_wv = 0;
1383 int i;
1384
1385 BLOCK_INPUT;
1386
1387 wv = malloc_widget_value ();
1388 wv->name = "menubar";
1389 wv->value = 0;
1390 wv->enabled = 1;
1391 first_wv = wv;
1392 items = FRAME_MENU_BAR_ITEMS (f);
1393 menu_items = f->menu_bar_vector;
1394 menu_items_allocated = XVECTOR (menu_items)->size;
1395 init_menu_items ();
1396
1397 for (i = 0; i < XVECTOR (items)->size; i += 3)
1398 {
1399 Lisp_Object key, string, maps;
1400
1401 key = XVECTOR (items)->contents[i];
1402 string = XVECTOR (items)->contents[i + 1];
1403 maps = XVECTOR (items)->contents[i + 2];
1404 if (NILP (string))
1405 break;
1406
1407 wv = single_submenu (key, string, maps);
1408 if (prev_wv)
1409 prev_wv->next = wv;
1410 else
1411 first_wv->contents = wv;
1412 /* Don't set wv->name here; GC during the loop might relocate it. */
1413 wv->enabled = 1;
1414 prev_wv = wv;
1415 }
1416
1417 /* Now GC cannot happen during the lifetime of the widget_value,
1418 so it's safe to store data from a Lisp_String. */
1419 wv = first_wv->contents;
1420 for (i = 0; i < XVECTOR (items)->size; i += 3)
1421 {
1422 Lisp_Object string;
1423 string = XVECTOR (items)->contents[i + 1];
1424 if (NILP (string))
1425 break;
1426 wv->name = (char *) XSTRING (string)->data;
1427 wv = wv->next;
1428 }
1429
1430 finish_menu_items ();
1431
1432 f->menu_bar_vector = menu_items;
1433 f->menu_bar_items_used = menu_items_used;
1434 menu_items = Qnil;
1435
1436 if (menubar_widget)
1437 {
1438 /* Disable resizing (done for Motif!) */
1439 lw_allow_resizing (f->display.x->widget, False);
1440
1441 /* The third arg is DEEP_P, which says to consider the entire
1442 menu trees we supply, rather than just the menu bar item names. */
1443 lw_modify_all_widgets (id, first_wv, 1);
1444
1445 /* Re-enable the edit widget to resize. */
1446 lw_allow_resizing (f->display.x->widget, True);
1447 }
1448 else
1449 {
1450 menubar_widget = lw_create_widget ("menubar", "menubar",
1451 id, first_wv,
1452 f->display.x->column_widget,
1453 0,
1454 popup_activate_callback,
1455 menubar_selection_callback,
1456 popup_deactivate_callback);
1457 f->display.x->menubar_widget = menubar_widget;
1458 }
1459
1460 free_menubar_widget_value_tree (first_wv);
1461
1462 /* Don't update the menubar the first time it is created via x_window. */
1463 if (!first_time)
1464 update_frame_menubar (f);
1465
1466 UNBLOCK_INPUT;
1467 }
1468
1469 /* Called from Fx_create_frame to create the inital menubar of a frame
1470 before it is mapped, so that the window is mapped with the menubar already
1471 there instead of us tacking it on later and thrashing the window after it
1472 is visible. */
1473
1474 void
1475 initialize_frame_menubar (f)
1476 FRAME_PTR f;
1477 {
1478 /* This function is called before the first chance to redisplay
1479 the frame. It has to be, so the frame will have the right size. */
1480 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1481 set_frame_menubar (f, 1);
1482 }
1483
1484 /* Get rid of the menu bar of frame F, and free its storage.
1485 This is used when deleting a frame, and when turning off the menu bar. */
1486
1487 void
1488 free_frame_menubar (f)
1489 FRAME_PTR f;
1490 {
1491 Widget menubar_widget;
1492 int id;
1493
1494 menubar_widget = f->display.x->menubar_widget;
1495 id = (int) f;
1496
1497 if (menubar_widget)
1498 {
1499 BLOCK_INPUT;
1500 lw_destroy_all_widgets (id);
1501 UNBLOCK_INPUT;
1502 }
1503 }
1504
1505 #endif /* USE_X_TOOLKIT */
1506 \f
1507 /* xmenu_show actually displays a menu using the panes and items in menu_items
1508 and returns the value selected from it.
1509 There are two versions of xmenu_show, one for Xt and one for Xlib.
1510 Both assume input is blocked by the caller. */
1511
1512 /* F is the frame the menu is for.
1513 X and Y are the frame-relative specified position,
1514 relative to the inside upper left corner of the frame F.
1515 MENUBARP is 1 if this menu came from the menu bar.
1516 FOR_CLICK if this menu was invoked for a mouse click.
1517 KEYMAPS is 1 if this menu was specified with keymaps;
1518 in that case, we return a list containing the chosen item's value
1519 and perhaps also the pane's prefix.
1520 TITLE is the specified menu title.
1521 ERROR is a place to store an error message string in case of failure.
1522 (We return nil on failure, but the value doesn't actually matter.) */
1523
1524 #ifdef USE_X_TOOLKIT
1525
1526 /* We need a unique id for each widget handled by the Lucid Widget
1527 library. This includes the frame main windows, popup menu and
1528 dialog box. */
1529 LWLIB_ID widget_id_tick;
1530
1531 #ifdef __STDC__
1532 static Lisp_Object *volatile menu_item_selection;
1533 #else
1534 static Lisp_Object *menu_item_selection;
1535 #endif
1536
1537 static void
1538 popup_selection_callback (widget, id, client_data)
1539 Widget widget;
1540 LWLIB_ID id;
1541 XtPointer client_data;
1542 {
1543 menu_item_selection = (Lisp_Object *) client_data;
1544 }
1545
1546 static Lisp_Object
1547 xmenu_show (f, x, y, menubarp, for_click, keymaps, title, error)
1548 FRAME_PTR f;
1549 int x;
1550 int y;
1551 int menubarp; /* This arg is unused in Xt version. */
1552 int for_click;
1553 int keymaps;
1554 Lisp_Object title;
1555 char **error;
1556 {
1557 int i;
1558 int menu_id;
1559 Widget menu;
1560 Arg av [2];
1561 int ac = 0;
1562 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1563 widget_value **submenu_stack
1564 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1565 Lisp_Object *subprefix_stack
1566 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1567 int submenu_depth = 0;
1568
1569 /* Define a queue to save up for later unreading
1570 all X events that don't pertain to the menu. */
1571 struct event_queue
1572 {
1573 XEvent event;
1574 struct event_queue *next;
1575 };
1576
1577 struct event_queue *queue = NULL;
1578 struct event_queue *queue_tmp;
1579
1580 Position root_x, root_y;
1581
1582 int first_pane;
1583 int next_release_must_exit = 0;
1584
1585 *error = NULL;
1586
1587 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1588 {
1589 *error = "Empty menu";
1590 return Qnil;
1591 }
1592
1593 /* Create a tree of widget_value objects
1594 representing the panes and their items. */
1595 wv = malloc_widget_value ();
1596 wv->name = "menu";
1597 wv->value = 0;
1598 wv->enabled = 1;
1599 first_wv = wv;
1600 first_pane = 1;
1601
1602 /* Loop over all panes and items, filling in the tree. */
1603 i = 0;
1604 while (i < menu_items_used)
1605 {
1606 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1607 {
1608 submenu_stack[submenu_depth++] = save_wv;
1609 save_wv = prev_wv;
1610 prev_wv = 0;
1611 first_pane = 1;
1612 i++;
1613 }
1614 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1615 {
1616 prev_wv = save_wv;
1617 save_wv = submenu_stack[--submenu_depth];
1618 first_pane = 0;
1619 i++;
1620 }
1621 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1622 && submenu_depth != 0)
1623 i += MENU_ITEMS_PANE_LENGTH;
1624 /* Ignore a nil in the item list.
1625 It's meaningful only for dialog boxes. */
1626 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1627 i += 1;
1628 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1629 {
1630 /* Create a new pane. */
1631 Lisp_Object pane_name, prefix;
1632 char *pane_string;
1633 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
1634 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1635 pane_string = (NILP (pane_name)
1636 ? "" : (char *) XSTRING (pane_name)->data);
1637 /* If there is just one top-level pane, put all its items directly
1638 under the top-level menu. */
1639 if (menu_items_n_panes == 1)
1640 pane_string = "";
1641
1642 /* If the pane has a meaningful name,
1643 make the pane a top-level menu item
1644 with its items as a submenu beneath it. */
1645 if (!keymaps && strcmp (pane_string, ""))
1646 {
1647 wv = malloc_widget_value ();
1648 if (save_wv)
1649 save_wv->next = wv;
1650 else
1651 first_wv->contents = wv;
1652 wv->name = pane_string;
1653 if (keymaps && !NILP (prefix))
1654 wv->name++;
1655 wv->value = 0;
1656 wv->enabled = 1;
1657 save_wv = wv;
1658 prev_wv = 0;
1659 }
1660 else if (first_pane)
1661 {
1662 save_wv = wv;
1663 prev_wv = 0;
1664 }
1665 first_pane = 0;
1666 i += MENU_ITEMS_PANE_LENGTH;
1667 }
1668 else
1669 {
1670 /* Create a new item within current pane. */
1671 Lisp_Object item_name, enable, descrip, def;
1672 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1673 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1674 descrip
1675 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1676 def = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_DEFINITION];
1677
1678 wv = malloc_widget_value ();
1679 if (prev_wv)
1680 prev_wv->next = wv;
1681 else
1682 save_wv->contents = wv;
1683 wv->name = (char *) XSTRING (item_name)->data;
1684 if (!NILP (descrip))
1685 wv->key = (char *) XSTRING (descrip)->data;
1686 wv->value = 0;
1687 /* If this item has a null value,
1688 make the call_data null so that it won't display a box
1689 when the mouse is on it. */
1690 wv->call_data
1691 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
1692 wv->enabled = !NILP (enable);
1693 prev_wv = wv;
1694
1695 i += MENU_ITEMS_ITEM_LENGTH;
1696 }
1697 }
1698
1699 /* Deal with the title, if it is non-nil. */
1700 if (!NILP (title))
1701 {
1702 widget_value *wv_title = malloc_widget_value ();
1703 widget_value *wv_sep1 = malloc_widget_value ();
1704 widget_value *wv_sep2 = malloc_widget_value ();
1705
1706 wv_sep2->name = "--";
1707 wv_sep2->next = first_wv->contents;
1708
1709 wv_sep1->name = "--";
1710 wv_sep1->next = wv_sep2;
1711
1712 wv_title->name = (char *) XSTRING (title)->data;
1713 wv_title->enabled = True;
1714 wv_title->next = wv_sep1;
1715 first_wv->contents = wv_title;
1716 }
1717
1718 /* Actually create the menu. */
1719 menu_id = ++widget_id_tick;
1720 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1721 f->display.x->widget, 1, 0,
1722 popup_selection_callback,
1723 popup_deactivate_callback);
1724
1725 /* Don't allow any geometry request from the user. */
1726 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1727 XtSetValues (menu, av, ac);
1728
1729 /* Free the widget_value objects we used to specify the contents. */
1730 free_menubar_widget_value_tree (first_wv);
1731
1732 /* No selection has been chosen yet. */
1733 menu_item_selection = 0;
1734
1735 /* Display the menu. */
1736 lw_popup_menu (menu);
1737 popup_activated_flag = 1;
1738
1739 /* Process events that apply to the menu. */
1740 popup_get_selection ((XEvent *) 0);
1741
1742 pop_down:
1743 /* fp turned off the following statement and wrote a comment
1744 that it is unnecessary--that the menu has already disappeared.
1745 I observer that is not so. -- rms. */
1746 /* Make sure the menu disappears. */
1747 lw_destroy_all_widgets (menu_id);
1748
1749 /* Unread any events that we got but did not handle. */
1750 while (queue != NULL)
1751 {
1752 queue_tmp = queue;
1753 XPutBackEvent (FRAME_X_DISPLAY (f), &queue_tmp->event);
1754 queue = queue_tmp->next;
1755 free ((char *)queue_tmp);
1756 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1757 interrupt_input_pending = 1;
1758 }
1759
1760 /* Find the selected item, and its pane, to return
1761 the proper value. */
1762 if (menu_item_selection != 0)
1763 {
1764 Lisp_Object prefix;
1765
1766 prefix = Qnil;
1767 i = 0;
1768 while (i < menu_items_used)
1769 {
1770 Lisp_Object entry;
1771
1772 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1773 {
1774 subprefix_stack[submenu_depth++] = prefix;
1775 prefix = entry;
1776 i++;
1777 }
1778 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1779 {
1780 prefix = subprefix_stack[--submenu_depth];
1781 i++;
1782 }
1783 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1784 {
1785 prefix
1786 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1787 i += MENU_ITEMS_PANE_LENGTH;
1788 }
1789 else
1790 {
1791 entry
1792 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1793 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1794 {
1795 if (keymaps != 0)
1796 {
1797 int j;
1798
1799 entry = Fcons (entry, Qnil);
1800 if (!NILP (prefix))
1801 entry = Fcons (prefix, entry);
1802 for (j = submenu_depth - 1; j >= 0; j--)
1803 if (!NILP (subprefix_stack[j]))
1804 entry = Fcons (subprefix_stack[j], entry);
1805 }
1806 return entry;
1807 }
1808 i += MENU_ITEMS_ITEM_LENGTH;
1809 }
1810 }
1811 }
1812
1813 return Qnil;
1814 }
1815 \f
1816 static void
1817 dialog_selection_callback (widget, id, client_data)
1818 Widget widget;
1819 LWLIB_ID id;
1820 XtPointer client_data;
1821 {
1822 if ((int)client_data != -1)
1823 menu_item_selection = (Lisp_Object *) client_data;
1824 BLOCK_INPUT;
1825 lw_destroy_all_widgets (id);
1826 UNBLOCK_INPUT;
1827 }
1828
1829 static char * button_names [] = {
1830 "button1", "button2", "button3", "button4", "button5",
1831 "button6", "button7", "button8", "button9", "button10" };
1832
1833 static Lisp_Object
1834 xdialog_show (f, menubarp, keymaps, title, error)
1835 FRAME_PTR f;
1836 int menubarp;
1837 int keymaps;
1838 Lisp_Object title;
1839 char **error;
1840 {
1841 int i, nb_buttons=0;
1842 int dialog_id;
1843 Widget menu;
1844 char dialog_name[6];
1845
1846 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1847
1848 /* Define a queue to save up for later unreading
1849 all X events that don't pertain to the menu. */
1850 struct event_queue
1851 {
1852 XEvent event;
1853 struct event_queue *next;
1854 };
1855
1856 struct event_queue *queue = NULL;
1857 struct event_queue *queue_tmp;
1858
1859 /* Number of elements seen so far, before boundary. */
1860 int left_count = 0;
1861 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1862 int boundary_seen = 0;
1863
1864 *error = NULL;
1865
1866 if (menu_items_n_panes > 1)
1867 {
1868 *error = "Multiple panes in dialog box";
1869 return Qnil;
1870 }
1871
1872 /* Create a tree of widget_value objects
1873 representing the text label and buttons. */
1874 {
1875 Lisp_Object pane_name, prefix;
1876 char *pane_string;
1877 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
1878 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
1879 pane_string = (NILP (pane_name)
1880 ? "" : (char *) XSTRING (pane_name)->data);
1881 prev_wv = malloc_widget_value ();
1882 prev_wv->value = pane_string;
1883 if (keymaps && !NILP (prefix))
1884 prev_wv->name++;
1885 prev_wv->enabled = 1;
1886 prev_wv->name = "message";
1887 first_wv = prev_wv;
1888
1889 /* Loop over all panes and items, filling in the tree. */
1890 i = MENU_ITEMS_PANE_LENGTH;
1891 while (i < menu_items_used)
1892 {
1893
1894 /* Create a new item within current pane. */
1895 Lisp_Object item_name, enable, descrip;
1896 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
1897 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
1898 descrip
1899 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
1900
1901 if (NILP (item_name))
1902 {
1903 free_menubar_widget_value_tree (first_wv);
1904 *error = "Submenu in dialog items";
1905 return Qnil;
1906 }
1907 if (EQ (item_name, Qquote))
1908 {
1909 /* This is the boundary between left-side elts
1910 and right-side elts. Stop incrementing right_count. */
1911 boundary_seen = 1;
1912 i++;
1913 continue;
1914 }
1915 if (nb_buttons >= 10)
1916 {
1917 free_menubar_widget_value_tree (first_wv);
1918 *error = "Too many dialog items";
1919 return Qnil;
1920 }
1921
1922 wv = malloc_widget_value ();
1923 prev_wv->next = wv;
1924 wv->name = (char *) button_names[nb_buttons];
1925 if (!NILP (descrip))
1926 wv->key = (char *) XSTRING (descrip)->data;
1927 wv->value = (char *) XSTRING (item_name)->data;
1928 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
1929 wv->enabled = !NILP (enable);
1930 prev_wv = wv;
1931
1932 if (! boundary_seen)
1933 left_count++;
1934
1935 nb_buttons++;
1936 i += MENU_ITEMS_ITEM_LENGTH;
1937 }
1938
1939 /* If the boundary was not specified,
1940 by default put half on the left and half on the right. */
1941 if (! boundary_seen)
1942 left_count = nb_buttons - nb_buttons / 2;
1943
1944 wv = malloc_widget_value ();
1945 wv->name = dialog_name;
1946
1947 /* Dialog boxes use a really stupid name encoding
1948 which specifies how many buttons to use
1949 and how many buttons are on the right.
1950 The Q means something also. */
1951 dialog_name[0] = 'Q';
1952 dialog_name[1] = '0' + nb_buttons;
1953 dialog_name[2] = 'B';
1954 dialog_name[3] = 'R';
1955 /* Number of buttons to put on the right. */
1956 dialog_name[4] = '0' + nb_buttons - left_count;
1957 dialog_name[5] = 0;
1958 wv->contents = first_wv;
1959 first_wv = wv;
1960 }
1961
1962 /* Actually create the dialog. */
1963 dialog_id = ++widget_id_tick;
1964 menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1965 f->display.x->widget, 1, 0,
1966 dialog_selection_callback, 0);
1967 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1968 /* Free the widget_value objects we used to specify the contents. */
1969 free_menubar_widget_value_tree (first_wv);
1970
1971 /* No selection has been chosen yet. */
1972 menu_item_selection = 0;
1973
1974 /* Display the menu. */
1975 lw_pop_up_all_widgets (dialog_id);
1976
1977 /* Process events that apply to the menu. */
1978 while (1)
1979 {
1980 XEvent event;
1981
1982 XtAppNextEvent (Xt_app_con, &event);
1983 if (event.type == ButtonRelease)
1984 {
1985 XtDispatchEvent (&event);
1986 break;
1987 }
1988 else if (event.type == Expose)
1989 process_expose_from_menu (event);
1990 XtDispatchEvent (&event);
1991 if (XtWindowToWidget (FRAME_X_DISPLAY (f), event.xany.window) != menu)
1992 {
1993 queue_tmp = (struct event_queue *) malloc (sizeof (struct event_queue));
1994
1995 if (queue_tmp != NULL)
1996 {
1997 queue_tmp->event = event;
1998 queue_tmp->next = queue;
1999 queue = queue_tmp;
2000 }
2001 }
2002 }
2003 pop_down:
2004
2005 #ifdef HAVE_X_WINDOWS
2006 /* State that no mouse buttons are now held.
2007 That is not necessarily true, but the fiction leads to reasonable
2008 results, and it is a pain to ask which are actually held now
2009 or track this in the loop above. */
2010 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2011 #endif
2012
2013 /* Unread any events that we got but did not handle. */
2014 while (queue != NULL)
2015 {
2016 queue_tmp = queue;
2017 XPutBackEvent (FRAME_X_DISPLAY (f), &queue_tmp->event);
2018 queue = queue_tmp->next;
2019 free ((char *)queue_tmp);
2020 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
2021 interrupt_input_pending = 1;
2022 }
2023
2024 /* Find the selected item, and its pane, to return
2025 the proper value. */
2026 if (menu_item_selection != 0)
2027 {
2028 Lisp_Object prefix;
2029
2030 prefix = Qnil;
2031 i = 0;
2032 while (i < menu_items_used)
2033 {
2034 Lisp_Object entry;
2035
2036 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2037 {
2038 prefix
2039 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2040 i += MENU_ITEMS_PANE_LENGTH;
2041 }
2042 else
2043 {
2044 entry
2045 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2046 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2047 {
2048 if (keymaps != 0)
2049 {
2050 entry = Fcons (entry, Qnil);
2051 if (!NILP (prefix))
2052 entry = Fcons (prefix, entry);
2053 }
2054 return entry;
2055 }
2056 i += MENU_ITEMS_ITEM_LENGTH;
2057 }
2058 }
2059 }
2060
2061 return Qnil;
2062 }
2063 #else /* not USE_X_TOOLKIT */
2064
2065 static Lisp_Object
2066 xmenu_show (f, x, y, menubarp, for_click, keymaps, title, error)
2067 FRAME_PTR f;
2068 int x, y;
2069 int menubarp;
2070 int for_click;
2071 int keymaps;
2072 Lisp_Object title;
2073 char **error;
2074 {
2075 Window root;
2076 XMenu *menu;
2077 int pane, selidx, lpane, status;
2078 Lisp_Object entry, pane_prefix;
2079 char *datap;
2080 int ulx, uly, width, height;
2081 int dispwidth, dispheight;
2082 int i, j;
2083 int maxwidth;
2084 int dummy_int;
2085 unsigned int dummy_uint;
2086
2087 *error = 0;
2088 if (menu_items_n_panes == 0)
2089 return Qnil;
2090
2091 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2092 {
2093 *error = "Empty menu";
2094 return Qnil;
2095 }
2096
2097 /* Figure out which root window F is on. */
2098 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2099 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2100 &dummy_uint, &dummy_uint);
2101
2102 /* Make the menu on that window. */
2103 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2104 if (menu == NULL)
2105 {
2106 *error = "Can't create menu";
2107 return Qnil;
2108 }
2109
2110 #ifdef HAVE_X_WINDOWS
2111 /* Adjust coordinates to relative to the outer (window manager) window. */
2112 {
2113 Window child;
2114 int win_x = 0, win_y = 0;
2115
2116 /* Find the position of the outside upper-left corner of
2117 the inner window, with respect to the outer window. */
2118 if (f->display.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
2119 {
2120 BLOCK_INPUT;
2121 XTranslateCoordinates (FRAME_X_DISPLAY (f),
2122
2123 /* From-window, to-window. */
2124 f->display.x->window_desc,
2125 f->display.x->parent_desc,
2126
2127 /* From-position, to-position. */
2128 0, 0, &win_x, &win_y,
2129
2130 /* Child of window. */
2131 &child);
2132 UNBLOCK_INPUT;
2133 x += win_x;
2134 y += win_y;
2135 }
2136 }
2137 #endif /* HAVE_X_WINDOWS */
2138
2139 /* Adjust coordinates to be root-window-relative. */
2140 x += f->display.x->left_pos;
2141 y += f->display.x->top_pos;
2142
2143 /* Create all the necessary panes and their items. */
2144 i = 0;
2145 while (i < menu_items_used)
2146 {
2147 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2148 {
2149 /* Create a new pane. */
2150 Lisp_Object pane_name, prefix;
2151 char *pane_string;
2152
2153 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2154 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2155 pane_string = (NILP (pane_name)
2156 ? "" : (char *) XSTRING (pane_name)->data);
2157 if (keymaps && !NILP (prefix))
2158 pane_string++;
2159
2160 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2161 if (lpane == XM_FAILURE)
2162 {
2163 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2164 *error = "Can't create pane";
2165 return Qnil;
2166 }
2167 i += MENU_ITEMS_PANE_LENGTH;
2168
2169 /* Find the width of the widest item in this pane. */
2170 maxwidth = 0;
2171 j = i;
2172 while (j < menu_items_used)
2173 {
2174 Lisp_Object item;
2175 item = XVECTOR (menu_items)->contents[j];
2176 if (EQ (item, Qt))
2177 break;
2178 if (NILP (item))
2179 {
2180 j++;
2181 continue;
2182 }
2183 width = XSTRING (item)->size;
2184 if (width > maxwidth)
2185 maxwidth = width;
2186
2187 j += MENU_ITEMS_ITEM_LENGTH;
2188 }
2189 }
2190 /* Ignore a nil in the item list.
2191 It's meaningful only for dialog boxes. */
2192 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2193 i += 1;
2194 else
2195 {
2196 /* Create a new item within current pane. */
2197 Lisp_Object item_name, enable, descrip;
2198 unsigned char *item_data;
2199
2200 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2201 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2202 descrip
2203 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2204 if (!NILP (descrip))
2205 {
2206 int gap = maxwidth - XSTRING (item_name)->size;
2207 #ifdef C_ALLOCA
2208 Lisp_Object spacer;
2209 spacer = Fmake_string (make_number (gap), make_number (' '));
2210 item_name = concat2 (item_name, spacer);
2211 item_name = concat2 (item_name, descrip);
2212 item_data = XSTRING (item_name)->data;
2213 #else
2214 /* if alloca is fast, use that to make the space,
2215 to reduce gc needs. */
2216 item_data
2217 = (unsigned char *) alloca (maxwidth
2218 + XSTRING (descrip)->size + 1);
2219 bcopy (XSTRING (item_name)->data, item_data,
2220 XSTRING (item_name)->size);
2221 for (j = XSTRING (item_name)->size; j < maxwidth; j++)
2222 item_data[j] = ' ';
2223 bcopy (XSTRING (descrip)->data, item_data + j,
2224 XSTRING (descrip)->size);
2225 item_data[j + XSTRING (descrip)->size] = 0;
2226 #endif
2227 }
2228 else
2229 item_data = XSTRING (item_name)->data;
2230
2231 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2232 menu, lpane, 0, item_data,
2233 !NILP (enable))
2234 == XM_FAILURE)
2235 {
2236 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2237 *error = "Can't add selection to menu";
2238 return Qnil;
2239 }
2240 i += MENU_ITEMS_ITEM_LENGTH;
2241 }
2242 }
2243
2244 /* All set and ready to fly. */
2245 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2246 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f),
2247 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2248 dispheight = DisplayHeight (FRAME_X_DISPLAY (f),
2249 XScreenNumberOfScreen (FRAME_X_SCREEN (f)));
2250 x = min (x, dispwidth);
2251 y = min (y, dispheight);
2252 x = max (x, 1);
2253 y = max (y, 1);
2254 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2255 &ulx, &uly, &width, &height);
2256 if (ulx+width > dispwidth)
2257 {
2258 x -= (ulx + width) - dispwidth;
2259 ulx = dispwidth - width;
2260 }
2261 if (uly+height > dispheight)
2262 {
2263 y -= (uly + height) - dispheight;
2264 uly = dispheight - height;
2265 }
2266 if (ulx < 0) x -= ulx;
2267 if (uly < 0) y -= uly;
2268
2269 XMenuSetAEQ (menu, TRUE);
2270 XMenuSetFreeze (menu, TRUE);
2271 pane = selidx = 0;
2272
2273 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2274 x, y, ButtonReleaseMask, &datap);
2275
2276
2277 /* Assume the mouse has moved out of the X window.
2278 If it has actually moved in, we will get an EnterNotify. */
2279 x_mouse_leave ();
2280
2281 switch (status)
2282 {
2283 case XM_SUCCESS:
2284 #ifdef XDEBUG
2285 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2286 #endif
2287
2288 /* Find the item number SELIDX in pane number PANE. */
2289 i = 0;
2290 while (i < menu_items_used)
2291 {
2292 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2293 {
2294 if (pane == 0)
2295 pane_prefix
2296 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2297 pane--;
2298 i += MENU_ITEMS_PANE_LENGTH;
2299 }
2300 else
2301 {
2302 if (pane == -1)
2303 {
2304 if (selidx == 0)
2305 {
2306 entry
2307 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2308 if (keymaps != 0)
2309 {
2310 entry = Fcons (entry, Qnil);
2311 if (!NILP (pane_prefix))
2312 entry = Fcons (pane_prefix, entry);
2313 }
2314 break;
2315 }
2316 selidx--;
2317 }
2318 i += MENU_ITEMS_ITEM_LENGTH;
2319 }
2320 }
2321 break;
2322
2323 case XM_FAILURE:
2324 *error = "Can't activate menu";
2325 case XM_IA_SELECT:
2326 case XM_NO_SELECT:
2327 entry = Qnil;
2328 break;
2329 }
2330 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2331
2332 #ifdef HAVE_X_WINDOWS
2333 /* State that no mouse buttons are now held.
2334 (The oldXMenu code doesn't track this info for us.)
2335 That is not necessarily true, but the fiction leads to reasonable
2336 results, and it is a pain to ask which are actually held now. */
2337 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2338 #endif
2339
2340 return entry;
2341 }
2342
2343 #endif /* not USE_X_TOOLKIT */
2344 \f
2345 syms_of_xmenu ()
2346 {
2347 staticpro (&menu_items);
2348 menu_items = Qnil;
2349
2350 #ifdef USE_X_TOOLKIT
2351 widget_id_tick = (1<<16);
2352 #endif
2353
2354 defsubr (&Sx_popup_menu);
2355 defsubr (&Sx_popup_dialog);
2356 }