1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2012
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* X pop-up deck-of-cards menu facility for GNU Emacs.
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. */
35 #if 0 /* Why was this included? And without syssignal.h? */
36 /* On 4.3 this loses if it comes after xterm.h. */
47 #include "termhooks.h"
49 #include "blockinput.h"
53 #include "sysselect.h"
60 /* This may include sys/types.h, and that somehow loses
61 if this is not done before the other system files. */
65 /* Load sys/types.h if not already loaded.
66 In some systems loading it twice is suicidal. */
68 #include <sys/types.h>
71 #include "dispextern.h"
74 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
75 code accepts the Emacs internal encoding. */
76 #undef HAVE_MULTILINGUAL_MENU
80 #include <X11/IntrinsicP.h>
81 #include <X11/CoreP.h>
82 #include <X11/StringDefs.h>
83 #include <X11/Shell.h>
85 #include "xsettings.h"
86 #include "../lwlib/xlwmenu.h"
88 #include <X11/Xaw3d/Paned.h>
89 #else /* !HAVE_XAW3D */
90 #include <X11/Xaw/Paned.h>
91 #endif /* HAVE_XAW3D */
92 #endif /* USE_LUCID */
94 #include "../lwlib/lwlib.h"
96 #else /* not USE_X_TOOLKIT */
98 #include "../oldXMenu/XMenu.h"
100 #endif /* not USE_X_TOOLKIT */
101 #endif /* HAVE_X_WINDOWS */
106 #include "xgselect.h"
116 static Lisp_Object Qdebug_on_next_call
;
118 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
119 static Lisp_Object
xdialog_show (FRAME_PTR
, int, Lisp_Object
, Lisp_Object
,
123 /* Flag which when set indicates a dialog or menu has been posted by
124 Xt on behalf of one of the widget sets. */
125 static int popup_activated_flag
;
130 static int next_menubar_widget_id
;
132 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
134 static struct frame
*
135 menubar_id_to_frame (LWLIB_ID id
)
137 Lisp_Object tail
, frame
;
140 for (tail
= Vframe_list
; CONSP (tail
); tail
= XCDR (tail
))
146 if (!FRAME_WINDOW_P (f
))
148 if (f
->output_data
.x
->id
== id
)
156 #ifdef HAVE_X_WINDOWS
157 /* Return the mouse position in *X and *Y. The coordinates are window
158 relative for the edit window in frame F.
159 This is for Fx_popup_menu. The mouse_position_hook can not
160 be used for X, as it returns window relative coordinates
161 for the window where the mouse is in. This could be the menu bar,
162 the scroll bar or the edit window. Fx_popup_menu needs to be
163 sure it is the edit window. */
165 mouse_position_for_popup (FRAME_PTR f
, int *x
, int *y
)
167 Window root
, dummy_window
;
175 XQueryPointer (FRAME_X_DISPLAY (f
),
176 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
178 /* The root window which contains the pointer. */
181 /* Window pointer is on, not used */
184 /* The position on that root window. */
187 /* x/y in dummy_window coordinates, not used. */
190 /* Modifier keys and pointer buttons, about which
192 (unsigned int *) &dummy
);
196 /* xmenu_show expects window coordinates, not root window
197 coordinates. Translate. */
198 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
199 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
202 #endif /* HAVE_X_WINDOWS */
206 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 3, 0,
207 doc
: /* Pop up a dialog box and return user's selection.
208 POSITION specifies which frame to use.
209 This is normally a mouse button event or a window or frame.
210 If POSITION is t, it means to use the frame the mouse is on.
211 The dialog box appears in the middle of the specified frame.
213 CONTENTS specifies the alternatives to display in the dialog box.
214 It is a list of the form (DIALOG ITEM1 ITEM2...).
215 Each ITEM is a cons cell (STRING . VALUE).
216 The return value is VALUE from the chosen item.
218 An ITEM may also be just a string--that makes a nonselectable item.
219 An ITEM may also be nil--that means to put all preceding items
220 on the left of the dialog box and all following items on the right.
221 \(By default, approximately half appear on each side.)
223 If HEADER is non-nil, the frame title for the box is "Information",
224 otherwise it is "Question".
226 If the user gets rid of the dialog box without making a valid choice,
227 for instance using the window manager, then this produces a quit and
228 `x-popup-dialog' does not return. */)
229 (Lisp_Object position
, Lisp_Object contents
, Lisp_Object header
)
236 /* Decode the first argument: find the window or frame to use. */
237 if (EQ (position
, Qt
)
238 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
239 || EQ (XCAR (position
), Qtool_bar
))))
241 #if 0 /* Using the frame the mouse is on may not be right. */
242 /* Use the mouse's current position. */
243 FRAME_PTR new_f
= SELECTED_FRAME ();
244 Lisp_Object bar_window
;
245 enum scroll_bar_part part
;
249 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
252 XSETFRAME (window
, new_f
);
254 window
= selected_window
;
256 window
= selected_window
;
258 else if (CONSP (position
))
261 tem
= Fcar (position
);
263 window
= Fcar (Fcdr (position
));
266 tem
= Fcar (Fcdr (position
)); /* EVENT_START (position) */
267 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
270 else if (WINDOWP (position
) || FRAMEP (position
))
275 /* Decode where to put the menu. */
279 else if (WINDOWP (window
))
281 CHECK_LIVE_WINDOW (window
);
282 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
285 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
286 but I don't want to make one now. */
287 CHECK_WINDOW (window
);
289 if (! FRAME_X_P (f
) && ! FRAME_MSDOS_P (f
))
290 error ("Can not put X dialog on this terminal");
292 /* Force a redisplay before showing the dialog. If a frame is created
293 just before showing the dialog, its contents may not have been fully
294 drawn, as this depends on timing of events from the X server. Redisplay
295 is not done when a dialog is shown. If redisplay could be done in the
296 X event loop (i.e. the X event loop does not run in a signal handler)
297 this would not be needed.
299 Do this before creating the widget value that points to Lisp
300 string contents, because Fredisplay may GC and relocate them. */
303 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
304 /* Display a menu with these alternatives
305 in the middle of frame F. */
307 Lisp_Object x
, y
, frame
, newpos
;
308 XSETFRAME (frame
, f
);
309 XSETINT (x
, x_pixel_width (f
) / 2);
310 XSETINT (y
, x_pixel_height (f
) / 2);
311 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
313 return Fx_popup_menu (newpos
,
314 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
319 const char *error_name
;
320 Lisp_Object selection
;
321 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
323 /* Decode the dialog items from what was specified. */
324 title
= Fcar (contents
);
325 CHECK_STRING (title
);
326 record_unwind_protect (unuse_menu_items
, Qnil
);
328 if (NILP (Fcar (Fcdr (contents
))))
329 /* No buttons specified, add an "Ok" button so users can pop down
330 the dialog. Also, the lesstif/motif version crashes if there are
332 contents
= Fcons (title
, Fcons (Fcons (build_string ("Ok"), Qt
), Qnil
));
334 list_of_panes (Fcons (contents
, Qnil
));
336 /* Display them in a dialog box. */
338 selection
= xdialog_show (f
, 0, title
, header
, &error_name
);
341 unbind_to (specpdl_count
, Qnil
);
342 discard_menu_items ();
344 if (error_name
) error ("%s", error_name
);
353 #if defined USE_GTK || defined USE_MOTIF
355 /* Set menu_items_inuse so no other popup menu or dialog is created. */
358 x_menu_set_in_use (int in_use
)
360 menu_items_inuse
= in_use
? Qt
: Qnil
;
361 popup_activated_flag
= in_use
;
363 if (popup_activated_flag
)
364 x_activate_timeout_atimer ();
370 /* Wait for an X event to arrive or for a timer to expire. */
376 x_menu_wait_for_event (void *data
)
378 /* Another way to do this is to register a timer callback, that can be
379 done in GTK and Xt. But we have to do it like this when using only X
380 anyway, and with callbacks we would have three variants for timer handling
381 instead of the small ifdefs below. */
385 ! XtAppPending (Xt_app_con
)
386 #elif defined USE_GTK
387 ! gtk_events_pending ()
389 ! XPending ((Display
*) data
)
393 EMACS_TIME next_time
= timer_check (), *ntp
;
394 long secs
= EMACS_SECS (next_time
);
395 long usecs
= EMACS_USECS (next_time
);
396 SELECT_TYPE read_fds
;
397 struct x_display_info
*dpyinfo
;
401 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
403 int fd
= ConnectionNumber (dpyinfo
->display
);
404 FD_SET (fd
, &read_fds
);
406 XFlush (dpyinfo
->display
);
409 if (secs
< 0 && usecs
< 0)
415 /* Gtk3 have arrows on menus when they don't fit. When the pointer is
416 over an arrow, a timeout scrolls it a bit. Use xg_select so that
417 timeout gets triggered. */
419 xg_select (n
+ 1, &read_fds
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0, ntp
);
421 select (n
+ 1, &read_fds
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0, ntp
);
428 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
432 /* Loop in Xt until the menu pulldown or dialog popup has been
433 popped down (deactivated). This is used for x-popup-menu
434 and x-popup-dialog; it is not used for the menu bar.
436 NOTE: All calls to popup_get_selection should be protected
437 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
440 popup_get_selection (XEvent
*initial_event
, struct x_display_info
*dpyinfo
, LWLIB_ID id
, int do_timers
)
444 while (popup_activated_flag
)
448 event
= *initial_event
;
453 if (do_timers
) x_menu_wait_for_event (0);
454 XtAppNextEvent (Xt_app_con
, &event
);
457 /* Make sure we don't consider buttons grabbed after menu goes.
458 And make sure to deactivate for any ButtonRelease,
459 even if XtDispatchEvent doesn't do that. */
460 if (event
.type
== ButtonRelease
461 && dpyinfo
->display
== event
.xbutton
.display
)
463 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
464 #ifdef USE_MOTIF /* Pretending that the event came from a
465 Btn1Down seems the only way to convince Motif to
466 activate its callbacks; setting the XmNmenuPost
467 isn't working. --marcus@sysc.pdx.edu. */
468 event
.xbutton
.button
= 1;
469 /* Motif only pops down menus when no Ctrl, Alt or Mod
470 key is pressed and the button is released. So reset key state
471 so Motif thinks this is the case. */
472 event
.xbutton
.state
= 0;
475 /* Pop down on C-g and Escape. */
476 else if (event
.type
== KeyPress
477 && dpyinfo
->display
== event
.xbutton
.display
)
479 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
481 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
482 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
483 popup_activated_flag
= 0;
486 x_dispatch_event (&event
, event
.xany
.display
);
490 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal
, Sx_menu_bar_open_internal
, 0, 1, "i",
491 doc
: /* Start key navigation of the menu bar in FRAME.
492 This initially opens the first menu bar item and you can then navigate with the
493 arrow keys, select a menu entry with the return key or cancel with the
494 escape key. If FRAME has no menu bar this function does nothing.
496 If FRAME is nil or not given, use the selected frame. */)
500 FRAME_PTR f
= check_x_frame (frame
);
504 if (FRAME_EXTERNAL_MENU_BAR (f
))
505 set_frame_menubar (f
, 0, 1);
507 menubar
= FRAME_X_OUTPUT (f
)->menubar_widget
;
513 x_catch_errors (FRAME_X_DISPLAY (f
));
514 memset (&ev
, 0, sizeof ev
);
515 ev
.xbutton
.display
= FRAME_X_DISPLAY (f
);
516 ev
.xbutton
.window
= XtWindow (menubar
);
517 ev
.xbutton
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
518 ev
.xbutton
.time
= XtLastTimestampProcessed (FRAME_X_DISPLAY (f
));
519 ev
.xbutton
.button
= Button1
;
520 ev
.xbutton
.x
= ev
.xbutton
.y
= FRAME_MENUBAR_HEIGHT (f
) / 2;
521 ev
.xbutton
.same_screen
= True
;
528 XtSetArg (al
[0], XtNchildren
, &list
);
529 XtSetArg (al
[1], XtNnumChildren
, &nr
);
530 XtGetValues (menubar
, al
, 2);
531 ev
.xbutton
.window
= XtWindow (list
[0]);
535 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
536 /* From-window, to-window. */
537 ev
.xbutton
.window
, ev
.xbutton
.root
,
539 /* From-position, to-position. */
540 ev
.xbutton
.x
, ev
.xbutton
.y
,
541 &ev
.xbutton
.x_root
, &ev
.xbutton
.y_root
,
545 error_p
= x_had_errors_p (FRAME_X_DISPLAY (f
));
550 ev
.type
= ButtonPress
;
551 ev
.xbutton
.state
= 0;
553 XtDispatchEvent (&ev
);
554 ev
.xbutton
.type
= ButtonRelease
;
555 ev
.xbutton
.state
= Button1Mask
;
556 XtDispatchEvent (&ev
);
564 #endif /* USE_X_TOOLKIT */
568 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal
, Sx_menu_bar_open_internal
, 0, 1, "i",
569 doc
: /* Start key navigation of the menu bar in FRAME.
570 This initially opens the first menu bar item and you can then navigate with the
571 arrow keys, select a menu entry with the return key or cancel with the
572 escape key. If FRAME has no menu bar this function does nothing.
574 If FRAME is nil or not given, use the selected frame. */)
580 /* gcc 2.95 doesn't accept the FRAME_PTR declaration after
584 f
= check_x_frame (frame
);
586 if (FRAME_EXTERNAL_MENU_BAR (f
))
587 set_frame_menubar (f
, 0, 1);
589 menubar
= FRAME_X_OUTPUT (f
)->menubar_widget
;
592 /* Activate the first menu. */
593 GList
*children
= gtk_container_get_children (GTK_CONTAINER (menubar
));
597 g_signal_emit_by_name (children
->data
, "activate_item");
598 popup_activated_flag
= 1;
599 g_list_free (children
);
607 /* Loop util popup_activated_flag is set to zero in a callback.
608 Used for popup menus and dialogs. */
611 popup_widget_loop (int do_timers
, GtkWidget
*widget
)
613 ++popup_activated_flag
;
615 /* Process events in the Gtk event loop until done. */
616 while (popup_activated_flag
)
618 if (do_timers
) x_menu_wait_for_event (0);
619 gtk_main_iteration ();
624 /* Activate the menu bar of frame F.
625 This is called from keyboard.c when it gets the
626 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
628 To activate the menu bar, we use the X button-press event
629 that was saved in saved_menu_event.
630 That makes the toolkit do its thing.
632 But first we recompute the menu bar contents (the whole tree).
634 The reason for saving the button event until here, instead of
635 passing it to the toolkit right away, is that we can safely
636 execute Lisp code. */
639 x_activate_menubar (FRAME_PTR f
)
644 if (!f
->output_data
.x
->saved_menu_event
->type
)
648 if (! xg_win_to_widget (FRAME_X_DISPLAY (f
),
649 f
->output_data
.x
->saved_menu_event
->xany
.window
))
653 set_frame_menubar (f
, 0, 1);
655 popup_activated_flag
= 1;
657 XPutBackEvent (f
->output_data
.x
->display_info
->display
,
658 f
->output_data
.x
->saved_menu_event
);
660 XtDispatchEvent (f
->output_data
.x
->saved_menu_event
);
664 /* Ignore this if we get it a second time. */
665 f
->output_data
.x
->saved_menu_event
->type
= 0;
668 /* This callback is invoked when the user selects a menubar cascade
669 pushbutton, but before the pulldown menu is posted. */
673 popup_activate_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
675 popup_activated_flag
= 1;
677 x_activate_timeout_atimer ();
682 /* This callback is invoked when a dialog or menu is finished being
683 used and has been unposted. */
687 popup_deactivate_callback (GtkWidget
*widget
, gpointer client_data
)
689 popup_activated_flag
= 0;
693 popup_deactivate_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
695 popup_activated_flag
= 0;
700 /* Function that finds the frame for WIDGET and shows the HELP text
702 F is the frame if known, or NULL if not known. */
704 show_help_event (FRAME_PTR f
, xt_or_gtk_widget widget
, Lisp_Object help
)
710 XSETFRAME (frame
, f
);
711 kbd_buffer_store_help_event (frame
, help
);
715 #if 0 /* This code doesn't do anything useful. ++kfs */
716 /* WIDGET is the popup menu. It's parent is the frame's
717 widget. See which frame that is. */
718 xt_or_gtk_widget frame_widget
= XtParent (widget
);
721 for (tail
= Vframe_list
; CONSP (tail
); tail
= XCDR (tail
))
725 && (f
= XFRAME (frame
),
726 FRAME_X_P (f
) && f
->output_data
.x
->widget
== frame_widget
))
730 show_help_echo (help
, Qnil
, Qnil
, Qnil
);
734 /* Callback called when menu items are highlighted/unhighlighted
735 while moving the mouse over them. WIDGET is the menu bar or menu
736 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
737 the data structure for the menu item, or null in case of
742 menu_highlight_callback (GtkWidget
*widget
, gpointer call_data
)
744 xg_menu_item_cb_data
*cb_data
;
747 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (widget
),
749 if (! cb_data
) return;
751 help
= call_data
? cb_data
->help
: Qnil
;
753 /* If popup_activated_flag is greater than 1 we are in a popup menu.
754 Don't pass the frame to show_help_event for those.
755 Passing frame creates an Emacs event. As we are looping in
756 popup_widget_loop, it won't be handled. Passing NULL shows the tip
757 directly without using an Emacs event. This is what the Lucid code
759 show_help_event (popup_activated_flag
<= 1 ? cb_data
->cl_data
->f
: NULL
,
764 menu_highlight_callback (Widget widget
, LWLIB_ID id
, void *call_data
)
769 widget_value
*wv
= (widget_value
*) call_data
;
771 help
= wv
? wv
->help
: Qnil
;
773 /* Determine the frame for the help event. */
774 f
= menubar_id_to_frame (id
);
776 show_help_event (f
, widget
, help
);
781 /* Gtk calls callbacks just because we tell it what item should be
782 selected in a radio group. If this variable is set to a non-zero
783 value, we are creating menus and don't want callbacks right now.
785 static int xg_crazy_callback_abort
;
787 /* This callback is called from the menu bar pulldown menu
788 when the user makes a selection.
789 Figure out what the user chose
790 and put the appropriate events into the keyboard buffer. */
792 menubar_selection_callback (GtkWidget
*widget
, gpointer client_data
)
794 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
796 if (xg_crazy_callback_abort
)
799 if (! cb_data
|| ! cb_data
->cl_data
|| ! cb_data
->cl_data
->f
)
802 /* For a group of radio buttons, GTK calls the selection callback first
803 for the item that was active before the selection and then for the one that
804 is active after the selection. For C-h k this means we get the help on
805 the deselected item and then the selected item is executed. Prevent that
806 by ignoring the non-active item. */
807 if (GTK_IS_RADIO_MENU_ITEM (widget
)
808 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget
)))
811 /* When a menu is popped down, X generates a focus event (i.e. focus
812 goes back to the frame below the menu). Since GTK buffers events,
813 we force it out here before the menu selection event. Otherwise
814 sit-for will exit at once if the focus event follows the menu selection
818 while (gtk_events_pending ())
819 gtk_main_iteration ();
822 find_and_call_menu_selection (cb_data
->cl_data
->f
,
823 cb_data
->cl_data
->menu_bar_items_used
,
824 cb_data
->cl_data
->menu_bar_vector
,
828 #else /* not USE_GTK */
830 /* This callback is called from the menu bar pulldown menu
831 when the user makes a selection.
832 Figure out what the user chose
833 and put the appropriate events into the keyboard buffer. */
835 menubar_selection_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
839 f
= menubar_id_to_frame (id
);
842 find_and_call_menu_selection (f
, f
->menu_bar_items_used
,
843 f
->menu_bar_vector
, client_data
);
845 #endif /* not USE_GTK */
847 /* Recompute all the widgets of frame F, when the menu bar has been
848 changed. Value is non-zero if widgets were updated. */
851 update_frame_menubar (FRAME_PTR f
)
854 return xg_update_frame_menubar (f
);
862 x
= f
->output_data
.x
;
864 if (!x
->menubar_widget
|| XtIsManaged (x
->menubar_widget
))
868 /* Save the size of the frame because the pane widget doesn't accept
869 to resize itself. So force it. */
870 columns
= FRAME_COLS (f
);
871 rows
= FRAME_LINES (f
);
873 /* Do the voodoo which means "I'm changing lots of things, don't try
874 to refigure sizes until I'm done." */
875 lw_refigure_widget (x
->column_widget
, False
);
877 /* The order in which children are managed is the top to bottom
878 order in which they are displayed in the paned window. First,
879 remove the text-area widget. */
880 XtUnmanageChild (x
->edit_widget
);
882 /* Remove the menubar that is there now, and put up the menubar that
884 XtManageChild (x
->menubar_widget
);
885 XtMapWidget (x
->menubar_widget
);
886 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, NULL
);
888 /* Re-manage the text-area widget, and then thrash the sizes. */
889 XtManageChild (x
->edit_widget
);
890 lw_refigure_widget (x
->column_widget
, True
);
892 /* Force the pane widget to resize itself with the right values. */
893 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
901 apply_systemfont_to_dialog (Widget w
)
903 const char *fn
= xsettings_get_system_normal_font ();
906 XrmDatabase db
= XtDatabase (XtDisplay (w
));
908 XrmPutStringResource (&db
, "*dialog.font", fn
);
913 apply_systemfont_to_menu (struct frame
*f
, Widget w
)
915 const char *fn
= xsettings_get_system_normal_font ();
919 XrmDatabase db
= XtDatabase (XtDisplay (w
));
922 XrmPutStringResource (&db
, "*menubar*font", fn
);
923 XrmPutStringResource (&db
, "*popup*font", fn
);
930 /* Set the contents of the menubar widgets of frame F.
931 The argument FIRST_TIME is currently ignored;
932 it is set the first time this is called, from initialize_frame_menubar. */
935 set_frame_menubar (FRAME_PTR f
, int first_time
, int deep_p
)
937 xt_or_gtk_widget menubar_widget
;
942 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
944 int *submenu_start
, *submenu_end
;
945 int *submenu_top_level_items
, *submenu_n_panes
;
950 menubar_widget
= f
->output_data
.x
->menubar_widget
;
952 XSETFRAME (Vmenu_updating_frame
, f
);
955 if (f
->output_data
.x
->id
== 0)
956 f
->output_data
.x
->id
= next_menubar_widget_id
++;
957 id
= f
->output_data
.x
->id
;
960 if (! menubar_widget
)
962 /* Make the first call for any given frame always go deep. */
963 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
966 f
->output_data
.x
->saved_menu_event
= (XEvent
*)xmalloc (sizeof (XEvent
));
967 f
->output_data
.x
->saved_menu_event
->type
= 0;
971 /* If we have detached menus, we must update deep so detached menus
972 also gets updated. */
973 deep_p
= deep_p
|| xg_have_tear_offs ();
978 /* Make a widget-value tree representing the entire menu trees. */
980 struct buffer
*prev
= current_buffer
;
982 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
983 int previous_menu_items_used
= f
->menu_bar_items_used
;
984 Lisp_Object
*previous_items
985 = (Lisp_Object
*) alloca (previous_menu_items_used
986 * sizeof (Lisp_Object
));
989 /* If we are making a new widget, its contents are empty,
990 do always reinitialize them. */
991 if (! menubar_widget
)
992 previous_menu_items_used
= 0;
994 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
995 specbind (Qinhibit_quit
, Qt
);
996 /* Don't let the debugger step into this code
997 because it is not reentrant. */
998 specbind (Qdebug_on_next_call
, Qnil
);
1000 record_unwind_save_match_data ();
1001 if (NILP (Voverriding_local_map_menu_flag
))
1003 specbind (Qoverriding_terminal_local_map
, Qnil
);
1004 specbind (Qoverriding_local_map
, Qnil
);
1007 set_buffer_internal_1 (XBUFFER (buffer
));
1009 /* Run the Lucid hook. */
1010 safe_run_hooks (Qactivate_menubar_hook
);
1012 /* If it has changed current-menubar from previous value,
1013 really recompute the menubar from the value. */
1014 if (! NILP (Vlucid_menu_bar_dirty_flag
))
1015 call0 (Qrecompute_lucid_menubar
);
1016 safe_run_hooks (Qmenu_bar_update_hook
);
1017 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1019 items
= FRAME_MENU_BAR_ITEMS (f
);
1021 /* Save the frame's previous menu bar contents data. */
1022 if (previous_menu_items_used
)
1023 memcpy (previous_items
, XVECTOR (f
->menu_bar_vector
)->contents
,
1024 previous_menu_items_used
* sizeof (Lisp_Object
));
1026 /* Fill in menu_items with the current menu bar contents.
1027 This can evaluate Lisp code. */
1030 menu_items
= f
->menu_bar_vector
;
1031 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
1032 subitems
= ASIZE (items
) / 4;
1033 submenu_start
= (int *) alloca ((subitems
+ 1) * sizeof (int));
1034 submenu_end
= (int *) alloca (subitems
* sizeof (int));
1035 submenu_n_panes
= (int *) alloca (subitems
* sizeof (int));
1036 submenu_top_level_items
= (int *) alloca (subitems
* sizeof (int));
1038 for (i
= 0; i
< subitems
; i
++)
1040 Lisp_Object key
, string
, maps
;
1042 key
= XVECTOR (items
)->contents
[4 * i
];
1043 string
= XVECTOR (items
)->contents
[4 * i
+ 1];
1044 maps
= XVECTOR (items
)->contents
[4 * i
+ 2];
1048 submenu_start
[i
] = menu_items_used
;
1050 menu_items_n_panes
= 0;
1051 submenu_top_level_items
[i
]
1052 = parse_single_submenu (key
, string
, maps
);
1053 submenu_n_panes
[i
] = menu_items_n_panes
;
1055 submenu_end
[i
] = menu_items_used
;
1058 submenu_start
[i
] = -1;
1059 finish_menu_items ();
1061 /* Convert menu_items into widget_value trees
1062 to display the menu. This cannot evaluate Lisp code. */
1064 wv
= xmalloc_widget_value ();
1065 wv
->name
= "menubar";
1068 wv
->button_type
= BUTTON_TYPE_NONE
;
1072 for (i
= 0; 0 <= submenu_start
[i
]; i
++)
1074 menu_items_n_panes
= submenu_n_panes
[i
];
1075 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
1076 submenu_top_level_items
[i
]);
1080 first_wv
->contents
= wv
;
1081 /* Don't set wv->name here; GC during the loop might relocate it. */
1083 wv
->button_type
= BUTTON_TYPE_NONE
;
1087 set_buffer_internal_1 (prev
);
1089 /* If there has been no change in the Lisp-level contents
1090 of the menu bar, skip redisplaying it. Just exit. */
1092 /* Compare the new menu items with the ones computed last time. */
1093 for (i
= 0; i
< previous_menu_items_used
; i
++)
1094 if (menu_items_used
== i
1095 || (!EQ (previous_items
[i
], XVECTOR (menu_items
)->contents
[i
])))
1097 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
1099 /* The menu items have not changed. Don't bother updating
1100 the menus in any form, since it would be a no-op. */
1101 free_menubar_widget_value_tree (first_wv
);
1102 discard_menu_items ();
1103 unbind_to (specpdl_count
, Qnil
);
1107 /* The menu items are different, so store them in the frame. */
1108 f
->menu_bar_vector
= menu_items
;
1109 f
->menu_bar_items_used
= menu_items_used
;
1111 /* This undoes save_menu_items. */
1112 unbind_to (specpdl_count
, Qnil
);
1114 /* Now GC cannot happen during the lifetime of the widget_value,
1115 so it's safe to store data from a Lisp_String. */
1116 wv
= first_wv
->contents
;
1117 for (i
= 0; i
< ASIZE (items
); i
+= 4)
1120 string
= XVECTOR (items
)->contents
[i
+ 1];
1123 wv
->name
= SSDATA (string
);
1124 update_submenu_strings (wv
->contents
);
1131 /* Make a widget-value tree containing
1132 just the top level menu bar strings. */
1134 wv
= xmalloc_widget_value ();
1135 wv
->name
= "menubar";
1138 wv
->button_type
= BUTTON_TYPE_NONE
;
1142 items
= FRAME_MENU_BAR_ITEMS (f
);
1143 for (i
= 0; i
< ASIZE (items
); i
+= 4)
1147 string
= XVECTOR (items
)->contents
[i
+ 1];
1151 wv
= xmalloc_widget_value ();
1152 wv
->name
= SSDATA (string
);
1155 wv
->button_type
= BUTTON_TYPE_NONE
;
1157 /* This prevents lwlib from assuming this
1158 menu item is really supposed to be empty. */
1159 /* The intptr_t cast avoids a warning.
1160 This value just has to be different from small integers. */
1161 wv
->call_data
= (void *) (intptr_t) (-1);
1166 first_wv
->contents
= wv
;
1170 /* Forget what we thought we knew about what is in the
1171 detailed contents of the menu bar menus.
1172 Changing the top level always destroys the contents. */
1173 f
->menu_bar_items_used
= 0;
1176 /* Create or update the menu bar widget. */
1181 xg_crazy_callback_abort
= 1;
1184 /* The fourth arg is DEEP_P, which says to consider the entire
1185 menu trees we supply, rather than just the menu bar item names. */
1186 xg_modify_menubar_widgets (menubar_widget
,
1190 G_CALLBACK (menubar_selection_callback
),
1191 G_CALLBACK (popup_deactivate_callback
),
1192 G_CALLBACK (menu_highlight_callback
));
1197 = xg_create_widget ("menubar", "menubar", f
, first_wv
,
1198 G_CALLBACK (menubar_selection_callback
),
1199 G_CALLBACK (popup_deactivate_callback
),
1200 G_CALLBACK (menu_highlight_callback
));
1202 f
->output_data
.x
->menubar_widget
= menubar_widget
;
1206 #else /* not USE_GTK */
1209 /* Disable resizing (done for Motif!) */
1210 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
1212 /* The third arg is DEEP_P, which says to consider the entire
1213 menu trees we supply, rather than just the menu bar item names. */
1214 lw_modify_all_widgets (id
, first_wv
, deep_p
);
1216 /* Re-enable the edit widget to resize. */
1217 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
1221 char menuOverride
[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
1222 XtTranslations override
= XtParseTranslationTable (menuOverride
);
1225 apply_systemfont_to_menu (f
, f
->output_data
.x
->column_widget
);
1227 menubar_widget
= lw_create_widget ("menubar", "menubar", id
,
1229 f
->output_data
.x
->column_widget
,
1231 popup_activate_callback
,
1232 menubar_selection_callback
,
1233 popup_deactivate_callback
,
1234 menu_highlight_callback
);
1235 f
->output_data
.x
->menubar_widget
= menubar_widget
;
1237 /* Make menu pop down on C-g. */
1238 XtOverrideTranslations (menubar_widget
, override
);
1243 if (f
->output_data
.x
->menubar_widget
)
1244 XtRealizeWidget (f
->output_data
.x
->menubar_widget
);
1247 = (f
->output_data
.x
->menubar_widget
1248 ? (f
->output_data
.x
->menubar_widget
->core
.height
1249 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
1252 #if 1 /* Experimentally, we now get the right results
1253 for -geometry -0-0 without this. 24 Aug 96, rms.
1254 Maybe so, but the menu bar size is missing the pixels so the
1255 WM size hints are off by these pixels. Jan D, oct 2009. */
1257 if (FRAME_EXTERNAL_MENU_BAR (f
))
1260 XtVaGetValues (f
->output_data
.x
->column_widget
,
1261 XtNinternalBorderWidth
, &ibw
, NULL
);
1262 menubar_size
+= ibw
;
1264 #endif /* USE_LUCID */
1267 f
->output_data
.x
->menubar_height
= menubar_size
;
1269 #endif /* not USE_GTK */
1271 free_menubar_widget_value_tree (first_wv
);
1272 update_frame_menubar (f
);
1275 xg_crazy_callback_abort
= 0;
1281 /* Called from Fx_create_frame to create the initial menubar of a frame
1282 before it is mapped, so that the window is mapped with the menubar already
1283 there instead of us tacking it on later and thrashing the window after it
1287 initialize_frame_menubar (FRAME_PTR f
)
1289 /* This function is called before the first chance to redisplay
1290 the frame. It has to be, so the frame will have the right size. */
1291 FRAME_MENU_BAR_ITEMS (f
) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f
));
1292 set_frame_menubar (f
, 1, 1);
1296 /* Get rid of the menu bar of frame F, and free its storage.
1297 This is used when deleting a frame, and when turning off the menu bar.
1298 For GTK this function is in gtkutil.c. */
1302 free_frame_menubar (FRAME_PTR f
)
1304 Widget menubar_widget
;
1306 if (! FRAME_X_P (f
))
1309 menubar_widget
= f
->output_data
.x
->menubar_widget
;
1311 f
->output_data
.x
->menubar_height
= 0;
1316 /* Removing the menu bar magically changes the shell widget's x
1317 and y position of (0, 0) which, when the menu bar is turned
1318 on again, leads to pull-down menus appearing in strange
1319 positions near the upper-left corner of the display. This
1320 happens only with some window managers like twm and ctwm,
1321 but not with other like Motif's mwm or kwm, because the
1322 latter generate ConfigureNotify events when the menu bar
1323 is switched off, which fixes the shell position. */
1324 Position x0
, y0
, x1
, y1
;
1330 if (f
->output_data
.x
->widget
)
1331 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x0
, XtNy
, &y0
, NULL
);
1334 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
1335 f
->output_data
.x
->menubar_widget
= NULL
;
1337 if (f
->output_data
.x
->widget
)
1340 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x1
, XtNy
, &y1
, NULL
);
1341 if (x1
== 0 && y1
== 0)
1342 XtVaSetValues (f
->output_data
.x
->widget
, XtNx
, x0
, XtNy
, y0
, NULL
);
1344 x_set_window_size (f
, 0, FRAME_COLS (f
), FRAME_LINES (f
));
1349 #endif /* not USE_GTK */
1351 #endif /* USE_X_TOOLKIT || USE_GTK */
1353 /* xmenu_show actually displays a menu using the panes and items in menu_items
1354 and returns the value selected from it.
1355 There are two versions of xmenu_show, one for Xt and one for Xlib.
1356 Both assume input is blocked by the caller. */
1358 /* F is the frame the menu is for.
1359 X and Y are the frame-relative specified position,
1360 relative to the inside upper left corner of the frame F.
1361 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1362 KEYMAPS is 1 if this menu was specified with keymaps;
1363 in that case, we return a list containing the chosen item's value
1364 and perhaps also the pane's prefix.
1365 TITLE is the specified menu title.
1366 ERROR is a place to store an error message string in case of failure.
1367 (We return nil on failure, but the value doesn't actually matter.) */
1369 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1371 /* The item selected in the popup menu. */
1372 static Lisp_Object
*volatile menu_item_selection
;
1376 /* Used when position a popup menu. See menu_position_func and
1377 create_and_show_popup_menu below. */
1378 struct next_popup_x_y
1385 /* The menu position function to use if we are not putting a popup
1386 menu where the pointer is.
1387 MENU is the menu to pop up.
1388 X and Y shall on exit contain x/y where the menu shall pop up.
1389 PUSH_IN is not documented in the GTK manual.
1390 USER_DATA is any data passed in when calling gtk_menu_popup.
1391 Here it points to a struct next_popup_x_y where the coordinates
1392 to store in *X and *Y are as well as the frame for the popup.
1394 Here only X and Y are used. */
1396 menu_position_func (GtkMenu
*menu
, gint
*x
, gint
*y
, gboolean
*push_in
, gpointer user_data
)
1398 struct next_popup_x_y
* data
= (struct next_popup_x_y
*)user_data
;
1400 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (data
->f
);
1401 int disp_width
= x_display_pixel_width (dpyinfo
);
1402 int disp_height
= x_display_pixel_height (dpyinfo
);
1407 /* Check if there is room for the menu. If not, adjust x/y so that
1408 the menu is fully visible. */
1409 gtk_widget_get_preferred_size (GTK_WIDGET (menu
), NULL
, &req
);
1410 if (data
->x
+ req
.width
> disp_width
)
1411 *x
-= data
->x
+ req
.width
- disp_width
;
1412 if (data
->y
+ req
.height
> disp_height
)
1413 *y
-= data
->y
+ req
.height
- disp_height
;
1417 popup_selection_callback (GtkWidget
*widget
, gpointer client_data
)
1419 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
1421 if (xg_crazy_callback_abort
) return;
1422 if (cb_data
) menu_item_selection
= (Lisp_Object
*) cb_data
->call_data
;
1426 pop_down_menu (Lisp_Object arg
)
1428 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
1430 popup_activated_flag
= 0;
1432 gtk_widget_destroy (GTK_WIDGET (p
->pointer
));
1437 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1439 menu_item_selection will be set to the selection. */
1441 create_and_show_popup_menu (FRAME_PTR f
, widget_value
*first_wv
, int x
, int y
,
1442 int for_click
, Time timestamp
)
1446 GtkMenuPositionFunc pos_func
= 0; /* Pop up at pointer. */
1447 struct next_popup_x_y popup_x_y
;
1448 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1449 int use_pos_func
= ! for_click
;
1452 /* Always use position function for Gtk3. Otherwise menus may become
1453 too small to show anything. */
1457 if (! FRAME_X_P (f
))
1460 xg_crazy_callback_abort
= 1;
1461 menu
= xg_create_widget ("popup", first_wv
->name
, f
, first_wv
,
1462 G_CALLBACK (popup_selection_callback
),
1463 G_CALLBACK (popup_deactivate_callback
),
1464 G_CALLBACK (menu_highlight_callback
));
1465 xg_crazy_callback_abort
= 0;
1469 /* Not invoked by a click. pop up at x/y. */
1470 pos_func
= menu_position_func
;
1472 /* Adjust coordinates to be root-window-relative. */
1473 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
1474 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
1480 i
= 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1485 for (i
= 0; i
< 5; i
++)
1486 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
1490 /* Display the menu. */
1491 gtk_widget_show_all (menu
);
1493 gtk_menu_popup (GTK_MENU (menu
), 0, 0, pos_func
, &popup_x_y
, i
,
1494 timestamp
? timestamp
: gtk_get_current_event_time ());
1496 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
1498 if (gtk_widget_get_mapped (menu
))
1500 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1501 two. show_help_echo uses this to detect popup menus. */
1502 popup_activated_flag
= 1;
1503 /* Process events that apply to the menu. */
1504 popup_widget_loop (1, menu
);
1507 unbind_to (specpdl_count
, Qnil
);
1509 /* Must reset this manually because the button release event is not passed
1510 to Emacs event loop. */
1511 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
1514 #else /* not USE_GTK */
1516 /* We need a unique id for each widget handled by the Lucid Widget
1519 For the main windows, and popup menus, we use this counter,
1520 which we increment each time after use. This starts from 1<<16.
1522 For menu bars, we use numbers starting at 0, counted in
1523 next_menubar_widget_id. */
1524 LWLIB_ID widget_id_tick
;
1527 popup_selection_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
1529 menu_item_selection
= (Lisp_Object
*) client_data
;
1532 /* ARG is the LWLIB ID of the dialog box, represented
1533 as a Lisp object as (HIGHPART . LOWPART). */
1536 pop_down_menu (Lisp_Object arg
)
1538 LWLIB_ID id
= (XINT (XCAR (arg
)) << 4 * sizeof (LWLIB_ID
)
1539 | XINT (XCDR (arg
)));
1542 lw_destroy_all_widgets (id
);
1544 popup_activated_flag
= 0;
1549 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1551 menu_item_selection will be set to the selection. */
1553 create_and_show_popup_menu (FRAME_PTR f
, widget_value
*first_wv
,
1554 int x
, int y
, int for_click
, Time timestamp
)
1560 XButtonPressedEvent
*event
= &(dummy
.xbutton
);
1564 if (! FRAME_X_P (f
))
1568 apply_systemfont_to_menu (f
, f
->output_data
.x
->widget
);
1571 menu_id
= widget_id_tick
++;
1572 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1573 f
->output_data
.x
->widget
, 1, 0,
1574 popup_selection_callback
,
1575 popup_deactivate_callback
,
1576 menu_highlight_callback
);
1578 event
->type
= ButtonPress
;
1580 event
->send_event
= 0;
1581 event
->display
= FRAME_X_DISPLAY (f
);
1582 event
->time
= CurrentTime
;
1583 event
->root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
1584 event
->window
= event
->subwindow
= event
->root
;
1588 /* Adjust coordinates to be root-window-relative. */
1589 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
1590 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
1597 for (i
= 0; i
< 5; i
++)
1598 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
1601 /* Don't allow any geometry request from the user. */
1602 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1603 XtSetValues (menu
, av
, ac
);
1605 /* Display the menu. */
1606 lw_popup_menu (menu
, &dummy
);
1607 popup_activated_flag
= 1;
1608 x_activate_timeout_atimer ();
1611 int fact
= 4 * sizeof (LWLIB_ID
);
1612 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1613 record_unwind_protect (pop_down_menu
,
1614 Fcons (make_number (menu_id
>> (fact
)),
1615 make_number (menu_id
& ~(-1 << (fact
)))));
1617 /* Process events that apply to the menu. */
1618 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
, 1);
1620 unbind_to (specpdl_count
, Qnil
);
1624 #endif /* not USE_GTK */
1627 cleanup_widget_value_tree (Lisp_Object arg
)
1629 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
1630 widget_value
*wv
= p
->pointer
;
1632 free_menubar_widget_value_tree (wv
);
1638 xmenu_show (FRAME_PTR f
, int x
, int y
, int for_click
, int keymaps
,
1639 Lisp_Object title
, const char **error_name
, Time timestamp
)
1642 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1643 widget_value
**submenu_stack
1644 = (widget_value
**) alloca (menu_items_used
* sizeof (widget_value
*));
1645 Lisp_Object
*subprefix_stack
1646 = (Lisp_Object
*) alloca (menu_items_used
* sizeof (Lisp_Object
));
1647 int submenu_depth
= 0;
1651 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1653 if (! FRAME_X_P (f
))
1658 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1660 *error_name
= "Empty menu";
1664 /* Create a tree of widget_value objects
1665 representing the panes and their items. */
1666 wv
= xmalloc_widget_value ();
1670 wv
->button_type
= BUTTON_TYPE_NONE
;
1675 /* Loop over all panes and items, filling in the tree. */
1677 while (i
< menu_items_used
)
1679 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1681 submenu_stack
[submenu_depth
++] = save_wv
;
1687 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1690 save_wv
= submenu_stack
[--submenu_depth
];
1694 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
)
1695 && submenu_depth
!= 0)
1696 i
+= MENU_ITEMS_PANE_LENGTH
;
1697 /* Ignore a nil in the item list.
1698 It's meaningful only for dialog boxes. */
1699 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1701 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1703 /* Create a new pane. */
1704 Lisp_Object pane_name
, prefix
;
1705 const char *pane_string
;
1707 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
1708 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
1710 #ifndef HAVE_MULTILINGUAL_MENU
1711 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1713 pane_name
= ENCODE_MENU_STRING (pane_name
);
1714 ASET (menu_items
, i
+ MENU_ITEMS_PANE_NAME
, pane_name
);
1717 pane_string
= (NILP (pane_name
)
1718 ? "" : SSDATA (pane_name
));
1719 /* If there is just one top-level pane, put all its items directly
1720 under the top-level menu. */
1721 if (menu_items_n_panes
== 1)
1724 /* If the pane has a meaningful name,
1725 make the pane a top-level menu item
1726 with its items as a submenu beneath it. */
1727 if (!keymaps
&& strcmp (pane_string
, ""))
1729 wv
= xmalloc_widget_value ();
1733 first_wv
->contents
= wv
;
1734 wv
->name
= pane_string
;
1735 if (keymaps
&& !NILP (prefix
))
1739 wv
->button_type
= BUTTON_TYPE_NONE
;
1744 else if (first_pane
)
1750 i
+= MENU_ITEMS_PANE_LENGTH
;
1754 /* Create a new item within current pane. */
1755 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
1756 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1757 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1758 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1759 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1760 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1761 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1762 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1764 #ifndef HAVE_MULTILINGUAL_MENU
1765 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
1767 item_name
= ENCODE_MENU_STRING (item_name
);
1768 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
, item_name
);
1771 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1773 descrip
= ENCODE_MENU_STRING (descrip
);
1774 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
, descrip
);
1776 #endif /* not HAVE_MULTILINGUAL_MENU */
1778 wv
= xmalloc_widget_value ();
1782 save_wv
->contents
= wv
;
1783 wv
->name
= SSDATA (item_name
);
1784 if (!NILP (descrip
))
1785 wv
->key
= SSDATA (descrip
);
1787 /* If this item has a null value,
1788 make the call_data null so that it won't display a box
1789 when the mouse is on it. */
1791 = (!NILP (def
) ? (void *) &XVECTOR (menu_items
)->contents
[i
] : 0);
1792 wv
->enabled
= !NILP (enable
);
1795 wv
->button_type
= BUTTON_TYPE_NONE
;
1796 else if (EQ (type
, QCtoggle
))
1797 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1798 else if (EQ (type
, QCradio
))
1799 wv
->button_type
= BUTTON_TYPE_RADIO
;
1803 wv
->selected
= !NILP (selected
);
1805 if (! STRINGP (help
))
1812 i
+= MENU_ITEMS_ITEM_LENGTH
;
1816 /* Deal with the title, if it is non-nil. */
1819 widget_value
*wv_title
= xmalloc_widget_value ();
1820 widget_value
*wv_sep1
= xmalloc_widget_value ();
1821 widget_value
*wv_sep2
= xmalloc_widget_value ();
1823 wv_sep2
->name
= "--";
1824 wv_sep2
->next
= first_wv
->contents
;
1825 wv_sep2
->help
= Qnil
;
1827 wv_sep1
->name
= "--";
1828 wv_sep1
->next
= wv_sep2
;
1829 wv_sep1
->help
= Qnil
;
1831 #ifndef HAVE_MULTILINGUAL_MENU
1832 if (STRING_MULTIBYTE (title
))
1833 title
= ENCODE_MENU_STRING (title
);
1836 wv_title
->name
= SSDATA (title
);
1837 wv_title
->enabled
= TRUE
;
1838 wv_title
->button_type
= BUTTON_TYPE_NONE
;
1839 wv_title
->help
= Qnil
;
1840 wv_title
->next
= wv_sep1
;
1841 first_wv
->contents
= wv_title
;
1844 /* No selection has been chosen yet. */
1845 menu_item_selection
= 0;
1847 /* Make sure to free the widget_value objects we used to specify the
1848 contents even with longjmp. */
1849 record_unwind_protect (cleanup_widget_value_tree
,
1850 make_save_value (first_wv
, 0));
1852 /* Actually create and show the menu until popped down. */
1853 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
, timestamp
);
1855 unbind_to (specpdl_count
, Qnil
);
1857 /* Find the selected item, and its pane, to return
1858 the proper value. */
1859 if (menu_item_selection
!= 0)
1861 Lisp_Object prefix
, entry
;
1863 prefix
= entry
= Qnil
;
1865 while (i
< menu_items_used
)
1867 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qnil
))
1869 subprefix_stack
[submenu_depth
++] = prefix
;
1873 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qlambda
))
1875 prefix
= subprefix_stack
[--submenu_depth
];
1878 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
1881 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
1882 i
+= MENU_ITEMS_PANE_LENGTH
;
1884 /* Ignore a nil in the item list.
1885 It's meaningful only for dialog boxes. */
1886 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
1891 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
1892 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
1898 entry
= Fcons (entry
, Qnil
);
1900 entry
= Fcons (prefix
, entry
);
1901 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1902 if (!NILP (subprefix_stack
[j
]))
1903 entry
= Fcons (subprefix_stack
[j
], entry
);
1907 i
+= MENU_ITEMS_ITEM_LENGTH
;
1911 else if (!for_click
)
1912 /* Make "Cancel" equivalent to C-g. */
1913 Fsignal (Qquit
, Qnil
);
1920 dialog_selection_callback (GtkWidget
*widget
, gpointer client_data
)
1922 /* Treat the pointer as an integer. There's no problem
1923 as long as pointers have enough bits to hold small integers. */
1924 if ((intptr_t) client_data
!= -1)
1925 menu_item_selection
= (Lisp_Object
*) client_data
;
1927 popup_activated_flag
= 0;
1930 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1932 menu_item_selection will be set to the selection. */
1934 create_and_show_dialog (FRAME_PTR f
, widget_value
*first_wv
)
1938 if (! FRAME_X_P (f
))
1941 menu
= xg_create_widget ("dialog", first_wv
->name
, f
, first_wv
,
1942 G_CALLBACK (dialog_selection_callback
),
1943 G_CALLBACK (popup_deactivate_callback
),
1948 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1949 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
1951 /* Display the menu. */
1952 gtk_widget_show_all (menu
);
1954 /* Process events that apply to the menu. */
1955 popup_widget_loop (1, menu
);
1957 unbind_to (specpdl_count
, Qnil
);
1961 #else /* not USE_GTK */
1963 dialog_selection_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
1965 /* Treat the pointer as an integer. There's no problem
1966 as long as pointers have enough bits to hold small integers. */
1967 if ((intptr_t) client_data
!= -1)
1968 menu_item_selection
= (Lisp_Object
*) client_data
;
1971 lw_destroy_all_widgets (id
);
1973 popup_activated_flag
= 0;
1977 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1979 menu_item_selection will be set to the selection. */
1981 create_and_show_dialog (FRAME_PTR f
, widget_value
*first_wv
)
1988 dialog_id
= widget_id_tick
++;
1990 apply_systemfont_to_dialog (f
->output_data
.x
->widget
);
1992 lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1993 f
->output_data
.x
->widget
, 1, 0,
1994 dialog_selection_callback
, 0, 0);
1995 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1996 /* Display the dialog box. */
1997 lw_pop_up_all_widgets (dialog_id
);
1998 popup_activated_flag
= 1;
1999 x_activate_timeout_atimer ();
2001 /* Process events that apply to the dialog box.
2002 Also handle timers. */
2004 ptrdiff_t count
= SPECPDL_INDEX ();
2005 int fact
= 4 * sizeof (LWLIB_ID
);
2007 /* xdialog_show_unwind is responsible for popping the dialog box down. */
2008 record_unwind_protect (pop_down_menu
,
2009 Fcons (make_number (dialog_id
>> (fact
)),
2010 make_number (dialog_id
& ~(-1 << (fact
)))));
2012 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
),
2015 unbind_to (count
, Qnil
);
2019 #endif /* not USE_GTK */
2021 static const char * button_names
[] = {
2022 "button1", "button2", "button3", "button4", "button5",
2023 "button6", "button7", "button8", "button9", "button10" };
2026 xdialog_show (FRAME_PTR f
,
2030 const char **error_name
)
2032 int i
, nb_buttons
=0;
2033 char dialog_name
[6];
2035 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
2037 /* Number of elements seen so far, before boundary. */
2039 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2040 int boundary_seen
= 0;
2042 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
2044 if (! FRAME_X_P (f
))
2049 if (menu_items_n_panes
> 1)
2051 *error_name
= "Multiple panes in dialog box";
2055 /* Create a tree of widget_value objects
2056 representing the text label and buttons. */
2058 Lisp_Object pane_name
, prefix
;
2059 const char *pane_string
;
2060 pane_name
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_NAME
];
2061 prefix
= XVECTOR (menu_items
)->contents
[MENU_ITEMS_PANE_PREFIX
];
2062 pane_string
= (NILP (pane_name
)
2063 ? "" : SSDATA (pane_name
));
2064 prev_wv
= xmalloc_widget_value ();
2065 prev_wv
->value
= pane_string
;
2066 if (keymaps
&& !NILP (prefix
))
2068 prev_wv
->enabled
= 1;
2069 prev_wv
->name
= "message";
2070 prev_wv
->help
= Qnil
;
2073 /* Loop over all panes and items, filling in the tree. */
2074 i
= MENU_ITEMS_PANE_LENGTH
;
2075 while (i
< menu_items_used
)
2078 /* Create a new item within current pane. */
2079 Lisp_Object item_name
, enable
, descrip
;
2080 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2081 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2083 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2085 if (NILP (item_name
))
2087 free_menubar_widget_value_tree (first_wv
);
2088 *error_name
= "Submenu in dialog items";
2091 if (EQ (item_name
, Qquote
))
2093 /* This is the boundary between left-side elts
2094 and right-side elts. Stop incrementing right_count. */
2099 if (nb_buttons
>= 9)
2101 free_menubar_widget_value_tree (first_wv
);
2102 *error_name
= "Too many dialog items";
2106 wv
= xmalloc_widget_value ();
2108 wv
->name
= (char *) button_names
[nb_buttons
];
2109 if (!NILP (descrip
))
2110 wv
->key
= SSDATA (descrip
);
2111 wv
->value
= SSDATA (item_name
);
2112 wv
->call_data
= (void *) &XVECTOR (menu_items
)->contents
[i
];
2113 wv
->enabled
= !NILP (enable
);
2117 if (! boundary_seen
)
2121 i
+= MENU_ITEMS_ITEM_LENGTH
;
2124 /* If the boundary was not specified,
2125 by default put half on the left and half on the right. */
2126 if (! boundary_seen
)
2127 left_count
= nb_buttons
- nb_buttons
/ 2;
2129 wv
= xmalloc_widget_value ();
2130 wv
->name
= dialog_name
;
2133 /* Frame title: 'Q' = Question, 'I' = Information.
2134 Can also have 'E' = Error if, one day, we want
2135 a popup for errors. */
2137 dialog_name
[0] = 'Q';
2139 dialog_name
[0] = 'I';
2141 /* Dialog boxes use a really stupid name encoding
2142 which specifies how many buttons to use
2143 and how many buttons are on the right. */
2144 dialog_name
[1] = '0' + nb_buttons
;
2145 dialog_name
[2] = 'B';
2146 dialog_name
[3] = 'R';
2147 /* Number of buttons to put on the right. */
2148 dialog_name
[4] = '0' + nb_buttons
- left_count
;
2150 wv
->contents
= first_wv
;
2154 /* No selection has been chosen yet. */
2155 menu_item_selection
= 0;
2157 /* Make sure to free the widget_value objects we used to specify the
2158 contents even with longjmp. */
2159 record_unwind_protect (cleanup_widget_value_tree
,
2160 make_save_value (first_wv
, 0));
2162 /* Actually create and show the dialog. */
2163 create_and_show_dialog (f
, first_wv
);
2165 unbind_to (specpdl_count
, Qnil
);
2167 /* Find the selected item, and its pane, to return
2168 the proper value. */
2169 if (menu_item_selection
!= 0)
2175 while (i
< menu_items_used
)
2179 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2182 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2183 i
+= MENU_ITEMS_PANE_LENGTH
;
2185 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2187 /* This is the boundary between left-side elts and
2194 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2195 if (menu_item_selection
== &XVECTOR (menu_items
)->contents
[i
])
2199 entry
= Fcons (entry
, Qnil
);
2201 entry
= Fcons (prefix
, entry
);
2205 i
+= MENU_ITEMS_ITEM_LENGTH
;
2210 /* Make "Cancel" equivalent to C-g. */
2211 Fsignal (Qquit
, Qnil
);
2216 #else /* not USE_X_TOOLKIT && not USE_GTK */
2218 /* The frame of the last activated non-toolkit menu bar.
2219 Used to generate menu help events. */
2221 static struct frame
*menu_help_frame
;
2224 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2226 PANE is the pane number, and ITEM is the menu item number in
2227 the menu (currently not used).
2229 This cannot be done with generating a HELP_EVENT because
2230 XMenuActivate contains a loop that doesn't let Emacs process
2234 menu_help_callback (char const *help_string
, int pane
, int item
)
2236 Lisp_Object
*first_item
;
2237 Lisp_Object pane_name
;
2238 Lisp_Object menu_object
;
2240 first_item
= XVECTOR (menu_items
)->contents
;
2241 if (EQ (first_item
[0], Qt
))
2242 pane_name
= first_item
[MENU_ITEMS_PANE_NAME
];
2243 else if (EQ (first_item
[0], Qquote
))
2244 /* This shouldn't happen, see xmenu_show. */
2245 pane_name
= empty_unibyte_string
;
2247 pane_name
= first_item
[MENU_ITEMS_ITEM_NAME
];
2249 /* (menu-item MENU-NAME PANE-NUMBER) */
2250 menu_object
= Fcons (Qmenu_item
,
2252 Fcons (make_number (pane
), Qnil
)));
2253 show_help_echo (help_string
? build_string (help_string
) : Qnil
,
2254 Qnil
, menu_object
, make_number (item
));
2258 pop_down_menu (Lisp_Object arg
)
2260 struct Lisp_Save_Value
*p1
= XSAVE_VALUE (Fcar (arg
));
2261 struct Lisp_Save_Value
*p2
= XSAVE_VALUE (Fcdr (arg
));
2263 FRAME_PTR f
= p1
->pointer
;
2264 XMenu
*menu
= p2
->pointer
;
2268 XUngrabPointer (FRAME_X_DISPLAY (f
), CurrentTime
);
2269 XUngrabKeyboard (FRAME_X_DISPLAY (f
), CurrentTime
);
2271 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2273 #ifdef HAVE_X_WINDOWS
2274 /* Assume the mouse has moved out of the X window.
2275 If it has actually moved in, we will get an EnterNotify. */
2276 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
2278 /* State that no mouse buttons are now held.
2279 (The oldXMenu code doesn't track this info for us.)
2280 That is not necessarily true, but the fiction leads to reasonable
2281 results, and it is a pain to ask which are actually held now. */
2282 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2284 #endif /* HAVE_X_WINDOWS */
2293 xmenu_show (FRAME_PTR f
, int x
, int y
, int for_click
, int keymaps
,
2294 Lisp_Object title
, const char **error_name
, Time timestamp
)
2298 int pane
, selidx
, lpane
, status
;
2299 Lisp_Object entry
, pane_prefix
;
2301 int ulx
, uly
, width
, height
;
2302 int dispwidth
, dispheight
;
2303 int i
, j
, lines
, maxlines
;
2306 unsigned int dummy_uint
;
2307 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
2309 if (! FRAME_X_P (f
) && ! FRAME_MSDOS_P (f
))
2313 if (menu_items_n_panes
== 0)
2316 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2318 *error_name
= "Empty menu";
2322 /* Figure out which root window F is on. */
2323 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2324 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2325 &dummy_uint
, &dummy_uint
);
2327 /* Make the menu on that window. */
2328 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2331 *error_name
= "Can't create menu";
2335 /* Don't GC while we prepare and show the menu,
2336 because we give the oldxmenu library pointers to the
2337 contents of strings. */
2338 inhibit_garbage_collection ();
2340 #ifdef HAVE_X_WINDOWS
2341 /* Adjust coordinates to relative to the outer (window manager) window. */
2342 x
+= FRAME_OUTER_TO_INNER_DIFF_X (f
);
2343 y
+= FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2344 #endif /* HAVE_X_WINDOWS */
2346 /* Adjust coordinates to be root-window-relative. */
2350 /* Create all the necessary panes and their items. */
2351 maxwidth
= maxlines
= lines
= i
= 0;
2353 while (i
< menu_items_used
)
2355 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2357 /* Create a new pane. */
2358 Lisp_Object pane_name
, prefix
;
2359 const char *pane_string
;
2361 maxlines
= max (maxlines
, lines
);
2363 pane_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_NAME
];
2364 prefix
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2365 pane_string
= (NILP (pane_name
)
2366 ? "" : SSDATA (pane_name
));
2367 if (keymaps
&& !NILP (prefix
))
2370 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
2371 if (lpane
== XM_FAILURE
)
2373 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2374 *error_name
= "Can't create pane";
2377 i
+= MENU_ITEMS_PANE_LENGTH
;
2379 /* Find the width of the widest item in this pane. */
2381 while (j
< menu_items_used
)
2384 item
= XVECTOR (menu_items
)->contents
[j
];
2392 width
= SBYTES (item
);
2393 if (width
> maxwidth
)
2396 j
+= MENU_ITEMS_ITEM_LENGTH
;
2399 /* Ignore a nil in the item list.
2400 It's meaningful only for dialog boxes. */
2401 else if (EQ (XVECTOR (menu_items
)->contents
[i
], Qquote
))
2405 /* Create a new item within current pane. */
2406 Lisp_Object item_name
, enable
, descrip
, help
;
2408 char const *help_string
;
2410 item_name
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_NAME
];
2411 enable
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_ENABLE
];
2413 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_EQUIV_KEY
];
2414 help
= XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_HELP
];
2415 help_string
= STRINGP (help
) ? SSDATA (help
) : NULL
;
2417 if (!NILP (descrip
))
2419 /* if alloca is fast, use that to make the space,
2420 to reduce gc needs. */
2421 item_data
= (char *) alloca (maxwidth
+ SBYTES (descrip
) + 1);
2422 memcpy (item_data
, SSDATA (item_name
), SBYTES (item_name
));
2423 for (j
= SCHARS (item_name
); j
< maxwidth
; j
++)
2425 memcpy (item_data
+ j
, SSDATA (descrip
), SBYTES (descrip
));
2426 item_data
[j
+ SBYTES (descrip
)] = 0;
2429 item_data
= SSDATA (item_name
);
2431 if (lpane
== XM_FAILURE
2432 || (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2433 menu
, lpane
, 0, item_data
,
2434 !NILP (enable
), help_string
)
2437 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2438 *error_name
= "Can't add selection to menu";
2441 i
+= MENU_ITEMS_ITEM_LENGTH
;
2446 maxlines
= max (maxlines
, lines
);
2448 /* All set and ready to fly. */
2449 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2450 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
2451 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
2452 x
= min (x
, dispwidth
);
2453 y
= min (y
, dispheight
);
2456 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2457 &ulx
, &uly
, &width
, &height
);
2458 if (ulx
+width
> dispwidth
)
2460 x
-= (ulx
+ width
) - dispwidth
;
2461 ulx
= dispwidth
- width
;
2463 if (uly
+height
> dispheight
)
2465 y
-= (uly
+ height
) - dispheight
;
2466 uly
= dispheight
- height
;
2468 #ifndef HAVE_X_WINDOWS
2469 if (FRAME_HAS_MINIBUF_P (f
) && uly
+height
> dispheight
- 1)
2471 /* Move the menu away of the echo area, to avoid overwriting the
2472 menu with help echo messages or vice versa. */
2473 if (BUFFERP (echo_area_buffer
[0]) && WINDOWP (echo_area_window
))
2475 y
-= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window
));
2476 uly
-= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window
));
2485 if (ulx
< 0) x
-= ulx
;
2486 if (uly
< 0) y
-= uly
;
2490 /* If position was not given by a mouse click, adjust so upper left
2491 corner of the menu as a whole ends up at given coordinates. This
2492 is what x-popup-menu says in its documentation. */
2494 y
+= 1.5*height
/(maxlines
+2);
2497 XMenuSetAEQ (menu
, TRUE
);
2498 XMenuSetFreeze (menu
, TRUE
);
2502 XMenuActivateSetWaitFunction (x_menu_wait_for_event
, FRAME_X_DISPLAY (f
));
2505 record_unwind_protect (pop_down_menu
,
2506 Fcons (make_save_value (f
, 0),
2507 make_save_value (menu
, 0)));
2509 /* Help display under X won't work because XMenuActivate contains
2510 a loop that doesn't give Emacs a chance to process it. */
2511 menu_help_frame
= f
;
2512 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2513 x
, y
, ButtonReleaseMask
, &datap
,
2514 menu_help_callback
);
2515 entry
= pane_prefix
= Qnil
;
2521 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2524 /* Find the item number SELIDX in pane number PANE. */
2526 while (i
< menu_items_used
)
2528 if (EQ (XVECTOR (menu_items
)->contents
[i
], Qt
))
2532 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_PANE_PREFIX
];
2534 i
+= MENU_ITEMS_PANE_LENGTH
;
2543 = XVECTOR (menu_items
)->contents
[i
+ MENU_ITEMS_ITEM_VALUE
];
2546 entry
= Fcons (entry
, Qnil
);
2547 if (!NILP (pane_prefix
))
2548 entry
= Fcons (pane_prefix
, entry
);
2554 i
+= MENU_ITEMS_ITEM_LENGTH
;
2560 *error_name
= "Can't activate menu";
2564 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2565 the menu was invoked with a mouse event as POSITION). */
2567 Fsignal (Qquit
, Qnil
);
2571 unbind_to (specpdl_count
, Qnil
);
2576 #endif /* not USE_X_TOOLKIT */
2578 #endif /* HAVE_MENUS */
2581 /* Detect if a dialog or menu has been posted. MSDOS has its own
2582 implementation on msdos.c. */
2585 popup_activated (void)
2587 return popup_activated_flag
;
2589 #endif /* not MSDOS */
2591 /* The following is used by delayed window autoselection. */
2593 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p
, Smenu_or_popup_active_p
, 0, 0, 0,
2594 doc
: /* Return t if a menu or popup dialog is active. */)
2598 return (popup_activated ()) ? Qt
: Qnil
;
2601 #endif /* HAVE_MENUS */
2605 syms_of_xmenu (void)
2607 DEFSYM (Qdebug_on_next_call
, "debug-on-next-call");
2609 #ifdef USE_X_TOOLKIT
2610 widget_id_tick
= (1<<16);
2611 next_menubar_widget_id
= 1;
2614 defsubr (&Smenu_or_popup_active_p
);
2616 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
2617 defsubr (&Sx_menu_bar_open_internal
);
2618 Ffset (intern_c_string ("accelerate-menu"),
2619 intern_c_string (Sx_menu_bar_open_internal
.symbol_name
));
2623 defsubr (&Sx_popup_dialog
);