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