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