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