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