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