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