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