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