1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* X pop-up deck-of-cards menu facility for gnuemacs.
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
28 /* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
31 /* Rewritten for clarity and GC protection by rms in Feb 94. */
33 /* On 4.3 this loses if it comes after xterm.h. */
42 #include "termhooks.h"
46 #include "blockinput.h"
54 /* This may include sys/types.h, and that somehow loses
55 if this is not done before the other system files. */
59 /* Load sys/types.h if not already loaded.
60 In some systems loading it twice is suicidal. */
62 #include <sys/types.h>
65 #include "dispextern.h"
68 #undef HAVE_MULTILINGUAL_MENU
71 #include <X11/IntrinsicP.h>
72 #include <X11/CoreP.h>
73 #include <X11/StringDefs.h>
74 #include <X11/Shell.h>
76 #include <X11/Xaw/Paned.h>
77 #endif /* USE_LUCID */
78 #include "../lwlib/lwlib.h"
79 #else /* not USE_X_TOOLKIT */
80 #include "../oldXMenu/XMenu.h"
81 #endif /* not USE_X_TOOLKIT */
82 #endif /* HAVE_X_WINDOWS */
85 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
88 #define min(x,y) (((x) < (y)) ? (x) : (y))
89 #define max(x,y) (((x) > (y)) ? (x) : (y))
96 Lisp_Object Vmenu_updating_frame
;
98 Lisp_Object Qdebug_on_next_call
;
100 extern Lisp_Object Qmenu_bar
;
101 extern Lisp_Object Qmouse_click
, Qevent_kind
;
103 extern Lisp_Object QCtoggle
, QCradio
;
105 extern Lisp_Object Voverriding_local_map
;
106 extern Lisp_Object Voverriding_local_map_menu_flag
;
108 extern Lisp_Object Qoverriding_local_map
, Qoverriding_terminal_local_map
;
110 extern Lisp_Object Qmenu_bar_update_hook
;
113 extern void set_frame_menubar ();
114 extern void process_expose_from_menu ();
115 extern XtAppContext Xt_app_con
;
117 static Lisp_Object
xdialog_show ();
118 void popup_get_selection ();
123 /* Define HAVE_BOXES if meus can handle radio and toggle buttons. */
128 static void push_menu_item
P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
,
129 Lisp_Object
, Lisp_Object
, Lisp_Object
,
131 static Lisp_Object
xmenu_show ();
132 static void keymap_panes ();
133 static void single_keymap_panes ();
134 static void single_menu_item ();
135 static void list_of_panes ();
136 static void list_of_items ();
138 /* This holds a Lisp vector that holds the results of decoding
139 the keymaps or alist-of-alists that specify a menu.
141 It describes the panes and items within the panes.
143 Each pane is described by 3 elements in the vector:
144 t, the pane name, the pane's prefix key.
145 Then follow the pane's items, with 5 elements per item:
146 the item string, the enable flag, the item's value,
147 the definition, and the equivalent keyboard key's description string.
149 In some cases, multiple levels of menus may be described.
150 A single vector slot containing nil indicates the start of a submenu.
151 A single vector slot containing lambda indicates the end of a submenu.
152 The submenu follows a menu item which is the way to reach the submenu.
154 A single vector slot containing quote indicates that the
155 following items should appear on the right of a dialog box.
157 Using a Lisp vector to hold this information while we decode it
158 takes care of protecting all the data from GC. */
160 #define MENU_ITEMS_PANE_NAME 1
161 #define MENU_ITEMS_PANE_PREFIX 2
162 #define MENU_ITEMS_PANE_LENGTH 3
164 #define MENU_ITEMS_ITEM_NAME 0
165 #define MENU_ITEMS_ITEM_ENABLE 1
166 #define MENU_ITEMS_ITEM_VALUE 2
167 #define MENU_ITEMS_ITEM_EQUIV_KEY 3
168 #define MENU_ITEMS_ITEM_DEFINITION 4
169 #define MENU_ITEMS_ITEM_TYPE 5
170 #define MENU_ITEMS_ITEM_SELECTED 6
171 #define MENU_ITEMS_ITEM_LENGTH 7
173 static Lisp_Object menu_items
;
175 /* Number of slots currently allocated in menu_items. */
176 static int menu_items_allocated
;
178 /* This is the index in menu_items of the first empty slot. */
179 static int menu_items_used
;
181 /* The number of panes currently recorded in menu_items,
182 excluding those within submenus. */
183 static int menu_items_n_panes
;
185 /* Current depth within submenus. */
186 static int menu_items_submenu_depth
;
188 /* Flag which when set indicates a dialog or menu has been posted by
189 Xt on behalf of one of the widget sets. */
190 static int popup_activated_flag
;
192 static int next_menubar_widget_id
;
194 /* This is set nonzero after the user activates the menu bar, and set
195 to zero again after the menu bars are redisplayed by prepare_menu_bar.
196 While it is nonzero, all calls to set_frame_menubar go deep.
198 I don't understand why this is needed, but it does seem to be
199 needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */
201 int pending_menu_activation
;
205 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
207 static struct frame
*
208 menubar_id_to_frame (id
)
211 Lisp_Object tail
, frame
;
214 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCONS (tail
)->cdr
)
216 frame
= XCONS (tail
)->car
;
217 if (!GC_FRAMEP (frame
))
220 if (f
->output_data
.nothing
== 1)
222 if (f
->output_data
.x
->id
== id
)
230 /* Initialize the menu_items structure if we haven't already done so.
231 Also mark it as currently empty. */
236 if (NILP (menu_items
))
238 menu_items_allocated
= 60;
239 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
243 menu_items_n_panes
= 0;
244 menu_items_submenu_depth
= 0;
247 /* Call at the end of generating the data in menu_items.
248 This fills in the number of items in the last pane. */
255 /* Call when finished using the data for the current menu
259 discard_menu_items ()
261 /* Free the structure if it is especially large.
262 Otherwise, hold on to it, to save time. */
263 if (menu_items_allocated
> 200)
266 menu_items_allocated
= 0;
270 /* Make the menu_items vector twice as large. */
276 int old_size
= menu_items_allocated
;
279 menu_items_allocated
*= 2;
280 menu_items
= Fmake_vector (make_number (menu_items_allocated
), Qnil
);
281 bcopy (XVECTOR (old
)->contents
, XVECTOR (menu_items
)->contents
,
282 old_size
* sizeof (Lisp_Object
));
285 /* Begin a submenu. */
288 push_submenu_start ()
290 if (menu_items_used
+ 1 > menu_items_allocated
)
293 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qnil
;
294 menu_items_submenu_depth
++;
302 if (menu_items_used
+ 1 > menu_items_allocated
)
305 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qlambda
;
306 menu_items_submenu_depth
--;
309 /* Indicate boundary between left and right. */
312 push_left_right_boundary ()
314 if (menu_items_used
+ 1 > menu_items_allocated
)
317 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qquote
;
320 /* Start a new menu pane in menu_items..
321 NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */
324 push_menu_pane (name
, prefix_vec
)
325 Lisp_Object name
, prefix_vec
;
327 if (menu_items_used
+ MENU_ITEMS_PANE_LENGTH
> menu_items_allocated
)
330 if (menu_items_submenu_depth
== 0)
331 menu_items_n_panes
++;
332 XVECTOR (menu_items
)->contents
[menu_items_used
++] = Qt
;
333 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
334 XVECTOR (menu_items
)->contents
[menu_items_used
++] = prefix_vec
;
337 /* Push one menu item into the current pane. NAME is the string to
338 display. ENABLE if non-nil means this item can be selected. KEY
339 is the key generated by choosing this item, or nil if this item
340 doesn't really have a definition. DEF is the definition of this
341 item. EQUIV is the textual description of the keyboard equivalent
342 for this item (or nil if none). TYPE is the type of this menu
343 item, one of nil, `toggle' or `radio'. */
346 push_menu_item (name
, enable
, key
, def
, equiv
, type
, selected
)
347 Lisp_Object name
, enable
, key
, def
, equiv
, type
, selected
;
349 if (menu_items_used
+ MENU_ITEMS_ITEM_LENGTH
> menu_items_allocated
)
352 XVECTOR (menu_items
)->contents
[menu_items_used
++] = name
;
353 XVECTOR (menu_items
)->contents
[menu_items_used
++] = enable
;
354 XVECTOR (menu_items
)->contents
[menu_items_used
++] = key
;
355 XVECTOR (menu_items
)->contents
[menu_items_used
++] = equiv
;
356 XVECTOR (menu_items
)->contents
[menu_items_used
++] = def
;
357 XVECTOR (menu_items
)->contents
[menu_items_used
++] = type
;
358 XVECTOR (menu_items
)->contents
[menu_items_used
++] = selected
;
361 /* Look through KEYMAPS, a vector of keymaps that is NMAPS long,
362 and generate menu panes for them in menu_items.
363 If NOTREAL is nonzero,
364 don't bother really computing whether an item is enabled. */
367 keymap_panes (keymaps
, nmaps
, notreal
)
368 Lisp_Object
*keymaps
;
376 /* Loop over the given keymaps, making a pane for each map.
377 But don't make a pane that is empty--ignore that map instead.
378 P is the number of panes we have made so far. */
379 for (mapno
= 0; mapno
< nmaps
; mapno
++)
380 single_keymap_panes (keymaps
[mapno
], Qnil
, Qnil
, notreal
, 10);
382 finish_menu_items ();
385 /* This is a recursive subroutine of keymap_panes.
386 It handles one keymap, KEYMAP.
387 The other arguments are passed along
388 or point to local variables of the previous function.
389 If NOTREAL is nonzero, only check for equivalent key bindings, don't
390 evaluate expressions in menu items and don't make any menu.
392 If we encounter submenus deeper than MAXDEPTH levels, ignore them. */
395 single_keymap_panes (keymap
, pane_name
, prefix
, notreal
, maxdepth
)
397 Lisp_Object pane_name
;
402 Lisp_Object pending_maps
= Qnil
;
403 Lisp_Object tail
, item
;
404 struct gcpro gcpro1
, gcpro2
;
410 push_menu_pane (pane_name
, prefix
);
413 /* Remember index for first item in this pane so we can go back and
414 add a prefix when (if) we see the first button. After that, notbuttons
415 is set to 0, to mark that we have seen a button and all non button
416 items need a prefix. */
417 notbuttons
= menu_items_used
;
420 for (tail
= keymap
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
422 GCPRO2 (keymap
, pending_maps
);
423 /* Look at each key binding, and if it is a menu item add it
425 item
= XCONS (tail
)->car
;
427 single_menu_item (XCONS (item
)->car
, XCONS (item
)->cdr
,
428 &pending_maps
, notreal
, maxdepth
, ¬buttons
);
429 else if (VECTORP (item
))
431 /* Loop over the char values represented in the vector. */
432 int len
= XVECTOR (item
)->size
;
434 for (c
= 0; c
< len
; c
++)
436 Lisp_Object character
;
437 XSETFASTINT (character
, c
);
438 single_menu_item (character
, XVECTOR (item
)->contents
[c
],
439 &pending_maps
, notreal
, maxdepth
, ¬buttons
);
445 /* Process now any submenus which want to be panes at this level. */
446 while (!NILP (pending_maps
))
448 Lisp_Object elt
, eltcdr
, string
;
449 elt
= Fcar (pending_maps
);
450 eltcdr
= XCONS (elt
)->cdr
;
451 string
= XCONS (eltcdr
)->car
;
452 /* We no longer discard the @ from the beginning of the string here.
453 Instead, we do this in xmenu_show. */
454 single_keymap_panes (Fcar (elt
), string
,
455 XCONS (eltcdr
)->cdr
, notreal
, maxdepth
- 1);
456 pending_maps
= Fcdr (pending_maps
);
460 /* This is a subroutine of single_keymap_panes that handles one
462 KEY is a key in a keymap and ITEM is its binding.
463 PENDING_MAPS_PTR points to a list of keymaps waiting to be made into
465 If NOTREAL is nonzero, only check for equivalent key bindings, don't
466 evaluate expressions in menu items and don't make any menu.
467 If we encounter submenus deeper than MAXDEPTH levels, ignore them.
468 NOTBUTTONS_PTR is only used when simulating toggle boxes and radio
469 buttons. It points to variable notbuttons in single_keymap_panes,
470 which keeps track of if we have seen a button in this menu or not. */
473 single_menu_item (key
, item
, pending_maps_ptr
, notreal
, maxdepth
,
475 Lisp_Object key
, item
;
476 Lisp_Object
*pending_maps_ptr
;
477 int maxdepth
, notreal
;
480 Lisp_Object def
, map
, item_string
, enabled
;
481 struct gcpro gcpro1
, gcpro2
;
484 /* Parse the menu item and leave the result in item_properties. */
486 res
= parse_menu_item (item
, notreal
, 0);
489 return; /* Not a menu item. */
491 map
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_MAP
];
495 /* We don't want to make a menu, just traverse the keymaps to
496 precompute equivalent key bindings. */
498 single_keymap_panes (map
, Qnil
, key
, 1, maxdepth
- 1);
502 enabled
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_ENABLE
];
503 item_string
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_NAME
];
505 if (!NILP (map
) && XSTRING (item_string
)->data
[0] == '@')
508 /* An enabled separate pane. Remember this to handle it later. */
509 *pending_maps_ptr
= Fcons (Fcons (map
, Fcons (item_string
, key
)),
515 /* Simulate radio buttons and toggle boxes by putting a prefix in
518 Lisp_Object prefix
= Qnil
;
519 Lisp_Object type
= XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
];
523 = XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
];
526 /* The first button. Line up previous items in this menu. */
528 int index
= *notbuttons_ptr
; /* Index for first item this menu. */
531 while (index
< menu_items_used
)
534 = XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
];
538 submenu
++; /* Skip sub menu. */
540 else if (EQ (tem
, Qlambda
))
543 submenu
--; /* End sub menu. */
545 else if (EQ (tem
, Qt
))
546 index
+= 3; /* Skip new pane marker. */
547 else if (EQ (tem
, Qquote
))
548 index
++; /* Skip a left, right divider. */
551 if (!submenu
&& XSTRING (tem
)->data
[0] != '\0'
552 && XSTRING (tem
)->data
[0] != '-')
553 XVECTOR (menu_items
)->contents
[index
+ MENU_ITEMS_ITEM_NAME
]
554 = concat2 (build_string (" "), tem
);
555 index
+= MENU_ITEMS_ITEM_LENGTH
;
561 /* Calculate prefix, if any, for this item. */
562 if (EQ (type
, QCtoggle
))
563 prefix
= build_string (NILP (selected
) ? "[ ] " : "[X] ");
564 else if (EQ (type
, QCradio
))
565 prefix
= build_string (NILP (selected
) ? "( ) " : "(*) ");
567 /* Not a button. If we have earlier buttons, then we need a prefix. */
568 else if (!*notbuttons_ptr
&& XSTRING (item_string
)->data
[0] != '\0'
569 && XSTRING (item_string
)->data
[0] != '-')
570 prefix
= build_string (" ");
573 item_string
= concat2 (prefix
, item_string
);
575 #endif /* not HAVE_BOXES */
577 #ifndef USE_X_TOOLKIT
579 /* Indicate visually that this is a submenu. */
580 item_string
= concat2 (item_string
, build_string (" >"));
583 push_menu_item (item_string
, enabled
, key
,
584 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_DEF
],
585 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_KEYEQ
],
586 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_TYPE
],
587 XVECTOR (item_properties
)->contents
[ITEM_PROPERTY_SELECTED
]);
590 /* Display a submenu using the toolkit. */
591 if (! (NILP (map
) || NILP (enabled
)))
593 push_submenu_start ();
594 single_keymap_panes (map
, Qnil
, key
, 0, maxdepth
- 1);
600 /* Push all the panes and items of a menu described by the
601 alist-of-alists MENU.
602 This handles old-fashioned calls to x-popup-menu. */
612 for (tail
= menu
; !NILP (tail
); tail
= Fcdr (tail
))
614 Lisp_Object elt
, pane_name
, pane_data
;
616 pane_name
= Fcar (elt
);
617 CHECK_STRING (pane_name
, 0);
618 push_menu_pane (pane_name
, Qnil
);
619 pane_data
= Fcdr (elt
);
620 CHECK_CONS (pane_data
, 0);
621 list_of_items (pane_data
);
624 finish_menu_items ();
627 /* Push the items in a single pane defined by the alist PANE. */
633 Lisp_Object tail
, item
, item1
;
635 for (tail
= pane
; !NILP (tail
); tail
= Fcdr (tail
))
639 push_menu_item (item
, Qnil
, Qnil
, Qt
, Qnil
, Qnil
, Qnil
);
640 else if (NILP (item
))
641 push_left_right_boundary ();
644 CHECK_CONS (item
, 0);
646 CHECK_STRING (item1
, 1);
647 push_menu_item (item1
, Qt
, Fcdr (item
), Qt
, Qnil
, Qnil
, Qnil
);
652 DEFUN ("x-popup-menu", Fx_popup_menu
, Sx_popup_menu
, 2, 2, 0,
653 "Pop up a deck-of-cards menu and return user's selection.\n\
654 POSITION is a position specification. This is either a mouse button event\n\
655 or a list ((XOFFSET YOFFSET) WINDOW)\n\
656 where XOFFSET and YOFFSET are positions in pixels from the top left\n\
657 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
658 This controls the position of the center of the first line\n\
659 in the first pane of the menu, not the top left of the menu as a whole.\n\
660 If POSITION is t, it means to use the current mouse position.\n\
662 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
663 The menu items come from key bindings that have a menu string as well as\n\
664 a definition; actually, the \"definition\" in such a key binding looks like\n\
665 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
666 the keymap as a top-level element.\n\n\
667 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.\n\
668 Otherwise, REAL-DEFINITION should be a valid key binding definition.\n\
670 You can also use a list of keymaps as MENU.\n\
671 Then each keymap makes a separate pane.\n\
672 When MENU is a keymap or a list of keymaps, the return value\n\
673 is a list of events.\n\n\
675 Alternatively, you can specify a menu of multiple panes\n\
676 with a list of the form (TITLE PANE1 PANE2...),\n\
677 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
678 Each ITEM is normally a cons cell (STRING . VALUE);\n\
679 but a string can appear as an item--that makes a nonselectable line\n\
681 With this form of menu, the return value is VALUE from the chosen item.\n\
683 If POSITION is nil, don't display the menu at all, just precalculate the\n\
684 cached information about equivalent key sequences.")
686 Lisp_Object position
, menu
;
688 int number_of_panes
, panes
;
689 Lisp_Object keymap
, tem
;
693 Lisp_Object selection
;
696 Lisp_Object x
, y
, window
;
702 if (! NILP (position
))
706 /* Decode the first argument: find the window and the coordinates. */
707 if (EQ (position
, Qt
)
708 || (CONSP (position
) && EQ (XCONS (position
)->car
, Qmenu_bar
)))
710 /* Use the mouse's current position. */
711 FRAME_PTR new_f
= selected_frame
;
712 Lisp_Object bar_window
;
713 enum scroll_bar_part part
;
716 if (mouse_position_hook
)
717 (*mouse_position_hook
) (&new_f
, 1, &bar_window
,
718 &part
, &x
, &y
, &time
);
720 XSETFRAME (window
, new_f
);
723 window
= selected_window
;
730 tem
= Fcar (position
);
733 window
= Fcar (Fcdr (position
));
735 y
= Fcar (Fcdr (tem
));
740 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
741 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
742 tem
= Fcar (Fcdr (Fcdr (tem
))); /* POSN_WINDOW_POSN (tem) */
751 /* Decode where to put the menu. */
759 else if (WINDOWP (window
))
761 CHECK_LIVE_WINDOW (window
, 0);
762 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
764 xpos
= (FONT_WIDTH (f
->output_data
.x
->font
)
765 * XFASTINT (XWINDOW (window
)->left
));
766 ypos
= (f
->output_data
.x
->line_height
767 * XFASTINT (XWINDOW (window
)->top
));
770 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
771 but I don't want to make one now. */
772 CHECK_WINDOW (window
, 0);
777 XSETFRAME (Vmenu_updating_frame
, f
);
779 Vmenu_updating_frame
= Qnil
;
780 #endif /* HAVE_MENUS */
785 /* Decode the menu items from what was specified. */
787 keymap
= Fkeymapp (menu
);
790 tem
= Fkeymapp (Fcar (menu
));
793 /* We were given a keymap. Extract menu info from the keymap. */
795 keymap
= get_keymap (menu
);
797 /* Extract the detailed info to make one pane. */
798 keymap_panes (&menu
, 1, NILP (position
));
800 /* Search for a string appearing directly as an element of the keymap.
801 That string is the title of the menu. */
802 prompt
= map_prompt (keymap
);
803 if (NILP (title
) && !NILP (prompt
))
806 /* Make that be the pane title of the first pane. */
807 if (!NILP (prompt
) && menu_items_n_panes
>= 0)
808 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = prompt
;
812 else if (!NILP (tem
))
814 /* We were given a list of keymaps. */
815 int nmaps
= XFASTINT (Flength (menu
));
817 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
822 /* The first keymap that has a prompt string
823 supplies the menu title. */
824 for (tem
= menu
, i
= 0; CONSP (tem
); tem
= Fcdr (tem
))
828 maps
[i
++] = keymap
= get_keymap (Fcar (tem
));
830 prompt
= map_prompt (keymap
);
831 if (NILP (title
) && !NILP (prompt
))
835 /* Extract the detailed info to make one pane. */
836 keymap_panes (maps
, nmaps
, NILP (position
));
838 /* Make the title be the pane title of the first pane. */
839 if (!NILP (title
) && menu_items_n_panes
>= 0)
840 XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
] = title
;
846 /* We were given an old-fashioned menu. */
848 CHECK_STRING (title
, 1);
850 list_of_panes (Fcdr (menu
));
857 discard_menu_items ();
863 /* Display them in a menu. */
866 selection
= xmenu_show (f
, xpos
, ypos
, for_click
,
867 keymaps
, title
, &error_name
);
870 discard_menu_items ();
873 #endif /* HAVE_MENUS */
875 if (error_name
) error (error_name
);
881 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 2, 0,
882 "Pop up a dialog box and return user's selection.\n\
883 POSITION specifies which frame to use.\n\
884 This is normally a mouse button event or a window or frame.\n\
885 If POSITION is t, it means to use the frame the mouse is on.\n\
886 The dialog box appears in the middle of the specified frame.\n\
888 CONTENTS specifies the alternatives to display in the dialog box.\n\
889 It is a list of the form (TITLE ITEM1 ITEM2...).\n\
890 Each ITEM is a cons cell (STRING . VALUE).\n\
891 The return value is VALUE from the chosen item.\n\n\
892 An ITEM may also be just a string--that makes a nonselectable item.\n\
893 An ITEM may also be nil--that means to put all preceding items\n\
894 on the left of the dialog box and all following items on the right.\n\
895 \(By default, approximately half appear on each side.)")
897 Lisp_Object position
, contents
;
904 /* Decode the first argument: find the window or frame to use. */
905 if (EQ (position
, Qt
)
906 || (CONSP (position
) && EQ (XCONS (position
)->car
, Qmenu_bar
)))
908 #if 0 /* Using the frame the mouse is on may not be right. */
909 /* Use the mouse's current position. */
910 FRAME_PTR new_f
= selected_frame
;
911 Lisp_Object bar_window
;
916 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
919 XSETFRAME (window
, new_f
);
921 window
= selected_window
;
923 window
= selected_window
;
925 else if (CONSP (position
))
928 tem
= Fcar (position
);
930 window
= Fcar (Fcdr (position
));
933 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
934 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
937 else if (WINDOWP (position
) || FRAMEP (position
))
942 /* Decode where to put the menu. */
946 else if (WINDOWP (window
))
948 CHECK_LIVE_WINDOW (window
, 0);
949 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
952 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
953 but I don't want to make one now. */
954 CHECK_WINDOW (window
, 0);
956 #ifndef USE_X_TOOLKIT
957 /* Display a menu with these alternatives
958 in the middle of frame F. */
960 Lisp_Object x
, y
, frame
, newpos
;
961 XSETFRAME (frame
, f
);
962 XSETINT (x
, x_pixel_width (f
) / 2);
963 XSETINT (y
, x_pixel_height (f
) / 2);
964 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
966 return Fx_popup_menu (newpos
,
967 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
973 Lisp_Object selection
;
975 /* Decode the dialog items from what was specified. */
976 title
= Fcar (contents
);
977 CHECK_STRING (title
, 1);
979 list_of_panes (Fcons (contents
, Qnil
));
981 /* Display them in a dialog box. */
983 selection
= xdialog_show (f
, 0, title
, &error_name
);
986 discard_menu_items ();
988 if (error_name
) error (error_name
);
996 /* Loop in Xt until the menu pulldown or dialog popup has been
997 popped down (deactivated). This is used for x-popup-menu
998 and x-popup-dialog; it is not used for the menu bar any more.
1000 NOTE: All calls to popup_get_selection should be protected
1001 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
1004 popup_get_selection (initial_event
, dpyinfo
, id
)
1005 XEvent
*initial_event
;
1006 struct x_display_info
*dpyinfo
;
1011 /* Define a queue to save up for later unreading
1012 all X events that don't pertain to the menu. */
1016 struct event_queue
*next
;
1019 struct event_queue
*queue
= NULL
;
1020 struct event_queue
*queue_tmp
;
1023 event
= *initial_event
;
1025 XtAppNextEvent (Xt_app_con
, &event
);
1029 /* Handle expose events for editor frames right away. */
1030 if (event
.type
== Expose
)
1031 process_expose_from_menu (event
);
1032 /* Make sure we don't consider buttons grabbed after menu goes.
1033 And make sure to deactivate for any ButtonRelease,
1034 even if XtDispatchEvent doesn't do that. */
1035 else if (event
.type
== ButtonRelease
1036 && dpyinfo
->display
== event
.xbutton
.display
)
1038 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
1039 popup_activated_flag
= 0;
1040 #ifdef USE_MOTIF /* Pretending that the event came from a
1041 Btn1Down seems the only way to convince Motif to
1042 activate its callbacks; setting the XmNmenuPost
1043 isn't working. --marcus@sysc.pdx.edu. */
1044 event
.xbutton
.button
= 1;
1047 /* If the user presses a key, deactivate the menu.
1048 The user is likely to do that if we get wedged. */
1049 else if (event
.type
== KeyPress
1050 && dpyinfo
->display
== event
.xbutton
.display
)
1052 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
1053 if (!IsModifierKey (keysym
))
1055 popup_activated_flag
= 0;
1059 /* Button presses outside the menu also pop it down. */
1060 else if (event
.type
== ButtonPress
1061 && event
.xany
.display
== dpyinfo
->display
1062 && x_any_window_to_frame (dpyinfo
, event
.xany
.window
))
1064 popup_activated_flag
= 0;
1068 /* Queue all events not for this popup,
1069 except for Expose, which we've already handled, and ButtonRelease.
1070 Note that the X window is associated with the frame if this
1071 is a menu bar popup, but not if it's a dialog box. So we use
1072 x_non_menubar_window_to_frame, not x_any_window_to_frame. */
1073 if (event
.type
!= Expose
1074 && !(event
.type
== ButtonRelease
1075 && dpyinfo
->display
== event
.xbutton
.display
)
1076 && (event
.xany
.display
!= dpyinfo
->display
1077 || x_non_menubar_window_to_frame (dpyinfo
, event
.xany
.window
)))
1079 queue_tmp
= (struct event_queue
*) malloc (sizeof (struct event_queue
));
1081 if (queue_tmp
!= NULL
)
1083 queue_tmp
->event
= event
;
1084 queue_tmp
->next
= queue
;
1089 XtDispatchEvent (&event
);
1091 if (!popup_activated ())
1093 XtAppNextEvent (Xt_app_con
, &event
);
1096 /* Unread any events that we got but did not handle. */
1097 while (queue
!= NULL
)
1100 XPutBackEvent (queue_tmp
->event
.xany
.display
, &queue_tmp
->event
);
1101 queue
= queue_tmp
->next
;
1102 free ((char *)queue_tmp
);
1103 /* Cause these events to get read as soon as we UNBLOCK_INPUT. */
1104 interrupt_input_pending
= 1;
1108 /* Activate the menu bar of frame F.
1109 This is called from keyboard.c when it gets the
1110 menu_bar_activate_event out of the Emacs event queue.
1112 To activate the menu bar, we use the X button-press event
1113 that was saved in saved_menu_event.
1114 That makes the toolkit do its thing.
1116 But first we recompute the menu bar contents (the whole tree).
1118 The reason for saving the button event until here, instead of
1119 passing it to the toolkit right away, is that we can safely
1120 execute Lisp code. */
1123 x_activate_menubar (f
)
1126 if (!f
->output_data
.x
->saved_menu_event
->type
)
1129 set_frame_menubar (f
, 0, 1);
1131 XtDispatchEvent ((XEvent
*) f
->output_data
.x
->saved_menu_event
);
1134 if (f
->output_data
.x
->saved_menu_event
->type
== ButtonRelease
)
1135 pending_menu_activation
= 1;
1138 /* Ignore this if we get it a second time. */
1139 f
->output_data
.x
->saved_menu_event
->type
= 0;
1142 /* Detect if a dialog or menu has been posted. */
1147 return popup_activated_flag
;
1151 /* This callback is invoked when the user selects a menubar cascade
1152 pushbutton, but before the pulldown menu is posted. */
1155 popup_activate_callback (widget
, id
, client_data
)
1158 XtPointer client_data
;
1160 popup_activated_flag
= 1;
1163 /* This callback is called from the menu bar pulldown menu
1164 when the user makes a selection.
1165 Figure out what the user chose
1166 and put the appropriate events into the keyboard buffer. */
1169 menubar_selection_callback (widget
, id
, client_data
)
1172 XtPointer client_data
;
1174 Lisp_Object prefix
, entry
;
1175 FRAME_PTR f
= menubar_id_to_frame (id
);
1177 Lisp_Object
*subprefix_stack
;
1178 int submenu_depth
= 0;
1183 subprefix_stack
= (Lisp_Object
*) alloca (f
->menu_bar_items_used
* sizeof (Lisp_Object
));
1184 vector
= f
->menu_bar_vector
;
1187 while (i
< f
->menu_bar_items_used
)
1189 if (EQ (XVECTOR (vector
)->contents
[i
], Qnil
))
1191 subprefix_stack
[submenu_depth
++] = prefix
;
1195 else if (EQ (XVECTOR (vector
)->contents
[i
], Qlambda
))
1197 prefix
= subprefix_stack
[--submenu_depth
];
1200 else if (EQ (XVECTOR (vector
)->contents
[i
], Qt
))
1202 prefix
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1203 i
+= MENU_ITEMS_PANE_LENGTH
;
1207 entry
= XVECTOR (vector
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1208 /* The EMACS_INT cast avoids a warning. There's no problem
1209 as long as pointers have enough bits to hold small integers. */
1210 if ((int) (EMACS_INT
) client_data
== i
)
1213 struct input_event buf
;
1216 XSETFRAME (frame
, f
);
1217 buf
.kind
= menu_bar_event
;
1218 buf
.frame_or_window
= Fcons (frame
, Fcons (Qmenu_bar
, Qnil
));
1219 kbd_buffer_store_event (&buf
);
1221 for (j
= 0; j
< submenu_depth
; j
++)
1222 if (!NILP (subprefix_stack
[j
]))
1224 buf
.kind
= menu_bar_event
;
1225 buf
.frame_or_window
= Fcons (frame
, subprefix_stack
[j
]);
1226 kbd_buffer_store_event (&buf
);
1231 buf
.kind
= menu_bar_event
;
1232 buf
.frame_or_window
= Fcons (frame
, prefix
);
1233 kbd_buffer_store_event (&buf
);
1236 buf
.kind
= menu_bar_event
;
1237 buf
.frame_or_window
= Fcons (frame
, entry
);
1238 kbd_buffer_store_event (&buf
);
1242 i
+= MENU_ITEMS_ITEM_LENGTH
;
1247 /* This callback is invoked when a dialog or menu is finished being
1248 used and has been unposted. */
1251 popup_deactivate_callback (widget
, id
, client_data
)
1254 XtPointer client_data
;
1256 popup_activated_flag
= 0;
1259 /* Allocate a widget_value, blocking input. */
1262 xmalloc_widget_value ()
1264 widget_value
*value
;
1267 value
= malloc_widget_value ();
1273 /* This recursively calls free_widget_value on the tree of widgets.
1274 It must free all data that was malloc'ed for these widget_values.
1275 In Emacs, many slots are pointers into the data of Lisp_Strings, and
1276 must be left alone. */
1279 free_menubar_widget_value_tree (wv
)
1284 wv
->name
= wv
->value
= wv
->key
= (char *) 0xDEADBEEF;
1286 if (wv
->contents
&& (wv
->contents
!= (widget_value
*)1))
1288 free_menubar_widget_value_tree (wv
->contents
);
1289 wv
->contents
= (widget_value
*) 0xDEADBEEF;
1293 free_menubar_widget_value_tree (wv
->next
);
1294 wv
->next
= (widget_value
*) 0xDEADBEEF;
1297 free_widget_value (wv
);
1301 /* Return a tree of widget_value structures for a menu bar item
1302 whose event type is ITEM_KEY (with string ITEM_NAME)
1303 and whose contents come from the list of keymaps MAPS. */
1305 static widget_value
*
1306 single_submenu (item_key
, item_name
, maps
)
1307 Lisp_Object item_key
, item_name
, maps
;
1309 widget_value
*wv
, *prev_wv
, *save_wv
, *first_wv
;
1311 int submenu_depth
= 0;
1314 Lisp_Object
*mapvec
;
1315 widget_value
**submenu_stack
;
1317 int previous_items
= menu_items_used
;
1318 int top_level_items
= 0;
1320 length
= Flength (maps
);
1321 len
= XINT (length
);
1323 /* Convert the list MAPS into a vector MAPVEC. */
1324 mapvec
= (Lisp_Object
*) alloca (len
* sizeof (Lisp_Object
));
1325 for (i
= 0; i
< len
; i
++)
1327 mapvec
[i
] = Fcar (maps
);
1331 menu_items_n_panes
= 0;
1333 /* Loop over the given keymaps, making a pane for each map.
1334 But don't make a pane that is empty--ignore that map instead. */
1335 for (i
= 0; i
< len
; i
++)
1337 if (SYMBOLP (mapvec
[i
])
1338 || (CONSP (mapvec
[i
])
1339 && NILP (Fkeymapp (mapvec
[i
]))))
1341 /* Here we have a command at top level in the menu bar
1342 as opposed to a submenu. */
1343 top_level_items
= 1;
1344 push_menu_pane (Qnil
, Qnil
);
1345 push_menu_item (item_name
, Qt
, item_key
, mapvec
[i
], Qnil
, Qnil
, Qnil
);
1348 single_keymap_panes (mapvec
[i
], item_name
, item_key
, 0, 10);
1351 /* Create a tree of widget_value objects
1352 representing the panes and their items. */
1355 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1356 wv
= xmalloc_widget_value ();
1360 wv
->button_type
= BUTTON_TYPE_NONE
;
1365 /* Loop over all panes and items made during this call
1366 and construct a tree of widget_value objects.
1367 Ignore the panes and items made by previous calls to
1368 single_submenu, even though those are also in menu_items. */
1370 while (i
< menu_items_used
)
1372 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1374 submenu_stack
[submenu_depth
++] = save_wv
;
1379 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1382 save_wv
= submenu_stack
[--submenu_depth
];
1385 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1386 && submenu_depth
!= 0)
1387 i
+= MENU_ITEMS_PANE_LENGTH
;
1388 /* Ignore a nil in the item list.
1389 It's meaningful only for dialog boxes. */
1390 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1392 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1394 /* Create a new pane. */
1395 Lisp_Object pane_name
, prefix
;
1397 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1398 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1399 #ifndef HAVE_MULTILINGUAL_MENU
1400 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1401 pane_name
= string_make_unibyte (pane_name
);
1403 pane_string
= (NILP (pane_name
)
1404 ? "" : (char *) XSTRING (pane_name
)->data
);
1405 /* If there is just one top-level pane, put all its items directly
1406 under the top-level menu. */
1407 if (menu_items_n_panes
== 1)
1410 /* If the pane has a meaningful name,
1411 make the pane a top-level menu item
1412 with its items as a submenu beneath it. */
1413 if (strcmp (pane_string
, ""))
1415 wv
= xmalloc_widget_value ();
1419 first_wv
->contents
= wv
;
1420 wv
->name
= pane_string
;
1421 /* Ignore the @ that means "separate pane".
1422 This is a kludge, but this isn't worth more time. */
1423 if (!NILP (prefix
) && wv
->name
[0] == '@')
1427 wv
->button_type
= BUTTON_TYPE_NONE
;
1431 i
+= MENU_ITEMS_PANE_LENGTH
;
1435 /* Create a new item within current pane. */
1436 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
1437 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
1438 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
1440 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
1441 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
1442 type
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_TYPE
];
1443 selected
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_SELECTED
];
1445 #ifndef HAVE_MULTILINGUAL_MENU
1446 if (STRING_MULTIBYTE (item_name
))
1447 item_name
= string_make_unibyte (item_name
);
1448 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1449 descrip
= string_make_unibyte (descrip
);
1452 wv
= xmalloc_widget_value ();
1456 save_wv
->contents
= wv
;
1458 wv
->name
= (char *) XSTRING (item_name
)->data
;
1459 if (!NILP (descrip
))
1460 wv
->key
= (char *) XSTRING (descrip
)->data
;
1462 /* The EMACS_INT cast avoids a warning. There's no problem
1463 as long as pointers have enough bits to hold small integers. */
1464 wv
->call_data
= (!NILP (def
) ? (void *) (EMACS_INT
) i
: 0);
1465 wv
->enabled
= !NILP (enable
);
1468 wv
->button_type
= BUTTON_TYPE_NONE
;
1469 else if (EQ (type
, QCradio
))
1470 wv
->button_type
= BUTTON_TYPE_RADIO
;
1471 else if (EQ (type
, QCtoggle
))
1472 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1476 wv
->selected
= !NILP (selected
);
1480 i
+= MENU_ITEMS_ITEM_LENGTH
;
1484 /* If we have just one "menu item"
1485 that was originally a button, return it by itself. */
1486 if (top_level_items
&& first_wv
->contents
&& first_wv
->contents
->next
== 0)
1488 wv
= first_wv
->contents
;
1489 free_widget_value (first_wv
);
1496 extern void EmacsFrameSetCharSize ();
1498 /* Recompute all the widgets of frame F, when the menu bar
1499 has been changed. */
1502 update_frame_menubar (f
)
1505 struct x_output
*x
= f
->output_data
.x
;
1507 int menubar_changed
;
1509 Dimension shell_height
;
1511 /* We assume the menubar contents has changed if the global flag is set,
1512 or if the current buffer has changed, or if the menubar has never
1513 been updated before.
1515 menubar_changed
= (x
->menubar_widget
1516 && !XtIsManaged (x
->menubar_widget
));
1518 if (! (menubar_changed
))
1522 /* Save the size of the frame because the pane widget doesn't accept to
1523 resize itself. So force it. */
1527 /* Do the voodoo which means "I'm changing lots of things, don't try to
1528 refigure sizes until I'm done." */
1529 lw_refigure_widget (x
->column_widget
, False
);
1531 /* the order in which children are managed is the top to
1532 bottom order in which they are displayed in the paned window.
1533 First, remove the text-area widget.
1535 XtUnmanageChild (x
->edit_widget
);
1537 /* remove the menubar that is there now, and put up the menubar that
1540 if (menubar_changed
)
1542 XtManageChild (x
->menubar_widget
);
1543 XtMapWidget (x
->menubar_widget
);
1544 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, 0);
1547 /* Re-manage the text-area widget, and then thrash the sizes. */
1548 XtManageChild (x
->edit_widget
);
1549 lw_refigure_widget (x
->column_widget
, True
);
1551 /* Force the pane widget to resize itself with the right values. */
1552 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
1557 /* Set the contents of the menubar widgets of frame F.
1558 The argument FIRST_TIME is currently ignored;
1559 it is set the first time this is called, from initialize_frame_menubar. */
1562 set_frame_menubar (f
, first_time
, deep_p
)
1567 Widget menubar_widget
= f
->output_data
.x
->menubar_widget
;
1568 Lisp_Object tail
, items
, frame
;
1569 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
1573 XSETFRAME (Vmenu_updating_frame
, f
);
1575 if (f
->output_data
.x
->id
== 0)
1576 f
->output_data
.x
->id
= next_menubar_widget_id
++;
1577 id
= f
->output_data
.x
->id
;
1579 if (! menubar_widget
)
1581 else if (pending_menu_activation
&& !deep_p
)
1583 /* Make the first call for any given frame always go deep. */
1584 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
1587 f
->output_data
.x
->saved_menu_event
= (XEvent
*)xmalloc (sizeof (XEvent
));
1588 f
->output_data
.x
->saved_menu_event
->type
= 0;
1591 wv
= xmalloc_widget_value ();
1592 wv
->name
= "menubar";
1595 wv
->button_type
= BUTTON_TYPE_NONE
;
1600 /* Make a widget-value tree representing the entire menu trees. */
1602 struct buffer
*prev
= current_buffer
;
1604 int specpdl_count
= specpdl_ptr
- specpdl
;
1605 int previous_menu_items_used
= f
->menu_bar_items_used
;
1606 Lisp_Object
*previous_items
1607 = (Lisp_Object
*) alloca (previous_menu_items_used
1608 * sizeof (Lisp_Object
));
1610 /* If we are making a new widget, its contents are empty,
1611 do always reinitialize them. */
1612 if (! menubar_widget
)
1613 previous_menu_items_used
= 0;
1615 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
1616 specbind (Qinhibit_quit
, Qt
);
1617 /* Don't let the debugger step into this code
1618 because it is not reentrant. */
1619 specbind (Qdebug_on_next_call
, Qnil
);
1621 record_unwind_protect (Fset_match_data
, Fmatch_data (Qnil
, Qnil
));
1622 if (NILP (Voverriding_local_map_menu_flag
))
1624 specbind (Qoverriding_terminal_local_map
, Qnil
);
1625 specbind (Qoverriding_local_map
, Qnil
);
1628 set_buffer_internal_1 (XBUFFER (buffer
));
1630 /* Run the Lucid hook. */
1631 call1 (Vrun_hooks
, Qactivate_menubar_hook
);
1632 /* If it has changed current-menubar from previous value,
1633 really recompute the menubar from the value. */
1634 if (! NILP (Vlucid_menu_bar_dirty_flag
))
1635 call0 (Qrecompute_lucid_menubar
);
1636 safe_run_hooks (Qmenu_bar_update_hook
);
1637 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1639 items
= FRAME_MENU_BAR_ITEMS (f
);
1641 inhibit_garbage_collection ();
1643 /* Save the frame's previous menu bar contents data. */
1644 bcopy (XVECTOR (f
->menu_bar_vector
)->contents
, previous_items
,
1645 previous_menu_items_used
* sizeof (Lisp_Object
));
1647 /* Fill in the current menu bar contents. */
1648 menu_items
= f
->menu_bar_vector
;
1649 menu_items_allocated
= XVECTOR (menu_items
)->size
;
1651 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1653 Lisp_Object key
, string
, maps
;
1655 key
= XVECTOR (items
)->contents
[i
];
1656 string
= XVECTOR (items
)->contents
[i
+ 1];
1657 maps
= XVECTOR (items
)->contents
[i
+ 2];
1661 wv
= single_submenu (key
, string
, maps
);
1665 first_wv
->contents
= wv
;
1666 /* Don't set wv->name here; GC during the loop might relocate it. */
1668 wv
->button_type
= BUTTON_TYPE_NONE
;
1672 finish_menu_items ();
1674 set_buffer_internal_1 (prev
);
1675 unbind_to (specpdl_count
, Qnil
);
1677 /* If there has been no change in the Lisp-level contents
1678 of the menu bar, skip redisplaying it. Just exit. */
1680 for (i
= 0; i
< previous_menu_items_used
; i
++)
1681 if (menu_items_used
== i
1682 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
1684 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
1686 free_menubar_widget_value_tree (first_wv
);
1692 /* Now GC cannot happen during the lifetime of the widget_value,
1693 so it's safe to store data from a Lisp_String. */
1694 wv
= first_wv
->contents
;
1695 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1698 string
= XVECTOR (items
)->contents
[i
+ 1];
1701 wv
->name
= (char *) XSTRING (string
)->data
;
1705 f
->menu_bar_vector
= menu_items
;
1706 f
->menu_bar_items_used
= menu_items_used
;
1711 /* Make a widget-value tree containing
1712 just the top level menu bar strings. */
1714 items
= FRAME_MENU_BAR_ITEMS (f
);
1715 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 4)
1719 string
= XVECTOR (items
)->contents
[i
+ 1];
1723 wv
= xmalloc_widget_value ();
1724 wv
->name
= (char *) XSTRING (string
)->data
;
1727 wv
->button_type
= BUTTON_TYPE_NONE
;
1728 /* This prevents lwlib from assuming this
1729 menu item is really supposed to be empty. */
1730 /* The EMACS_INT cast avoids a warning.
1731 This value just has to be different from small integers. */
1732 wv
->call_data
= (void *) (EMACS_INT
) (-1);
1737 first_wv
->contents
= wv
;
1741 /* Forget what we thought we knew about what is in the
1742 detailed contents of the menu bar menus.
1743 Changing the top level always destroys the contents. */
1744 f
->menu_bar_items_used
= 0;
1747 /* Create or update the menu bar widget. */
1753 /* Disable resizing (done for Motif!) */
1754 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
1756 /* The third arg is DEEP_P, which says to consider the entire
1757 menu trees we supply, rather than just the menu bar item names. */
1758 lw_modify_all_widgets (id
, first_wv
, deep_p
);
1760 /* Re-enable the edit widget to resize. */
1761 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
1765 menubar_widget
= lw_create_widget ("menubar", "menubar", id
, first_wv
,
1766 f
->output_data
.x
->column_widget
,
1768 popup_activate_callback
,
1769 menubar_selection_callback
,
1770 popup_deactivate_callback
);
1771 f
->output_data
.x
->menubar_widget
= menubar_widget
;
1776 = (f
->output_data
.x
->menubar_widget
1777 ? (f
->output_data
.x
->menubar_widget
->core
.height
1778 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
1781 #if 0 /* Experimentally, we now get the right results
1782 for -geometry -0-0 without this. 24 Aug 96, rms. */
1784 if (FRAME_EXTERNAL_MENU_BAR (f
))
1787 XtVaGetValues (f
->output_data
.x
->column_widget
,
1788 XtNinternalBorderWidth
, &ibw
, NULL
);
1789 menubar_size
+= ibw
;
1791 #endif /* USE_LUCID */
1794 f
->output_data
.x
->menubar_height
= menubar_size
;
1797 free_menubar_widget_value_tree (first_wv
);
1799 update_frame_menubar (f
);
1804 /* Called from Fx_create_frame to create the initial menubar of a frame
1805 before it is mapped, so that the window is mapped with the menubar already
1806 there instead of us tacking it on later and thrashing the window after it
1810 initialize_frame_menubar (f
)
1813 /* This function is called before the first chance to redisplay
1814 the frame. It has to be, so the frame will have the right size. */
1815 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1816 set_frame_menubar (f
, 1, 1);
1819 /* Get rid of the menu bar of frame F, and free its storage.
1820 This is used when deleting a frame, and when turning off the menu bar. */
1823 free_frame_menubar (f
)
1826 Widget menubar_widget
;
1829 menubar_widget
= f
->output_data
.x
->menubar_widget
;
1831 f
->output_data
.x
->menubar_height
= 0;
1836 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
1841 #endif /* USE_X_TOOLKIT */
1843 /* xmenu_show actually displays a menu using the panes and items in menu_items
1844 and returns the value selected from it.
1845 There are two versions of xmenu_show, one for Xt and one for Xlib.
1846 Both assume input is blocked by the caller. */
1848 /* F is the frame the menu is for.
1849 X and Y are the frame-relative specified position,
1850 relative to the inside upper left corner of the frame F.
1851 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1852 KEYMAPS is 1 if this menu was specified with keymaps;
1853 in that case, we return a list containing the chosen item's value
1854 and perhaps also the pane's prefix.
1855 TITLE is the specified menu title.
1856 ERROR is a place to store an error message string in case of failure.
1857 (We return nil on failure, but the value doesn't actually matter.) */
1859 #ifdef USE_X_TOOLKIT
1861 /* We need a unique id for each widget handled by the Lucid Widget
1864 For the main windows, and popup menus, we use this counter,
1865 which we increment each time after use. This starts from 1<<16.
1867 For menu bars, we use numbers starting at 0, counted in
1868 next_menubar_widget_id. */
1869 LWLIB_ID widget_id_tick
;
1872 static Lisp_Object
*volatile menu_item_selection
;
1874 static Lisp_Object
*menu_item_selection
;
1878 popup_selection_callback (widget
, id
, client_data
)
1881 XtPointer client_data
;
1883 menu_item_selection
= (Lisp_Object
*) client_data
;
1887 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
1901 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1902 widget_value
**submenu_stack
1903 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1904 Lisp_Object
*subprefix_stack
1905 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1906 int submenu_depth
= 0;
1907 XButtonPressedEvent dummy
;
1910 int next_release_must_exit
= 0;
1914 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1916 *error
= "Empty menu";
1920 /* Create a tree of widget_value objects
1921 representing the panes and their items. */
1922 wv
= xmalloc_widget_value ();
1926 wv
->button_type
= BUTTON_TYPE_NONE
;
1930 /* Loop over all panes and items, filling in the tree. */
1932 while (i
< menu_items_used
)
1934 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1936 submenu_stack
[submenu_depth
++] = save_wv
;
1942 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1945 save_wv
= submenu_stack
[--submenu_depth
];
1949 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1950 && submenu_depth
!= 0)
1951 i
+= MENU_ITEMS_PANE_LENGTH
;
1952 /* Ignore a nil in the item list.
1953 It's meaningful only for dialog boxes. */
1954 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1956 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1958 /* Create a new pane. */
1959 Lisp_Object pane_name
, prefix
;
1961 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
1962 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1963 #ifndef HAVE_MULTILINGUAL_MENU
1964 if (!NILP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1965 pane_name
= string_make_unibyte (pane_name
);
1967 pane_string
= (NILP (pane_name
)
1968 ? "" : (char *) XSTRING (pane_name
)->data
);
1969 /* If there is just one top-level pane, put all its items directly
1970 under the top-level menu. */
1971 if (menu_items_n_panes
== 1)
1974 /* If the pane has a meaningful name,
1975 make the pane a top-level menu item
1976 with its items as a submenu beneath it. */
1977 if (!keymaps
&& strcmp (pane_string
, ""))
1979 wv
= xmalloc_widget_value ();
1983 first_wv
->contents
= wv
;
1984 wv
->name
= pane_string
;
1985 if (keymaps
&& !NILP (prefix
))
1989 wv
->button_type
= BUTTON_TYPE_NONE
;
1993 else if (first_pane
)
1999 i
+= MENU_ITEMS_PANE_LENGTH
;
2003 /* Create a new item within current pane. */
2004 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
;
2005 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2006 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2008 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2009 def
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_DEFINITION
];
2010 type
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_TYPE
];
2011 selected
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_SELECTED
];
2013 #ifndef HAVE_MULTILINGUAL_MENU
2014 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
2015 item_name
= string_make_unibyte (item_name
);
2016 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
2017 item_name
= string_make_unibyte (descrip
);
2020 wv
= xmalloc_widget_value ();
2024 save_wv
->contents
= wv
;
2025 wv
->name
= (char *) XSTRING (item_name
)->data
;
2026 if (!NILP (descrip
))
2027 wv
->key
= (char *) XSTRING (descrip
)->data
;
2029 /* If this item has a null value,
2030 make the call_data null so that it won't display a box
2031 when the mouse is on it. */
2033 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
2034 wv
->enabled
= !NILP (enable
);
2037 wv
->button_type
= BUTTON_TYPE_NONE
;
2038 else if (EQ (type
, QCtoggle
))
2039 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
2040 else if (EQ (type
, QCradio
))
2041 wv
->button_type
= BUTTON_TYPE_RADIO
;
2045 wv
->selected
= !NILP (selected
);
2049 i
+= MENU_ITEMS_ITEM_LENGTH
;
2053 /* Deal with the title, if it is non-nil. */
2056 widget_value
*wv_title
= xmalloc_widget_value ();
2057 widget_value
*wv_sep1
= xmalloc_widget_value ();
2058 widget_value
*wv_sep2
= xmalloc_widget_value ();
2060 wv_sep2
->name
= "--";
2061 wv_sep2
->next
= first_wv
->contents
;
2063 wv_sep1
->name
= "--";
2064 wv_sep1
->next
= wv_sep2
;
2066 #ifndef HAVE_MULTILINGUAL_MENU
2067 if (STRING_MULTIBYTE (title
))
2068 title
= string_make_unibyte (title
);
2070 wv_title
->name
= (char *) XSTRING (title
)->data
;
2071 wv_title
->enabled
= True
;
2072 wv_title
->button_type
= BUTTON_TYPE_NONE
;
2073 wv_title
->next
= wv_sep1
;
2074 first_wv
->contents
= wv_title
;
2077 /* Actually create the menu. */
2078 menu_id
= widget_id_tick
++;
2079 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
2080 f
->output_data
.x
->widget
, 1, 0,
2081 popup_selection_callback
,
2082 popup_deactivate_callback
);
2084 /* Adjust coordinates to relative to the outer (window manager) window. */
2087 int win_x
= 0, win_y
= 0;
2089 /* Find the position of the outside upper-left corner of
2090 the inner window, with respect to the outer window. */
2091 if (f
->output_data
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2094 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2096 /* From-window, to-window. */
2097 f
->output_data
.x
->window_desc
,
2098 f
->output_data
.x
->parent_desc
,
2100 /* From-position, to-position. */
2101 0, 0, &win_x
, &win_y
,
2103 /* Child of window. */
2111 /* Adjust coordinates to be root-window-relative. */
2112 x
+= f
->output_data
.x
->left_pos
;
2113 y
+= f
->output_data
.x
->top_pos
;
2115 dummy
.type
= ButtonPress
;
2117 dummy
.send_event
= 0;
2118 dummy
.display
= FRAME_X_DISPLAY (f
);
2119 dummy
.time
= CurrentTime
;
2120 dummy
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
2121 dummy
.window
= dummy
.root
;
2122 dummy
.subwindow
= dummy
.root
;
2127 dummy
.state
= (FRAME_X_DISPLAY_INFO (f
)->grabbed
>> 1) * Button1Mask
;
2129 for (i
= 0; i
< 5; i
++)
2130 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
2133 /* Don't allow any geometry request from the user. */
2134 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
2135 XtSetValues (menu
, av
, ac
);
2137 /* Free the widget_value objects we used to specify the contents. */
2138 free_menubar_widget_value_tree (first_wv
);
2140 /* No selection has been chosen yet. */
2141 menu_item_selection
= 0;
2143 /* Display the menu. */
2144 lw_popup_menu (menu
, &dummy
);
2145 popup_activated_flag
= 1;
2147 /* Process events that apply to the menu. */
2148 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
);
2150 #ifdef LESSTIF_VERSION
2151 /* Nov 1998: For an unknown reason a button grab remains active
2152 after the popup menu has gone. */
2153 XUngrabButton (XtDisplay (f
->output_data
.x
->widget
),
2154 AnyButton
, AnyModifier
,
2155 XtWindow (f
->output_data
.x
->widget
));
2156 XUngrabButton (XtDisplay (f
->output_data
.x
->edit_widget
),
2157 AnyButton
, AnyModifier
,
2158 XtWindow (f
->output_data
.x
->edit_widget
));
2159 #endif /* LESSTIF_VERSION */
2161 /* fp turned off the following statement and wrote a comment
2162 that it is unnecessary--that the menu has already disappeared.
2163 Nowadays the menu disappears ok, all right, but
2164 we need to delete the widgets or multiple ones will pile up. */
2165 lw_destroy_all_widgets (menu_id
);
2167 /* Find the selected item, and its pane, to return
2168 the proper value. */
2169 if (menu_item_selection
!= 0)
2171 Lisp_Object prefix
, entry
;
2175 while (i
< menu_items_used
)
2177 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
2179 subprefix_stack
[submenu_depth
++] = prefix
;
2183 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
2185 prefix
= subprefix_stack
[--submenu_depth
];
2188 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2191 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2192 i
+= MENU_ITEMS_PANE_LENGTH
;
2194 /* Ignore a nil in the item list.
2195 It's meaningful only for dialog boxes. */
2196 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2201 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2202 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2208 entry
= Fcons (entry
, Qnil
);
2210 entry
= Fcons (prefix
, entry
);
2211 for (j
= submenu_depth
- 1; j
>= 0; j
--)
2212 if (!NILP (subprefix_stack
[j
]))
2213 entry
= Fcons (subprefix_stack
[j
], entry
);
2217 i
+= MENU_ITEMS_ITEM_LENGTH
;
2226 dialog_selection_callback (widget
, id
, client_data
)
2229 XtPointer client_data
;
2231 /* The EMACS_INT cast avoids a warning. There's no problem
2232 as long as pointers have enough bits to hold small integers. */
2233 if ((int) (EMACS_INT
) client_data
!= -1)
2234 menu_item_selection
= (Lisp_Object
*) client_data
;
2236 lw_destroy_all_widgets (id
);
2238 popup_activated_flag
= 0;
2241 static char * button_names
[] = {
2242 "button1", "button2", "button3", "button4", "button5",
2243 "button6", "button7", "button8", "button9", "button10" };
2246 xdialog_show (f
, keymaps
, title
, error
)
2252 int i
, nb_buttons
=0;
2255 char dialog_name
[6];
2257 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
2259 /* Number of elements seen so far, before boundary. */
2261 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2262 int boundary_seen
= 0;
2266 if (menu_items_n_panes
> 1)
2268 *error
= "Multiple panes in dialog box";
2272 /* Create a tree of widget_value objects
2273 representing the text label and buttons. */
2275 Lisp_Object pane_name
, prefix
;
2277 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
2278 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
2279 pane_string
= (NILP (pane_name
)
2280 ? "" : (char *) XSTRING (pane_name
)->data
);
2281 prev_wv
= xmalloc_widget_value ();
2282 prev_wv
->value
= pane_string
;
2283 if (keymaps
&& !NILP (prefix
))
2285 prev_wv
->enabled
= 1;
2286 prev_wv
->name
= "message";
2289 /* Loop over all panes and items, filling in the tree. */
2290 i
= MENU_ITEMS_PANE_LENGTH
;
2291 while (i
< menu_items_used
)
2294 /* Create a new item within current pane. */
2295 Lisp_Object item_name
, enable
, descrip
;
2296 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2297 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2299 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2301 if (NILP (item_name
))
2303 free_menubar_widget_value_tree (first_wv
);
2304 *error
= "Submenu in dialog items";
2307 if (EQ (item_name
, Qquote
))
2309 /* This is the boundary between left-side elts
2310 and right-side elts. Stop incrementing right_count. */
2315 if (nb_buttons
>= 9)
2317 free_menubar_widget_value_tree (first_wv
);
2318 *error
= "Too many dialog items";
2322 wv
= xmalloc_widget_value ();
2324 wv
->name
= (char *) button_names
[nb_buttons
];
2325 if (!NILP (descrip
))
2326 wv
->key
= (char *) XSTRING (descrip
)->data
;
2327 wv
->value
= (char *) XSTRING (item_name
)->data
;
2328 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
2329 wv
->enabled
= !NILP (enable
);
2332 if (! boundary_seen
)
2336 i
+= MENU_ITEMS_ITEM_LENGTH
;
2339 /* If the boundary was not specified,
2340 by default put half on the left and half on the right. */
2341 if (! boundary_seen
)
2342 left_count
= nb_buttons
- nb_buttons
/ 2;
2344 wv
= xmalloc_widget_value ();
2345 wv
->name
= dialog_name
;
2347 /* Dialog boxes use a really stupid name encoding
2348 which specifies how many buttons to use
2349 and how many buttons are on the right.
2350 The Q means something also. */
2351 dialog_name
[0] = 'Q';
2352 dialog_name
[1] = '0' + nb_buttons
;
2353 dialog_name
[2] = 'B';
2354 dialog_name
[3] = 'R';
2355 /* Number of buttons to put on the right. */
2356 dialog_name
[4] = '0' + nb_buttons
- left_count
;
2358 wv
->contents
= first_wv
;
2362 /* Actually create the dialog. */
2363 dialog_id
= widget_id_tick
++;
2364 menu
= lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
2365 f
->output_data
.x
->widget
, 1, 0,
2366 dialog_selection_callback
, 0);
2367 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
2368 /* Free the widget_value objects we used to specify the contents. */
2369 free_menubar_widget_value_tree (first_wv
);
2371 /* No selection has been chosen yet. */
2372 menu_item_selection
= 0;
2374 /* Display the menu. */
2375 lw_pop_up_all_widgets (dialog_id
);
2376 popup_activated_flag
= 1;
2378 /* Process events that apply to the menu. */
2379 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), dialog_id
);
2381 lw_destroy_all_widgets (dialog_id
);
2383 /* Find the selected item, and its pane, to return
2384 the proper value. */
2385 if (menu_item_selection
!= 0)
2391 while (i
< menu_items_used
)
2395 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2398 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2399 i
+= MENU_ITEMS_PANE_LENGTH
;
2404 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2405 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2409 entry
= Fcons (entry
, Qnil
);
2411 entry
= Fcons (prefix
, entry
);
2415 i
+= MENU_ITEMS_ITEM_LENGTH
;
2422 #else /* not USE_X_TOOLKIT */
2425 xmenu_show (f
, x
, y
, for_click
, keymaps
, title
, error
)
2435 int pane
, selidx
, lpane
, status
;
2436 Lisp_Object entry
, pane_prefix
;
2438 int ulx
, uly
, width
, height
;
2439 int dispwidth
, dispheight
;
2443 unsigned int dummy_uint
;
2446 if (menu_items_n_panes
== 0)
2449 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2451 *error
= "Empty menu";
2455 /* Figure out which root window F is on. */
2456 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2457 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2458 &dummy_uint
, &dummy_uint
);
2460 /* Make the menu on that window. */
2461 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2464 *error
= "Can't create menu";
2468 #ifdef HAVE_X_WINDOWS
2469 /* Adjust coordinates to relative to the outer (window manager) window. */
2472 int win_x
= 0, win_y
= 0;
2474 /* Find the position of the outside upper-left corner of
2475 the inner window, with respect to the outer window. */
2476 if (f
->output_data
.x
->parent_desc
!= FRAME_X_DISPLAY_INFO (f
)->root_window
)
2479 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
2481 /* From-window, to-window. */
2482 f
->output_data
.x
->window_desc
,
2483 f
->output_data
.x
->parent_desc
,
2485 /* From-position, to-position. */
2486 0, 0, &win_x
, &win_y
,
2488 /* Child of window. */
2495 #endif /* HAVE_X_WINDOWS */
2497 /* Adjust coordinates to be root-window-relative. */
2498 x
+= f
->output_data
.x
->left_pos
;
2499 y
+= f
->output_data
.x
->top_pos
;
2501 /* Create all the necessary panes and their items. */
2503 while (i
< menu_items_used
)
2505 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2507 /* Create a new pane. */
2508 Lisp_Object pane_name
, prefix
;
2511 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2512 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2513 pane_string
= (NILP (pane_name
)
2514 ? "" : (char *) XSTRING (pane_name
)->data
);
2515 if (keymaps
&& !NILP (prefix
))
2518 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
2519 if (lpane
== XM_FAILURE
)
2521 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2522 *error
= "Can't create pane";
2525 i
+= MENU_ITEMS_PANE_LENGTH
;
2527 /* Find the width of the widest item in this pane. */
2530 while (j
< menu_items_used
)
2533 item
= XVECTOR (menu_items
)->contents
[j
];
2541 width
= STRING_BYTES (XSTRING (item
));
2542 if (width
> maxwidth
)
2545 j
+= MENU_ITEMS_ITEM_LENGTH
;
2548 /* Ignore a nil in the item list.
2549 It's meaningful only for dialog boxes. */
2550 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2554 /* Create a new item within current pane. */
2555 Lisp_Object item_name
, enable
, descrip
;
2556 unsigned char *item_data
;
2558 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2559 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2561 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2562 if (!NILP (descrip
))
2564 int gap
= maxwidth
- STRING_BYTES (XSTRING (item_name
));
2567 spacer
= Fmake_string (make_number (gap
), make_number (' '));
2568 item_name
= concat2 (item_name
, spacer
);
2569 item_name
= concat2 (item_name
, descrip
);
2570 item_data
= XSTRING (item_name
)->data
;
2572 /* if alloca is fast, use that to make the space,
2573 to reduce gc needs. */
2575 = (unsigned char *) alloca (maxwidth
2576 + STRING_BYTES (XSTRING (descrip
)) + 1);
2577 bcopy (XSTRING (item_name
)->data
, item_data
,
2578 STRING_BYTES (XSTRING (item_name
)));
2579 for (j
= XSTRING (item_name
)->size
; j
< maxwidth
; j
++)
2581 bcopy (XSTRING (descrip
)->data
, item_data
+ j
,
2582 STRING_BYTES (XSTRING (descrip
)));
2583 item_data
[j
+ STRING_BYTES (XSTRING (descrip
))] = 0;
2587 item_data
= XSTRING (item_name
)->data
;
2589 if (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2590 menu
, lpane
, 0, item_data
,
2594 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2595 *error
= "Can't add selection to menu";
2598 i
+= MENU_ITEMS_ITEM_LENGTH
;
2602 /* All set and ready to fly. */
2603 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2604 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
),
2605 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2606 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
),
2607 XScreenNumberOfScreen (FRAME_X_SCREEN (f
)));
2608 x
= min (x
, dispwidth
);
2609 y
= min (y
, dispheight
);
2612 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2613 &ulx
, &uly
, &width
, &height
);
2614 if (ulx
+width
> dispwidth
)
2616 x
-= (ulx
+ width
) - dispwidth
;
2617 ulx
= dispwidth
- width
;
2619 if (uly
+height
> dispheight
)
2621 y
-= (uly
+ height
) - dispheight
;
2622 uly
= dispheight
- height
;
2624 if (ulx
< 0) x
-= ulx
;
2625 if (uly
< 0) y
-= uly
;
2627 XMenuSetAEQ (menu
, TRUE
);
2628 XMenuSetFreeze (menu
, TRUE
);
2631 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2632 x
, y
, ButtonReleaseMask
, &datap
);
2635 #ifdef HAVE_X_WINDOWS
2636 /* Assume the mouse has moved out of the X window.
2637 If it has actually moved in, we will get an EnterNotify. */
2638 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
2645 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2648 /* Find the item number SELIDX in pane number PANE. */
2650 while (i
< menu_items_used
)
2652 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2656 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2658 i
+= MENU_ITEMS_PANE_LENGTH
;
2667 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2670 entry
= Fcons (entry
, Qnil
);
2671 if (!NILP (pane_prefix
))
2672 entry
= Fcons (pane_prefix
, entry
);
2678 i
+= MENU_ITEMS_ITEM_LENGTH
;
2684 *error
= "Can't activate menu";
2690 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2692 #ifdef HAVE_X_WINDOWS
2693 /* State that no mouse buttons are now held.
2694 (The oldXMenu code doesn't track this info for us.)
2695 That is not necessarily true, but the fiction leads to reasonable
2696 results, and it is a pain to ask which are actually held now. */
2697 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2703 #endif /* not USE_X_TOOLKIT */
2705 #endif /* HAVE_MENUS */
2710 staticpro (&menu_items
);
2713 Qdebug_on_next_call
= intern ("debug-on-next-call");
2714 staticpro (&Qdebug_on_next_call
);
2716 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame
,
2717 "Frame for which we are updating a menu.\n\
2718 The enable predicate for a menu command should check this variable.");
2719 Vmenu_updating_frame
= Qnil
;
2721 #ifdef USE_X_TOOLKIT
2722 widget_id_tick
= (1<<16);
2723 next_menubar_widget_id
= 1;
2726 defsubr (&Sx_popup_menu
);
2728 defsubr (&Sx_popup_dialog
);