Merge from trunk.
[emacs.git] / src / xmenu.c
blob054a52e7760b86714700fc61455f9a01de819d0d
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2013 Free Software
4 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. */
33 #include <config.h>
35 #include <stdio.h>
37 #include "lisp.h"
38 #include "keyboard.h"
39 #include "keymap.h"
40 #include "frame.h"
41 #include "termhooks.h"
42 #include "window.h"
43 #include "blockinput.h"
44 #include "character.h"
45 #include "buffer.h"
46 #include "charset.h"
47 #include "coding.h"
48 #include "sysselect.h"
50 #ifdef MSDOS
51 #include "msdos.h"
52 #endif
54 #ifdef HAVE_X_WINDOWS
55 /* This may include sys/types.h, and that somehow loses
56 if this is not done before the other system files. */
57 #include "xterm.h"
58 #endif
60 /* Load sys/types.h if not already loaded.
61 In some systems loading it twice is suicidal. */
62 #ifndef makedev
63 #include <sys/types.h>
64 #endif
66 #include "dispextern.h"
68 #ifdef HAVE_X_WINDOWS
69 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
70 code accepts the Emacs internal encoding. */
71 #undef HAVE_MULTILINGUAL_MENU
72 #ifdef USE_X_TOOLKIT
73 #include "widget.h"
74 #include <X11/Xlib.h>
75 #include <X11/IntrinsicP.h>
76 #include <X11/CoreP.h>
77 #include <X11/StringDefs.h>
78 #include <X11/Shell.h>
79 #ifdef USE_LUCID
80 #include "xsettings.h"
81 #include "../lwlib/xlwmenu.h"
82 #ifdef HAVE_XAW3D
83 #include <X11/Xaw3d/Paned.h>
84 #else /* !HAVE_XAW3D */
85 #include <X11/Xaw/Paned.h>
86 #endif /* HAVE_XAW3D */
87 #endif /* USE_LUCID */
88 #ifdef USE_MOTIF
89 #include "../lwlib/lwlib.h"
90 #endif
91 #else /* not USE_X_TOOLKIT */
92 #ifndef USE_GTK
93 #include "../oldXMenu/XMenu.h"
94 #endif
95 #endif /* not USE_X_TOOLKIT */
96 #endif /* HAVE_X_WINDOWS */
98 #ifdef USE_GTK
99 #include "gtkutil.h"
100 #ifdef HAVE_GTK3
101 #include "xgselect.h"
102 #endif
103 #endif
105 #include "menu.h"
107 #ifndef TRUE
108 #define TRUE 1
109 #endif /* no TRUE */
111 static Lisp_Object Qdebug_on_next_call;
113 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
114 static Lisp_Object xdialog_show (struct frame *, bool, Lisp_Object, Lisp_Object,
115 const char **);
116 #endif
118 /* Flag which when set indicates a dialog or menu has been posted by
119 Xt on behalf of one of the widget sets. */
120 static int popup_activated_flag;
123 #ifdef USE_X_TOOLKIT
125 static int next_menubar_widget_id;
127 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
129 static struct frame *
130 menubar_id_to_frame (LWLIB_ID id)
132 Lisp_Object tail, frame;
133 struct frame *f;
135 FOR_EACH_FRAME (tail, frame)
137 f = XFRAME (frame);
138 if (!FRAME_WINDOW_P (f))
139 continue;
140 if (f->output_data.x->id == id)
141 return f;
143 return 0;
146 #endif
148 #ifdef HAVE_X_WINDOWS
149 /* Return the mouse position in *X and *Y. The coordinates are window
150 relative for the edit window in frame F.
151 This is for Fx_popup_menu. The mouse_position_hook can not
152 be used for X, as it returns window relative coordinates
153 for the window where the mouse is in. This could be the menu bar,
154 the scroll bar or the edit window. Fx_popup_menu needs to be
155 sure it is the edit window. */
156 void
157 mouse_position_for_popup (struct frame *f, int *x, int *y)
159 Window root, dummy_window;
160 int dummy;
162 eassert (FRAME_X_P (f));
164 block_input ();
166 XQueryPointer (FRAME_X_DISPLAY (f),
167 DefaultRootWindow (FRAME_X_DISPLAY (f)),
169 /* The root window which contains the pointer. */
170 &root,
172 /* Window pointer is on, not used */
173 &dummy_window,
175 /* The position on that root window. */
176 x, y,
178 /* x/y in dummy_window coordinates, not used. */
179 &dummy, &dummy,
181 /* Modifier keys and pointer buttons, about which
182 we don't care. */
183 (unsigned int *) &dummy);
185 unblock_input ();
187 /* xmenu_show expects window coordinates, not root window
188 coordinates. Translate. */
189 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
190 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
193 #endif /* HAVE_X_WINDOWS */
195 #ifdef HAVE_MENUS
197 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
198 doc: /* Pop up a dialog box and return user's selection.
199 POSITION specifies which frame to use.
200 This is normally a mouse button event or a window or frame.
201 If POSITION is t, it means to use the frame the mouse is on.
202 The dialog box appears in the middle of the specified frame.
204 CONTENTS specifies the alternatives to display in the dialog box.
205 It is a list of the form (DIALOG ITEM1 ITEM2...).
206 Each ITEM is a cons cell (STRING . VALUE).
207 The return value is VALUE from the chosen item.
209 An ITEM may also be just a string--that makes a nonselectable item.
210 An ITEM may also be nil--that means to put all preceding items
211 on the left of the dialog box and all following items on the right.
212 \(By default, approximately half appear on each side.)
214 If HEADER is non-nil, the frame title for the box is "Information",
215 otherwise it is "Question".
217 If the user gets rid of the dialog box without making a valid choice,
218 for instance using the window manager, then this produces a quit and
219 `x-popup-dialog' does not return. */)
220 (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
222 struct frame *f = NULL;
223 Lisp_Object window;
225 /* Decode the first argument: find the window or frame to use. */
226 if (EQ (position, Qt)
227 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
228 || EQ (XCAR (position), Qtool_bar))))
230 #if 0 /* Using the frame the mouse is on may not be right. */
231 /* Use the mouse's current position. */
232 struct frame *new_f = SELECTED_FRAME ();
233 Lisp_Object bar_window;
234 enum scroll_bar_part part;
235 Time time;
236 Lisp_Object x, y;
238 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
240 if (new_f != 0)
241 XSETFRAME (window, new_f);
242 else
243 window = selected_window;
244 #endif
245 window = selected_window;
247 else if (CONSP (position))
249 Lisp_Object tem = XCAR (position);
250 if (CONSP (tem))
251 window = Fcar (XCDR (position));
252 else
254 tem = Fcar (XCDR (position)); /* EVENT_START (position) */
255 window = Fcar (tem); /* POSN_WINDOW (tem) */
258 else if (WINDOWP (position) || FRAMEP (position))
259 window = position;
260 else
261 window = Qnil;
263 /* Decode where to put the menu. */
265 if (FRAMEP (window))
266 f = XFRAME (window);
267 else if (WINDOWP (window))
269 CHECK_LIVE_WINDOW (window);
270 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
272 else
273 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
274 but I don't want to make one now. */
275 CHECK_WINDOW (window);
277 check_window_system (f);
279 /* Force a redisplay before showing the dialog. If a frame is created
280 just before showing the dialog, its contents may not have been fully
281 drawn, as this depends on timing of events from the X server. Redisplay
282 is not done when a dialog is shown. If redisplay could be done in the
283 X event loop (i.e. the X event loop does not run in a signal handler)
284 this would not be needed.
286 Do this before creating the widget value that points to Lisp
287 string contents, because Fredisplay may GC and relocate them. */
288 Fredisplay (Qt);
290 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
291 /* Display a menu with these alternatives
292 in the middle of frame F. */
294 Lisp_Object x, y, frame, newpos;
295 XSETFRAME (frame, f);
296 XSETINT (x, FRAME_PIXEL_WIDTH (f) / 2);
297 XSETINT (y, FRAME_PIXEL_HEIGHT (f) / 2);
298 newpos = list2 (list2 (x, y), frame);
300 return Fx_popup_menu (newpos,
301 list2 (Fcar (contents), contents));
303 #else
305 Lisp_Object title;
306 const char *error_name;
307 Lisp_Object selection;
308 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
310 /* Decode the dialog items from what was specified. */
311 title = Fcar (contents);
312 CHECK_STRING (title);
313 record_unwind_protect_void (unuse_menu_items);
315 if (NILP (Fcar (Fcdr (contents))))
316 /* No buttons specified, add an "Ok" button so users can pop down
317 the dialog. Also, the lesstif/motif version crashes if there are
318 no buttons. */
319 contents = list2 (title, Fcons (build_string ("Ok"), Qt));
321 list_of_panes (list1 (contents));
323 /* Display them in a dialog box. */
324 block_input ();
325 selection = xdialog_show (f, 0, title, header, &error_name);
326 unblock_input ();
328 unbind_to (specpdl_count, Qnil);
329 discard_menu_items ();
331 if (error_name) error ("%s", error_name);
332 return selection;
334 #endif
338 #ifndef MSDOS
340 #if defined USE_GTK || defined USE_MOTIF
342 /* Set menu_items_inuse so no other popup menu or dialog is created. */
344 void
345 x_menu_set_in_use (int in_use)
347 menu_items_inuse = in_use ? Qt : Qnil;
348 popup_activated_flag = in_use;
349 #ifdef USE_X_TOOLKIT
350 if (popup_activated_flag)
351 x_activate_timeout_atimer ();
352 #endif
355 #endif
357 /* Wait for an X event to arrive or for a timer to expire. */
359 #ifndef USE_MOTIF
360 static
361 #endif
362 void
363 x_menu_wait_for_event (void *data)
365 /* Another way to do this is to register a timer callback, that can be
366 done in GTK and Xt. But we have to do it like this when using only X
367 anyway, and with callbacks we would have three variants for timer handling
368 instead of the small ifdefs below. */
370 while (
371 #ifdef USE_X_TOOLKIT
372 ! XtAppPending (Xt_app_con)
373 #elif defined USE_GTK
374 ! gtk_events_pending ()
375 #else
376 ! XPending (data)
377 #endif
380 struct timespec next_time = timer_check (), *ntp;
381 fd_set read_fds;
382 struct x_display_info *dpyinfo;
383 int n = 0;
385 FD_ZERO (&read_fds);
386 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
388 int fd = ConnectionNumber (dpyinfo->display);
389 FD_SET (fd, &read_fds);
390 if (fd > n) n = fd;
391 XFlush (dpyinfo->display);
394 if (! timespec_valid_p (next_time))
395 ntp = 0;
396 else
397 ntp = &next_time;
399 #if defined USE_GTK && defined HAVE_GTK3
400 /* Gtk3 have arrows on menus when they don't fit. When the
401 pointer is over an arrow, a timeout scrolls it a bit. Use
402 xg_select so that timeout gets triggered. */
403 xg_select (n + 1, &read_fds, NULL, NULL, ntp, NULL);
404 #else
405 pselect (n + 1, &read_fds, NULL, NULL, ntp, NULL);
406 #endif
409 #endif /* ! MSDOS */
412 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
414 #ifdef USE_X_TOOLKIT
416 /* Loop in Xt until the menu pulldown or dialog popup has been
417 popped down (deactivated). This is used for x-popup-menu
418 and x-popup-dialog; it is not used for the menu bar.
420 NOTE: All calls to popup_get_selection should be protected
421 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
423 static void
424 popup_get_selection (XEvent *initial_event, struct x_display_info *dpyinfo, LWLIB_ID id, int do_timers)
426 XEvent event;
428 while (popup_activated_flag)
430 if (initial_event)
432 event = *initial_event;
433 initial_event = 0;
435 else
437 if (do_timers) x_menu_wait_for_event (0);
438 XtAppNextEvent (Xt_app_con, &event);
441 /* Make sure we don't consider buttons grabbed after menu goes.
442 And make sure to deactivate for any ButtonRelease,
443 even if XtDispatchEvent doesn't do that. */
444 if (event.type == ButtonRelease
445 && dpyinfo->display == event.xbutton.display)
447 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
448 #ifdef USE_MOTIF /* Pretending that the event came from a
449 Btn1Down seems the only way to convince Motif to
450 activate its callbacks; setting the XmNmenuPost
451 isn't working. --marcus@sysc.pdx.edu. */
452 event.xbutton.button = 1;
453 /* Motif only pops down menus when no Ctrl, Alt or Mod
454 key is pressed and the button is released. So reset key state
455 so Motif thinks this is the case. */
456 event.xbutton.state = 0;
457 #endif
459 /* Pop down on C-g and Escape. */
460 else if (event.type == KeyPress
461 && dpyinfo->display == event.xbutton.display)
463 KeySym keysym = XLookupKeysym (&event.xkey, 0);
465 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
466 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
467 popup_activated_flag = 0;
470 x_dispatch_event (&event, event.xany.display);
474 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
475 doc: /* Start key navigation of the menu bar in FRAME.
476 This initially opens the first menu bar item and you can then navigate with the
477 arrow keys, select a menu entry with the return key or cancel with the
478 escape key. If FRAME has no menu bar this function does nothing.
480 If FRAME is nil or not given, use the selected frame. */)
481 (Lisp_Object frame)
483 XEvent ev;
484 struct frame *f = decode_window_system_frame (frame);
485 Widget menubar;
486 block_input ();
488 if (FRAME_EXTERNAL_MENU_BAR (f))
489 set_frame_menubar (f, 0, 1);
491 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
492 if (menubar)
494 Window child;
495 bool error_p = 0;
497 x_catch_errors (FRAME_X_DISPLAY (f));
498 memset (&ev, 0, sizeof ev);
499 ev.xbutton.display = FRAME_X_DISPLAY (f);
500 ev.xbutton.window = XtWindow (menubar);
501 ev.xbutton.root = FRAME_DISPLAY_INFO (f)->root_window;
502 ev.xbutton.time = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
503 ev.xbutton.button = Button1;
504 ev.xbutton.x = ev.xbutton.y = FRAME_MENUBAR_HEIGHT (f) / 2;
505 ev.xbutton.same_screen = True;
507 #ifdef USE_MOTIF
509 Arg al[2];
510 WidgetList list;
511 Cardinal nr;
512 XtSetArg (al[0], XtNchildren, &list);
513 XtSetArg (al[1], XtNnumChildren, &nr);
514 XtGetValues (menubar, al, 2);
515 ev.xbutton.window = XtWindow (list[0]);
517 #endif
519 XTranslateCoordinates (FRAME_X_DISPLAY (f),
520 /* From-window, to-window. */
521 ev.xbutton.window, ev.xbutton.root,
523 /* From-position, to-position. */
524 ev.xbutton.x, ev.xbutton.y,
525 &ev.xbutton.x_root, &ev.xbutton.y_root,
527 /* Child of win. */
528 &child);
529 error_p = x_had_errors_p (FRAME_X_DISPLAY (f));
530 x_uncatch_errors ();
532 if (! error_p)
534 ev.type = ButtonPress;
535 ev.xbutton.state = 0;
537 XtDispatchEvent (&ev);
538 ev.xbutton.type = ButtonRelease;
539 ev.xbutton.state = Button1Mask;
540 XtDispatchEvent (&ev);
544 unblock_input ();
546 return Qnil;
548 #endif /* USE_X_TOOLKIT */
551 #ifdef USE_GTK
552 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
553 doc: /* Start key navigation of the menu bar in FRAME.
554 This initially opens the first menu bar item and you can then navigate with the
555 arrow keys, select a menu entry with the return key or cancel with the
556 escape key. If FRAME has no menu bar this function does nothing.
558 If FRAME is nil or not given, use the selected frame. */)
559 (Lisp_Object frame)
561 GtkWidget *menubar;
562 struct frame *f;
564 block_input ();
565 f = decode_window_system_frame (frame);
567 if (FRAME_EXTERNAL_MENU_BAR (f))
568 set_frame_menubar (f, 0, 1);
570 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
571 if (menubar)
573 /* Activate the first menu. */
574 GList *children = gtk_container_get_children (GTK_CONTAINER (menubar));
576 if (children)
578 g_signal_emit_by_name (children->data, "activate_item");
579 popup_activated_flag = 1;
580 g_list_free (children);
583 unblock_input ();
585 return Qnil;
588 /* Loop util popup_activated_flag is set to zero in a callback.
589 Used for popup menus and dialogs. */
591 static void
592 popup_widget_loop (int do_timers, GtkWidget *widget)
594 ++popup_activated_flag;
596 /* Process events in the Gtk event loop until done. */
597 while (popup_activated_flag)
599 if (do_timers) x_menu_wait_for_event (0);
600 gtk_main_iteration ();
603 #endif
605 /* Activate the menu bar of frame F.
606 This is called from keyboard.c when it gets the
607 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
609 To activate the menu bar, we use the X button-press event
610 that was saved in saved_menu_event.
611 That makes the toolkit do its thing.
613 But first we recompute the menu bar contents (the whole tree).
615 The reason for saving the button event until here, instead of
616 passing it to the toolkit right away, is that we can safely
617 execute Lisp code. */
619 void
620 x_activate_menubar (struct frame *f)
622 eassert (FRAME_X_P (f));
624 if (!f->output_data.x->saved_menu_event->type)
625 return;
627 #ifdef USE_GTK
628 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
629 f->output_data.x->saved_menu_event->xany.window))
630 return;
631 #endif
633 set_frame_menubar (f, 0, 1);
634 block_input ();
635 popup_activated_flag = 1;
636 #ifdef USE_GTK
637 XPutBackEvent (f->output_data.x->display_info->display,
638 f->output_data.x->saved_menu_event);
639 #else
640 XtDispatchEvent (f->output_data.x->saved_menu_event);
641 #endif
642 unblock_input ();
644 /* Ignore this if we get it a second time. */
645 f->output_data.x->saved_menu_event->type = 0;
648 /* This callback is invoked when the user selects a menubar cascade
649 pushbutton, but before the pulldown menu is posted. */
651 #ifndef USE_GTK
652 static void
653 popup_activate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
655 popup_activated_flag = 1;
656 #ifdef USE_X_TOOLKIT
657 x_activate_timeout_atimer ();
658 #endif
660 #endif
662 /* This callback is invoked when a dialog or menu is finished being
663 used and has been unposted. */
665 static void
666 popup_deactivate_callback (
667 #ifdef USE_GTK
668 GtkWidget *widget, gpointer client_data
669 #else
670 Widget widget, LWLIB_ID id, XtPointer client_data
671 #endif
674 popup_activated_flag = 0;
678 /* Function that finds the frame for WIDGET and shows the HELP text
679 for that widget.
680 F is the frame if known, or NULL if not known. */
681 static void
682 show_help_event (struct frame *f, xt_or_gtk_widget widget, Lisp_Object help)
684 Lisp_Object frame;
686 if (f)
688 XSETFRAME (frame, f);
689 kbd_buffer_store_help_event (frame, help);
691 else
693 #if 0 /* This code doesn't do anything useful. ++kfs */
694 /* WIDGET is the popup menu. It's parent is the frame's
695 widget. See which frame that is. */
696 xt_or_gtk_widget frame_widget = XtParent (widget);
697 Lisp_Object tail;
699 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
701 frame = XCAR (tail);
702 if (FRAMEP (frame)
703 && (f = XFRAME (frame),
704 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
705 break;
707 #endif
708 show_help_echo (help, Qnil, Qnil, Qnil);
712 /* Callback called when menu items are highlighted/unhighlighted
713 while moving the mouse over them. WIDGET is the menu bar or menu
714 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
715 the data structure for the menu item, or null in case of
716 unhighlighting. */
718 #ifdef USE_GTK
719 static void
720 menu_highlight_callback (GtkWidget *widget, gpointer call_data)
722 xg_menu_item_cb_data *cb_data;
723 Lisp_Object help;
725 cb_data = g_object_get_data (G_OBJECT (widget), XG_ITEM_DATA);
726 if (! cb_data) return;
728 help = call_data ? cb_data->help : Qnil;
730 /* If popup_activated_flag is greater than 1 we are in a popup menu.
731 Don't pass the frame to show_help_event for those.
732 Passing frame creates an Emacs event. As we are looping in
733 popup_widget_loop, it won't be handled. Passing NULL shows the tip
734 directly without using an Emacs event. This is what the Lucid code
735 does below. */
736 show_help_event (popup_activated_flag <= 1 ? cb_data->cl_data->f : NULL,
737 widget, help);
739 #else
740 static void
741 menu_highlight_callback (Widget widget, LWLIB_ID id, void *call_data)
743 widget_value *wv = call_data;
744 Lisp_Object help = wv ? wv->help : Qnil;
746 /* Determine the frame for the help event. */
747 struct frame *f = menubar_id_to_frame (id);
749 show_help_event (f, widget, help);
751 #endif
753 #ifdef USE_GTK
754 /* Gtk calls callbacks just because we tell it what item should be
755 selected in a radio group. If this variable is set to a non-zero
756 value, we are creating menus and don't want callbacks right now.
758 static int xg_crazy_callback_abort;
760 /* This callback is called from the menu bar pulldown menu
761 when the user makes a selection.
762 Figure out what the user chose
763 and put the appropriate events into the keyboard buffer. */
764 static void
765 menubar_selection_callback (GtkWidget *widget, gpointer client_data)
767 xg_menu_item_cb_data *cb_data = client_data;
769 if (xg_crazy_callback_abort)
770 return;
772 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
773 return;
775 /* For a group of radio buttons, GTK calls the selection callback first
776 for the item that was active before the selection and then for the one that
777 is active after the selection. For C-h k this means we get the help on
778 the deselected item and then the selected item is executed. Prevent that
779 by ignoring the non-active item. */
780 if (GTK_IS_RADIO_MENU_ITEM (widget)
781 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
782 return;
784 /* When a menu is popped down, X generates a focus event (i.e. focus
785 goes back to the frame below the menu). Since GTK buffers events,
786 we force it out here before the menu selection event. Otherwise
787 sit-for will exit at once if the focus event follows the menu selection
788 event. */
790 block_input ();
791 while (gtk_events_pending ())
792 gtk_main_iteration ();
793 unblock_input ();
795 find_and_call_menu_selection (cb_data->cl_data->f,
796 cb_data->cl_data->menu_bar_items_used,
797 cb_data->cl_data->menu_bar_vector,
798 cb_data->call_data);
801 #else /* not USE_GTK */
803 /* This callback is called from the menu bar pulldown menu
804 when the user makes a selection.
805 Figure out what the user chose
806 and put the appropriate events into the keyboard buffer. */
807 static void
808 menubar_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
810 struct frame *f;
812 f = menubar_id_to_frame (id);
813 if (!f)
814 return;
815 find_and_call_menu_selection (f, f->menu_bar_items_used,
816 f->menu_bar_vector, client_data);
818 #endif /* not USE_GTK */
820 /* Recompute all the widgets of frame F, when the menu bar has been
821 changed. */
823 static void
824 update_frame_menubar (struct frame *f)
826 #ifdef USE_GTK
827 xg_update_frame_menubar (f);
828 #else
829 struct x_output *x;
830 int columns, rows;
832 eassert (FRAME_X_P (f));
834 x = f->output_data.x;
836 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
837 return;
839 block_input ();
840 /* Save the size of the frame because the pane widget doesn't accept
841 to resize itself. So force it. */
842 columns = FRAME_COLS (f);
843 rows = FRAME_LINES (f);
845 /* Do the voodoo which means "I'm changing lots of things, don't try
846 to refigure sizes until I'm done." */
847 lw_refigure_widget (x->column_widget, False);
849 /* The order in which children are managed is the top to bottom
850 order in which they are displayed in the paned window. First,
851 remove the text-area widget. */
852 XtUnmanageChild (x->edit_widget);
854 /* Remove the menubar that is there now, and put up the menubar that
855 should be there. */
856 XtManageChild (x->menubar_widget);
857 XtMapWidget (x->menubar_widget);
858 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
860 /* Re-manage the text-area widget, and then thrash the sizes. */
861 XtManageChild (x->edit_widget);
862 lw_refigure_widget (x->column_widget, True);
864 /* Force the pane widget to resize itself with the right values. */
865 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
866 unblock_input ();
867 #endif
870 #ifdef USE_LUCID
871 static void
872 apply_systemfont_to_dialog (Widget w)
874 const char *fn = xsettings_get_system_normal_font ();
875 if (fn)
877 XrmDatabase db = XtDatabase (XtDisplay (w));
878 if (db)
879 XrmPutStringResource (&db, "*dialog.font", fn);
883 static void
884 apply_systemfont_to_menu (struct frame *f, Widget w)
886 const char *fn = xsettings_get_system_normal_font ();
888 if (fn)
890 XrmDatabase db = XtDatabase (XtDisplay (w));
891 if (db)
893 XrmPutStringResource (&db, "*menubar*font", fn);
894 XrmPutStringResource (&db, "*popup*font", fn);
899 #endif
901 /* Set the contents of the menubar widgets of frame F.
902 The argument FIRST_TIME is currently ignored;
903 it is set the first time this is called, from initialize_frame_menubar. */
905 void
906 set_frame_menubar (struct frame *f, bool first_time, bool deep_p)
908 xt_or_gtk_widget menubar_widget;
909 #ifdef USE_X_TOOLKIT
910 LWLIB_ID id;
911 #endif
912 Lisp_Object items;
913 widget_value *wv, *first_wv, *prev_wv = 0;
914 int i;
915 int *submenu_start, *submenu_end;
916 bool *submenu_top_level_items;
917 int *submenu_n_panes;
919 eassert (FRAME_X_P (f));
921 menubar_widget = f->output_data.x->menubar_widget;
923 XSETFRAME (Vmenu_updating_frame, f);
925 #ifdef USE_X_TOOLKIT
926 if (f->output_data.x->id == 0)
927 f->output_data.x->id = next_menubar_widget_id++;
928 id = f->output_data.x->id;
929 #endif
931 if (! menubar_widget)
932 deep_p = 1;
933 /* Make the first call for any given frame always go deep. */
934 else if (!f->output_data.x->saved_menu_event && !deep_p)
936 deep_p = 1;
937 f->output_data.x->saved_menu_event = xmalloc (sizeof (XEvent));
938 f->output_data.x->saved_menu_event->type = 0;
941 #ifdef USE_GTK
942 /* If we have detached menus, we must update deep so detached menus
943 also gets updated. */
944 deep_p = deep_p || xg_have_tear_offs ();
945 #endif
947 if (deep_p)
949 /* Make a widget-value tree representing the entire menu trees. */
951 struct buffer *prev = current_buffer;
952 Lisp_Object buffer;
953 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
954 int previous_menu_items_used = f->menu_bar_items_used;
955 Lisp_Object *previous_items
956 = alloca (previous_menu_items_used * sizeof *previous_items);
957 int subitems;
959 /* If we are making a new widget, its contents are empty,
960 do always reinitialize them. */
961 if (! menubar_widget)
962 previous_menu_items_used = 0;
964 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->contents;
965 specbind (Qinhibit_quit, Qt);
966 /* Don't let the debugger step into this code
967 because it is not reentrant. */
968 specbind (Qdebug_on_next_call, Qnil);
970 record_unwind_save_match_data ();
971 if (NILP (Voverriding_local_map_menu_flag))
973 specbind (Qoverriding_terminal_local_map, Qnil);
974 specbind (Qoverriding_local_map, Qnil);
977 set_buffer_internal_1 (XBUFFER (buffer));
979 /* Run the Lucid hook. */
980 safe_run_hooks (Qactivate_menubar_hook);
982 /* If it has changed current-menubar from previous value,
983 really recompute the menubar from the value. */
984 if (! NILP (Vlucid_menu_bar_dirty_flag))
985 call0 (Qrecompute_lucid_menubar);
986 safe_run_hooks (Qmenu_bar_update_hook);
987 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
989 items = FRAME_MENU_BAR_ITEMS (f);
991 /* Save the frame's previous menu bar contents data. */
992 if (previous_menu_items_used)
993 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->u.contents,
994 previous_menu_items_used * word_size);
996 /* Fill in menu_items with the current menu bar contents.
997 This can evaluate Lisp code. */
998 save_menu_items ();
1000 menu_items = f->menu_bar_vector;
1001 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
1002 subitems = ASIZE (items) / 4;
1003 submenu_start = alloca ((subitems + 1) * sizeof *submenu_start);
1004 submenu_end = alloca (subitems * sizeof *submenu_end);
1005 submenu_n_panes = alloca (subitems * sizeof *submenu_n_panes);
1006 submenu_top_level_items = alloca (subitems
1007 * sizeof *submenu_top_level_items);
1008 init_menu_items ();
1009 for (i = 0; i < subitems; i++)
1011 Lisp_Object key, string, maps;
1013 key = AREF (items, 4 * i);
1014 string = AREF (items, 4 * i + 1);
1015 maps = AREF (items, 4 * i + 2);
1016 if (NILP (string))
1017 break;
1019 submenu_start[i] = menu_items_used;
1021 menu_items_n_panes = 0;
1022 submenu_top_level_items[i]
1023 = parse_single_submenu (key, string, maps);
1024 submenu_n_panes[i] = menu_items_n_panes;
1026 submenu_end[i] = menu_items_used;
1029 submenu_start[i] = -1;
1030 finish_menu_items ();
1032 /* Convert menu_items into widget_value trees
1033 to display the menu. This cannot evaluate Lisp code. */
1035 wv = xmalloc_widget_value ();
1036 wv->name = "menubar";
1037 wv->value = 0;
1038 wv->enabled = 1;
1039 wv->button_type = BUTTON_TYPE_NONE;
1040 wv->help = Qnil;
1041 first_wv = wv;
1043 for (i = 0; submenu_start[i] >= 0; i++)
1045 menu_items_n_panes = submenu_n_panes[i];
1046 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
1047 submenu_top_level_items[i]);
1048 if (prev_wv)
1049 prev_wv->next = wv;
1050 else
1051 first_wv->contents = wv;
1052 /* Don't set wv->name here; GC during the loop might relocate it. */
1053 wv->enabled = 1;
1054 wv->button_type = BUTTON_TYPE_NONE;
1055 prev_wv = wv;
1058 set_buffer_internal_1 (prev);
1060 /* If there has been no change in the Lisp-level contents
1061 of the menu bar, skip redisplaying it. Just exit. */
1063 /* Compare the new menu items with the ones computed last time. */
1064 for (i = 0; i < previous_menu_items_used; i++)
1065 if (menu_items_used == i
1066 || (!EQ (previous_items[i], AREF (menu_items, i))))
1067 break;
1068 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1070 /* The menu items have not changed. Don't bother updating
1071 the menus in any form, since it would be a no-op. */
1072 free_menubar_widget_value_tree (first_wv);
1073 discard_menu_items ();
1074 unbind_to (specpdl_count, Qnil);
1075 return;
1078 /* The menu items are different, so store them in the frame. */
1079 fset_menu_bar_vector (f, menu_items);
1080 f->menu_bar_items_used = menu_items_used;
1082 /* This undoes save_menu_items. */
1083 unbind_to (specpdl_count, Qnil);
1085 /* Now GC cannot happen during the lifetime of the widget_value,
1086 so it's safe to store data from a Lisp_String. */
1087 wv = first_wv->contents;
1088 for (i = 0; i < ASIZE (items); i += 4)
1090 Lisp_Object string;
1091 string = AREF (items, i + 1);
1092 if (NILP (string))
1093 break;
1094 wv->name = SSDATA (string);
1095 update_submenu_strings (wv->contents);
1096 wv = wv->next;
1100 else
1102 /* Make a widget-value tree containing
1103 just the top level menu bar strings. */
1105 wv = xmalloc_widget_value ();
1106 wv->name = "menubar";
1107 wv->value = 0;
1108 wv->enabled = 1;
1109 wv->button_type = BUTTON_TYPE_NONE;
1110 wv->help = Qnil;
1111 first_wv = wv;
1113 items = FRAME_MENU_BAR_ITEMS (f);
1114 for (i = 0; i < ASIZE (items); i += 4)
1116 Lisp_Object string;
1118 string = AREF (items, i + 1);
1119 if (NILP (string))
1120 break;
1122 wv = xmalloc_widget_value ();
1123 wv->name = SSDATA (string);
1124 wv->value = 0;
1125 wv->enabled = 1;
1126 wv->button_type = BUTTON_TYPE_NONE;
1127 wv->help = Qnil;
1128 /* This prevents lwlib from assuming this
1129 menu item is really supposed to be empty. */
1130 /* The intptr_t cast avoids a warning.
1131 This value just has to be different from small integers. */
1132 wv->call_data = (void *) (intptr_t) (-1);
1134 if (prev_wv)
1135 prev_wv->next = wv;
1136 else
1137 first_wv->contents = wv;
1138 prev_wv = wv;
1141 /* Forget what we thought we knew about what is in the
1142 detailed contents of the menu bar menus.
1143 Changing the top level always destroys the contents. */
1144 f->menu_bar_items_used = 0;
1147 /* Create or update the menu bar widget. */
1149 block_input ();
1151 #ifdef USE_GTK
1152 xg_crazy_callback_abort = 1;
1153 if (menubar_widget)
1155 /* The fourth arg is DEEP_P, which says to consider the entire
1156 menu trees we supply, rather than just the menu bar item names. */
1157 xg_modify_menubar_widgets (menubar_widget,
1159 first_wv,
1160 deep_p,
1161 G_CALLBACK (menubar_selection_callback),
1162 G_CALLBACK (popup_deactivate_callback),
1163 G_CALLBACK (menu_highlight_callback));
1165 else
1167 menubar_widget
1168 = xg_create_widget ("menubar", "menubar", f, first_wv,
1169 G_CALLBACK (menubar_selection_callback),
1170 G_CALLBACK (popup_deactivate_callback),
1171 G_CALLBACK (menu_highlight_callback));
1173 f->output_data.x->menubar_widget = menubar_widget;
1177 #else /* not USE_GTK */
1178 if (menubar_widget)
1180 /* Disable resizing (done for Motif!) */
1181 lw_allow_resizing (f->output_data.x->widget, False);
1183 /* The third arg is DEEP_P, which says to consider the entire
1184 menu trees we supply, rather than just the menu bar item names. */
1185 lw_modify_all_widgets (id, first_wv, deep_p);
1187 /* Re-enable the edit widget to resize. */
1188 lw_allow_resizing (f->output_data.x->widget, True);
1190 else
1192 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
1193 XtTranslations override = XtParseTranslationTable (menuOverride);
1195 #ifdef USE_LUCID
1196 apply_systemfont_to_menu (f, f->output_data.x->column_widget);
1197 #endif
1198 menubar_widget = lw_create_widget ("menubar", "menubar", id,
1199 first_wv,
1200 f->output_data.x->column_widget,
1202 popup_activate_callback,
1203 menubar_selection_callback,
1204 popup_deactivate_callback,
1205 menu_highlight_callback);
1206 f->output_data.x->menubar_widget = menubar_widget;
1208 /* Make menu pop down on C-g. */
1209 XtOverrideTranslations (menubar_widget, override);
1213 int menubar_size;
1214 if (f->output_data.x->menubar_widget)
1215 XtRealizeWidget (f->output_data.x->menubar_widget);
1217 menubar_size
1218 = (f->output_data.x->menubar_widget
1219 ? (f->output_data.x->menubar_widget->core.height
1220 + f->output_data.x->menubar_widget->core.border_width)
1221 : 0);
1223 #if 1 /* Experimentally, we now get the right results
1224 for -geometry -0-0 without this. 24 Aug 96, rms.
1225 Maybe so, but the menu bar size is missing the pixels so the
1226 WM size hints are off by these pixels. Jan D, oct 2009. */
1227 #ifdef USE_LUCID
1228 if (FRAME_EXTERNAL_MENU_BAR (f))
1230 Dimension ibw = 0;
1231 XtVaGetValues (f->output_data.x->column_widget,
1232 XtNinternalBorderWidth, &ibw, NULL);
1233 menubar_size += ibw;
1235 #endif /* USE_LUCID */
1236 #endif /* 1 */
1238 f->output_data.x->menubar_height = menubar_size;
1240 #endif /* not USE_GTK */
1242 free_menubar_widget_value_tree (first_wv);
1243 update_frame_menubar (f);
1245 #ifdef USE_GTK
1246 xg_crazy_callback_abort = 0;
1247 #endif
1249 unblock_input ();
1252 /* Called from Fx_create_frame to create the initial menubar of a frame
1253 before it is mapped, so that the window is mapped with the menubar already
1254 there instead of us tacking it on later and thrashing the window after it
1255 is visible. */
1257 void
1258 initialize_frame_menubar (struct frame *f)
1260 /* This function is called before the first chance to redisplay
1261 the frame. It has to be, so the frame will have the right size. */
1262 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
1263 set_frame_menubar (f, 1, 1);
1267 /* Get rid of the menu bar of frame F, and free its storage.
1268 This is used when deleting a frame, and when turning off the menu bar.
1269 For GTK this function is in gtkutil.c. */
1271 #ifndef USE_GTK
1272 void
1273 free_frame_menubar (struct frame *f)
1275 Widget menubar_widget;
1277 eassert (FRAME_X_P (f));
1279 menubar_widget = f->output_data.x->menubar_widget;
1281 f->output_data.x->menubar_height = 0;
1283 if (menubar_widget)
1285 #ifdef USE_MOTIF
1286 /* Removing the menu bar magically changes the shell widget's x
1287 and y position of (0, 0) which, when the menu bar is turned
1288 on again, leads to pull-down menus appearing in strange
1289 positions near the upper-left corner of the display. This
1290 happens only with some window managers like twm and ctwm,
1291 but not with other like Motif's mwm or kwm, because the
1292 latter generate ConfigureNotify events when the menu bar
1293 is switched off, which fixes the shell position. */
1294 Position x0, y0, x1, y1;
1295 #endif
1297 block_input ();
1299 #ifdef USE_MOTIF
1300 if (f->output_data.x->widget)
1301 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
1302 #endif
1304 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1305 f->output_data.x->menubar_widget = NULL;
1307 if (f->output_data.x->widget)
1309 #ifdef USE_MOTIF
1310 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
1311 if (x1 == 0 && y1 == 0)
1312 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
1313 #endif
1314 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1316 unblock_input ();
1319 #endif /* not USE_GTK */
1321 #endif /* USE_X_TOOLKIT || USE_GTK */
1323 /* xmenu_show actually displays a menu using the panes and items in menu_items
1324 and returns the value selected from it.
1325 There are two versions of xmenu_show, one for Xt and one for Xlib.
1326 Both assume input is blocked by the caller. */
1328 /* F is the frame the menu is for.
1329 X and Y are the frame-relative specified position,
1330 relative to the inside upper left corner of the frame F.
1331 FOR_CLICK is true if this menu was invoked for a mouse click.
1332 KEYMAPS is true if this menu was specified with keymaps;
1333 in that case, we return a list containing the chosen item's value
1334 and perhaps also the pane's prefix.
1335 TITLE is the specified menu title.
1336 ERROR is a place to store an error message string in case of failure.
1337 (We return nil on failure, but the value doesn't actually matter.) */
1339 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1341 /* The item selected in the popup menu. */
1342 static Lisp_Object *volatile menu_item_selection;
1344 #ifdef USE_GTK
1346 /* Used when position a popup menu. See menu_position_func and
1347 create_and_show_popup_menu below. */
1348 struct next_popup_x_y
1350 struct frame *f;
1351 int x;
1352 int y;
1355 /* The menu position function to use if we are not putting a popup
1356 menu where the pointer is.
1357 MENU is the menu to pop up.
1358 X and Y shall on exit contain x/y where the menu shall pop up.
1359 PUSH_IN is not documented in the GTK manual.
1360 USER_DATA is any data passed in when calling gtk_menu_popup.
1361 Here it points to a struct next_popup_x_y where the coordinates
1362 to store in *X and *Y are as well as the frame for the popup.
1364 Here only X and Y are used. */
1365 static void
1366 menu_position_func (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer user_data)
1368 struct next_popup_x_y *data = user_data;
1369 GtkRequisition req;
1370 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (data->f);
1371 int disp_width = x_display_pixel_width (dpyinfo);
1372 int disp_height = x_display_pixel_height (dpyinfo);
1374 *x = data->x;
1375 *y = data->y;
1377 /* Check if there is room for the menu. If not, adjust x/y so that
1378 the menu is fully visible. */
1379 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1380 if (data->x + req.width > disp_width)
1381 *x -= data->x + req.width - disp_width;
1382 if (data->y + req.height > disp_height)
1383 *y -= data->y + req.height - disp_height;
1386 static void
1387 popup_selection_callback (GtkWidget *widget, gpointer client_data)
1389 xg_menu_item_cb_data *cb_data = client_data;
1391 if (xg_crazy_callback_abort) return;
1392 if (cb_data) menu_item_selection = cb_data->call_data;
1395 static void
1396 pop_down_menu (void *arg)
1398 popup_activated_flag = 0;
1399 block_input ();
1400 gtk_widget_destroy (GTK_WIDGET (arg));
1401 unblock_input ();
1404 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1405 menu pops down.
1406 menu_item_selection will be set to the selection. */
1407 static void
1408 create_and_show_popup_menu (struct frame *f, widget_value *first_wv, int x, int y,
1409 bool for_click, Time timestamp)
1411 int i;
1412 GtkWidget *menu;
1413 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
1414 struct next_popup_x_y popup_x_y;
1415 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1416 bool use_pos_func = ! for_click;
1418 #ifdef HAVE_GTK3
1419 /* Always use position function for Gtk3. Otherwise menus may become
1420 too small to show anything. */
1421 use_pos_func = 1;
1422 #endif
1424 eassert (FRAME_X_P (f));
1426 xg_crazy_callback_abort = 1;
1427 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
1428 G_CALLBACK (popup_selection_callback),
1429 G_CALLBACK (popup_deactivate_callback),
1430 G_CALLBACK (menu_highlight_callback));
1431 xg_crazy_callback_abort = 0;
1433 if (use_pos_func)
1435 /* Not invoked by a click. pop up at x/y. */
1436 pos_func = menu_position_func;
1438 /* Adjust coordinates to be root-window-relative. */
1439 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1440 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1442 popup_x_y.x = x;
1443 popup_x_y.y = y;
1444 popup_x_y.f = f;
1446 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1449 if (for_click)
1451 for (i = 0; i < 5; i++)
1452 if (FRAME_DISPLAY_INFO (f)->grabbed & (1 << i))
1453 break;
1456 /* Display the menu. */
1457 gtk_widget_show_all (menu);
1459 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i,
1460 timestamp ? timestamp : gtk_get_current_event_time ());
1462 record_unwind_protect_ptr (pop_down_menu, menu);
1464 if (gtk_widget_get_mapped (menu))
1466 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1467 two. show_help_echo uses this to detect popup menus. */
1468 popup_activated_flag = 1;
1469 /* Process events that apply to the menu. */
1470 popup_widget_loop (1, menu);
1473 unbind_to (specpdl_count, Qnil);
1475 /* Must reset this manually because the button release event is not passed
1476 to Emacs event loop. */
1477 FRAME_DISPLAY_INFO (f)->grabbed = 0;
1480 #else /* not USE_GTK */
1482 /* We need a unique id for each widget handled by the Lucid Widget
1483 library.
1485 For the main windows, and popup menus, we use this counter,
1486 which we increment each time after use. This starts from 1<<16.
1488 For menu bars, we use numbers starting at 0, counted in
1489 next_menubar_widget_id. */
1490 LWLIB_ID widget_id_tick;
1492 static void
1493 popup_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1495 menu_item_selection = client_data;
1498 /* ARG is the LWLIB ID of the dialog box, represented
1499 as a Lisp object as (HIGHPART . LOWPART). */
1501 static void
1502 pop_down_menu (Lisp_Object arg)
1504 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
1505 | XINT (XCDR (arg)));
1507 block_input ();
1508 lw_destroy_all_widgets (id);
1509 unblock_input ();
1510 popup_activated_flag = 0;
1513 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1514 menu pops down.
1515 menu_item_selection will be set to the selection. */
1516 static void
1517 create_and_show_popup_menu (struct frame *f, widget_value *first_wv,
1518 int x, int y, bool for_click, Time timestamp)
1520 int i;
1521 Arg av[2];
1522 int ac = 0;
1523 XEvent dummy;
1524 XButtonPressedEvent *event = &(dummy.xbutton);
1525 LWLIB_ID menu_id;
1526 Widget menu;
1528 eassert (FRAME_X_P (f));
1530 #ifdef USE_LUCID
1531 apply_systemfont_to_menu (f, f->output_data.x->widget);
1532 #endif
1534 menu_id = widget_id_tick++;
1535 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1536 f->output_data.x->widget, 1, 0,
1537 popup_selection_callback,
1538 popup_deactivate_callback,
1539 menu_highlight_callback);
1541 event->type = ButtonPress;
1542 event->serial = 0;
1543 event->send_event = 0;
1544 event->display = FRAME_X_DISPLAY (f);
1545 event->time = CurrentTime;
1546 event->root = FRAME_DISPLAY_INFO (f)->root_window;
1547 event->window = event->subwindow = event->root;
1548 event->x = x;
1549 event->y = y;
1551 /* Adjust coordinates to be root-window-relative. */
1552 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1553 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1555 event->x_root = x;
1556 event->y_root = y;
1558 event->state = 0;
1559 event->button = 0;
1560 for (i = 0; i < 5; i++)
1561 if (FRAME_DISPLAY_INFO (f)->grabbed & (1 << i))
1562 event->button = i;
1564 /* Don't allow any geometry request from the user. */
1565 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1566 XtSetValues (menu, av, ac);
1568 /* Display the menu. */
1569 lw_popup_menu (menu, &dummy);
1570 popup_activated_flag = 1;
1571 x_activate_timeout_atimer ();
1574 int fact = 4 * sizeof (LWLIB_ID);
1575 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1576 record_unwind_protect (pop_down_menu,
1577 Fcons (make_number (menu_id >> (fact)),
1578 make_number (menu_id & ~(-1 << (fact)))));
1580 /* Process events that apply to the menu. */
1581 popup_get_selection (0, FRAME_DISPLAY_INFO (f), menu_id, 1);
1583 unbind_to (specpdl_count, Qnil);
1587 #endif /* not USE_GTK */
1589 static void
1590 cleanup_widget_value_tree (void *arg)
1592 free_menubar_widget_value_tree (arg);
1595 Lisp_Object
1596 xmenu_show (struct frame *f, int x, int y, bool for_click, bool keymaps,
1597 Lisp_Object title, const char **error_name, Time timestamp)
1599 int i;
1600 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1601 widget_value **submenu_stack
1602 = alloca (menu_items_used * sizeof *submenu_stack);
1603 Lisp_Object *subprefix_stack
1604 = alloca (menu_items_used * sizeof *subprefix_stack);
1605 int submenu_depth = 0;
1607 int first_pane;
1609 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1611 eassert (FRAME_X_P (f));
1613 *error_name = NULL;
1615 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1617 *error_name = "Empty menu";
1618 return Qnil;
1621 block_input ();
1623 /* Create a tree of widget_value objects
1624 representing the panes and their items. */
1625 wv = xmalloc_widget_value ();
1626 wv->name = "menu";
1627 wv->value = 0;
1628 wv->enabled = 1;
1629 wv->button_type = BUTTON_TYPE_NONE;
1630 wv->help =Qnil;
1631 first_wv = wv;
1632 first_pane = 1;
1634 /* Loop over all panes and items, filling in the tree. */
1635 i = 0;
1636 while (i < menu_items_used)
1638 if (EQ (AREF (menu_items, i), Qnil))
1640 submenu_stack[submenu_depth++] = save_wv;
1641 save_wv = prev_wv;
1642 prev_wv = 0;
1643 first_pane = 1;
1644 i++;
1646 else if (EQ (AREF (menu_items, i), Qlambda))
1648 prev_wv = save_wv;
1649 save_wv = submenu_stack[--submenu_depth];
1650 first_pane = 0;
1651 i++;
1653 else if (EQ (AREF (menu_items, i), Qt)
1654 && submenu_depth != 0)
1655 i += MENU_ITEMS_PANE_LENGTH;
1656 /* Ignore a nil in the item list.
1657 It's meaningful only for dialog boxes. */
1658 else if (EQ (AREF (menu_items, i), Qquote))
1659 i += 1;
1660 else if (EQ (AREF (menu_items, i), Qt))
1662 /* Create a new pane. */
1663 Lisp_Object pane_name, prefix;
1664 const char *pane_string;
1666 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1667 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1669 #ifndef HAVE_MULTILINGUAL_MENU
1670 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1672 pane_name = ENCODE_MENU_STRING (pane_name);
1673 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
1675 #endif
1676 pane_string = (NILP (pane_name)
1677 ? "" : SSDATA (pane_name));
1678 /* If there is just one top-level pane, put all its items directly
1679 under the top-level menu. */
1680 if (menu_items_n_panes == 1)
1681 pane_string = "";
1683 /* If the pane has a meaningful name,
1684 make the pane a top-level menu item
1685 with its items as a submenu beneath it. */
1686 if (!keymaps && strcmp (pane_string, ""))
1688 wv = xmalloc_widget_value ();
1689 if (save_wv)
1690 save_wv->next = wv;
1691 else
1692 first_wv->contents = wv;
1693 wv->name = (char *) pane_string;
1694 if (keymaps && !NILP (prefix))
1695 wv->name++;
1696 wv->value = 0;
1697 wv->enabled = 1;
1698 wv->button_type = BUTTON_TYPE_NONE;
1699 wv->help = Qnil;
1700 save_wv = wv;
1701 prev_wv = 0;
1703 else if (first_pane)
1705 save_wv = wv;
1706 prev_wv = 0;
1708 first_pane = 0;
1709 i += MENU_ITEMS_PANE_LENGTH;
1711 else
1713 /* Create a new item within current pane. */
1714 Lisp_Object item_name, enable, descrip, def, type, selected, help;
1715 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1716 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1717 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1718 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1719 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1720 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1721 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1723 #ifndef HAVE_MULTILINGUAL_MENU
1724 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
1726 item_name = ENCODE_MENU_STRING (item_name);
1727 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
1730 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1732 descrip = ENCODE_MENU_STRING (descrip);
1733 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
1735 #endif /* not HAVE_MULTILINGUAL_MENU */
1737 wv = xmalloc_widget_value ();
1738 if (prev_wv)
1739 prev_wv->next = wv;
1740 else
1741 save_wv->contents = wv;
1742 wv->name = SSDATA (item_name);
1743 if (!NILP (descrip))
1744 wv->key = SSDATA (descrip);
1745 wv->value = 0;
1746 /* If this item has a null value,
1747 make the call_data null so that it won't display a box
1748 when the mouse is on it. */
1749 wv->call_data = !NILP (def) ? aref_addr (menu_items, i) : 0;
1750 wv->enabled = !NILP (enable);
1752 if (NILP (type))
1753 wv->button_type = BUTTON_TYPE_NONE;
1754 else if (EQ (type, QCtoggle))
1755 wv->button_type = BUTTON_TYPE_TOGGLE;
1756 else if (EQ (type, QCradio))
1757 wv->button_type = BUTTON_TYPE_RADIO;
1758 else
1759 emacs_abort ();
1761 wv->selected = !NILP (selected);
1763 if (! STRINGP (help))
1764 help = Qnil;
1766 wv->help = help;
1768 prev_wv = wv;
1770 i += MENU_ITEMS_ITEM_LENGTH;
1774 /* Deal with the title, if it is non-nil. */
1775 if (!NILP (title))
1777 widget_value *wv_title = xmalloc_widget_value ();
1778 widget_value *wv_sep1 = xmalloc_widget_value ();
1779 widget_value *wv_sep2 = xmalloc_widget_value ();
1781 wv_sep2->name = "--";
1782 wv_sep2->next = first_wv->contents;
1783 wv_sep2->help = Qnil;
1785 wv_sep1->name = "--";
1786 wv_sep1->next = wv_sep2;
1787 wv_sep1->help = Qnil;
1789 #ifndef HAVE_MULTILINGUAL_MENU
1790 if (STRING_MULTIBYTE (title))
1791 title = ENCODE_MENU_STRING (title);
1792 #endif
1794 wv_title->name = SSDATA (title);
1795 wv_title->enabled = TRUE;
1796 wv_title->button_type = BUTTON_TYPE_NONE;
1797 wv_title->help = Qnil;
1798 wv_title->next = wv_sep1;
1799 first_wv->contents = wv_title;
1802 /* No selection has been chosen yet. */
1803 menu_item_selection = 0;
1805 /* Make sure to free the widget_value objects we used to specify the
1806 contents even with longjmp. */
1807 record_unwind_protect_ptr (cleanup_widget_value_tree, first_wv);
1809 /* Actually create and show the menu until popped down. */
1810 create_and_show_popup_menu (f, first_wv, x, y, for_click, timestamp);
1812 unbind_to (specpdl_count, Qnil);
1814 /* Find the selected item, and its pane, to return
1815 the proper value. */
1816 if (menu_item_selection != 0)
1818 Lisp_Object prefix, entry;
1820 prefix = entry = Qnil;
1821 i = 0;
1822 while (i < menu_items_used)
1824 if (EQ (AREF (menu_items, i), Qnil))
1826 subprefix_stack[submenu_depth++] = prefix;
1827 prefix = entry;
1828 i++;
1830 else if (EQ (AREF (menu_items, i), Qlambda))
1832 prefix = subprefix_stack[--submenu_depth];
1833 i++;
1835 else if (EQ (AREF (menu_items, i), Qt))
1837 prefix
1838 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1839 i += MENU_ITEMS_PANE_LENGTH;
1841 /* Ignore a nil in the item list.
1842 It's meaningful only for dialog boxes. */
1843 else if (EQ (AREF (menu_items, i), Qquote))
1844 i += 1;
1845 else
1847 entry
1848 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
1849 if (menu_item_selection == aref_addr (menu_items, i))
1851 if (keymaps)
1853 int j;
1855 entry = list1 (entry);
1856 if (!NILP (prefix))
1857 entry = Fcons (prefix, entry);
1858 for (j = submenu_depth - 1; j >= 0; j--)
1859 if (!NILP (subprefix_stack[j]))
1860 entry = Fcons (subprefix_stack[j], entry);
1862 unblock_input ();
1863 return entry;
1865 i += MENU_ITEMS_ITEM_LENGTH;
1869 else if (!for_click)
1871 unblock_input ();
1872 /* Make "Cancel" equivalent to C-g. */
1873 Fsignal (Qquit, Qnil);
1876 unblock_input ();
1877 return Qnil;
1880 #ifdef USE_GTK
1881 static void
1882 dialog_selection_callback (GtkWidget *widget, gpointer client_data)
1884 /* Treat the pointer as an integer. There's no problem
1885 as long as pointers have enough bits to hold small integers. */
1886 if ((intptr_t) client_data != -1)
1887 menu_item_selection = client_data;
1889 popup_activated_flag = 0;
1892 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1893 dialog pops down.
1894 menu_item_selection will be set to the selection. */
1895 static void
1896 create_and_show_dialog (struct frame *f, widget_value *first_wv)
1898 GtkWidget *menu;
1900 eassert (FRAME_X_P (f));
1902 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
1903 G_CALLBACK (dialog_selection_callback),
1904 G_CALLBACK (popup_deactivate_callback),
1907 if (menu)
1909 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1910 record_unwind_protect_ptr (pop_down_menu, menu);
1912 /* Display the menu. */
1913 gtk_widget_show_all (menu);
1915 /* Process events that apply to the menu. */
1916 popup_widget_loop (1, menu);
1918 unbind_to (specpdl_count, Qnil);
1922 #else /* not USE_GTK */
1923 static void
1924 dialog_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1926 /* Treat the pointer as an integer. There's no problem
1927 as long as pointers have enough bits to hold small integers. */
1928 if ((intptr_t) client_data != -1)
1929 menu_item_selection = client_data;
1931 block_input ();
1932 lw_destroy_all_widgets (id);
1933 unblock_input ();
1934 popup_activated_flag = 0;
1938 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1939 dialog pops down.
1940 menu_item_selection will be set to the selection. */
1941 static void
1942 create_and_show_dialog (struct frame *f, widget_value *first_wv)
1944 LWLIB_ID dialog_id;
1946 eassert (FRAME_X_P (f));
1948 dialog_id = widget_id_tick++;
1949 #ifdef USE_LUCID
1950 apply_systemfont_to_dialog (f->output_data.x->widget);
1951 #endif
1952 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1953 f->output_data.x->widget, 1, 0,
1954 dialog_selection_callback, 0, 0);
1955 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1956 /* Display the dialog box. */
1957 lw_pop_up_all_widgets (dialog_id);
1958 popup_activated_flag = 1;
1959 x_activate_timeout_atimer ();
1961 /* Process events that apply to the dialog box.
1962 Also handle timers. */
1964 ptrdiff_t count = SPECPDL_INDEX ();
1965 int fact = 4 * sizeof (LWLIB_ID);
1967 /* xdialog_show_unwind is responsible for popping the dialog box down. */
1968 record_unwind_protect (pop_down_menu,
1969 Fcons (make_number (dialog_id >> (fact)),
1970 make_number (dialog_id & ~(-1 << (fact)))));
1972 popup_get_selection (0, FRAME_DISPLAY_INFO (f), dialog_id, 1);
1974 unbind_to (count, Qnil);
1978 #endif /* not USE_GTK */
1980 static const char * button_names [] = {
1981 "button1", "button2", "button3", "button4", "button5",
1982 "button6", "button7", "button8", "button9", "button10" };
1984 static Lisp_Object
1985 xdialog_show (struct frame *f,
1986 bool keymaps,
1987 Lisp_Object title,
1988 Lisp_Object header,
1989 const char **error_name)
1991 int i, nb_buttons=0;
1992 char dialog_name[6];
1994 widget_value *wv, *first_wv = 0, *prev_wv = 0;
1996 /* Number of elements seen so far, before boundary. */
1997 int left_count = 0;
1998 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1999 int boundary_seen = 0;
2001 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
2003 eassert (FRAME_X_P (f));
2005 *error_name = NULL;
2007 if (menu_items_n_panes > 1)
2009 *error_name = "Multiple panes in dialog box";
2010 return Qnil;
2013 /* Create a tree of widget_value objects
2014 representing the text label and buttons. */
2016 Lisp_Object pane_name, prefix;
2017 const char *pane_string;
2018 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
2019 prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX);
2020 pane_string = (NILP (pane_name)
2021 ? "" : SSDATA (pane_name));
2022 prev_wv = xmalloc_widget_value ();
2023 prev_wv->value = (char *) pane_string;
2024 if (keymaps && !NILP (prefix))
2025 prev_wv->name++;
2026 prev_wv->enabled = 1;
2027 prev_wv->name = "message";
2028 prev_wv->help = Qnil;
2029 first_wv = prev_wv;
2031 /* Loop over all panes and items, filling in the tree. */
2032 i = MENU_ITEMS_PANE_LENGTH;
2033 while (i < menu_items_used)
2036 /* Create a new item within current pane. */
2037 Lisp_Object item_name, enable, descrip;
2038 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2039 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2040 descrip
2041 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2043 if (NILP (item_name))
2045 free_menubar_widget_value_tree (first_wv);
2046 *error_name = "Submenu in dialog items";
2047 return Qnil;
2049 if (EQ (item_name, Qquote))
2051 /* This is the boundary between left-side elts
2052 and right-side elts. Stop incrementing right_count. */
2053 boundary_seen = 1;
2054 i++;
2055 continue;
2057 if (nb_buttons >= 9)
2059 free_menubar_widget_value_tree (first_wv);
2060 *error_name = "Too many dialog items";
2061 return Qnil;
2064 wv = xmalloc_widget_value ();
2065 prev_wv->next = wv;
2066 wv->name = (char *) button_names[nb_buttons];
2067 if (!NILP (descrip))
2068 wv->key = SSDATA (descrip);
2069 wv->value = SSDATA (item_name);
2070 wv->call_data = aref_addr (menu_items, i);
2071 wv->enabled = !NILP (enable);
2072 wv->help = Qnil;
2073 prev_wv = wv;
2075 if (! boundary_seen)
2076 left_count++;
2078 nb_buttons++;
2079 i += MENU_ITEMS_ITEM_LENGTH;
2082 /* If the boundary was not specified,
2083 by default put half on the left and half on the right. */
2084 if (! boundary_seen)
2085 left_count = nb_buttons - nb_buttons / 2;
2087 wv = xmalloc_widget_value ();
2088 wv->name = dialog_name;
2089 wv->help = Qnil;
2091 /* Frame title: 'Q' = Question, 'I' = Information.
2092 Can also have 'E' = Error if, one day, we want
2093 a popup for errors. */
2094 if (NILP (header))
2095 dialog_name[0] = 'Q';
2096 else
2097 dialog_name[0] = 'I';
2099 /* Dialog boxes use a really stupid name encoding
2100 which specifies how many buttons to use
2101 and how many buttons are on the right. */
2102 dialog_name[1] = '0' + nb_buttons;
2103 dialog_name[2] = 'B';
2104 dialog_name[3] = 'R';
2105 /* Number of buttons to put on the right. */
2106 dialog_name[4] = '0' + nb_buttons - left_count;
2107 dialog_name[5] = 0;
2108 wv->contents = first_wv;
2109 first_wv = wv;
2112 /* No selection has been chosen yet. */
2113 menu_item_selection = 0;
2115 /* Make sure to free the widget_value objects we used to specify the
2116 contents even with longjmp. */
2117 record_unwind_protect_ptr (cleanup_widget_value_tree, first_wv);
2119 /* Actually create and show the dialog. */
2120 create_and_show_dialog (f, first_wv);
2122 unbind_to (specpdl_count, Qnil);
2124 /* Find the selected item, and its pane, to return
2125 the proper value. */
2126 if (menu_item_selection != 0)
2128 Lisp_Object prefix;
2130 prefix = Qnil;
2131 i = 0;
2132 while (i < menu_items_used)
2134 Lisp_Object entry;
2136 if (EQ (AREF (menu_items, i), Qt))
2138 prefix
2139 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2140 i += MENU_ITEMS_PANE_LENGTH;
2142 else if (EQ (AREF (menu_items, i), Qquote))
2144 /* This is the boundary between left-side elts and
2145 right-side elts. */
2146 ++i;
2148 else
2150 entry
2151 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
2152 if (menu_item_selection == aref_addr (menu_items, i))
2154 if (keymaps != 0)
2156 entry = list1 (entry);
2157 if (!NILP (prefix))
2158 entry = Fcons (prefix, entry);
2160 return entry;
2162 i += MENU_ITEMS_ITEM_LENGTH;
2166 else
2167 /* Make "Cancel" equivalent to C-g. */
2168 Fsignal (Qquit, Qnil);
2170 return Qnil;
2173 #else /* not USE_X_TOOLKIT && not USE_GTK */
2175 /* The frame of the last activated non-toolkit menu bar.
2176 Used to generate menu help events. */
2178 static struct frame *menu_help_frame;
2181 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2183 PANE is the pane number, and ITEM is the menu item number in
2184 the menu (currently not used).
2186 This cannot be done with generating a HELP_EVENT because
2187 XMenuActivate contains a loop that doesn't let Emacs process
2188 keyboard events. */
2190 static void
2191 menu_help_callback (char const *help_string, int pane, int item)
2193 Lisp_Object *first_item;
2194 Lisp_Object pane_name;
2195 Lisp_Object menu_object;
2197 first_item = XVECTOR (menu_items)->u.contents;
2198 if (EQ (first_item[0], Qt))
2199 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2200 else if (EQ (first_item[0], Qquote))
2201 /* This shouldn't happen, see xmenu_show. */
2202 pane_name = empty_unibyte_string;
2203 else
2204 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2206 /* (menu-item MENU-NAME PANE-NUMBER) */
2207 menu_object = list3 (Qmenu_item, pane_name, make_number (pane));
2208 show_help_echo (help_string ? build_string (help_string) : Qnil,
2209 Qnil, menu_object, make_number (item));
2212 static void
2213 pop_down_menu (Lisp_Object arg)
2215 struct frame *f = XSAVE_POINTER (arg, 0);
2216 XMenu *menu = XSAVE_POINTER (arg, 1);
2218 block_input ();
2219 #ifndef MSDOS
2220 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
2221 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
2222 #endif
2223 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2225 #ifdef HAVE_X_WINDOWS
2226 /* Assume the mouse has moved out of the X window.
2227 If it has actually moved in, we will get an EnterNotify. */
2228 x_mouse_leave (FRAME_DISPLAY_INFO (f));
2230 /* State that no mouse buttons are now held.
2231 (The oldXMenu code doesn't track this info for us.)
2232 That is not necessarily true, but the fiction leads to reasonable
2233 results, and it is a pain to ask which are actually held now. */
2234 FRAME_DISPLAY_INFO (f)->grabbed = 0;
2236 #endif /* HAVE_X_WINDOWS */
2238 unblock_input ();
2242 Lisp_Object
2243 xmenu_show (struct frame *f, int x, int y, bool for_click, bool keymaps,
2244 Lisp_Object title, const char **error_name, Time timestamp)
2246 Window root;
2247 XMenu *menu;
2248 int pane, selidx, lpane, status;
2249 Lisp_Object entry, pane_prefix;
2250 char *datap;
2251 int ulx, uly, width, height;
2252 int dispwidth, dispheight;
2253 int i, j, lines, maxlines;
2254 int maxwidth;
2255 int dummy_int;
2256 unsigned int dummy_uint;
2257 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
2259 eassert (FRAME_X_P (f) || FRAME_MSDOS_P (f));
2261 *error_name = 0;
2262 if (menu_items_n_panes == 0)
2263 return Qnil;
2265 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2267 *error_name = "Empty menu";
2268 return Qnil;
2271 block_input ();
2273 /* Figure out which root window F is on. */
2274 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2275 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2276 &dummy_uint, &dummy_uint);
2278 /* Make the menu on that window. */
2279 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2280 if (menu == NULL)
2282 *error_name = "Can't create menu";
2283 unblock_input ();
2284 return Qnil;
2287 /* Don't GC while we prepare and show the menu,
2288 because we give the oldxmenu library pointers to the
2289 contents of strings. */
2290 inhibit_garbage_collection ();
2292 #ifdef HAVE_X_WINDOWS
2293 /* Adjust coordinates to relative to the outer (window manager) window. */
2294 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
2295 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
2296 #endif /* HAVE_X_WINDOWS */
2298 /* Adjust coordinates to be root-window-relative. */
2299 x += f->left_pos;
2300 y += f->top_pos;
2302 /* Create all the necessary panes and their items. */
2303 maxwidth = maxlines = lines = i = 0;
2304 lpane = XM_FAILURE;
2305 while (i < menu_items_used)
2307 if (EQ (AREF (menu_items, i), Qt))
2309 /* Create a new pane. */
2310 Lisp_Object pane_name, prefix;
2311 const char *pane_string;
2313 maxlines = max (maxlines, lines);
2314 lines = 0;
2315 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2316 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2317 pane_string = (NILP (pane_name)
2318 ? "" : SSDATA (pane_name));
2319 if (keymaps && !NILP (prefix))
2320 pane_string++;
2322 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2323 if (lpane == XM_FAILURE)
2325 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2326 *error_name = "Can't create pane";
2327 unblock_input ();
2328 return Qnil;
2330 i += MENU_ITEMS_PANE_LENGTH;
2332 /* Find the width of the widest item in this pane. */
2333 j = i;
2334 while (j < menu_items_used)
2336 Lisp_Object item;
2337 item = AREF (menu_items, j);
2338 if (EQ (item, Qt))
2339 break;
2340 if (NILP (item))
2342 j++;
2343 continue;
2345 width = SBYTES (item);
2346 if (width > maxwidth)
2347 maxwidth = width;
2349 j += MENU_ITEMS_ITEM_LENGTH;
2352 /* Ignore a nil in the item list.
2353 It's meaningful only for dialog boxes. */
2354 else if (EQ (AREF (menu_items, i), Qquote))
2355 i += 1;
2356 else
2358 /* Create a new item within current pane. */
2359 Lisp_Object item_name, enable, descrip, help;
2360 char *item_data;
2361 char const *help_string;
2363 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2364 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2365 descrip
2366 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2367 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2368 help_string = STRINGP (help) ? SSDATA (help) : NULL;
2370 if (!NILP (descrip))
2372 /* if alloca is fast, use that to make the space,
2373 to reduce gc needs. */
2374 item_data = alloca (maxwidth + SBYTES (descrip) + 1);
2375 memcpy (item_data, SSDATA (item_name), SBYTES (item_name));
2376 for (j = SCHARS (item_name); j < maxwidth; j++)
2377 item_data[j] = ' ';
2378 memcpy (item_data + j, SSDATA (descrip), SBYTES (descrip));
2379 item_data[j + SBYTES (descrip)] = 0;
2381 else
2382 item_data = SSDATA (item_name);
2384 if (lpane == XM_FAILURE
2385 || (XMenuAddSelection (FRAME_X_DISPLAY (f),
2386 menu, lpane, 0, item_data,
2387 !NILP (enable), help_string)
2388 == XM_FAILURE))
2390 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2391 *error_name = "Can't add selection to menu";
2392 unblock_input ();
2393 return Qnil;
2395 i += MENU_ITEMS_ITEM_LENGTH;
2396 lines++;
2400 maxlines = max (maxlines, lines);
2402 /* All set and ready to fly. */
2403 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2404 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2405 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2406 x = min (x, dispwidth);
2407 y = min (y, dispheight);
2408 x = max (x, 1);
2409 y = max (y, 1);
2410 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2411 &ulx, &uly, &width, &height);
2412 if (ulx+width > dispwidth)
2414 x -= (ulx + width) - dispwidth;
2415 ulx = dispwidth - width;
2417 if (uly+height > dispheight)
2419 y -= (uly + height) - dispheight;
2420 uly = dispheight - height;
2422 #ifndef HAVE_X_WINDOWS
2423 if (FRAME_HAS_MINIBUF_P (f) && uly+height > dispheight - 1)
2425 /* Move the menu away of the echo area, to avoid overwriting the
2426 menu with help echo messages or vice versa. */
2427 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
2429 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2430 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2432 else
2434 y--;
2435 uly--;
2438 #endif
2439 if (ulx < 0) x -= ulx;
2440 if (uly < 0) y -= uly;
2442 if (! for_click)
2444 /* If position was not given by a mouse click, adjust so upper left
2445 corner of the menu as a whole ends up at given coordinates. This
2446 is what x-popup-menu says in its documentation. */
2447 x += width/2;
2448 y += 1.5*height/(maxlines+2);
2451 XMenuSetAEQ (menu, TRUE);
2452 XMenuSetFreeze (menu, TRUE);
2453 pane = selidx = 0;
2455 #ifndef MSDOS
2456 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
2457 #endif
2459 record_unwind_protect (pop_down_menu, make_save_ptr_ptr (f, menu));
2461 /* Help display under X won't work because XMenuActivate contains
2462 a loop that doesn't give Emacs a chance to process it. */
2463 menu_help_frame = f;
2464 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2465 x, y, ButtonReleaseMask, &datap,
2466 menu_help_callback);
2467 entry = pane_prefix = Qnil;
2469 switch (status)
2471 case XM_SUCCESS:
2472 #ifdef XDEBUG
2473 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2474 #endif
2476 /* Find the item number SELIDX in pane number PANE. */
2477 i = 0;
2478 while (i < menu_items_used)
2480 if (EQ (AREF (menu_items, i), Qt))
2482 if (pane == 0)
2483 pane_prefix
2484 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2485 pane--;
2486 i += MENU_ITEMS_PANE_LENGTH;
2488 else
2490 if (pane == -1)
2492 if (selidx == 0)
2494 entry
2495 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
2496 if (keymaps)
2498 entry = list1 (entry);
2499 if (!NILP (pane_prefix))
2500 entry = Fcons (pane_prefix, entry);
2502 break;
2504 selidx--;
2506 i += MENU_ITEMS_ITEM_LENGTH;
2509 break;
2511 case XM_FAILURE:
2512 *error_name = "Can't activate menu";
2513 case XM_IA_SELECT:
2514 break;
2515 case XM_NO_SELECT:
2516 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2517 the menu was invoked with a mouse event as POSITION). */
2518 if (! for_click)
2520 unblock_input ();
2521 Fsignal (Qquit, Qnil);
2523 break;
2526 unblock_input ();
2527 unbind_to (specpdl_count, Qnil);
2529 return entry;
2532 #endif /* not USE_X_TOOLKIT */
2534 #endif /* HAVE_MENUS */
2536 #ifndef MSDOS
2537 /* Detect if a dialog or menu has been posted. MSDOS has its own
2538 implementation on msdos.c. */
2540 int ATTRIBUTE_CONST
2541 popup_activated (void)
2543 return popup_activated_flag;
2545 #endif /* not MSDOS */
2547 /* The following is used by delayed window autoselection. */
2549 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
2550 doc: /* Return t if a menu or popup dialog is active. */)
2551 (void)
2553 #ifdef HAVE_MENUS
2554 return (popup_activated ()) ? Qt : Qnil;
2555 #else
2556 return Qnil;
2557 #endif /* HAVE_MENUS */
2560 void
2561 syms_of_xmenu (void)
2563 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
2565 #ifdef USE_X_TOOLKIT
2566 widget_id_tick = (1<<16);
2567 next_menubar_widget_id = 1;
2568 #endif
2570 defsubr (&Smenu_or_popup_active_p);
2572 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
2573 defsubr (&Sx_menu_bar_open_internal);
2574 Ffset (intern_c_string ("accelerate-menu"),
2575 intern_c_string (Sx_menu_bar_open_internal.symbol_name));
2576 #endif
2578 #ifdef HAVE_MENUS
2579 defsubr (&Sx_popup_dialog);
2580 #endif