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