1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2016 Free Software
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 (at
11 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. */
41 #include "termhooks.h"
43 #include "blockinput.h"
46 #include "sysselect.h"
53 /* This may include sys/types.h, and that somehow loses
54 if this is not done before the other system files. */
58 /* Load sys/types.h if not already loaded.
59 In some systems loading it twice is suicidal. */
61 #include <sys/types.h>
65 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
66 code accepts the Emacs internal encoding. */
67 #undef HAVE_MULTILINGUAL_MENU
71 #include <X11/IntrinsicP.h>
72 #include <X11/CoreP.h>
73 #include <X11/StringDefs.h>
74 #include <X11/Shell.h>
76 #include "xsettings.h"
77 #include "../lwlib/xlwmenu.h"
79 #include <X11/Xaw3d/Paned.h>
80 #else /* !HAVE_XAW3D */
81 #include <X11/Xaw/Paned.h>
82 #endif /* HAVE_XAW3D */
83 #endif /* USE_LUCID */
85 #include "../lwlib/lwlib.h"
87 #else /* not USE_X_TOOLKIT */
89 #include "../oldXMenu/XMenu.h"
91 #endif /* not USE_X_TOOLKIT */
92 #endif /* HAVE_X_WINDOWS */
104 /* Flag which when set indicates a dialog or menu has been posted by
105 Xt on behalf of one of the widget sets. */
106 static int popup_activated_flag
;
111 static LWLIB_ID next_menubar_widget_id
;
113 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
115 static struct frame
*
116 menubar_id_to_frame (LWLIB_ID id
)
118 Lisp_Object tail
, frame
;
121 FOR_EACH_FRAME (tail
, frame
)
124 if (!FRAME_WINDOW_P (f
))
126 if (f
->output_data
.x
->id
== id
)
136 #if defined USE_GTK || defined USE_MOTIF
138 /* Set menu_items_inuse so no other popup menu or dialog is created. */
141 x_menu_set_in_use (bool in_use
)
143 menu_items_inuse
= in_use
? Qt
: Qnil
;
144 popup_activated_flag
= in_use
;
146 if (popup_activated_flag
)
147 x_activate_timeout_atimer ();
153 /* Wait for an X event to arrive or for a timer to expire. */
156 x_menu_wait_for_event (void *data
)
158 /* Another way to do this is to register a timer callback, that can be
159 done in GTK and Xt. But we have to do it like this when using only X
160 anyway, and with callbacks we would have three variants for timer handling
161 instead of the small ifdefs below. */
165 ! XtAppPending (Xt_app_con
)
166 #elif defined USE_GTK
167 ! gtk_events_pending ()
173 struct timespec next_time
= timer_check (), *ntp
;
175 struct x_display_info
*dpyinfo
;
179 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
181 int fd
= ConnectionNumber (dpyinfo
->display
);
182 FD_SET (fd
, &read_fds
);
184 XFlush (dpyinfo
->display
);
187 if (! timespec_valid_p (next_time
))
192 #if defined USE_GTK && defined HAVE_GTK3
193 /* Gtk3 have arrows on menus when they don't fit. When the
194 pointer is over an arrow, a timeout scrolls it a bit. Use
195 xg_select so that timeout gets triggered. */
196 xg_select (n
+ 1, &read_fds
, NULL
, NULL
, ntp
, NULL
);
198 pselect (n
+ 1, &read_fds
, NULL
, NULL
, ntp
, NULL
);
205 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
209 /* Loop in Xt until the menu pulldown or dialog popup has been
210 popped down (deactivated). This is used for x-popup-menu
211 and x-popup-dialog; it is not used for the menu bar.
213 NOTE: All calls to popup_get_selection should be protected
214 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
217 popup_get_selection (XEvent
*initial_event
, struct x_display_info
*dpyinfo
,
218 LWLIB_ID id
, bool do_timers
)
222 while (popup_activated_flag
)
226 event
= *initial_event
;
231 if (do_timers
) x_menu_wait_for_event (0);
232 XtAppNextEvent (Xt_app_con
, &event
);
235 /* Make sure we don't consider buttons grabbed after menu goes.
236 And make sure to deactivate for any ButtonRelease,
237 even if XtDispatchEvent doesn't do that. */
238 if (event
.type
== ButtonRelease
239 && dpyinfo
->display
== event
.xbutton
.display
)
241 dpyinfo
->grabbed
&= ~(1 << event
.xbutton
.button
);
242 #ifdef USE_MOTIF /* Pretending that the event came from a
243 Btn1Down seems the only way to convince Motif to
244 activate its callbacks; setting the XmNmenuPost
245 isn't working. --marcus@sysc.pdx.edu. */
246 event
.xbutton
.button
= 1;
247 /* Motif only pops down menus when no Ctrl, Alt or Mod
248 key is pressed and the button is released. So reset key state
249 so Motif thinks this is the case. */
250 event
.xbutton
.state
= 0;
253 /* Pop down on C-g and Escape. */
254 else if (event
.type
== KeyPress
255 && dpyinfo
->display
== event
.xbutton
.display
)
257 KeySym keysym
= XLookupKeysym (&event
.xkey
, 0);
259 if ((keysym
== XK_g
&& (event
.xkey
.state
& ControlMask
) != 0)
260 || keysym
== XK_Escape
) /* Any escape, ignore modifiers. */
261 popup_activated_flag
= 0;
264 x_dispatch_event (&event
, event
.xany
.display
);
268 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal
, Sx_menu_bar_open_internal
, 0, 1, "i",
269 doc
: /* Start key navigation of the menu bar in FRAME.
270 This initially opens the first menu bar item and you can then navigate with the
271 arrow keys, select a menu entry with the return key or cancel with the
272 escape key. If FRAME has no menu bar this function does nothing.
274 If FRAME is nil or not given, use the selected frame. */)
278 struct frame
*f
= decode_window_system_frame (frame
);
282 if (FRAME_EXTERNAL_MENU_BAR (f
))
283 set_frame_menubar (f
, false, true);
285 menubar
= FRAME_X_OUTPUT (f
)->menubar_widget
;
289 bool error_p
= false;
291 x_catch_errors (FRAME_X_DISPLAY (f
));
292 memset (&ev
, 0, sizeof ev
);
293 ev
.xbutton
.display
= FRAME_X_DISPLAY (f
);
294 ev
.xbutton
.window
= XtWindow (menubar
);
295 ev
.xbutton
.root
= FRAME_DISPLAY_INFO (f
)->root_window
;
296 ev
.xbutton
.time
= XtLastTimestampProcessed (FRAME_X_DISPLAY (f
));
297 ev
.xbutton
.button
= Button1
;
298 ev
.xbutton
.x
= ev
.xbutton
.y
= FRAME_MENUBAR_HEIGHT (f
) / 2;
299 ev
.xbutton
.same_screen
= True
;
306 XtSetArg (al
[0], XtNchildren
, &list
);
307 XtSetArg (al
[1], XtNnumChildren
, &nr
);
308 XtGetValues (menubar
, al
, 2);
309 ev
.xbutton
.window
= XtWindow (list
[0]);
313 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
314 /* From-window, to-window. */
315 ev
.xbutton
.window
, ev
.xbutton
.root
,
317 /* From-position, to-position. */
318 ev
.xbutton
.x
, ev
.xbutton
.y
,
319 &ev
.xbutton
.x_root
, &ev
.xbutton
.y_root
,
323 error_p
= x_had_errors_p (FRAME_X_DISPLAY (f
));
324 x_uncatch_errors_after_check ();
328 ev
.type
= ButtonPress
;
329 ev
.xbutton
.state
= 0;
331 XtDispatchEvent (&ev
);
332 ev
.xbutton
.type
= ButtonRelease
;
333 ev
.xbutton
.state
= Button1Mask
;
334 XtDispatchEvent (&ev
);
342 #endif /* USE_X_TOOLKIT */
346 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal
, Sx_menu_bar_open_internal
, 0, 1, "i",
347 doc
: /* Start key navigation of the menu bar in FRAME.
348 This initially opens the first menu bar item and you can then navigate with the
349 arrow keys, select a menu entry with the return key or cancel with the
350 escape key. If FRAME has no menu bar this function does nothing.
352 If FRAME is nil or not given, use the selected frame. */)
359 f
= decode_window_system_frame (frame
);
361 if (FRAME_EXTERNAL_MENU_BAR (f
))
362 set_frame_menubar (f
, false, true);
364 menubar
= FRAME_X_OUTPUT (f
)->menubar_widget
;
367 /* Activate the first menu. */
368 GList
*children
= gtk_container_get_children (GTK_CONTAINER (menubar
));
372 g_signal_emit_by_name (children
->data
, "activate_item");
373 popup_activated_flag
= 1;
374 g_list_free (children
);
382 /* Loop util popup_activated_flag is set to zero in a callback.
383 Used for popup menus and dialogs. */
386 popup_widget_loop (bool do_timers
, GtkWidget
*widget
)
388 ++popup_activated_flag
;
390 /* Process events in the Gtk event loop until done. */
391 while (popup_activated_flag
)
393 if (do_timers
) x_menu_wait_for_event (0);
394 gtk_main_iteration ();
399 /* Activate the menu bar of frame F.
400 This is called from keyboard.c when it gets the
401 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
403 To activate the menu bar, we use the X button-press event
404 that was saved in saved_menu_event.
405 That makes the toolkit do its thing.
407 But first we recompute the menu bar contents (the whole tree).
409 The reason for saving the button event until here, instead of
410 passing it to the toolkit right away, is that we can safely
411 execute Lisp code. */
414 x_activate_menubar (struct frame
*f
)
416 eassert (FRAME_X_P (f
));
418 if (!f
->output_data
.x
->saved_menu_event
->type
)
422 if (! xg_win_to_widget (FRAME_X_DISPLAY (f
),
423 f
->output_data
.x
->saved_menu_event
->xany
.window
))
427 set_frame_menubar (f
, false, true);
429 popup_activated_flag
= 1;
431 XPutBackEvent (f
->output_data
.x
->display_info
->display
,
432 f
->output_data
.x
->saved_menu_event
);
434 XtDispatchEvent (f
->output_data
.x
->saved_menu_event
);
438 /* Ignore this if we get it a second time. */
439 f
->output_data
.x
->saved_menu_event
->type
= 0;
442 /* This callback is invoked when the user selects a menubar cascade
443 pushbutton, but before the pulldown menu is posted. */
447 popup_activate_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
449 popup_activated_flag
= 1;
450 x_activate_timeout_atimer ();
454 /* This callback is invoked when a dialog or menu is finished being
455 used and has been unposted. */
458 popup_deactivate_callback (
460 GtkWidget
*widget
, gpointer client_data
462 Widget widget
, LWLIB_ID id
, XtPointer client_data
466 popup_activated_flag
= 0;
470 /* Function that finds the frame for WIDGET and shows the HELP text
472 F is the frame if known, or NULL if not known. */
474 show_help_event (struct frame
*f
, xt_or_gtk_widget widget
, Lisp_Object help
)
480 XSETFRAME (frame
, f
);
481 kbd_buffer_store_help_event (frame
, help
);
484 show_help_echo (help
, Qnil
, Qnil
, Qnil
);
487 /* Callback called when menu items are highlighted/unhighlighted
488 while moving the mouse over them. WIDGET is the menu bar or menu
489 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
490 the data structure for the menu item, or null in case of
495 menu_highlight_callback (GtkWidget
*widget
, gpointer call_data
)
497 xg_menu_item_cb_data
*cb_data
;
500 cb_data
= g_object_get_data (G_OBJECT (widget
), XG_ITEM_DATA
);
501 if (! cb_data
) return;
503 help
= call_data
? cb_data
->help
: Qnil
;
505 /* If popup_activated_flag is greater than 1 we are in a popup menu.
506 Don't pass the frame to show_help_event for those.
507 Passing frame creates an Emacs event. As we are looping in
508 popup_widget_loop, it won't be handled. Passing NULL shows the tip
509 directly without using an Emacs event. This is what the Lucid code
511 show_help_event (popup_activated_flag
<= 1 ? cb_data
->cl_data
->f
: NULL
,
516 menu_highlight_callback (Widget widget
, LWLIB_ID id
, void *call_data
)
518 widget_value
*wv
= call_data
;
519 Lisp_Object help
= wv
? wv
->help
: Qnil
;
521 /* Determine the frame for the help event. */
522 struct frame
*f
= menubar_id_to_frame (id
);
524 show_help_event (f
, widget
, help
);
529 /* Gtk calls callbacks just because we tell it what item should be
530 selected in a radio group. If this variable is set to a non-zero
531 value, we are creating menus and don't want callbacks right now.
533 static bool xg_crazy_callback_abort
;
535 /* This callback is called from the menu bar pulldown menu
536 when the user makes a selection.
537 Figure out what the user chose
538 and put the appropriate events into the keyboard buffer. */
540 menubar_selection_callback (GtkWidget
*widget
, gpointer client_data
)
542 xg_menu_item_cb_data
*cb_data
= client_data
;
544 if (xg_crazy_callback_abort
)
547 if (! cb_data
|| ! cb_data
->cl_data
|| ! cb_data
->cl_data
->f
)
550 /* For a group of radio buttons, GTK calls the selection callback first
551 for the item that was active before the selection and then for the one that
552 is active after the selection. For C-h k this means we get the help on
553 the deselected item and then the selected item is executed. Prevent that
554 by ignoring the non-active item. */
555 if (GTK_IS_RADIO_MENU_ITEM (widget
)
556 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget
)))
559 /* When a menu is popped down, X generates a focus event (i.e. focus
560 goes back to the frame below the menu). Since GTK buffers events,
561 we force it out here before the menu selection event. Otherwise
562 sit-for will exit at once if the focus event follows the menu selection
566 while (gtk_events_pending ())
567 gtk_main_iteration ();
570 find_and_call_menu_selection (cb_data
->cl_data
->f
,
571 cb_data
->cl_data
->menu_bar_items_used
,
572 cb_data
->cl_data
->menu_bar_vector
,
576 #else /* not USE_GTK */
578 /* This callback is called from the menu bar pulldown menu
579 when the user makes a selection.
580 Figure out what the user chose
581 and put the appropriate events into the keyboard buffer. */
583 menubar_selection_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
587 f
= menubar_id_to_frame (id
);
590 find_and_call_menu_selection (f
, f
->menu_bar_items_used
,
591 f
->menu_bar_vector
, client_data
);
593 #endif /* not USE_GTK */
595 /* Recompute all the widgets of frame F, when the menu bar has been
599 update_frame_menubar (struct frame
*f
)
602 xg_update_frame_menubar (f
);
606 eassert (FRAME_X_P (f
));
608 x
= f
->output_data
.x
;
610 if (!x
->menubar_widget
|| XtIsManaged (x
->menubar_widget
))
615 /* Do the voodoo which means "I'm changing lots of things, don't try
616 to refigure sizes until I'm done." */
617 lw_refigure_widget (x
->column_widget
, False
);
619 /* The order in which children are managed is the top to bottom
620 order in which they are displayed in the paned window. First,
621 remove the text-area widget. */
622 XtUnmanageChild (x
->edit_widget
);
624 /* Remove the menubar that is there now, and put up the menubar that
626 XtManageChild (x
->menubar_widget
);
627 XtMapWidget (x
->menubar_widget
);
628 XtVaSetValues (x
->menubar_widget
, XtNmappedWhenManaged
, 1, NULL
);
630 /* Re-manage the text-area widget, and then thrash the sizes. */
631 XtManageChild (x
->edit_widget
);
632 lw_refigure_widget (x
->column_widget
, True
);
634 /* Force the pane widget to resize itself. */
635 adjust_frame_size (f
, -1, -1, 2, false, Qupdate_frame_menubar
);
642 apply_systemfont_to_dialog (Widget w
)
644 const char *fn
= xsettings_get_system_normal_font ();
647 XrmDatabase db
= XtDatabase (XtDisplay (w
));
649 XrmPutStringResource (&db
, "*dialog.font", fn
);
654 apply_systemfont_to_menu (struct frame
*f
, Widget w
)
656 const char *fn
= xsettings_get_system_normal_font ();
660 XrmDatabase db
= XtDatabase (XtDisplay (w
));
663 XrmPutStringResource (&db
, "*menubar*font", fn
);
664 XrmPutStringResource (&db
, "*popup*font", fn
);
671 /* Set the contents of the menubar widgets of frame F.
672 The argument FIRST_TIME is currently ignored;
673 it is set the first time this is called, from initialize_frame_menubar. */
676 set_frame_menubar (struct frame
*f
, bool first_time
, bool deep_p
)
678 xt_or_gtk_widget menubar_widget
, old_widget
;
683 widget_value
*wv
, *first_wv
, *prev_wv
= 0;
685 int *submenu_start
, *submenu_end
;
686 bool *submenu_top_level_items
;
687 int *submenu_n_panes
;
689 eassert (FRAME_X_P (f
));
691 menubar_widget
= old_widget
= f
->output_data
.x
->menubar_widget
;
693 XSETFRAME (Vmenu_updating_frame
, f
);
696 if (f
->output_data
.x
->id
== 0)
697 f
->output_data
.x
->id
= next_menubar_widget_id
++;
698 id
= f
->output_data
.x
->id
;
701 if (! menubar_widget
)
703 /* Make the first call for any given frame always go deep. */
704 else if (!f
->output_data
.x
->saved_menu_event
&& !deep_p
)
707 f
->output_data
.x
->saved_menu_event
= xmalloc (sizeof (XEvent
));
708 f
->output_data
.x
->saved_menu_event
->type
= 0;
713 /* Make a widget-value tree representing the entire menu trees. */
715 struct buffer
*prev
= current_buffer
;
717 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
718 int previous_menu_items_used
= f
->menu_bar_items_used
;
719 Lisp_Object
*previous_items
720 = alloca (previous_menu_items_used
* sizeof *previous_items
);
723 /* If we are making a new widget, its contents are empty,
724 do always reinitialize them. */
725 if (! menubar_widget
)
726 previous_menu_items_used
= 0;
728 buffer
= XWINDOW (FRAME_SELECTED_WINDOW (f
))->contents
;
729 specbind (Qinhibit_quit
, Qt
);
730 /* Don't let the debugger step into this code
731 because it is not reentrant. */
732 specbind (Qdebug_on_next_call
, Qnil
);
734 record_unwind_save_match_data ();
735 if (NILP (Voverriding_local_map_menu_flag
))
737 specbind (Qoverriding_terminal_local_map
, Qnil
);
738 specbind (Qoverriding_local_map
, Qnil
);
741 set_buffer_internal_1 (XBUFFER (buffer
));
743 /* Run the Lucid hook. */
744 safe_run_hooks (Qactivate_menubar_hook
);
746 /* If it has changed current-menubar from previous value,
747 really recompute the menubar from the value. */
748 if (! NILP (Vlucid_menu_bar_dirty_flag
))
749 call0 (Qrecompute_lucid_menubar
);
750 safe_run_hooks (Qmenu_bar_update_hook
);
751 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
753 items
= FRAME_MENU_BAR_ITEMS (f
);
755 /* Save the frame's previous menu bar contents data. */
756 if (previous_menu_items_used
)
757 memcpy (previous_items
, XVECTOR (f
->menu_bar_vector
)->contents
,
758 previous_menu_items_used
* word_size
);
760 /* Fill in menu_items with the current menu bar contents.
761 This can evaluate Lisp code. */
764 menu_items
= f
->menu_bar_vector
;
765 menu_items_allocated
= VECTORP (menu_items
) ? ASIZE (menu_items
) : 0;
766 subitems
= ASIZE (items
) / 4;
767 submenu_start
= alloca ((subitems
+ 1) * sizeof *submenu_start
);
768 submenu_end
= alloca (subitems
* sizeof *submenu_end
);
769 submenu_n_panes
= alloca (subitems
* sizeof *submenu_n_panes
);
770 submenu_top_level_items
= alloca (subitems
771 * sizeof *submenu_top_level_items
);
773 for (i
= 0; i
< subitems
; i
++)
775 Lisp_Object key
, string
, maps
;
777 key
= AREF (items
, 4 * i
);
778 string
= AREF (items
, 4 * i
+ 1);
779 maps
= AREF (items
, 4 * i
+ 2);
783 submenu_start
[i
] = menu_items_used
;
785 menu_items_n_panes
= 0;
786 submenu_top_level_items
[i
]
787 = parse_single_submenu (key
, string
, maps
);
788 submenu_n_panes
[i
] = menu_items_n_panes
;
790 submenu_end
[i
] = menu_items_used
;
793 submenu_start
[i
] = -1;
794 finish_menu_items ();
796 /* Convert menu_items into widget_value trees
797 to display the menu. This cannot evaluate Lisp code. */
799 wv
= make_widget_value ("menubar", NULL
, true, Qnil
);
800 wv
->button_type
= BUTTON_TYPE_NONE
;
803 for (i
= 0; submenu_start
[i
] >= 0; i
++)
805 menu_items_n_panes
= submenu_n_panes
[i
];
806 wv
= digest_single_submenu (submenu_start
[i
], submenu_end
[i
],
807 submenu_top_level_items
[i
]);
811 first_wv
->contents
= wv
;
812 /* Don't set wv->name here; GC during the loop might relocate it. */
814 wv
->button_type
= BUTTON_TYPE_NONE
;
818 set_buffer_internal_1 (prev
);
820 /* If there has been no change in the Lisp-level contents
821 of the menu bar, skip redisplaying it. Just exit. */
823 /* Compare the new menu items with the ones computed last time. */
824 for (i
= 0; i
< previous_menu_items_used
; i
++)
825 if (menu_items_used
== i
826 || (!EQ (previous_items
[i
], AREF (menu_items
, i
))))
828 if (i
== menu_items_used
&& i
== previous_menu_items_used
&& i
!= 0)
830 /* The menu items have not changed. Don't bother updating
831 the menus in any form, since it would be a no-op. */
832 free_menubar_widget_value_tree (first_wv
);
833 discard_menu_items ();
834 unbind_to (specpdl_count
, Qnil
);
838 /* The menu items are different, so store them in the frame. */
839 fset_menu_bar_vector (f
, menu_items
);
840 f
->menu_bar_items_used
= menu_items_used
;
842 /* This undoes save_menu_items. */
843 unbind_to (specpdl_count
, Qnil
);
845 /* Now GC cannot happen during the lifetime of the widget_value,
846 so it's safe to store data from a Lisp_String. */
847 wv
= first_wv
->contents
;
848 for (i
= 0; i
< ASIZE (items
); i
+= 4)
851 string
= AREF (items
, i
+ 1);
854 wv
->name
= SSDATA (string
);
855 update_submenu_strings (wv
->contents
);
862 /* Make a widget-value tree containing
863 just the top level menu bar strings. */
865 wv
= make_widget_value ("menubar", NULL
, true, Qnil
);
866 wv
->button_type
= BUTTON_TYPE_NONE
;
869 items
= FRAME_MENU_BAR_ITEMS (f
);
870 for (i
= 0; i
< ASIZE (items
); i
+= 4)
874 string
= AREF (items
, i
+ 1);
878 wv
= make_widget_value (SSDATA (string
), NULL
, true, Qnil
);
879 wv
->button_type
= BUTTON_TYPE_NONE
;
880 /* This prevents lwlib from assuming this
881 menu item is really supposed to be empty. */
882 /* The intptr_t cast avoids a warning.
883 This value just has to be different from small integers. */
884 wv
->call_data
= (void *) (intptr_t) (-1);
889 first_wv
->contents
= wv
;
893 /* Forget what we thought we knew about what is in the
894 detailed contents of the menu bar menus.
895 Changing the top level always destroys the contents. */
896 f
->menu_bar_items_used
= 0;
899 /* Create or update the menu bar widget. */
904 xg_crazy_callback_abort
= true;
907 /* The fourth arg is DEEP_P, which says to consider the entire
908 menu trees we supply, rather than just the menu bar item names. */
909 xg_modify_menubar_widgets (menubar_widget
,
913 G_CALLBACK (menubar_selection_callback
),
914 G_CALLBACK (popup_deactivate_callback
),
915 G_CALLBACK (menu_highlight_callback
));
920 = xg_create_widget ("menubar", "menubar", f
, first_wv
,
921 G_CALLBACK (menubar_selection_callback
),
922 G_CALLBACK (popup_deactivate_callback
),
923 G_CALLBACK (menu_highlight_callback
));
925 f
->output_data
.x
->menubar_widget
= menubar_widget
;
929 #else /* not USE_GTK */
932 /* Disable resizing (done for Motif!) */
933 lw_allow_resizing (f
->output_data
.x
->widget
, False
);
935 /* The third arg is DEEP_P, which says to consider the entire
936 menu trees we supply, rather than just the menu bar item names. */
937 lw_modify_all_widgets (id
, first_wv
, deep_p
);
939 /* Re-enable the edit widget to resize. */
940 lw_allow_resizing (f
->output_data
.x
->widget
, True
);
944 char menuOverride
[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
945 XtTranslations override
= XtParseTranslationTable (menuOverride
);
948 apply_systemfont_to_menu (f
, f
->output_data
.x
->column_widget
);
950 menubar_widget
= lw_create_widget ("menubar", "menubar", id
,
952 f
->output_data
.x
->column_widget
,
954 popup_activate_callback
,
955 menubar_selection_callback
,
956 popup_deactivate_callback
,
957 menu_highlight_callback
);
958 f
->output_data
.x
->menubar_widget
= menubar_widget
;
960 /* Make menu pop down on C-g. */
961 XtOverrideTranslations (menubar_widget
, override
);
966 if (f
->output_data
.x
->menubar_widget
)
967 XtRealizeWidget (f
->output_data
.x
->menubar_widget
);
970 = (f
->output_data
.x
->menubar_widget
971 ? (f
->output_data
.x
->menubar_widget
->core
.height
973 /* Damn me... With Lucid I get a core.border_width of 1
974 only the first time this is called and an ibw of 1 every
975 time this is called. So the first time this is called I
976 was off by one. Fix that here by never adding
977 core.border_width for Lucid. */
978 + f
->output_data
.x
->menubar_widget
->core
.border_width
979 #endif /* USE_LUCID */
984 /* Experimentally, we now get the right results
985 for -geometry -0-0 without this. 24 Aug 96, rms.
986 Maybe so, but the menu bar size is missing the pixels so the
987 WM size hints are off by these pixels. Jan D, oct 2009. */
988 if (FRAME_EXTERNAL_MENU_BAR (f
))
992 XtVaGetValues (f
->output_data
.x
->column_widget
,
993 XtNinternalBorderWidth
, &ibw
, NULL
);
996 #endif /* USE_LUCID */
998 FRAME_MENUBAR_HEIGHT (f
) = menubar_size
;
1000 #endif /* not USE_GTK */
1002 free_menubar_widget_value_tree (first_wv
);
1003 update_frame_menubar (f
);
1006 xg_crazy_callback_abort
= false;
1012 /* Called from Fx_create_frame to create the initial menubar of a frame
1013 before it is mapped, so that the window is mapped with the menubar already
1014 there instead of us tacking it on later and thrashing the window after it
1018 initialize_frame_menubar (struct frame
*f
)
1020 /* This function is called before the first chance to redisplay
1021 the frame. It has to be, so the frame will have the right size. */
1022 fset_menu_bar_items (f
, menu_bar_items (FRAME_MENU_BAR_ITEMS (f
)));
1023 set_frame_menubar (f
, true, true);
1027 /* Get rid of the menu bar of frame F, and free its storage.
1028 This is used when deleting a frame, and when turning off the menu bar.
1029 For GTK this function is in gtkutil.c. */
1033 free_frame_menubar (struct frame
*f
)
1035 Widget menubar_widget
;
1037 /* Motif automatically shrinks the frame in lw_destroy_all_widgets.
1038 If we want to preserve the old height, calculate it now so we can
1039 restore it below. */
1040 int old_height
= FRAME_TEXT_HEIGHT (f
) + FRAME_MENUBAR_HEIGHT (f
);
1043 eassert (FRAME_X_P (f
));
1045 menubar_widget
= f
->output_data
.x
->menubar_widget
;
1047 FRAME_MENUBAR_HEIGHT (f
) = 0;
1052 /* Removing the menu bar magically changes the shell widget's x
1053 and y position of (0, 0) which, when the menu bar is turned
1054 on again, leads to pull-down menus appearing in strange
1055 positions near the upper-left corner of the display. This
1056 happens only with some window managers like twm and ctwm,
1057 but not with other like Motif's mwm or kwm, because the
1058 latter generate ConfigureNotify events when the menu bar
1059 is switched off, which fixes the shell position. */
1060 Position x0
, y0
, x1
, y1
;
1066 if (f
->output_data
.x
->widget
)
1067 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x0
, XtNy
, &y0
, NULL
);
1070 lw_destroy_all_widgets ((LWLIB_ID
) f
->output_data
.x
->id
);
1071 f
->output_data
.x
->menubar_widget
= NULL
;
1073 if (f
->output_data
.x
->widget
)
1076 XtVaGetValues (f
->output_data
.x
->widget
, XtNx
, &x1
, XtNy
, &y1
, NULL
);
1077 if (x1
== 0 && y1
== 0)
1078 XtVaSetValues (f
->output_data
.x
->widget
, XtNx
, x0
, XtNy
, y0
, NULL
);
1079 if (frame_inhibit_resize (f
, false, Qmenu_bar_lines
))
1080 adjust_frame_size (f
, -1, old_height
, 1, false, Qfree_frame_menubar_1
);
1082 adjust_frame_size (f
, -1, -1, 2, false, Qfree_frame_menubar_1
);
1084 adjust_frame_size (f
, -1, -1, 2, false, Qfree_frame_menubar_1
);
1085 #endif /* USE_MOTIF */
1090 if (WINDOWP (FRAME_ROOT_WINDOW (f
))
1091 && frame_inhibit_resize (f
, false, Qmenu_bar_lines
))
1092 adjust_frame_size (f
, -1, old_height
, 1, false, Qfree_frame_menubar_2
);
1099 #endif /* not USE_GTK */
1101 #endif /* USE_X_TOOLKIT || USE_GTK */
1103 /* x_menu_show actually displays a menu using the panes and items in menu_items
1104 and returns the value selected from it.
1105 There are two versions of x_menu_show, one for Xt and one for Xlib.
1106 Both assume input is blocked by the caller. */
1108 /* F is the frame the menu is for.
1109 X and Y are the frame-relative specified position,
1110 relative to the inside upper left corner of the frame F.
1111 Bitfield MENUFLAGS bits are:
1112 MENU_FOR_CLICK is set if this menu was invoked for a mouse click.
1113 MENU_KEYMAPS is set if this menu was specified with keymaps;
1114 in that case, we return a list containing the chosen item's value
1115 and perhaps also the pane's prefix.
1116 TITLE is the specified menu title.
1117 ERROR is a place to store an error message string in case of failure.
1118 (We return nil on failure, but the value doesn't actually matter.) */
1120 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1122 /* The item selected in the popup menu. */
1123 static Lisp_Object
*volatile menu_item_selection
;
1127 /* Used when position a popup menu. See menu_position_func and
1128 create_and_show_popup_menu below. */
1129 struct next_popup_x_y
1136 /* The menu position function to use if we are not putting a popup
1137 menu where the pointer is.
1138 MENU is the menu to pop up.
1139 X and Y shall on exit contain x/y where the menu shall pop up.
1140 PUSH_IN is not documented in the GTK manual.
1141 USER_DATA is any data passed in when calling gtk_menu_popup.
1142 Here it points to a struct next_popup_x_y where the coordinates
1143 to store in *X and *Y are as well as the frame for the popup.
1145 Here only X and Y are used. */
1147 menu_position_func (GtkMenu
*menu
, gint
*x
, gint
*y
, gboolean
*push_in
, gpointer user_data
)
1149 struct next_popup_x_y
*data
= user_data
;
1151 struct x_display_info
*dpyinfo
= FRAME_DISPLAY_INFO (data
->f
);
1152 int disp_width
= x_display_pixel_width (dpyinfo
);
1153 int disp_height
= x_display_pixel_height (dpyinfo
);
1158 /* Check if there is room for the menu. If not, adjust x/y so that
1159 the menu is fully visible. */
1160 gtk_widget_get_preferred_size (GTK_WIDGET (menu
), NULL
, &req
);
1161 if (data
->x
+ req
.width
> disp_width
)
1162 *x
-= data
->x
+ req
.width
- disp_width
;
1163 if (data
->y
+ req
.height
> disp_height
)
1164 *y
-= data
->y
+ req
.height
- disp_height
;
1168 popup_selection_callback (GtkWidget
*widget
, gpointer client_data
)
1170 xg_menu_item_cb_data
*cb_data
= client_data
;
1172 if (xg_crazy_callback_abort
) return;
1173 if (cb_data
) menu_item_selection
= cb_data
->call_data
;
1177 pop_down_menu (void *arg
)
1179 popup_activated_flag
= 0;
1181 gtk_widget_destroy (GTK_WIDGET (arg
));
1185 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1187 menu_item_selection will be set to the selection. */
1189 create_and_show_popup_menu (struct frame
*f
, widget_value
*first_wv
,
1190 int x
, int y
, bool for_click
)
1194 GtkMenuPositionFunc pos_func
= 0; /* Pop up at pointer. */
1195 struct next_popup_x_y popup_x_y
;
1196 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1197 bool use_pos_func
= ! for_click
;
1200 /* Always use position function for Gtk3. Otherwise menus may become
1201 too small to show anything. */
1202 use_pos_func
= true;
1205 eassert (FRAME_X_P (f
));
1207 xg_crazy_callback_abort
= true;
1208 menu
= xg_create_widget ("popup", first_wv
->name
, f
, first_wv
,
1209 G_CALLBACK (popup_selection_callback
),
1210 G_CALLBACK (popup_deactivate_callback
),
1211 G_CALLBACK (menu_highlight_callback
));
1212 xg_crazy_callback_abort
= false;
1216 Window dummy_window
;
1218 /* Not invoked by a click. pop up at x/y. */
1219 pos_func
= menu_position_func
;
1221 /* Adjust coordinates to be root-window-relative. */
1223 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
1225 /* From-window, to-window. */
1227 FRAME_DISPLAY_INFO (f
)->root_window
,
1229 /* From-position, to-position. */
1239 i
= 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1244 for (i
= 0; i
< 5; i
++)
1245 if (FRAME_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
1247 /* If keys aren't grabbed (i.e., a mouse up event), use 0. */
1251 /* Display the menu. */
1252 gtk_widget_show_all (menu
);
1254 gtk_menu_popup (GTK_MENU (menu
), 0, 0, pos_func
, &popup_x_y
, i
,
1255 FRAME_DISPLAY_INFO (f
)->last_user_time
);
1257 record_unwind_protect_ptr (pop_down_menu
, menu
);
1259 if (gtk_widget_get_mapped (menu
))
1261 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1262 two. show_help_echo uses this to detect popup menus. */
1263 popup_activated_flag
= 1;
1264 /* Process events that apply to the menu. */
1265 popup_widget_loop (true, menu
);
1268 unbind_to (specpdl_count
, Qnil
);
1270 /* Must reset this manually because the button release event is not passed
1271 to Emacs event loop. */
1272 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
1275 #else /* not USE_GTK */
1277 /* We need a unique id for each widget handled by the Lucid Widget
1280 For the main windows, and popup menus, we use this counter, which we
1281 increment each time after use. This starts from WIDGET_ID_TICK_START.
1283 For menu bars, we use numbers starting at 0, counted in
1284 next_menubar_widget_id. */
1285 LWLIB_ID widget_id_tick
;
1288 popup_selection_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
1290 menu_item_selection
= client_data
;
1293 /* ID is the LWLIB ID of the dialog box. */
1296 pop_down_menu (int id
)
1299 lw_destroy_all_widgets ((LWLIB_ID
) id
);
1301 popup_activated_flag
= 0;
1304 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1306 menu_item_selection will be set to the selection. */
1308 create_and_show_popup_menu (struct frame
*f
, widget_value
*first_wv
,
1309 int x
, int y
, bool for_click
)
1315 XButtonPressedEvent
*event
= &(dummy
.xbutton
);
1318 Window dummy_window
;
1320 eassert (FRAME_X_P (f
));
1323 apply_systemfont_to_menu (f
, f
->output_data
.x
->widget
);
1326 menu_id
= widget_id_tick
++;
1327 menu
= lw_create_widget ("popup", first_wv
->name
, menu_id
, first_wv
,
1328 f
->output_data
.x
->widget
, true, 0,
1329 popup_selection_callback
,
1330 popup_deactivate_callback
,
1331 menu_highlight_callback
);
1333 event
->type
= ButtonPress
;
1335 event
->send_event
= false;
1336 event
->display
= FRAME_X_DISPLAY (f
);
1337 event
->time
= CurrentTime
;
1338 event
->root
= FRAME_DISPLAY_INFO (f
)->root_window
;
1339 event
->window
= event
->subwindow
= event
->root
;
1343 /* Adjust coordinates to be root-window-relative. */
1345 x
+= FRAME_LEFT_SCROLL_BAR_AREA_WIDTH (f
);
1346 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
1348 /* From-window, to-window. */
1350 FRAME_DISPLAY_INFO (f
)->root_window
,
1352 /* From-position, to-position. */
1364 for (i
= 0; i
< 5; i
++)
1365 if (FRAME_DISPLAY_INFO (f
)->grabbed
& (1 << i
))
1368 /* Don't allow any geometry request from the user. */
1369 XtSetArg (av
[ac
], XtNgeometry
, 0); ac
++;
1370 XtSetValues (menu
, av
, ac
);
1372 /* Display the menu. */
1373 lw_popup_menu (menu
, &dummy
);
1374 popup_activated_flag
= 1;
1375 x_activate_timeout_atimer ();
1378 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1380 record_unwind_protect_int (pop_down_menu
, (int) menu_id
);
1382 /* Process events that apply to the menu. */
1383 popup_get_selection (0, FRAME_DISPLAY_INFO (f
), menu_id
, true);
1385 unbind_to (specpdl_count
, Qnil
);
1389 #endif /* not USE_GTK */
1392 cleanup_widget_value_tree (void *arg
)
1394 free_menubar_widget_value_tree (arg
);
1398 x_menu_show (struct frame
*f
, int x
, int y
, int menuflags
,
1399 Lisp_Object title
, const char **error_name
)
1402 widget_value
*wv
, *save_wv
= 0, *first_wv
= 0, *prev_wv
= 0;
1403 widget_value
**submenu_stack
1404 = alloca (menu_items_used
* sizeof *submenu_stack
);
1405 Lisp_Object
*subprefix_stack
1406 = alloca (menu_items_used
* sizeof *subprefix_stack
);
1407 int submenu_depth
= 0;
1409 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1411 eassert (FRAME_X_P (f
));
1415 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
1417 *error_name
= "Empty menu";
1423 /* Create a tree of widget_value objects
1424 representing the panes and their items. */
1425 wv
= make_widget_value ("menu", NULL
, true, Qnil
);
1426 wv
->button_type
= BUTTON_TYPE_NONE
;
1428 bool first_pane
= true;
1430 /* Loop over all panes and items, filling in the tree. */
1432 while (i
< menu_items_used
)
1434 if (EQ (AREF (menu_items
, i
), Qnil
))
1436 submenu_stack
[submenu_depth
++] = save_wv
;
1442 else if (EQ (AREF (menu_items
, i
), Qlambda
))
1445 save_wv
= submenu_stack
[--submenu_depth
];
1449 else if (EQ (AREF (menu_items
, i
), Qt
)
1450 && submenu_depth
!= 0)
1451 i
+= MENU_ITEMS_PANE_LENGTH
;
1452 /* Ignore a nil in the item list.
1453 It's meaningful only for dialog boxes. */
1454 else if (EQ (AREF (menu_items
, i
), Qquote
))
1456 else if (EQ (AREF (menu_items
, i
), Qt
))
1458 /* Create a new pane. */
1459 Lisp_Object pane_name
, prefix
;
1460 const char *pane_string
;
1462 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
1463 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
1465 #ifndef HAVE_MULTILINGUAL_MENU
1466 if (STRINGP (pane_name
) && STRING_MULTIBYTE (pane_name
))
1468 pane_name
= ENCODE_MENU_STRING (pane_name
);
1469 ASET (menu_items
, i
+ MENU_ITEMS_PANE_NAME
, pane_name
);
1472 pane_string
= (NILP (pane_name
)
1473 ? "" : SSDATA (pane_name
));
1474 /* If there is just one top-level pane, put all its items directly
1475 under the top-level menu. */
1476 if (menu_items_n_panes
== 1)
1479 /* If the pane has a meaningful name,
1480 make the pane a top-level menu item
1481 with its items as a submenu beneath it. */
1482 if (!(menuflags
& MENU_KEYMAPS
) && strcmp (pane_string
, ""))
1484 wv
= make_widget_value (pane_string
, NULL
, true, Qnil
);
1488 first_wv
->contents
= wv
;
1489 if ((menuflags
& MENU_KEYMAPS
) && !NILP (prefix
))
1491 wv
->button_type
= BUTTON_TYPE_NONE
;
1495 else if (first_pane
)
1501 i
+= MENU_ITEMS_PANE_LENGTH
;
1505 /* Create a new item within current pane. */
1506 Lisp_Object item_name
, enable
, descrip
, def
, type
, selected
, help
;
1507 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1508 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1509 descrip
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1510 def
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_DEFINITION
);
1511 type
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_TYPE
);
1512 selected
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_SELECTED
);
1513 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
1515 #ifndef HAVE_MULTILINGUAL_MENU
1516 if (STRINGP (item_name
) && STRING_MULTIBYTE (item_name
))
1518 item_name
= ENCODE_MENU_STRING (item_name
);
1519 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
, item_name
);
1522 if (STRINGP (descrip
) && STRING_MULTIBYTE (descrip
))
1524 descrip
= ENCODE_MENU_STRING (descrip
);
1525 ASET (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
, descrip
);
1527 #endif /* not HAVE_MULTILINGUAL_MENU */
1529 wv
= make_widget_value (SSDATA (item_name
), NULL
, !NILP (enable
),
1530 STRINGP (help
) ? help
: Qnil
);
1534 save_wv
->contents
= wv
;
1535 if (!NILP (descrip
))
1536 wv
->key
= SSDATA (descrip
);
1537 /* If this item has a null value,
1538 make the call_data null so that it won't display a box
1539 when the mouse is on it. */
1540 wv
->call_data
= !NILP (def
) ? aref_addr (menu_items
, i
) : 0;
1543 wv
->button_type
= BUTTON_TYPE_NONE
;
1544 else if (EQ (type
, QCtoggle
))
1545 wv
->button_type
= BUTTON_TYPE_TOGGLE
;
1546 else if (EQ (type
, QCradio
))
1547 wv
->button_type
= BUTTON_TYPE_RADIO
;
1551 wv
->selected
= !NILP (selected
);
1555 i
+= MENU_ITEMS_ITEM_LENGTH
;
1559 /* Deal with the title, if it is non-nil. */
1562 widget_value
*wv_title
;
1563 widget_value
*wv_sep1
= make_widget_value ("--", NULL
, false, Qnil
);
1564 widget_value
*wv_sep2
= make_widget_value ("--", NULL
, false, Qnil
);
1566 wv_sep2
->next
= first_wv
->contents
;
1567 wv_sep1
->next
= wv_sep2
;
1569 #ifndef HAVE_MULTILINGUAL_MENU
1570 if (STRING_MULTIBYTE (title
))
1571 title
= ENCODE_MENU_STRING (title
);
1574 wv_title
= make_widget_value (SSDATA (title
), NULL
, true, Qnil
);
1575 wv_title
->button_type
= BUTTON_TYPE_NONE
;
1576 wv_title
->next
= wv_sep1
;
1577 first_wv
->contents
= wv_title
;
1580 /* No selection has been chosen yet. */
1581 menu_item_selection
= 0;
1583 /* Make sure to free the widget_value objects we used to specify the
1584 contents even with longjmp. */
1585 record_unwind_protect_ptr (cleanup_widget_value_tree
, first_wv
);
1587 /* Actually create and show the menu until popped down. */
1588 create_and_show_popup_menu (f
, first_wv
, x
, y
,
1589 menuflags
& MENU_FOR_CLICK
);
1591 unbind_to (specpdl_count
, Qnil
);
1593 /* Find the selected item, and its pane, to return
1594 the proper value. */
1595 if (menu_item_selection
!= 0)
1597 Lisp_Object prefix
, entry
;
1599 prefix
= entry
= Qnil
;
1601 while (i
< menu_items_used
)
1603 if (EQ (AREF (menu_items
, i
), Qnil
))
1605 subprefix_stack
[submenu_depth
++] = prefix
;
1609 else if (EQ (AREF (menu_items
, i
), Qlambda
))
1611 prefix
= subprefix_stack
[--submenu_depth
];
1614 else if (EQ (AREF (menu_items
, i
), Qt
))
1617 = AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
1618 i
+= MENU_ITEMS_PANE_LENGTH
;
1620 /* Ignore a nil in the item list.
1621 It's meaningful only for dialog boxes. */
1622 else if (EQ (AREF (menu_items
, i
), Qquote
))
1627 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
1628 if (menu_item_selection
== aref_addr (menu_items
, i
))
1630 if (menuflags
& MENU_KEYMAPS
)
1634 entry
= list1 (entry
);
1636 entry
= Fcons (prefix
, entry
);
1637 for (j
= submenu_depth
- 1; j
>= 0; j
--)
1638 if (!NILP (subprefix_stack
[j
]))
1639 entry
= Fcons (subprefix_stack
[j
], entry
);
1644 i
+= MENU_ITEMS_ITEM_LENGTH
;
1648 else if (!(menuflags
& MENU_FOR_CLICK
))
1651 /* Make "Cancel" equivalent to C-g. */
1661 dialog_selection_callback (GtkWidget
*widget
, gpointer client_data
)
1663 /* Treat the pointer as an integer. There's no problem
1664 as long as pointers have enough bits to hold small integers. */
1665 if ((intptr_t) client_data
!= -1)
1666 menu_item_selection
= client_data
;
1668 popup_activated_flag
= 0;
1671 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1673 menu_item_selection will be set to the selection. */
1675 create_and_show_dialog (struct frame
*f
, widget_value
*first_wv
)
1679 eassert (FRAME_X_P (f
));
1681 menu
= xg_create_widget ("dialog", first_wv
->name
, f
, first_wv
,
1682 G_CALLBACK (dialog_selection_callback
),
1683 G_CALLBACK (popup_deactivate_callback
),
1688 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1689 record_unwind_protect_ptr (pop_down_menu
, menu
);
1691 /* Display the menu. */
1692 gtk_widget_show_all (menu
);
1694 /* Process events that apply to the menu. */
1695 popup_widget_loop (true, menu
);
1697 unbind_to (specpdl_count
, Qnil
);
1701 #else /* not USE_GTK */
1703 dialog_selection_callback (Widget widget
, LWLIB_ID id
, XtPointer client_data
)
1705 /* Treat the pointer as an integer. There's no problem
1706 as long as pointers have enough bits to hold small integers. */
1707 if ((intptr_t) client_data
!= -1)
1708 menu_item_selection
= client_data
;
1711 lw_destroy_all_widgets (id
);
1713 popup_activated_flag
= 0;
1717 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1719 menu_item_selection will be set to the selection. */
1721 create_and_show_dialog (struct frame
*f
, widget_value
*first_wv
)
1725 eassert (FRAME_X_P (f
));
1727 dialog_id
= widget_id_tick
++;
1729 apply_systemfont_to_dialog (f
->output_data
.x
->widget
);
1731 lw_create_widget (first_wv
->name
, "dialog", dialog_id
, first_wv
,
1732 f
->output_data
.x
->widget
, true, 0,
1733 dialog_selection_callback
, 0, 0);
1734 lw_modify_all_widgets (dialog_id
, first_wv
->contents
, True
);
1735 /* Display the dialog box. */
1736 lw_pop_up_all_widgets (dialog_id
);
1737 popup_activated_flag
= 1;
1738 x_activate_timeout_atimer ();
1740 /* Process events that apply to the dialog box.
1741 Also handle timers. */
1743 ptrdiff_t count
= SPECPDL_INDEX ();
1745 /* xdialog_show_unwind is responsible for popping the dialog box down. */
1747 record_unwind_protect_int (pop_down_menu
, (int) dialog_id
);
1749 popup_get_selection (0, FRAME_DISPLAY_INFO (f
), dialog_id
, true);
1751 unbind_to (count
, Qnil
);
1755 #endif /* not USE_GTK */
1757 static const char * button_names
[] = {
1758 "button1", "button2", "button3", "button4", "button5",
1759 "button6", "button7", "button8", "button9", "button10" };
1762 x_dialog_show (struct frame
*f
, Lisp_Object title
,
1763 Lisp_Object header
, const char **error_name
)
1765 int i
, nb_buttons
=0;
1766 char dialog_name
[6];
1768 widget_value
*wv
, *first_wv
= 0, *prev_wv
= 0;
1770 /* Number of elements seen so far, before boundary. */
1772 /* Whether we've seen the boundary between left-hand elts and right-hand. */
1773 bool boundary_seen
= false;
1775 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1777 eassert (FRAME_X_P (f
));
1781 if (menu_items_n_panes
> 1)
1783 *error_name
= "Multiple panes in dialog box";
1787 /* Create a tree of widget_value objects
1788 representing the text label and buttons. */
1790 Lisp_Object pane_name
;
1791 const char *pane_string
;
1792 pane_name
= AREF (menu_items
, MENU_ITEMS_PANE_NAME
);
1793 pane_string
= (NILP (pane_name
)
1794 ? "" : SSDATA (pane_name
));
1795 prev_wv
= make_widget_value ("message", (char *) pane_string
, true, Qnil
);
1798 /* Loop over all panes and items, filling in the tree. */
1799 i
= MENU_ITEMS_PANE_LENGTH
;
1800 while (i
< menu_items_used
)
1803 /* Create a new item within current pane. */
1804 Lisp_Object item_name
, enable
, descrip
;
1805 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
1806 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
1808 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
1810 if (NILP (item_name
))
1812 free_menubar_widget_value_tree (first_wv
);
1813 *error_name
= "Submenu in dialog items";
1816 if (EQ (item_name
, Qquote
))
1818 /* This is the boundary between left-side elts
1819 and right-side elts. Stop incrementing right_count. */
1820 boundary_seen
= true;
1824 if (nb_buttons
>= 9)
1826 free_menubar_widget_value_tree (first_wv
);
1827 *error_name
= "Too many dialog items";
1831 wv
= make_widget_value (button_names
[nb_buttons
],
1833 !NILP (enable
), Qnil
);
1835 if (!NILP (descrip
))
1836 wv
->key
= SSDATA (descrip
);
1837 wv
->call_data
= aref_addr (menu_items
, i
);
1840 if (! boundary_seen
)
1844 i
+= MENU_ITEMS_ITEM_LENGTH
;
1847 /* If the boundary was not specified,
1848 by default put half on the left and half on the right. */
1849 if (! boundary_seen
)
1850 left_count
= nb_buttons
- nb_buttons
/ 2;
1852 wv
= make_widget_value (dialog_name
, NULL
, false, Qnil
);
1854 /* Frame title: 'Q' = Question, 'I' = Information.
1855 Can also have 'E' = Error if, one day, we want
1856 a popup for errors. */
1858 dialog_name
[0] = 'Q';
1860 dialog_name
[0] = 'I';
1862 /* Dialog boxes use a really stupid name encoding
1863 which specifies how many buttons to use
1864 and how many buttons are on the right. */
1865 dialog_name
[1] = '0' + nb_buttons
;
1866 dialog_name
[2] = 'B';
1867 dialog_name
[3] = 'R';
1868 /* Number of buttons to put on the right. */
1869 dialog_name
[4] = '0' + nb_buttons
- left_count
;
1871 wv
->contents
= first_wv
;
1875 /* No selection has been chosen yet. */
1876 menu_item_selection
= 0;
1878 /* Make sure to free the widget_value objects we used to specify the
1879 contents even with longjmp. */
1880 record_unwind_protect_ptr (cleanup_widget_value_tree
, first_wv
);
1882 /* Actually create and show the dialog. */
1883 create_and_show_dialog (f
, first_wv
);
1885 unbind_to (specpdl_count
, Qnil
);
1887 /* Find the selected item, and its pane, to return
1888 the proper value. */
1889 if (menu_item_selection
!= 0)
1892 while (i
< menu_items_used
)
1896 if (EQ (AREF (menu_items
, i
), Qt
))
1897 i
+= MENU_ITEMS_PANE_LENGTH
;
1898 else if (EQ (AREF (menu_items
, i
), Qquote
))
1900 /* This is the boundary between left-side elts and
1907 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
1908 if (menu_item_selection
== aref_addr (menu_items
, i
))
1910 i
+= MENU_ITEMS_ITEM_LENGTH
;
1915 /* Make "Cancel" equivalent to C-g. */
1922 xw_popup_dialog (struct frame
*f
, Lisp_Object header
, Lisp_Object contents
)
1925 const char *error_name
;
1926 Lisp_Object selection
;
1927 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
1929 check_window_system (f
);
1931 /* Decode the dialog items from what was specified. */
1932 title
= Fcar (contents
);
1933 CHECK_STRING (title
);
1934 record_unwind_protect_void (unuse_menu_items
);
1936 if (NILP (Fcar (Fcdr (contents
))))
1937 /* No buttons specified, add an "Ok" button so users can pop down
1938 the dialog. Also, the lesstif/motif version crashes if there are
1940 contents
= list2 (title
, Fcons (build_string ("Ok"), Qt
));
1942 list_of_panes (list1 (contents
));
1944 /* Display them in a dialog box. */
1946 selection
= x_dialog_show (f
, title
, header
, &error_name
);
1949 unbind_to (specpdl_count
, Qnil
);
1950 discard_menu_items ();
1952 if (error_name
) error ("%s", error_name
);
1956 #else /* not USE_X_TOOLKIT && not USE_GTK */
1958 /* The frame of the last activated non-toolkit menu bar.
1959 Used to generate menu help events. */
1961 static struct frame
*menu_help_frame
;
1964 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
1966 PANE is the pane number, and ITEM is the menu item number in
1967 the menu (currently not used).
1969 This cannot be done with generating a HELP_EVENT because
1970 XMenuActivate contains a loop that doesn't let Emacs process
1974 menu_help_callback (char const *help_string
, int pane
, int item
)
1976 Lisp_Object
*first_item
;
1977 Lisp_Object pane_name
;
1978 Lisp_Object menu_object
;
1980 first_item
= XVECTOR (menu_items
)->contents
;
1981 if (EQ (first_item
[0], Qt
))
1982 pane_name
= first_item
[MENU_ITEMS_PANE_NAME
];
1983 else if (EQ (first_item
[0], Qquote
))
1984 /* This shouldn't happen, see x_menu_show. */
1985 pane_name
= empty_unibyte_string
;
1987 pane_name
= first_item
[MENU_ITEMS_ITEM_NAME
];
1989 /* (menu-item MENU-NAME PANE-NUMBER) */
1990 menu_object
= list3 (Qmenu_item
, pane_name
, make_number (pane
));
1991 show_help_echo (help_string
? build_string (help_string
) : Qnil
,
1992 Qnil
, menu_object
, make_number (item
));
1996 pop_down_menu (Lisp_Object arg
)
1998 struct frame
*f
= XSAVE_POINTER (arg
, 0);
1999 XMenu
*menu
= XSAVE_POINTER (arg
, 1);
2003 XUngrabPointer (FRAME_X_DISPLAY (f
), CurrentTime
);
2004 XUngrabKeyboard (FRAME_X_DISPLAY (f
), CurrentTime
);
2006 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2008 #ifdef HAVE_X_WINDOWS
2009 /* Assume the mouse has moved out of the X window.
2010 If it has actually moved in, we will get an EnterNotify. */
2011 x_mouse_leave (FRAME_DISPLAY_INFO (f
));
2013 /* State that no mouse buttons are now held.
2014 (The oldXMenu code doesn't track this info for us.)
2015 That is not necessarily true, but the fiction leads to reasonable
2016 results, and it is a pain to ask which are actually held now. */
2017 FRAME_DISPLAY_INFO (f
)->grabbed
= 0;
2019 #endif /* HAVE_X_WINDOWS */
2026 x_menu_show (struct frame
*f
, int x
, int y
, int menuflags
,
2027 Lisp_Object title
, const char **error_name
)
2031 int pane
, selidx
, lpane
, status
;
2032 Lisp_Object entry
= Qnil
;
2033 Lisp_Object pane_prefix
;
2035 int ulx
, uly
, width
, height
;
2036 int dispwidth
, dispheight
;
2037 int i
, j
, lines
, maxlines
;
2040 unsigned int dummy_uint
;
2041 ptrdiff_t specpdl_count
= SPECPDL_INDEX ();
2043 eassert (FRAME_X_P (f
) || FRAME_MSDOS_P (f
));
2046 if (menu_items_n_panes
== 0)
2049 if (menu_items_used
<= MENU_ITEMS_PANE_LENGTH
)
2051 *error_name
= "Empty menu";
2058 /* Figure out which root window F is on. */
2059 XGetGeometry (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &root
,
2060 &dummy_int
, &dummy_int
, &dummy_uint
, &dummy_uint
,
2061 &dummy_uint
, &dummy_uint
);
2063 /* Make the menu on that window. */
2064 menu
= XMenuCreate (FRAME_X_DISPLAY (f
), root
, "emacs");
2067 *error_name
= "Can't create menu";
2071 /* Don't GC while we prepare and show the menu,
2072 because we give the oldxmenu library pointers to the
2073 contents of strings. */
2074 inhibit_garbage_collection ();
2076 #ifdef HAVE_X_WINDOWS
2078 /* Adjust coordinates to relative to the outer (window manager) window. */
2079 int left_off
, top_off
;
2081 x_real_pos_and_offsets (f
, &left_off
, NULL
, &top_off
, NULL
,
2082 NULL
, NULL
, NULL
, NULL
, NULL
);
2087 #endif /* HAVE_X_WINDOWS */
2092 /* Create all the necessary panes and their items. */
2093 maxwidth
= maxlines
= lines
= i
= 0;
2095 while (i
< menu_items_used
)
2097 if (EQ (AREF (menu_items
, i
), Qt
))
2099 /* Create a new pane. */
2100 Lisp_Object pane_name
, prefix
;
2101 const char *pane_string
;
2103 maxlines
= max (maxlines
, lines
);
2105 pane_name
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_NAME
);
2106 prefix
= AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2107 pane_string
= (NILP (pane_name
)
2108 ? "" : SSDATA (pane_name
));
2109 if ((menuflags
& MENU_KEYMAPS
) && !NILP (prefix
))
2112 lpane
= XMenuAddPane (FRAME_X_DISPLAY (f
), menu
, pane_string
, true);
2113 if (lpane
== XM_FAILURE
)
2115 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2116 *error_name
= "Can't create pane";
2119 i
+= MENU_ITEMS_PANE_LENGTH
;
2121 /* Find the width of the widest item in this pane. */
2123 while (j
< menu_items_used
)
2126 item
= AREF (menu_items
, j
);
2134 width
= SBYTES (item
);
2135 if (width
> maxwidth
)
2138 j
+= MENU_ITEMS_ITEM_LENGTH
;
2141 /* Ignore a nil in the item list.
2142 It's meaningful only for dialog boxes. */
2143 else if (EQ (AREF (menu_items
, i
), Qquote
))
2147 /* Create a new item within current pane. */
2148 Lisp_Object item_name
, enable
, descrip
, help
;
2150 char const *help_string
;
2152 item_name
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_NAME
);
2153 enable
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_ENABLE
);
2155 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_EQUIV_KEY
);
2156 help
= AREF (menu_items
, i
+ MENU_ITEMS_ITEM_HELP
);
2157 help_string
= STRINGP (help
) ? SSDATA (help
) : NULL
;
2159 if (!NILP (descrip
))
2161 item_data
= SAFE_ALLOCA (maxwidth
+ SBYTES (descrip
) + 1);
2162 memcpy (item_data
, SSDATA (item_name
), SBYTES (item_name
));
2163 for (j
= SCHARS (item_name
); j
< maxwidth
; j
++)
2165 memcpy (item_data
+ j
, SSDATA (descrip
), SBYTES (descrip
));
2166 item_data
[j
+ SBYTES (descrip
)] = 0;
2169 item_data
= SSDATA (item_name
);
2171 if (lpane
== XM_FAILURE
2172 || (XMenuAddSelection (FRAME_X_DISPLAY (f
),
2173 menu
, lpane
, 0, item_data
,
2174 !NILP (enable
), help_string
)
2177 XMenuDestroy (FRAME_X_DISPLAY (f
), menu
);
2178 *error_name
= "Can't add selection to menu";
2181 i
+= MENU_ITEMS_ITEM_LENGTH
;
2186 maxlines
= max (maxlines
, lines
);
2188 /* All set and ready to fly. */
2189 XMenuRecompute (FRAME_X_DISPLAY (f
), menu
);
2190 dispwidth
= DisplayWidth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
2191 dispheight
= DisplayHeight (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
));
2192 x
= min (x
, dispwidth
);
2193 y
= min (y
, dispheight
);
2196 XMenuLocate (FRAME_X_DISPLAY (f
), menu
, 0, 0, x
, y
,
2197 &ulx
, &uly
, &width
, &height
);
2198 if (ulx
+width
> dispwidth
)
2200 x
-= (ulx
+ width
) - dispwidth
;
2201 ulx
= dispwidth
- width
;
2203 if (uly
+height
> dispheight
)
2205 y
-= (uly
+ height
) - dispheight
;
2206 uly
= dispheight
- height
;
2208 #ifndef HAVE_X_WINDOWS
2209 if (FRAME_HAS_MINIBUF_P (f
) && uly
+height
> dispheight
- 1)
2211 /* Move the menu away of the echo area, to avoid overwriting the
2212 menu with help echo messages or vice versa. */
2213 if (BUFFERP (echo_area_buffer
[0]) && WINDOWP (echo_area_window
))
2215 y
-= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window
));
2216 uly
-= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window
));
2225 if (ulx
< 0) x
-= ulx
;
2226 if (uly
< 0) y
-= uly
;
2228 if (!(menuflags
& MENU_FOR_CLICK
))
2230 /* If position was not given by a mouse click, adjust so upper left
2231 corner of the menu as a whole ends up at given coordinates. This
2232 is what x-popup-menu says in its documentation. */
2234 y
+= 1.5*height
/(maxlines
+2);
2237 XMenuSetAEQ (menu
, true);
2238 XMenuSetFreeze (menu
, true);
2242 XMenuActivateSetWaitFunction (x_menu_wait_for_event
, FRAME_X_DISPLAY (f
));
2245 record_unwind_protect (pop_down_menu
, make_save_ptr_ptr (f
, menu
));
2247 /* Help display under X won't work because XMenuActivate contains
2248 a loop that doesn't give Emacs a chance to process it. */
2249 menu_help_frame
= f
;
2250 status
= XMenuActivate (FRAME_X_DISPLAY (f
), menu
, &pane
, &selidx
,
2251 x
, y
, ButtonReleaseMask
, &datap
,
2252 menu_help_callback
);
2259 fprintf (stderr
, "pane= %d line = %d\n", panes
, selidx
);
2262 /* Find the item number SELIDX in pane number PANE. */
2264 while (i
< menu_items_used
)
2266 if (EQ (AREF (menu_items
, i
), Qt
))
2270 = AREF (menu_items
, i
+ MENU_ITEMS_PANE_PREFIX
);
2272 i
+= MENU_ITEMS_PANE_LENGTH
;
2281 = AREF (menu_items
, i
+ MENU_ITEMS_ITEM_VALUE
);
2282 if (menuflags
& MENU_KEYMAPS
)
2284 entry
= list1 (entry
);
2285 if (!NILP (pane_prefix
))
2286 entry
= Fcons (pane_prefix
, entry
);
2292 i
+= MENU_ITEMS_ITEM_LENGTH
;
2298 *error_name
= "Can't activate menu";
2302 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2303 the menu was invoked with a mouse event as POSITION). */
2304 if (!(menuflags
& MENU_FOR_CLICK
))
2315 return unbind_to (specpdl_count
, entry
);
2318 #endif /* not USE_X_TOOLKIT */
2321 /* Detect if a dialog or menu has been posted. MSDOS has its own
2322 implementation on msdos.c. */
2325 popup_activated (void)
2327 return popup_activated_flag
;
2329 #endif /* not MSDOS */
2331 /* The following is used by delayed window autoselection. */
2333 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p
, Smenu_or_popup_active_p
, 0, 0, 0,
2334 doc
: /* Return t if a menu or popup dialog is active. */)
2337 return (popup_activated ()) ? Qt
: Qnil
;
2341 syms_of_xmenu (void)
2343 #ifdef USE_X_TOOLKIT
2344 enum { WIDGET_ID_TICK_START
= 1 << 16 };
2345 widget_id_tick
= WIDGET_ID_TICK_START
;
2346 next_menubar_widget_id
= 1;
2349 DEFSYM (Qdebug_on_next_call
, "debug-on-next-call");
2350 defsubr (&Smenu_or_popup_active_p
);
2352 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
2353 defsubr (&Sx_menu_bar_open_internal
);
2354 Ffset (intern_c_string ("accelerate-menu"),
2355 intern_c_string (Sx_menu_bar_open_internal
.symbol_name
));