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