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. */
42 #include "termhooks.h"
44 #include "blockinput.h"
45 #include "character.h"
49 #include "sysselect.h"
56 /* This may include sys/types.h, and that somehow loses
57 if this is not done before the other system files. */
61 /* Load sys/types.h if not already loaded.
62 In some systems loading it twice is suicidal. */
64 #include <sys/types.h>
67 #include "dispextern.h"
70 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
71 code accepts the Emacs internal encoding. */
72 #undef HAVE_MULTILINGUAL_MENU
76 #include <X11/IntrinsicP.h>
77 #include <X11/CoreP.h>
78 #include <X11/StringDefs.h>
79 #include <X11/Shell.h>
81 #include "xsettings.h"
82 #include "../lwlib/xlwmenu.h"
84 #include <X11/Xaw3d/Paned.h>
85 #else /* !HAVE_XAW3D */
86 #include <X11/Xaw/Paned.h>
87 #endif /* HAVE_XAW3D */
88 #endif /* USE_LUCID */
90 #include "../lwlib/lwlib.h"
92 #else /* not USE_X_TOOLKIT */
94 #include "../oldXMenu/XMenu.h"
96 #endif /* not USE_X_TOOLKIT */
97 #endif /* HAVE_X_WINDOWS */
102 #include "xgselect.h"
112 static Lisp_Object Qdebug_on_next_call
;
114 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
115 static Lisp_Object
xdialog_show (FRAME_PTR
, int, Lisp_Object
, Lisp_Object
,
119 /* Flag which when set indicates a dialog or menu has been posted by
120 Xt on behalf of one of the widget sets. */
121 static int popup_activated_flag
;
126 static int next_menubar_widget_id
;
128 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
130 static struct frame
*
131 menubar_id_to_frame (LWLIB_ID id
)
133 Lisp_Object tail
, frame
;
136 for (tail
= Vframe_list
; CONSP (tail
); tail
= XCDR (tail
))
142 if (!FRAME_WINDOW_P (f
))
144 if (f
->output_data
.x
->id
== id
)
152 #ifdef HAVE_X_WINDOWS
153 /* Return the mouse position in *X and *Y. The coordinates are window
154 relative for the edit window in frame F.
155 This is for Fx_popup_menu. The mouse_position_hook can not
156 be used for X, as it returns window relative coordinates
157 for the window where the mouse is in. This could be the menu bar,
158 the scroll bar or the edit window. Fx_popup_menu needs to be
159 sure it is the edit window. */
161 mouse_position_for_popup (FRAME_PTR f
, int *x
, int *y
)
163 Window root
, dummy_window
;
171 XQueryPointer (FRAME_X_DISPLAY (f
),
172 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
174 /* The root window which contains the pointer. */
177 /* Window pointer is on, not used */
180 /* The position on that root window. */
183 /* x/y in dummy_window coordinates, not used. */
186 /* Modifier keys and pointer buttons, about which
188 (unsigned int *) &dummy
);
192 /* xmenu_show expects window coordinates, not root window
193 coordinates. Translate. */
194 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
195 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
198 #endif /* HAVE_X_WINDOWS */
202 DEFUN ("x-popup-dialog", Fx_popup_dialog
, Sx_popup_dialog
, 2, 3, 0,
203 doc
: /* Pop up a dialog box and return user's selection.
204 POSITION specifies which frame to use.
205 This is normally a mouse button event or a window or frame.
206 If POSITION is t, it means to use the frame the mouse is on.
207 The dialog box appears in the middle of the specified frame.
209 CONTENTS specifies the alternatives to display in the dialog box.
210 It is a list of the form (DIALOG ITEM1 ITEM2...).
211 Each ITEM is a cons cell (STRING . VALUE).
212 The return value is VALUE from the chosen item.
214 An ITEM may also be just a string--that makes a nonselectable item.
215 An ITEM may also be nil--that means to put all preceding items
216 on the left of the dialog box and all following items on the right.
217 \(By default, approximately half appear on each side.)
219 If HEADER is non-nil, the frame title for the box is "Information",
220 otherwise it is "Question".
222 If the user gets rid of the dialog box without making a valid choice,
223 for instance using the window manager, then this produces a quit and
224 `x-popup-dialog' does not return. */)
225 (Lisp_Object position
, Lisp_Object contents
, Lisp_Object header
)
232 /* Decode the first argument: find the window or frame to use. */
233 if (EQ (position
, Qt
)
234 || (CONSP (position
) && (EQ (XCAR (position
), Qmenu_bar
)
235 || EQ (XCAR (position
), Qtool_bar
))))
237 #if 0 /* Using the frame the mouse is on may not be right. */
238 /* Use the mouse's current position. */
239 FRAME_PTR new_f
= SELECTED_FRAME ();
240 Lisp_Object bar_window
;
241 enum scroll_bar_part part
;
245 (*mouse_position_hook
) (&new_f
, 1, &bar_window
, &part
, &x
, &y
, &time
);
248 XSETFRAME (window
, new_f
);
250 window
= selected_window
;
252 window
= selected_window
;
254 else if (CONSP (position
))
256 Lisp_Object tem
= XCAR (position
);
258 window
= Fcar (XCDR (position
));
261 tem
= Fcar (XCDR (position
)); /* EVENT_START (position) */
262 window
= Fcar (tem
); /* POSN_WINDOW (tem) */
265 else if (WINDOWP (position
) || FRAMEP (position
))
270 /* Decode where to put the menu. */
274 else if (WINDOWP (window
))
276 CHECK_LIVE_WINDOW (window
);
277 f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
280 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
281 but I don't want to make one now. */
282 CHECK_WINDOW (window
);
284 if (! FRAME_X_P (f
) && ! FRAME_MSDOS_P (f
))
285 error ("Can not put X dialog on this terminal");
287 /* Force a redisplay before showing the dialog. If a frame is created
288 just before showing the dialog, its contents may not have been fully
289 drawn, as this depends on timing of events from the X server. Redisplay
290 is not done when a dialog is shown. If redisplay could be done in the
291 X event loop (i.e. the X event loop does not run in a signal handler)
292 this would not be needed.
294 Do this before creating the widget value that points to Lisp
295 string contents, because Fredisplay may GC and relocate them. */
298 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
299 /* Display a menu with these alternatives
300 in the middle of frame F. */
302 Lisp_Object x
, y
, frame
, newpos
;
303 XSETFRAME (frame
, f
);
304 XSETINT (x
, x_pixel_width (f
) / 2);
305 XSETINT (y
, x_pixel_height (f
) / 2);
306 newpos
= Fcons (Fcons (x
, Fcons (y
, Qnil
)), Fcons (frame
, Qnil
));
308 return Fx_popup_menu (newpos
,
309 Fcons (Fcar (contents
), Fcons (contents
, Qnil
)));
314 const char *error_name
;
315 Lisp_Object selection
;
316 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
318 /* Decode the dialog items from what was specified. */
319 title
= Fcar (contents
);
320 CHECK_STRING (title
);
321 record_unwind_protect (unuse_menu_items
, Qnil
);
323 if (NILP (Fcar (Fcdr (contents
))))
324 /* No buttons specified, add an "Ok" button so users can pop down
325 the dialog. Also, the lesstif/motif version crashes if there are
327 contents
= Fcons (title
, Fcons (Fcons (build_string ("Ok"), Qt
), Qnil
));
329 list_of_panes (Fcons (contents
, Qnil
));
331 /* Display them in a dialog box. */
333 selection
= xdialog_show (f
, 0, title
, header
, &error_name
);
336 unbind_to (specpdl_count
, Qnil
);
337 discard_menu_items ();
339 if (error_name
) error ("%s", error_name
);
348 #if defined USE_GTK || defined USE_MOTIF
350 /* Set menu_items_inuse so no other popup menu or dialog is created. */
353 x_menu_set_in_use (int in_use
)
355 menu_items_inuse
= in_use
? Qt
: Qnil
;
356 popup_activated_flag
= in_use
;
358 if (popup_activated_flag
)
359 x_activate_timeout_atimer ();
365 /* Wait for an X event to arrive or for a timer to expire. */
371 x_menu_wait_for_event (void *data
)
373 /* Another way to do this is to register a timer callback, that can be
374 done in GTK and Xt. But we have to do it like this when using only X
375 anyway, and with callbacks we would have three variants for timer handling
376 instead of the small ifdefs below. */
380 ! XtAppPending (Xt_app_con
)
381 #elif defined USE_GTK
382 ! gtk_events_pending ()
384 ! XPending ((Display
*) data
)
388 EMACS_TIME next_time
= timer_check (), *ntp
;
389 SELECT_TYPE read_fds
;
390 struct x_display_info
*dpyinfo
;
394 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
396 int fd
= ConnectionNumber (dpyinfo
->display
);
397 FD_SET (fd
, &read_fds
);
399 XFlush (dpyinfo
->display
);
402 if (! EMACS_TIME_VALID_P (next_time
))
408 /* Gtk3 have arrows on menus when they don't fit. When the
409 pointer is over an arrow, a timeout scrolls it a bit. Use
410 xg_select so that timeout gets triggered. */
411 xg_select (n
+ 1, &read_fds
, NULL
, NULL
, ntp
, NULL
);
413 pselect (n
+ 1, &read_fds
, NULL
, NULL
, ntp
, NULL
);
420 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
424 /* Loop in Xt until the menu pulldown or dialog popup has been
425 popped down (deactivated). This is used for x-popup-menu
426 and x-popup-dialog; it is not used for the menu bar.
428 NOTE: All calls to popup_get_selection should be protected
429 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
432 popup_get_selection (XEvent
*initial_event
, struct x_display_info
*dpyinfo
, LWLIB_ID id
, int do_timers
)
436 while (popup_activated_flag
)
440 event
= *initial_event
;
445 if (do_timers
) x_menu_wait_for_event (0);
446 XtAppNextEvent (Xt_app_con
, &event
);
449 /* Make sure we don't consider buttons grabbed after menu goes.
450 And make sure to deactivate for any ButtonRelease,
451 even if XtDispatchEvent doesn't do that. */
452 if (event
.type
== ButtonRelease
453 && dpyinfo
->display
== event
.xbutton
.display
)
455 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
456 #ifdef USE_MOTIF /* Pretending that the event came from a
457 Btn1Down seems the only way to convince Motif to
458 activate its callbacks; setting the XmNmenuPost
459 isn't working. --marcus@sysc.pdx.edu. */
460 event
.xbutton
.button
= 1;
461 /* Motif only pops down menus when no Ctrl, Alt or Mod
462 key is pressed and the button is released. So reset key state
463 so Motif thinks this is the case. */
464 event
.xbutton
.state
= 0;
467 /* Pop down on C-g and Escape. */
468 else if (event
.type
== KeyPress
469 && dpyinfo
->display
== event
.xbutton
.display
)
471 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
473 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
474 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
475 popup_activated_flag
= 0;
478 x_dispatch_event (&event
, event
.xany
.display
);
482 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal
, Sx_menu_bar_open_internal
, 0, 1, "i",
483 doc
: /* Start key navigation of the menu bar in FRAME.
484 This initially opens the first menu bar item and you can then navigate with the
485 arrow keys, select a menu entry with the return key or cancel with the
486 escape key. If FRAME has no menu bar this function does nothing.
488 If FRAME is nil or not given, use the selected frame. */)
492 FRAME_PTR f
= check_x_frame (frame
);
496 if (FRAME_EXTERNAL_MENU_BAR (f
))
497 set_frame_menubar (f
, 0, 1);
499 menubar
= FRAME_X_OUTPUT (f
)->menubar_widget
;
505 x_catch_errors (FRAME_X_DISPLAY (f
));
506 memset (&ev
, 0, sizeof ev
);
507 ev
.xbutton
.display
= FRAME_X_DISPLAY (f
);
508 ev
.xbutton
.window
= XtWindow (menubar
);
509 ev
.xbutton
.root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
510 ev
.xbutton
.time
= XtLastTimestampProcessed (FRAME_X_DISPLAY (f
));
511 ev
.xbutton
.button
= Button1
;
512 ev
.xbutton
.x
= ev
.xbutton
.y
= FRAME_MENUBAR_HEIGHT (f
) / 2;
513 ev
.xbutton
.same_screen
= True
;
520 XtSetArg (al
[0], XtNchildren
, &list
);
521 XtSetArg (al
[1], XtNnumChildren
, &nr
);
522 XtGetValues (menubar
, al
, 2);
523 ev
.xbutton
.window
= XtWindow (list
[0]);
527 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
528 /* From-window, to-window. */
529 ev
.xbutton
.window
, ev
.xbutton
.root
,
531 /* From-position, to-position. */
532 ev
.xbutton
.x
, ev
.xbutton
.y
,
533 &ev
.xbutton
.x_root
, &ev
.xbutton
.y_root
,
537 error_p
= x_had_errors_p (FRAME_X_DISPLAY (f
));
542 ev
.type
= ButtonPress
;
543 ev
.xbutton
.state
= 0;
545 XtDispatchEvent (&ev
);
546 ev
.xbutton
.type
= ButtonRelease
;
547 ev
.xbutton
.state
= Button1Mask
;
548 XtDispatchEvent (&ev
);
556 #endif /* USE_X_TOOLKIT */
560 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal
, Sx_menu_bar_open_internal
, 0, 1, "i",
561 doc
: /* Start key navigation of the menu bar in FRAME.
562 This initially opens the first menu bar item and you can then navigate with the
563 arrow keys, select a menu entry with the return key or cancel with the
564 escape key. If FRAME has no menu bar this function does nothing.
566 If FRAME is nil or not given, use the selected frame. */)
572 /* gcc 2.95 doesn't accept the FRAME_PTR declaration after
576 f
= check_x_frame (frame
);
578 if (FRAME_EXTERNAL_MENU_BAR (f
))
579 set_frame_menubar (f
, 0, 1);
581 menubar
= FRAME_X_OUTPUT (f
)->menubar_widget
;
584 /* Activate the first menu. */
585 GList
*children
= gtk_container_get_children (GTK_CONTAINER (menubar
));
589 g_signal_emit_by_name (children
->data
, "activate_item");
590 popup_activated_flag
= 1;
591 g_list_free (children
);
599 /* Loop util popup_activated_flag is set to zero in a callback.
600 Used for popup menus and dialogs. */
603 popup_widget_loop (int do_timers
, GtkWidget
*widget
)
605 ++popup_activated_flag
;
607 /* Process events in the Gtk event loop until done. */
608 while (popup_activated_flag
)
610 if (do_timers
) x_menu_wait_for_event (0);
611 gtk_main_iteration ();
616 /* Activate the menu bar of frame F.
617 This is called from keyboard.c when it gets the
618 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
620 To activate the menu bar, we use the X button-press event
621 that was saved in saved_menu_event.
622 That makes the toolkit do its thing.
624 But first we recompute the menu bar contents (the whole tree).
626 The reason for saving the button event until here, instead of
627 passing it to the toolkit right away, is that we can safely
628 execute Lisp code. */
631 x_activate_menubar (FRAME_PTR f
)
636 if (!f
->output_data
.x
->saved_menu_event
->type
)
640 if (! xg_win_to_widget (FRAME_X_DISPLAY (f
),
641 f
->output_data
.x
->saved_menu_event
->xany
.window
))
645 set_frame_menubar (f
, 0, 1);
647 popup_activated_flag
= 1;
649 XPutBackEvent (f
->output_data
.x
->display_info
->display
,
650 f
->output_data
.x
->saved_menu_event
);
652 XtDispatchEvent (f
->output_data
.x
->saved_menu_event
);
656 /* Ignore this if we get it a second time. */
657 f
->output_data
.x
->saved_menu_event
->type
= 0;
660 /* This callback is invoked when the user selects a menubar cascade
661 pushbutton, but before the pulldown menu is posted. */
665 popup_activate_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
667 popup_activated_flag
= 1;
669 x_activate_timeout_atimer ();
674 /* This callback is invoked when a dialog or menu is finished being
675 used and has been unposted. */
678 popup_deactivate_callback (
680 GtkWidget
*widget
, gpointer client_data
682 Widget widget
, LWLIB_ID id
, XtPointer client_data
686 popup_activated_flag
= 0;
690 /* Function that finds the frame for WIDGET and shows the HELP text
692 F is the frame if known, or NULL if not known. */
694 show_help_event (FRAME_PTR f
, xt_or_gtk_widget widget
, Lisp_Object help
)
700 XSETFRAME (frame
, f
);
701 kbd_buffer_store_help_event (frame
, help
);
705 #if 0 /* This code doesn't do anything useful. ++kfs */
706 /* WIDGET is the popup menu. It's parent is the frame's
707 widget. See which frame that is. */
708 xt_or_gtk_widget frame_widget
= XtParent (widget
);
711 for (tail
= Vframe_list
; CONSP (tail
); tail
= XCDR (tail
))
715 && (f
= XFRAME (frame
),
716 FRAME_X_P (f
) && f
->output_data
.x
->widget
== frame_widget
))
720 show_help_echo (help
, Qnil
, Qnil
, Qnil
);
724 /* Callback called when menu items are highlighted/unhighlighted
725 while moving the mouse over them. WIDGET is the menu bar or menu
726 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
727 the data structure for the menu item, or null in case of
732 menu_highlight_callback (GtkWidget
*widget
, gpointer call_data
)
734 xg_menu_item_cb_data
*cb_data
;
737 cb_data
= (xg_menu_item_cb_data
*) g_object_get_data (G_OBJECT (widget
),
739 if (! cb_data
) return;
741 help
= call_data
? cb_data
->help
: Qnil
;
743 /* If popup_activated_flag is greater than 1 we are in a popup menu.
744 Don't pass the frame to show_help_event for those.
745 Passing frame creates an Emacs event. As we are looping in
746 popup_widget_loop, it won't be handled. Passing NULL shows the tip
747 directly without using an Emacs event. This is what the Lucid code
749 show_help_event (popup_activated_flag
<= 1 ? cb_data
->cl_data
->f
: NULL
,
754 menu_highlight_callback (Widget widget
, LWLIB_ID id
, void *call_data
)
759 widget_value
*wv
= (widget_value
*) call_data
;
761 help
= wv
? wv
->help
: Qnil
;
763 /* Determine the frame for the help event. */
764 f
= menubar_id_to_frame (id
);
766 show_help_event (f
, widget
, help
);
771 /* Gtk calls callbacks just because we tell it what item should be
772 selected in a radio group. If this variable is set to a non-zero
773 value, we are creating menus and don't want callbacks right now.
775 static int xg_crazy_callback_abort
;
777 /* This callback is called from the menu bar pulldown menu
778 when the user makes a selection.
779 Figure out what the user chose
780 and put the appropriate events into the keyboard buffer. */
782 menubar_selection_callback (GtkWidget
*widget
, gpointer client_data
)
784 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
786 if (xg_crazy_callback_abort
)
789 if (! cb_data
|| ! cb_data
->cl_data
|| ! cb_data
->cl_data
->f
)
792 /* For a group of radio buttons, GTK calls the selection callback first
793 for the item that was active before the selection and then for the one that
794 is active after the selection. For C-h k this means we get the help on
795 the deselected item and then the selected item is executed. Prevent that
796 by ignoring the non-active item. */
797 if (GTK_IS_RADIO_MENU_ITEM (widget
)
798 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget
)))
801 /* When a menu is popped down, X generates a focus event (i.e. focus
802 goes back to the frame below the menu). Since GTK buffers events,
803 we force it out here before the menu selection event. Otherwise
804 sit-for will exit at once if the focus event follows the menu selection
808 while (gtk_events_pending ())
809 gtk_main_iteration ();
812 find_and_call_menu_selection (cb_data
->cl_data
->f
,
813 cb_data
->cl_data
->menu_bar_items_used
,
814 cb_data
->cl_data
->menu_bar_vector
,
818 #else /* not USE_GTK */
820 /* This callback is called from the menu bar pulldown menu
821 when the user makes a selection.
822 Figure out what the user chose
823 and put the appropriate events into the keyboard buffer. */
825 menubar_selection_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
829 f
= menubar_id_to_frame (id
);
832 find_and_call_menu_selection (f
, f
->menu_bar_items_used
,
833 f
->menu_bar_vector
, client_data
);
835 #endif /* not USE_GTK */
837 /* Recompute all the widgets of frame F, when the menu bar has been
838 changed. Value is non-zero if widgets were updated. */
841 update_frame_menubar (FRAME_PTR f
)
844 return xg_update_frame_menubar (f
);
852 x
= f
->output_data
.x
;
854 if (!x
->menubar_widget
|| XtIsManaged (x
->menubar_widget
))
858 /* Save the size of the frame because the pane widget doesn't accept
859 to resize itself. So force it. */
860 columns
= FRAME_COLS (f
);
861 rows
= FRAME_LINES (f
);
863 /* Do the voodoo which means "I'm changing lots of things, don't try
864 to refigure sizes until I'm done." */
865 lw_refigure_widget (x
->column_widget
, False
);
867 /* The order in which children are managed is the top to bottom
868 order in which they are displayed in the paned window. First,
869 remove the text-area widget. */
870 XtUnmanageChild (x
->edit_widget
);
872 /* Remove the menubar that is there now, and put up the menubar that
874 XtManageChild (x
->menubar_widget
);
875 XtMapWidget (x
->menubar_widget
);
876 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, NULL
);
878 /* Re-manage the text-area widget, and then thrash the sizes. */
879 XtManageChild (x
->edit_widget
);
880 lw_refigure_widget (x
->column_widget
, True
);
882 /* Force the pane widget to resize itself with the right values. */
883 EmacsFrameSetCharSize (x
->edit_widget
, columns
, rows
);
891 apply_systemfont_to_dialog (Widget w
)
893 const char *fn
= xsettings_get_system_normal_font ();
896 XrmDatabase db
= XtDatabase (XtDisplay (w
));
898 XrmPutStringResource (&db
, "*dialog.font", fn
);
903 apply_systemfont_to_menu (struct frame
*f
, Widget w
)
905 const char *fn
= xsettings_get_system_normal_font ();
909 XrmDatabase db
= XtDatabase (XtDisplay (w
));
912 XrmPutStringResource (&db
, "*menubar*font", fn
);
913 XrmPutStringResource (&db
, "*popup*font", fn
);
920 /* Set the contents of the menubar widgets of frame F.
921 The argument FIRST_TIME is currently ignored;
922 it is set the first time this is called, from initialize_frame_menubar. */
925 set_frame_menubar (FRAME_PTR f
, int first_time
, int deep_p
)
927 xt_or_gtk_widget menubar_widget
;
932 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
934 int *submenu_start
, *submenu_end
;
935 int *submenu_top_level_items
, *submenu_n_panes
;
940 menubar_widget
= f
->output_data
.x
->menubar_widget
;
942 XSETFRAME (Vmenu_updating_frame
, f
);
945 if (f
->output_data
.x
->id
== 0)
946 f
->output_data
.x
->id
= next_menubar_widget_id
++;
947 id
= f
->output_data
.x
->id
;
950 if (! menubar_widget
)
952 /* Make the first call for any given frame always go deep. */
953 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
956 f
->output_data
.x
->saved_menu_event
= xmalloc (sizeof (XEvent
));
957 f
->output_data
.x
->saved_menu_event
->type
= 0;
961 /* If we have detached menus, we must update deep so detached menus
962 also gets updated. */
963 deep_p
= deep_p
|| xg_have_tear_offs ();
968 /* Make a widget-value tree representing the entire menu trees. */
970 struct buffer
*prev
= current_buffer
;
972 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
973 int previous_menu_items_used
= f
->menu_bar_items_used
;
974 Lisp_Object
*previous_items
975 = alloca (previous_menu_items_used
* sizeof *previous_items
);
978 /* If we are making a new widget, its contents are empty,
979 do always reinitialize them. */
980 if (! menubar_widget
)
981 previous_menu_items_used
= 0;
983 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->buffer
;
984 specbind (Qinhibit_quit
, Qt
);
985 /* Don't let the debugger step into this code
986 because it is not reentrant. */
987 specbind (Qdebug_on_next_call
, Qnil
);
989 record_unwind_save_match_data ();
990 if (NILP (Voverriding_local_map_menu_flag
))
992 specbind (Qoverriding_terminal_local_map
, Qnil
);
993 specbind (Qoverriding_local_map
, Qnil
);
996 set_buffer_internal_1 (XBUFFER (buffer
));
998 /* Run the Lucid hook. */
999 safe_run_hooks (Qactivate_menubar_hook
);
1001 /* If it has changed current-menubar from previous value,
1002 really recompute the menubar from the value. */
1003 if (! NILP (Vlucid_menu_bar_dirty_flag
))
1004 call0 (Qrecompute_lucid_menubar
);
1005 safe_run_hooks (Qmenu_bar_update_hook
);
1006 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
1008 items
= FRAME_MENU_BAR_ITEMS (f
);
1010 /* Save the frame's previous menu bar contents data. */
1011 if (previous_menu_items_used
)
1012 memcpy (previous_items
, XVECTOR (f
->menu_bar_vector
)->contents
,
1013 previous_menu_items_used
* word_size
);
1015 /* Fill in menu_items with the current menu bar contents.
1016 This can evaluate Lisp code. */
1019 menu_items
= f
->menu_bar_vector
;
1020 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
1021 subitems
= ASIZE (items
) / 4;
1022 submenu_start
= alloca ((subitems
+ 1) * sizeof *submenu_start
);
1023 submenu_end
= alloca (subitems
* sizeof *submenu_end
);
1024 submenu_n_panes
= alloca (subitems
* sizeof *submenu_n_panes
);
1025 submenu_top_level_items
= alloca (subitems
1026 * sizeof *submenu_top_level_items
);
1028 for (i
= 0; i
< subitems
; i
++)
1030 Lisp_Object key
, string
, maps
;
1032 key
= AREF (items
, 4 * i
);
1033 string
= AREF (items
, 4 * i
+ 1);
1034 maps
= AREF (items
, 4 * i
+ 2);
1038 submenu_start
[i
] = menu_items_used
;
1040 menu_items_n_panes
= 0;
1041 submenu_top_level_items
[i
]
1042 = parse_single_submenu (key
, string
, maps
);
1043 submenu_n_panes
[i
] = menu_items_n_panes
;
1045 submenu_end
[i
] = menu_items_used
;
1048 submenu_start
[i
] = -1;
1049 finish_menu_items ();
1051 /* Convert menu_items into widget_value trees
1052 to display the menu. This cannot evaluate Lisp code. */
1054 wv
= xmalloc_widget_value ();
1055 wv
->name
= "menubar";
1058 wv
->button_type
= BUTTON_TYPE_NONE
;
1062 for (i
= 0; 0 <= submenu_start
[i
]; i
++)
1064 menu_items_n_panes
= submenu_n_panes
[i
];
1065 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
1066 submenu_top_level_items
[i
]);
1070 first_wv
->contents
= wv
;
1071 /* Don't set wv->name here; GC during the loop might relocate it. */
1073 wv
->button_type
= BUTTON_TYPE_NONE
;
1077 set_buffer_internal_1 (prev
);
1079 /* If there has been no change in the Lisp-level contents
1080 of the menu bar, skip redisplaying it. Just exit. */
1082 /* Compare the new menu items with the ones computed last time. */
1083 for (i
= 0; i
< previous_menu_items_used
; i
++)
1084 if (menu_items_used
== i
1085 || (!EQ (previous_items
[i
], AREF (menu_items
, i
))))
1087 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
1089 /* The menu items have not changed. Don't bother updating
1090 the menus in any form, since it would be a no-op. */
1091 free_menubar_widget_value_tree (first_wv
);
1092 discard_menu_items ();
1093 unbind_to (specpdl_count
, Qnil
);
1097 /* The menu items are different, so store them in the frame. */
1098 fset_menu_bar_vector (f
, menu_items
);
1099 f
->menu_bar_items_used
= menu_items_used
;
1101 /* This undoes save_menu_items. */
1102 unbind_to (specpdl_count
, Qnil
);
1104 /* Now GC cannot happen during the lifetime of the widget_value,
1105 so it's safe to store data from a Lisp_String. */
1106 wv
= first_wv
->contents
;
1107 for (i
= 0; i
< ASIZE (items
); i
+= 4)
1110 string
= AREF (items
, i
+ 1);
1113 wv
->name
= SSDATA (string
);
1114 update_submenu_strings (wv
->contents
);
1121 /* Make a widget-value tree containing
1122 just the top level menu bar strings. */
1124 wv
= xmalloc_widget_value ();
1125 wv
->name
= "menubar";
1128 wv
->button_type
= BUTTON_TYPE_NONE
;
1132 items
= FRAME_MENU_BAR_ITEMS (f
);
1133 for (i
= 0; i
< ASIZE (items
); i
+= 4)
1137 string
= AREF (items
, i
+ 1);
1141 wv
= xmalloc_widget_value ();
1142 wv
->name
= SSDATA (string
);
1145 wv
->button_type
= BUTTON_TYPE_NONE
;
1147 /* This prevents lwlib from assuming this
1148 menu item is really supposed to be empty. */
1149 /* The intptr_t cast avoids a warning.
1150 This value just has to be different from small integers. */
1151 wv
->call_data
= (void *) (intptr_t) (-1);
1156 first_wv
->contents
= wv
;
1160 /* Forget what we thought we knew about what is in the
1161 detailed contents of the menu bar menus.
1162 Changing the top level always destroys the contents. */
1163 f
->menu_bar_items_used
= 0;
1166 /* Create or update the menu bar widget. */
1171 xg_crazy_callback_abort
= 1;
1174 /* The fourth arg is DEEP_P, which says to consider the entire
1175 menu trees we supply, rather than just the menu bar item names. */
1176 xg_modify_menubar_widgets (menubar_widget
,
1180 G_CALLBACK (menubar_selection_callback
),
1181 G_CALLBACK (popup_deactivate_callback
),
1182 G_CALLBACK (menu_highlight_callback
));
1187 = xg_create_widget ("menubar", "menubar", f
, first_wv
,
1188 G_CALLBACK (menubar_selection_callback
),
1189 G_CALLBACK (popup_deactivate_callback
),
1190 G_CALLBACK (menu_highlight_callback
));
1192 f
->output_data
.x
->menubar_widget
= menubar_widget
;
1196 #else /* not USE_GTK */
1199 /* Disable resizing (done for Motif!) */
1200 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
1202 /* The third arg is DEEP_P, which says to consider the entire
1203 menu trees we supply, rather than just the menu bar item names. */
1204 lw_modify_all_widgets (id
, first_wv
, deep_p
);
1206 /* Re-enable the edit widget to resize. */
1207 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
1211 char menuOverride
[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
1212 XtTranslations override
= XtParseTranslationTable (menuOverride
);
1215 apply_systemfont_to_menu (f
, f
->output_data
.x
->column_widget
);
1217 menubar_widget
= lw_create_widget ("menubar", "menubar", id
,
1219 f
->output_data
.x
->column_widget
,
1221 popup_activate_callback
,
1222 menubar_selection_callback
,
1223 popup_deactivate_callback
,
1224 menu_highlight_callback
);
1225 f
->output_data
.x
->menubar_widget
= menubar_widget
;
1227 /* Make menu pop down on C-g. */
1228 XtOverrideTranslations (menubar_widget
, override
);
1233 if (f
->output_data
.x
->menubar_widget
)
1234 XtRealizeWidget (f
->output_data
.x
->menubar_widget
);
1237 = (f
->output_data
.x
->menubar_widget
1238 ? (f
->output_data
.x
->menubar_widget
->core
.height
1239 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
1242 #if 1 /* Experimentally, we now get the right results
1243 for -geometry -0-0 without this. 24 Aug 96, rms.
1244 Maybe so, but the menu bar size is missing the pixels so the
1245 WM size hints are off by these pixels. Jan D, oct 2009. */
1247 if (FRAME_EXTERNAL_MENU_BAR (f
))
1250 XtVaGetValues (f
->output_data
.x
->column_widget
,
1251 XtNinternalBorderWidth
, &ibw
, NULL
);
1252 menubar_size
+= ibw
;
1254 #endif /* USE_LUCID */
1257 f
->output_data
.x
->menubar_height
= menubar_size
;
1259 #endif /* not USE_GTK */
1261 free_menubar_widget_value_tree (first_wv
);
1262 update_frame_menubar (f
);
1265 xg_crazy_callback_abort
= 0;
1271 /* Called from Fx_create_frame to create the initial menubar of a frame
1272 before it is mapped, so that the window is mapped with the menubar already
1273 there instead of us tacking it on later and thrashing the window after it
1277 initialize_frame_menubar (FRAME_PTR f
)
1279 /* This function is called before the first chance to redisplay
1280 the frame. It has to be, so the frame will have the right size. */
1281 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
1282 set_frame_menubar (f
, 1, 1);
1286 /* Get rid of the menu bar of frame F, and free its storage.
1287 This is used when deleting a frame, and when turning off the menu bar.
1288 For GTK this function is in gtkutil.c. */
1292 free_frame_menubar (FRAME_PTR f
)
1294 Widget menubar_widget
;
1296 if (! FRAME_X_P (f
))
1299 menubar_widget
= f
->output_data
.x
->menubar_widget
;
1301 f
->output_data
.x
->menubar_height
= 0;
1306 /* Removing the menu bar magically changes the shell widget's x
1307 and y position of (0, 0) which, when the menu bar is turned
1308 on again, leads to pull-down menus appearing in strange
1309 positions near the upper-left corner of the display. This
1310 happens only with some window managers like twm and ctwm,
1311 but not with other like Motif's mwm or kwm, because the
1312 latter generate ConfigureNotify events when the menu bar
1313 is switched off, which fixes the shell position. */
1314 Position x0
, y0
, x1
, y1
;
1320 if (f
->output_data
.x
->widget
)
1321 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x0
, XtNy
, &y0
, NULL
);
1324 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
1325 f
->output_data
.x
->menubar_widget
= NULL
;
1327 if (f
->output_data
.x
->widget
)
1330 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x1
, XtNy
, &y1
, NULL
);
1331 if (x1
== 0 && y1
== 0)
1332 XtVaSetValues (f
->output_data
.x
->widget
, XtNx
, x0
, XtNy
, y0
, NULL
);
1334 x_set_window_size (f
, 0, FRAME_COLS (f
), FRAME_LINES (f
));
1339 #endif /* not USE_GTK */
1341 #endif /* USE_X_TOOLKIT || USE_GTK */
1343 /* xmenu_show actually displays a menu using the panes and items in menu_items
1344 and returns the value selected from it.
1345 There are two versions of xmenu_show, one for Xt and one for Xlib.
1346 Both assume input is blocked by the caller. */
1348 /* F is the frame the menu is for.
1349 X and Y are the frame-relative specified position,
1350 relative to the inside upper left corner of the frame F.
1351 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1352 KEYMAPS is 1 if this menu was specified with keymaps;
1353 in that case, we return a list containing the chosen item's value
1354 and perhaps also the pane's prefix.
1355 TITLE is the specified menu title.
1356 ERROR is a place to store an error message string in case of failure.
1357 (We return nil on failure, but the value doesn't actually matter.) */
1359 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1361 /* The item selected in the popup menu. */
1362 static Lisp_Object
*volatile menu_item_selection
;
1366 /* Used when position a popup menu. See menu_position_func and
1367 create_and_show_popup_menu below. */
1368 struct next_popup_x_y
1375 /* The menu position function to use if we are not putting a popup
1376 menu where the pointer is.
1377 MENU is the menu to pop up.
1378 X and Y shall on exit contain x/y where the menu shall pop up.
1379 PUSH_IN is not documented in the GTK manual.
1380 USER_DATA is any data passed in when calling gtk_menu_popup.
1381 Here it points to a struct next_popup_x_y where the coordinates
1382 to store in *X and *Y are as well as the frame for the popup.
1384 Here only X and Y are used. */
1386 menu_position_func (GtkMenu
*menu
, gint
*x
, gint
*y
, gboolean
*push_in
, gpointer user_data
)
1388 struct next_popup_x_y
* data
= (struct next_popup_x_y
*)user_data
;
1390 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (data
->f
);
1391 int disp_width
= x_display_pixel_width (dpyinfo
);
1392 int disp_height
= x_display_pixel_height (dpyinfo
);
1397 /* Check if there is room for the menu. If not, adjust x/y so that
1398 the menu is fully visible. */
1399 gtk_widget_get_preferred_size (GTK_WIDGET (menu
), NULL
, &req
);
1400 if (data
->x
+ req
.width
> disp_width
)
1401 *x
-= data
->x
+ req
.width
- disp_width
;
1402 if (data
->y
+ req
.height
> disp_height
)
1403 *y
-= data
->y
+ req
.height
- disp_height
;
1407 popup_selection_callback (GtkWidget
*widget
, gpointer client_data
)
1409 xg_menu_item_cb_data
*cb_data
= (xg_menu_item_cb_data
*) client_data
;
1411 if (xg_crazy_callback_abort
) return;
1412 if (cb_data
) menu_item_selection
= (Lisp_Object
*) cb_data
->call_data
;
1416 pop_down_menu (Lisp_Object arg
)
1418 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
1420 popup_activated_flag
= 0;
1422 gtk_widget_destroy (GTK_WIDGET (p
->pointer
));
1427 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1429 menu_item_selection will be set to the selection. */
1431 create_and_show_popup_menu (FRAME_PTR f
, widget_value
*first_wv
, int x
, int y
,
1432 int for_click
, Time timestamp
)
1436 GtkMenuPositionFunc pos_func
= 0; /* Pop up at pointer. */
1437 struct next_popup_x_y popup_x_y
;
1438 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1439 int use_pos_func
= ! for_click
;
1442 /* Always use position function for Gtk3. Otherwise menus may become
1443 too small to show anything. */
1447 if (! FRAME_X_P (f
))
1450 xg_crazy_callback_abort
= 1;
1451 menu
= xg_create_widget ("popup", first_wv
->name
, f
, first_wv
,
1452 G_CALLBACK (popup_selection_callback
),
1453 G_CALLBACK (popup_deactivate_callback
),
1454 G_CALLBACK (menu_highlight_callback
));
1455 xg_crazy_callback_abort
= 0;
1459 /* Not invoked by a click. pop up at x/y. */
1460 pos_func
= menu_position_func
;
1462 /* Adjust coordinates to be root-window-relative. */
1463 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
1464 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
1470 i
= 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1475 for (i
= 0; i
< 5; i
++)
1476 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
1480 /* Display the menu. */
1481 gtk_widget_show_all (menu
);
1483 gtk_menu_popup (GTK_MENU (menu
), 0, 0, pos_func
, &popup_x_y
, i
,
1484 timestamp
? timestamp
: gtk_get_current_event_time ());
1486 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
1488 if (gtk_widget_get_mapped (menu
))
1490 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1491 two. show_help_echo uses this to detect popup menus. */
1492 popup_activated_flag
= 1;
1493 /* Process events that apply to the menu. */
1494 popup_widget_loop (1, menu
);
1497 unbind_to (specpdl_count
, Qnil
);
1499 /* Must reset this manually because the button release event is not passed
1500 to Emacs event loop. */
1501 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
1504 #else /* not USE_GTK */
1506 /* We need a unique id for each widget handled by the Lucid Widget
1509 For the main windows, and popup menus, we use this counter,
1510 which we increment each time after use. This starts from 1<<16.
1512 For menu bars, we use numbers starting at 0, counted in
1513 next_menubar_widget_id. */
1514 LWLIB_ID widget_id_tick
;
1517 popup_selection_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
1519 menu_item_selection
= (Lisp_Object
*) client_data
;
1522 /* ARG is the LWLIB ID of the dialog box, represented
1523 as a Lisp object as (HIGHPART . LOWPART). */
1526 pop_down_menu (Lisp_Object arg
)
1528 LWLIB_ID id
= (XINT (XCAR (arg
)) << 4 * sizeof (LWLIB_ID
)
1529 | XINT (XCDR (arg
)));
1532 lw_destroy_all_widgets (id
);
1534 popup_activated_flag
= 0;
1539 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1541 menu_item_selection will be set to the selection. */
1543 create_and_show_popup_menu (FRAME_PTR f
, widget_value
*first_wv
,
1544 int x
, int y
, int for_click
, Time timestamp
)
1550 XButtonPressedEvent
*event
= &(dummy
.xbutton
);
1554 if (! FRAME_X_P (f
))
1558 apply_systemfont_to_menu (f
, f
->output_data
.x
->widget
);
1561 menu_id
= widget_id_tick
++;
1562 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1563 f
->output_data
.x
->widget
, 1, 0,
1564 popup_selection_callback
,
1565 popup_deactivate_callback
,
1566 menu_highlight_callback
);
1568 event
->type
= ButtonPress
;
1570 event
->send_event
= 0;
1571 event
->display
= FRAME_X_DISPLAY (f
);
1572 event
->time
= CurrentTime
;
1573 event
->root
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
1574 event
->window
= event
->subwindow
= event
->root
;
1578 /* Adjust coordinates to be root-window-relative. */
1579 x
+= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
1580 y
+= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
1587 for (i
= 0; i
< 5; i
++)
1588 if (FRAME_X_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
1591 /* Don't allow any geometry request from the user. */
1592 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1593 XtSetValues (menu
, av
, ac
);
1595 /* Display the menu. */
1596 lw_popup_menu (menu
, &dummy
);
1597 popup_activated_flag
= 1;
1598 x_activate_timeout_atimer ();
1601 int fact
= 4 * sizeof (LWLIB_ID
);
1602 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1603 record_unwind_protect (pop_down_menu
,
1604 Fcons (make_number (menu_id
>> (fact
)),
1605 make_number (menu_id
& ~(-1 << (fact
)))));
1607 /* Process events that apply to the menu. */
1608 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
), menu_id
, 1);
1610 unbind_to (specpdl_count
, Qnil
);
1614 #endif /* not USE_GTK */
1617 cleanup_widget_value_tree (Lisp_Object arg
)
1619 struct Lisp_Save_Value
*p
= XSAVE_VALUE (arg
);
1620 widget_value
*wv
= p
->pointer
;
1622 free_menubar_widget_value_tree (wv
);
1628 xmenu_show (FRAME_PTR f
, int x
, int y
, int for_click
, int keymaps
,
1629 Lisp_Object title
, const char **error_name
, Time timestamp
)
1632 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1633 widget_value
**submenu_stack
1634 = alloca (menu_items_used
* sizeof *submenu_stack
);
1635 Lisp_Object
*subprefix_stack
1636 = alloca (menu_items_used
* sizeof *subprefix_stack
);
1637 int submenu_depth
= 0;
1641 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1643 if (! FRAME_X_P (f
))
1648 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1650 *error_name
= "Empty menu";
1654 /* Create a tree of widget_value objects
1655 representing the panes and their items. */
1656 wv
= xmalloc_widget_value ();
1660 wv
->button_type
= BUTTON_TYPE_NONE
;
1665 /* Loop over all panes and items, filling in the tree. */
1667 while (i
< menu_items_used
)
1669 if (EQ (AREF (menu_items
, i
), Qnil
))
1671 submenu_stack
[submenu_depth
++] = save_wv
;
1677 else if (EQ (AREF (menu_items
, i
), Qlambda
))
1680 save_wv
= submenu_stack
[--submenu_depth
];
1684 else if (EQ (AREF (menu_items
, i
), Qt
)
1685 && submenu_depth
!= 0)
1686 i
+= MENU_ITEMS_PANE_LENGTH
;
1687 /* Ignore a nil in the item list.
1688 It's meaningful only for dialog boxes. */
1689 else if (EQ (AREF (menu_items
, i
), Qquote
))
1691 else if (EQ (AREF (menu_items
, i
), Qt
))
1693 /* Create a new pane. */
1694 Lisp_Object pane_name
, prefix
;
1695 const char *pane_string
;
1697 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
1698 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
1700 #ifndef HAVE_MULTILINGUAL_MENU
1701 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1703 pane_name
= ENCODE_MENU_STRING (pane_name
);
1704 ASET (menu_items
, i
+ MENU_ITEMS_PANE_NAME
, pane_name
);
1707 pane_string
= (NILP (pane_name
)
1708 ? "" : SSDATA (pane_name
));
1709 /* If there is just one top-level pane, put all its items directly
1710 under the top-level menu. */
1711 if (menu_items_n_panes
== 1)
1714 /* If the pane has a meaningful name,
1715 make the pane a top-level menu item
1716 with its items as a submenu beneath it. */
1717 if (!keymaps
&& strcmp (pane_string
, ""))
1719 wv
= xmalloc_widget_value ();
1723 first_wv
->contents
= wv
;
1724 wv
->name
= (char *) pane_string
;
1725 if (keymaps
&& !NILP (prefix
))
1729 wv
->button_type
= BUTTON_TYPE_NONE
;
1734 else if (first_pane
)
1740 i
+= MENU_ITEMS_PANE_LENGTH
;
1744 /* Create a new item within current pane. */
1745 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
1746 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1747 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1748 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1749 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1750 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1751 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1752 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1754 #ifndef HAVE_MULTILINGUAL_MENU
1755 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
1757 item_name
= ENCODE_MENU_STRING (item_name
);
1758 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
, item_name
);
1761 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1763 descrip
= ENCODE_MENU_STRING (descrip
);
1764 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
, descrip
);
1766 #endif /* not HAVE_MULTILINGUAL_MENU */
1768 wv
= xmalloc_widget_value ();
1772 save_wv
->contents
= wv
;
1773 wv
->name
= SSDATA (item_name
);
1774 if (!NILP (descrip
))
1775 wv
->key
= SSDATA (descrip
);
1777 /* If this item has a null value,
1778 make the call_data null so that it won't display a box
1779 when the mouse is on it. */
1780 wv
->call_data
= !NILP (def
) ? aref_addr (menu_items
, i
) : 0;
1781 wv
->enabled
= !NILP (enable
);
1784 wv
->button_type
= BUTTON_TYPE_NONE
;
1785 else if (EQ (type
, QCtoggle
))
1786 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1787 else if (EQ (type
, QCradio
))
1788 wv
->button_type
= BUTTON_TYPE_RADIO
;
1792 wv
->selected
= !NILP (selected
);
1794 if (! STRINGP (help
))
1801 i
+= MENU_ITEMS_ITEM_LENGTH
;
1805 /* Deal with the title, if it is non-nil. */
1808 widget_value
*wv_title
= xmalloc_widget_value ();
1809 widget_value
*wv_sep1
= xmalloc_widget_value ();
1810 widget_value
*wv_sep2
= xmalloc_widget_value ();
1812 wv_sep2
->name
= "--";
1813 wv_sep2
->next
= first_wv
->contents
;
1814 wv_sep2
->help
= Qnil
;
1816 wv_sep1
->name
= "--";
1817 wv_sep1
->next
= wv_sep2
;
1818 wv_sep1
->help
= Qnil
;
1820 #ifndef HAVE_MULTILINGUAL_MENU
1821 if (STRING_MULTIBYTE (title
))
1822 title
= ENCODE_MENU_STRING (title
);
1825 wv_title
->name
= SSDATA (title
);
1826 wv_title
->enabled
= TRUE
;
1827 wv_title
->button_type
= BUTTON_TYPE_NONE
;
1828 wv_title
->help
= Qnil
;
1829 wv_title
->next
= wv_sep1
;
1830 first_wv
->contents
= wv_title
;
1833 /* No selection has been chosen yet. */
1834 menu_item_selection
= 0;
1836 /* Make sure to free the widget_value objects we used to specify the
1837 contents even with longjmp. */
1838 record_unwind_protect (cleanup_widget_value_tree
,
1839 make_save_value (first_wv
, 0));
1841 /* Actually create and show the menu until popped down. */
1842 create_and_show_popup_menu (f
, first_wv
, x
, y
, for_click
, timestamp
);
1844 unbind_to (specpdl_count
, Qnil
);
1846 /* Find the selected item, and its pane, to return
1847 the proper value. */
1848 if (menu_item_selection
!= 0)
1850 Lisp_Object prefix
, entry
;
1852 prefix
= entry
= Qnil
;
1854 while (i
< menu_items_used
)
1856 if (EQ (AREF (menu_items
, i
), Qnil
))
1858 subprefix_stack
[submenu_depth
++] = prefix
;
1862 else if (EQ (AREF (menu_items
, i
), Qlambda
))
1864 prefix
= subprefix_stack
[--submenu_depth
];
1867 else if (EQ (AREF (menu_items
, i
), Qt
))
1870 = AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
1871 i
+= MENU_ITEMS_PANE_LENGTH
;
1873 /* Ignore a nil in the item list.
1874 It's meaningful only for dialog boxes. */
1875 else if (EQ (AREF (menu_items
, i
), Qquote
))
1880 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
1881 if (menu_item_selection
== aref_addr (menu_items
, i
))
1887 entry
= Fcons (entry
, Qnil
);
1889 entry
= Fcons (prefix
, entry
);
1890 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1891 if (!NILP (subprefix_stack
[j
]))
1892 entry
= Fcons (subprefix_stack
[j
], entry
);
1896 i
+= MENU_ITEMS_ITEM_LENGTH
;
1900 else if (!for_click
)
1901 /* Make "Cancel" equivalent to C-g. */
1902 Fsignal (Qquit
, Qnil
);
1909 dialog_selection_callback (GtkWidget
*widget
, gpointer client_data
)
1911 /* Treat the pointer as an integer. There's no problem
1912 as long as pointers have enough bits to hold small integers. */
1913 if ((intptr_t) client_data
!= -1)
1914 menu_item_selection
= (Lisp_Object
*) client_data
;
1916 popup_activated_flag
= 0;
1919 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1921 menu_item_selection will be set to the selection. */
1923 create_and_show_dialog (FRAME_PTR f
, widget_value
*first_wv
)
1927 if (! FRAME_X_P (f
))
1930 menu
= xg_create_widget ("dialog", first_wv
->name
, f
, first_wv
,
1931 G_CALLBACK (dialog_selection_callback
),
1932 G_CALLBACK (popup_deactivate_callback
),
1937 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1938 record_unwind_protect (pop_down_menu
, make_save_value (menu
, 0));
1940 /* Display the menu. */
1941 gtk_widget_show_all (menu
);
1943 /* Process events that apply to the menu. */
1944 popup_widget_loop (1, menu
);
1946 unbind_to (specpdl_count
, Qnil
);
1950 #else /* not USE_GTK */
1952 dialog_selection_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
1954 /* Treat the pointer as an integer. There's no problem
1955 as long as pointers have enough bits to hold small integers. */
1956 if ((intptr_t) client_data
!= -1)
1957 menu_item_selection
= (Lisp_Object
*) client_data
;
1960 lw_destroy_all_widgets (id
);
1962 popup_activated_flag
= 0;
1966 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1968 menu_item_selection will be set to the selection. */
1970 create_and_show_dialog (FRAME_PTR f
, widget_value
*first_wv
)
1977 dialog_id
= widget_id_tick
++;
1979 apply_systemfont_to_dialog (f
->output_data
.x
->widget
);
1981 lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1982 f
->output_data
.x
->widget
, 1, 0,
1983 dialog_selection_callback
, 0, 0);
1984 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1985 /* Display the dialog box. */
1986 lw_pop_up_all_widgets (dialog_id
);
1987 popup_activated_flag
= 1;
1988 x_activate_timeout_atimer ();
1990 /* Process events that apply to the dialog box.
1991 Also handle timers. */
1993 ptrdiff_t count
= SPECPDL_INDEX ();
1994 int fact
= 4 * sizeof (LWLIB_ID
);
1996 /* xdialog_show_unwind is responsible for popping the dialog box down. */
1997 record_unwind_protect (pop_down_menu
,
1998 Fcons (make_number (dialog_id
>> (fact
)),
1999 make_number (dialog_id
& ~(-1 << (fact
)))));
2001 popup_get_selection ((XEvent
*) 0, FRAME_X_DISPLAY_INFO (f
),
2004 unbind_to (count
, Qnil
);
2008 #endif /* not USE_GTK */
2010 static const char * button_names
[] = {
2011 "button1", "button2", "button3", "button4", "button5",
2012 "button6", "button7", "button8", "button9", "button10" };
2015 xdialog_show (FRAME_PTR f
,
2019 const char **error_name
)
2021 int i
, nb_buttons
=0;
2022 char dialog_name
[6];
2024 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
2026 /* Number of elements seen so far, before boundary. */
2028 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2029 int boundary_seen
= 0;
2031 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
2033 if (! FRAME_X_P (f
))
2038 if (menu_items_n_panes
> 1)
2040 *error_name
= "Multiple panes in dialog box";
2044 /* Create a tree of widget_value objects
2045 representing the text label and buttons. */
2047 Lisp_Object pane_name
, prefix
;
2048 const char *pane_string
;
2049 pane_name
= AREF (menu_items
, MENU_ITEMS_PANE_NAME
);
2050 prefix
= AREF (menu_items
, MENU_ITEMS_PANE_PREFIX
);
2051 pane_string
= (NILP (pane_name
)
2052 ? "" : SSDATA (pane_name
));
2053 prev_wv
= xmalloc_widget_value ();
2054 prev_wv
->value
= (char *) pane_string
;
2055 if (keymaps
&& !NILP (prefix
))
2057 prev_wv
->enabled
= 1;
2058 prev_wv
->name
= "message";
2059 prev_wv
->help
= Qnil
;
2062 /* Loop over all panes and items, filling in the tree. */
2063 i
= MENU_ITEMS_PANE_LENGTH
;
2064 while (i
< menu_items_used
)
2067 /* Create a new item within current pane. */
2068 Lisp_Object item_name
, enable
, descrip
;
2069 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
2070 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
2072 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
2074 if (NILP (item_name
))
2076 free_menubar_widget_value_tree (first_wv
);
2077 *error_name
= "Submenu in dialog items";
2080 if (EQ (item_name
, Qquote
))
2082 /* This is the boundary between left-side elts
2083 and right-side elts. Stop incrementing right_count. */
2088 if (nb_buttons
>= 9)
2090 free_menubar_widget_value_tree (first_wv
);
2091 *error_name
= "Too many dialog items";
2095 wv
= xmalloc_widget_value ();
2097 wv
->name
= (char *) button_names
[nb_buttons
];
2098 if (!NILP (descrip
))
2099 wv
->key
= SSDATA (descrip
);
2100 wv
->value
= SSDATA (item_name
);
2101 wv
->call_data
= aref_addr (menu_items
, i
);
2102 wv
->enabled
= !NILP (enable
);
2106 if (! boundary_seen
)
2110 i
+= MENU_ITEMS_ITEM_LENGTH
;
2113 /* If the boundary was not specified,
2114 by default put half on the left and half on the right. */
2115 if (! boundary_seen
)
2116 left_count
= nb_buttons
- nb_buttons
/ 2;
2118 wv
= xmalloc_widget_value ();
2119 wv
->name
= dialog_name
;
2122 /* Frame title: 'Q' = Question, 'I' = Information.
2123 Can also have 'E' = Error if, one day, we want
2124 a popup for errors. */
2126 dialog_name
[0] = 'Q';
2128 dialog_name
[0] = 'I';
2130 /* Dialog boxes use a really stupid name encoding
2131 which specifies how many buttons to use
2132 and how many buttons are on the right. */
2133 dialog_name
[1] = '0' + nb_buttons
;
2134 dialog_name
[2] = 'B';
2135 dialog_name
[3] = 'R';
2136 /* Number of buttons to put on the right. */
2137 dialog_name
[4] = '0' + nb_buttons
- left_count
;
2139 wv
->contents
= first_wv
;
2143 /* No selection has been chosen yet. */
2144 menu_item_selection
= 0;
2146 /* Make sure to free the widget_value objects we used to specify the
2147 contents even with longjmp. */
2148 record_unwind_protect (cleanup_widget_value_tree
,
2149 make_save_value (first_wv
, 0));
2151 /* Actually create and show the dialog. */
2152 create_and_show_dialog (f
, first_wv
);
2154 unbind_to (specpdl_count
, Qnil
);
2156 /* Find the selected item, and its pane, to return
2157 the proper value. */
2158 if (menu_item_selection
!= 0)
2164 while (i
< menu_items_used
)
2168 if (EQ (AREF (menu_items
, i
), Qt
))
2171 = AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2172 i
+= MENU_ITEMS_PANE_LENGTH
;
2174 else if (EQ (AREF (menu_items
, i
), Qquote
))
2176 /* This is the boundary between left-side elts and
2183 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
2184 if (menu_item_selection
== aref_addr (menu_items
, i
))
2188 entry
= Fcons (entry
, Qnil
);
2190 entry
= Fcons (prefix
, entry
);
2194 i
+= MENU_ITEMS_ITEM_LENGTH
;
2199 /* Make "Cancel" equivalent to C-g. */
2200 Fsignal (Qquit
, Qnil
);
2205 #else /* not USE_X_TOOLKIT && not USE_GTK */
2207 /* The frame of the last activated non-toolkit menu bar.
2208 Used to generate menu help events. */
2210 static struct frame
*menu_help_frame
;
2213 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2215 PANE is the pane number, and ITEM is the menu item number in
2216 the menu (currently not used).
2218 This cannot be done with generating a HELP_EVENT because
2219 XMenuActivate contains a loop that doesn't let Emacs process
2223 menu_help_callback (char const *help_string
, int pane
, int item
)
2225 Lisp_Object
*first_item
;
2226 Lisp_Object pane_name
;
2227 Lisp_Object menu_object
;
2229 first_item
= XVECTOR (menu_items
)->contents
;
2230 if (EQ (first_item
[0], Qt
))
2231 pane_name
= first_item
[MENU_ITEMS_PANE_NAME
];
2232 else if (EQ (first_item
[0], Qquote
))
2233 /* This shouldn't happen, see xmenu_show. */
2234 pane_name
= empty_unibyte_string
;
2236 pane_name
= first_item
[MENU_ITEMS_ITEM_NAME
];
2238 /* (menu-item MENU-NAME PANE-NUMBER) */
2239 menu_object
= Fcons (Qmenu_item
,
2241 Fcons (make_number (pane
), Qnil
)));
2242 show_help_echo (help_string
? build_string (help_string
) : Qnil
,
2243 Qnil
, menu_object
, make_number (item
));
2247 pop_down_menu (Lisp_Object arg
)
2249 struct Lisp_Save_Value
*p1
= XSAVE_VALUE (Fcar (arg
));
2250 struct Lisp_Save_Value
*p2
= XSAVE_VALUE (Fcdr (arg
));
2252 FRAME_PTR f
= p1
->pointer
;
2253 XMenu
*menu
= p2
->pointer
;
2257 XUngrabPointer (FRAME_X_DISPLAY (f
), CurrentTime
);
2258 XUngrabKeyboard (FRAME_X_DISPLAY (f
), CurrentTime
);
2260 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2262 #ifdef HAVE_X_WINDOWS
2263 /* Assume the mouse has moved out of the X window.
2264 If it has actually moved in, we will get an EnterNotify. */
2265 x_mouse_leave (FRAME_X_DISPLAY_INFO (f
));
2267 /* State that no mouse buttons are now held.
2268 (The oldXMenu code doesn't track this info for us.)
2269 That is not necessarily true, but the fiction leads to reasonable
2270 results, and it is a pain to ask which are actually held now. */
2271 FRAME_X_DISPLAY_INFO (f
)->grabbed
= 0;
2273 #endif /* HAVE_X_WINDOWS */
2282 xmenu_show (FRAME_PTR f
, int x
, int y
, int for_click
, int keymaps
,
2283 Lisp_Object title
, const char **error_name
, Time timestamp
)
2287 int pane
, selidx
, lpane
, status
;
2288 Lisp_Object entry
, pane_prefix
;
2290 int ulx
, uly
, width
, height
;
2291 int dispwidth
, dispheight
;
2292 int i
, j
, lines
, maxlines
;
2295 unsigned int dummy_uint
;
2296 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
2298 if (! FRAME_X_P (f
) && ! FRAME_MSDOS_P (f
))
2302 if (menu_items_n_panes
== 0)
2305 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2307 *error_name
= "Empty menu";
2311 /* Figure out which root window F is on. */
2312 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2313 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2314 &dummy_uint
, &dummy_uint
);
2316 /* Make the menu on that window. */
2317 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2320 *error_name
= "Can't create menu";
2324 /* Don't GC while we prepare and show the menu,
2325 because we give the oldxmenu library pointers to the
2326 contents of strings. */
2327 inhibit_garbage_collection ();
2329 #ifdef HAVE_X_WINDOWS
2330 /* Adjust coordinates to relative to the outer (window manager) window. */
2331 x
+= FRAME_OUTER_TO_INNER_DIFF_X (f
);
2332 y
+= FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2333 #endif /* HAVE_X_WINDOWS */
2335 /* Adjust coordinates to be root-window-relative. */
2339 /* Create all the necessary panes and their items. */
2340 maxwidth
= maxlines
= lines
= i
= 0;
2342 while (i
< menu_items_used
)
2344 if (EQ (AREF (menu_items
, i
), Qt
))
2346 /* Create a new pane. */
2347 Lisp_Object pane_name
, prefix
;
2348 const char *pane_string
;
2350 maxlines
= max (maxlines
, lines
);
2352 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
2353 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2354 pane_string
= (NILP (pane_name
)
2355 ? "" : SSDATA (pane_name
));
2356 if (keymaps
&& !NILP (prefix
))
2359 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, TRUE
);
2360 if (lpane
== XM_FAILURE
)
2362 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2363 *error_name
= "Can't create pane";
2366 i
+= MENU_ITEMS_PANE_LENGTH
;
2368 /* Find the width of the widest item in this pane. */
2370 while (j
< menu_items_used
)
2373 item
= AREF (menu_items
, j
);
2381 width
= SBYTES (item
);
2382 if (width
> maxwidth
)
2385 j
+= MENU_ITEMS_ITEM_LENGTH
;
2388 /* Ignore a nil in the item list.
2389 It's meaningful only for dialog boxes. */
2390 else if (EQ (AREF (menu_items
, i
), Qquote
))
2394 /* Create a new item within current pane. */
2395 Lisp_Object item_name
, enable
, descrip
, help
;
2397 char const *help_string
;
2399 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
2400 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
2402 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
2403 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
2404 help_string
= STRINGP (help
) ? SSDATA (help
) : NULL
;
2406 if (!NILP (descrip
))
2408 /* if alloca is fast, use that to make the space,
2409 to reduce gc needs. */
2410 item_data
= alloca (maxwidth
+ SBYTES (descrip
) + 1);
2411 memcpy (item_data
, SSDATA (item_name
), SBYTES (item_name
));
2412 for (j
= SCHARS (item_name
); j
< maxwidth
; j
++)
2414 memcpy (item_data
+ j
, SSDATA (descrip
), SBYTES (descrip
));
2415 item_data
[j
+ SBYTES (descrip
)] = 0;
2418 item_data
= SSDATA (item_name
);
2420 if (lpane
== XM_FAILURE
2421 || (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2422 menu
, lpane
, 0, item_data
,
2423 !NILP (enable
), help_string
)
2426 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2427 *error_name
= "Can't add selection to menu";
2430 i
+= MENU_ITEMS_ITEM_LENGTH
;
2435 maxlines
= max (maxlines
, lines
);
2437 /* All set and ready to fly. */
2438 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2439 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
2440 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
2441 x
= min (x
, dispwidth
);
2442 y
= min (y
, dispheight
);
2445 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2446 &ulx
, &uly
, &width
, &height
);
2447 if (ulx
+width
> dispwidth
)
2449 x
-= (ulx
+ width
) - dispwidth
;
2450 ulx
= dispwidth
- width
;
2452 if (uly
+height
> dispheight
)
2454 y
-= (uly
+ height
) - dispheight
;
2455 uly
= dispheight
- height
;
2457 #ifndef HAVE_X_WINDOWS
2458 if (FRAME_HAS_MINIBUF_P (f
) && uly
+height
> dispheight
- 1)
2460 /* Move the menu away of the echo area, to avoid overwriting the
2461 menu with help echo messages or vice versa. */
2462 if (BUFFERP (echo_area_buffer
[0]) && WINDOWP (echo_area_window
))
2464 y
-= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window
));
2465 uly
-= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window
));
2474 if (ulx
< 0) x
-= ulx
;
2475 if (uly
< 0) y
-= uly
;
2479 /* If position was not given by a mouse click, adjust so upper left
2480 corner of the menu as a whole ends up at given coordinates. This
2481 is what x-popup-menu says in its documentation. */
2483 y
+= 1.5*height
/(maxlines
+2);
2486 XMenuSetAEQ (menu
, TRUE
);
2487 XMenuSetFreeze (menu
, TRUE
);
2491 XMenuActivateSetWaitFunction (x_menu_wait_for_event
, FRAME_X_DISPLAY (f
));
2494 record_unwind_protect (pop_down_menu
,
2495 Fcons (make_save_value (f
, 0),
2496 make_save_value (menu
, 0)));
2498 /* Help display under X won't work because XMenuActivate contains
2499 a loop that doesn't give Emacs a chance to process it. */
2500 menu_help_frame
= f
;
2501 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2502 x
, y
, ButtonReleaseMask
, &datap
,
2503 menu_help_callback
);
2504 entry
= pane_prefix
= Qnil
;
2510 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2513 /* Find the item number SELIDX in pane number PANE. */
2515 while (i
< menu_items_used
)
2517 if (EQ (AREF (menu_items
, i
), Qt
))
2521 = AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2523 i
+= MENU_ITEMS_PANE_LENGTH
;
2532 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
2535 entry
= Fcons (entry
, Qnil
);
2536 if (!NILP (pane_prefix
))
2537 entry
= Fcons (pane_prefix
, entry
);
2543 i
+= MENU_ITEMS_ITEM_LENGTH
;
2549 *error_name
= "Can't activate menu";
2553 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2554 the menu was invoked with a mouse event as POSITION). */
2556 Fsignal (Qquit
, Qnil
);
2560 unbind_to (specpdl_count
, Qnil
);
2565 #endif /* not USE_X_TOOLKIT */
2567 #endif /* HAVE_MENUS */
2570 /* Detect if a dialog or menu has been posted. MSDOS has its own
2571 implementation on msdos.c. */
2574 popup_activated (void)
2576 return popup_activated_flag
;
2578 #endif /* not MSDOS */
2580 /* The following is used by delayed window autoselection. */
2582 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p
, Smenu_or_popup_active_p
, 0, 0, 0,
2583 doc
: /* Return t if a menu or popup dialog is active. */)
2587 return (popup_activated ()) ? Qt
: Qnil
;
2590 #endif /* HAVE_MENUS */
2594 syms_of_xmenu (void)
2596 DEFSYM (Qdebug_on_next_call
, "debug-on-next-call");
2598 #ifdef USE_X_TOOLKIT
2599 widget_id_tick
= (1<<16);
2600 next_menubar_widget_id
= 1;
2603 defsubr (&Smenu_or_popup_active_p
);
2605 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
2606 defsubr (&Sx_menu_bar_open_internal
);
2607 Ffset (intern_c_string ("accelerate-menu"),
2608 intern_c_string (Sx_menu_bar_open_internal
.symbol_name
));
2612 defsubr (&Sx_popup_dialog
);