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