auto upstream
[emacs.git] / src / xmenu.c
blob3d76070c3364f3e0a464e99a480a4dd098bb2706
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 (FRAME_PTR, 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 FRAME_PTR 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 (FRAME_PTR f, int *x, int *y)
159 Window root, dummy_window;
160 int dummy;
162 if (! FRAME_X_P (f))
163 emacs_abort ();
165 block_input ();
167 XQueryPointer (FRAME_X_DISPLAY (f),
168 DefaultRootWindow (FRAME_X_DISPLAY (f)),
170 /* The root window which contains the pointer. */
171 &root,
173 /* Window pointer is on, not used */
174 &dummy_window,
176 /* The position on that root window. */
177 x, y,
179 /* x/y in dummy_window coordinates, not used. */
180 &dummy, &dummy,
182 /* Modifier keys and pointer buttons, about which
183 we don't care. */
184 (unsigned int *) &dummy);
186 unblock_input ();
188 /* xmenu_show expects window coordinates, not root window
189 coordinates. Translate. */
190 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
191 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
194 #endif /* HAVE_X_WINDOWS */
196 #ifdef HAVE_MENUS
198 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
199 doc: /* Pop up a dialog box and return user's selection.
200 POSITION specifies which frame to use.
201 This is normally a mouse button event or a window or frame.
202 If POSITION is t, it means to use the frame the mouse is on.
203 The dialog box appears in the middle of the specified frame.
205 CONTENTS specifies the alternatives to display in the dialog box.
206 It is a list of the form (DIALOG ITEM1 ITEM2...).
207 Each ITEM is a cons cell (STRING . VALUE).
208 The return value is VALUE from the chosen item.
210 An ITEM may also be just a string--that makes a nonselectable item.
211 An ITEM may also be nil--that means to put all preceding items
212 on the left of the dialog box and all following items on the right.
213 \(By default, approximately half appear on each side.)
215 If HEADER is non-nil, the frame title for the box is "Information",
216 otherwise it is "Question".
218 If the user gets rid of the dialog box without making a valid choice,
219 for instance using the window manager, then this produces a quit and
220 `x-popup-dialog' does not return. */)
221 (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
223 FRAME_PTR f = NULL;
224 Lisp_Object window;
226 check_x ();
228 /* Decode the first argument: find the window or frame to use. */
229 if (EQ (position, Qt)
230 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
231 || EQ (XCAR (position), Qtool_bar))))
233 #if 0 /* Using the frame the mouse is on may not be right. */
234 /* Use the mouse's current position. */
235 FRAME_PTR new_f = SELECTED_FRAME ();
236 Lisp_Object bar_window;
237 enum scroll_bar_part part;
238 Time time;
239 Lisp_Object x, y;
241 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
243 if (new_f != 0)
244 XSETFRAME (window, new_f);
245 else
246 window = selected_window;
247 #endif
248 window = selected_window;
250 else if (CONSP (position))
252 Lisp_Object tem = XCAR (position);
253 if (CONSP (tem))
254 window = Fcar (XCDR (position));
255 else
257 tem = Fcar (XCDR (position)); /* EVENT_START (position) */
258 window = Fcar (tem); /* POSN_WINDOW (tem) */
261 else if (WINDOWP (position) || FRAMEP (position))
262 window = position;
263 else
264 window = Qnil;
266 /* Decode where to put the menu. */
268 if (FRAMEP (window))
269 f = XFRAME (window);
270 else if (WINDOWP (window))
272 CHECK_LIVE_WINDOW (window);
273 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
275 else
276 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
277 but I don't want to make one now. */
278 CHECK_WINDOW (window);
280 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
281 error ("Can not put X dialog on this terminal");
283 /* Force a redisplay before showing the dialog. If a frame is created
284 just before showing the dialog, its contents may not have been fully
285 drawn, as this depends on timing of events from the X server. Redisplay
286 is not done when a dialog is shown. If redisplay could be done in the
287 X event loop (i.e. the X event loop does not run in a signal handler)
288 this would not be needed.
290 Do this before creating the widget value that points to Lisp
291 string contents, because Fredisplay may GC and relocate them. */
292 Fredisplay (Qt);
294 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
295 /* Display a menu with these alternatives
296 in the middle of frame F. */
298 Lisp_Object x, y, frame, newpos;
299 XSETFRAME (frame, f);
300 XSETINT (x, x_pixel_width (f) / 2);
301 XSETINT (y, x_pixel_height (f) / 2);
302 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
304 return Fx_popup_menu (newpos,
305 Fcons (Fcar (contents), Fcons (contents, Qnil)));
307 #else
309 Lisp_Object title;
310 const char *error_name;
311 Lisp_Object selection;
312 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
314 /* Decode the dialog items from what was specified. */
315 title = Fcar (contents);
316 CHECK_STRING (title);
317 record_unwind_protect (unuse_menu_items, Qnil);
319 if (NILP (Fcar (Fcdr (contents))))
320 /* No buttons specified, add an "Ok" button so users can pop down
321 the dialog. Also, the lesstif/motif version crashes if there are
322 no buttons. */
323 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
325 list_of_panes (Fcons (contents, Qnil));
327 /* Display them in a dialog box. */
328 block_input ();
329 selection = xdialog_show (f, 0, title, header, &error_name);
330 unblock_input ();
332 unbind_to (specpdl_count, Qnil);
333 discard_menu_items ();
335 if (error_name) error ("%s", error_name);
336 return selection;
338 #endif
342 #ifndef MSDOS
344 #if defined USE_GTK || defined USE_MOTIF
346 /* Set menu_items_inuse so no other popup menu or dialog is created. */
348 void
349 x_menu_set_in_use (int in_use)
351 menu_items_inuse = in_use ? Qt : Qnil;
352 popup_activated_flag = in_use;
353 #ifdef USE_X_TOOLKIT
354 if (popup_activated_flag)
355 x_activate_timeout_atimer ();
356 #endif
359 #endif
361 /* Wait for an X event to arrive or for a timer to expire. */
363 #ifndef USE_MOTIF
364 static
365 #endif
366 void
367 x_menu_wait_for_event (void *data)
369 /* Another way to do this is to register a timer callback, that can be
370 done in GTK and Xt. But we have to do it like this when using only X
371 anyway, and with callbacks we would have three variants for timer handling
372 instead of the small ifdefs below. */
374 while (
375 #ifdef USE_X_TOOLKIT
376 ! XtAppPending (Xt_app_con)
377 #elif defined USE_GTK
378 ! gtk_events_pending ()
379 #else
380 ! XPending ((Display*) data)
381 #endif
384 EMACS_TIME next_time = timer_check (), *ntp;
385 SELECT_TYPE read_fds;
386 struct x_display_info *dpyinfo;
387 int n = 0;
389 FD_ZERO (&read_fds);
390 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
392 int fd = ConnectionNumber (dpyinfo->display);
393 FD_SET (fd, &read_fds);
394 if (fd > n) n = fd;
395 XFlush (dpyinfo->display);
398 if (! EMACS_TIME_VALID_P (next_time))
399 ntp = 0;
400 else
401 ntp = &next_time;
403 #ifdef HAVE_GTK3
404 /* Gtk3 have arrows on menus when they don't fit. When the
405 pointer is over an arrow, a timeout scrolls it a bit. Use
406 xg_select so that timeout gets triggered. */
407 xg_select (n + 1, &read_fds, NULL, NULL, ntp, NULL);
408 #else
409 pselect (n + 1, &read_fds, NULL, NULL, ntp, NULL);
410 #endif
413 #endif /* ! MSDOS */
416 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
418 #ifdef USE_X_TOOLKIT
420 /* Loop in Xt until the menu pulldown or dialog popup has been
421 popped down (deactivated). This is used for x-popup-menu
422 and x-popup-dialog; it is not used for the menu bar.
424 NOTE: All calls to popup_get_selection should be protected
425 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
427 static void
428 popup_get_selection (XEvent *initial_event, struct x_display_info *dpyinfo, LWLIB_ID id, int do_timers)
430 XEvent event;
432 while (popup_activated_flag)
434 if (initial_event)
436 event = *initial_event;
437 initial_event = 0;
439 else
441 if (do_timers) x_menu_wait_for_event (0);
442 XtAppNextEvent (Xt_app_con, &event);
445 /* Make sure we don't consider buttons grabbed after menu goes.
446 And make sure to deactivate for any ButtonRelease,
447 even if XtDispatchEvent doesn't do that. */
448 if (event.type == ButtonRelease
449 && dpyinfo->display == event.xbutton.display)
451 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
452 #ifdef USE_MOTIF /* Pretending that the event came from a
453 Btn1Down seems the only way to convince Motif to
454 activate its callbacks; setting the XmNmenuPost
455 isn't working. --marcus@sysc.pdx.edu. */
456 event.xbutton.button = 1;
457 /* Motif only pops down menus when no Ctrl, Alt or Mod
458 key is pressed and the button is released. So reset key state
459 so Motif thinks this is the case. */
460 event.xbutton.state = 0;
461 #endif
463 /* Pop down on C-g and Escape. */
464 else if (event.type == KeyPress
465 && dpyinfo->display == event.xbutton.display)
467 KeySym keysym = XLookupKeysym (&event.xkey, 0);
469 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
470 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
471 popup_activated_flag = 0;
474 x_dispatch_event (&event, event.xany.display);
478 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
479 doc: /* Start key navigation of the menu bar in FRAME.
480 This initially opens the first menu bar item and you can then navigate with the
481 arrow keys, select a menu entry with the return key or cancel with the
482 escape key. If FRAME has no menu bar this function does nothing.
484 If FRAME is nil or not given, use the selected frame. */)
485 (Lisp_Object frame)
487 XEvent ev;
488 FRAME_PTR f = check_x_frame (frame);
489 Widget menubar;
490 block_input ();
492 if (FRAME_EXTERNAL_MENU_BAR (f))
493 set_frame_menubar (f, 0, 1);
495 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
496 if (menubar)
498 Window child;
499 int error_p = 0;
501 x_catch_errors (FRAME_X_DISPLAY (f));
502 memset (&ev, 0, sizeof ev);
503 ev.xbutton.display = FRAME_X_DISPLAY (f);
504 ev.xbutton.window = XtWindow (menubar);
505 ev.xbutton.root = FRAME_X_DISPLAY_INFO (f)->root_window;
506 ev.xbutton.time = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
507 ev.xbutton.button = Button1;
508 ev.xbutton.x = ev.xbutton.y = FRAME_MENUBAR_HEIGHT (f) / 2;
509 ev.xbutton.same_screen = True;
511 #ifdef USE_MOTIF
513 Arg al[2];
514 WidgetList list;
515 Cardinal nr;
516 XtSetArg (al[0], XtNchildren, &list);
517 XtSetArg (al[1], XtNnumChildren, &nr);
518 XtGetValues (menubar, al, 2);
519 ev.xbutton.window = XtWindow (list[0]);
521 #endif
523 XTranslateCoordinates (FRAME_X_DISPLAY (f),
524 /* From-window, to-window. */
525 ev.xbutton.window, ev.xbutton.root,
527 /* From-position, to-position. */
528 ev.xbutton.x, ev.xbutton.y,
529 &ev.xbutton.x_root, &ev.xbutton.y_root,
531 /* Child of win. */
532 &child);
533 error_p = x_had_errors_p (FRAME_X_DISPLAY (f));
534 x_uncatch_errors ();
536 if (! error_p)
538 ev.type = ButtonPress;
539 ev.xbutton.state = 0;
541 XtDispatchEvent (&ev);
542 ev.xbutton.type = ButtonRelease;
543 ev.xbutton.state = Button1Mask;
544 XtDispatchEvent (&ev);
548 unblock_input ();
550 return Qnil;
552 #endif /* USE_X_TOOLKIT */
555 #ifdef USE_GTK
556 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
557 doc: /* Start key navigation of the menu bar in FRAME.
558 This initially opens the first menu bar item and you can then navigate with the
559 arrow keys, select a menu entry with the return key or cancel with the
560 escape key. If FRAME has no menu bar this function does nothing.
562 If FRAME is nil or not given, use the selected frame. */)
563 (Lisp_Object frame)
565 GtkWidget *menubar;
566 FRAME_PTR f;
568 /* gcc 2.95 doesn't accept the FRAME_PTR declaration after
569 block_input (). */
571 block_input ();
572 f = check_x_frame (frame);
574 if (FRAME_EXTERNAL_MENU_BAR (f))
575 set_frame_menubar (f, 0, 1);
577 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
578 if (menubar)
580 /* Activate the first menu. */
581 GList *children = gtk_container_get_children (GTK_CONTAINER (menubar));
583 if (children)
585 g_signal_emit_by_name (children->data, "activate_item");
586 popup_activated_flag = 1;
587 g_list_free (children);
590 unblock_input ();
592 return Qnil;
595 /* Loop util popup_activated_flag is set to zero in a callback.
596 Used for popup menus and dialogs. */
598 static void
599 popup_widget_loop (int do_timers, GtkWidget *widget)
601 ++popup_activated_flag;
603 /* Process events in the Gtk event loop until done. */
604 while (popup_activated_flag)
606 if (do_timers) x_menu_wait_for_event (0);
607 gtk_main_iteration ();
610 #endif
612 /* Activate the menu bar of frame F.
613 This is called from keyboard.c when it gets the
614 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
616 To activate the menu bar, we use the X button-press event
617 that was saved in saved_menu_event.
618 That makes the toolkit do its thing.
620 But first we recompute the menu bar contents (the whole tree).
622 The reason for saving the button event until here, instead of
623 passing it to the toolkit right away, is that we can safely
624 execute Lisp code. */
626 void
627 x_activate_menubar (FRAME_PTR f)
629 if (! FRAME_X_P (f))
630 emacs_abort ();
632 if (!f->output_data.x->saved_menu_event->type)
633 return;
635 #ifdef USE_GTK
636 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
637 f->output_data.x->saved_menu_event->xany.window))
638 return;
639 #endif
641 set_frame_menubar (f, 0, 1);
642 block_input ();
643 popup_activated_flag = 1;
644 #ifdef USE_GTK
645 XPutBackEvent (f->output_data.x->display_info->display,
646 f->output_data.x->saved_menu_event);
647 #else
648 XtDispatchEvent (f->output_data.x->saved_menu_event);
649 #endif
650 unblock_input ();
652 /* Ignore this if we get it a second time. */
653 f->output_data.x->saved_menu_event->type = 0;
656 /* This callback is invoked when the user selects a menubar cascade
657 pushbutton, but before the pulldown menu is posted. */
659 #ifndef USE_GTK
660 static void
661 popup_activate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
663 popup_activated_flag = 1;
664 #ifdef USE_X_TOOLKIT
665 x_activate_timeout_atimer ();
666 #endif
668 #endif
670 /* This callback is invoked when a dialog or menu is finished being
671 used and has been unposted. */
673 static void
674 popup_deactivate_callback (
675 #ifdef USE_GTK
676 GtkWidget *widget, gpointer client_data
677 #else
678 Widget widget, LWLIB_ID id, XtPointer client_data
679 #endif
682 popup_activated_flag = 0;
686 /* Function that finds the frame for WIDGET and shows the HELP text
687 for that widget.
688 F is the frame if known, or NULL if not known. */
689 static void
690 show_help_event (FRAME_PTR f, xt_or_gtk_widget widget, Lisp_Object help)
692 Lisp_Object frame;
694 if (f)
696 XSETFRAME (frame, f);
697 kbd_buffer_store_help_event (frame, help);
699 else
701 #if 0 /* This code doesn't do anything useful. ++kfs */
702 /* WIDGET is the popup menu. It's parent is the frame's
703 widget. See which frame that is. */
704 xt_or_gtk_widget frame_widget = XtParent (widget);
705 Lisp_Object tail;
707 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
709 frame = XCAR (tail);
710 if (FRAMEP (frame)
711 && (f = XFRAME (frame),
712 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
713 break;
715 #endif
716 show_help_echo (help, Qnil, Qnil, Qnil);
720 /* Callback called when menu items are highlighted/unhighlighted
721 while moving the mouse over them. WIDGET is the menu bar or menu
722 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
723 the data structure for the menu item, or null in case of
724 unhighlighting. */
726 #ifdef USE_GTK
727 static void
728 menu_highlight_callback (GtkWidget *widget, gpointer call_data)
730 xg_menu_item_cb_data *cb_data;
731 Lisp_Object help;
733 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
734 XG_ITEM_DATA);
735 if (! cb_data) return;
737 help = call_data ? cb_data->help : Qnil;
739 /* If popup_activated_flag is greater than 1 we are in a popup menu.
740 Don't pass the frame to show_help_event for those.
741 Passing frame creates an Emacs event. As we are looping in
742 popup_widget_loop, it won't be handled. Passing NULL shows the tip
743 directly without using an Emacs event. This is what the Lucid code
744 does below. */
745 show_help_event (popup_activated_flag <= 1 ? cb_data->cl_data->f : NULL,
746 widget, help);
748 #else
749 static void
750 menu_highlight_callback (Widget widget, LWLIB_ID id, void *call_data)
752 struct frame *f;
753 Lisp_Object help;
755 widget_value *wv = (widget_value *) call_data;
757 help = wv ? wv->help : Qnil;
759 /* Determine the frame for the help event. */
760 f = menubar_id_to_frame (id);
762 show_help_event (f, widget, help);
764 #endif
766 #ifdef USE_GTK
767 /* Gtk calls callbacks just because we tell it what item should be
768 selected in a radio group. If this variable is set to a non-zero
769 value, we are creating menus and don't want callbacks right now.
771 static int xg_crazy_callback_abort;
773 /* This callback is called from the menu bar pulldown menu
774 when the user makes a selection.
775 Figure out what the user chose
776 and put the appropriate events into the keyboard buffer. */
777 static void
778 menubar_selection_callback (GtkWidget *widget, gpointer client_data)
780 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
782 if (xg_crazy_callback_abort)
783 return;
785 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
786 return;
788 /* For a group of radio buttons, GTK calls the selection callback first
789 for the item that was active before the selection and then for the one that
790 is active after the selection. For C-h k this means we get the help on
791 the deselected item and then the selected item is executed. Prevent that
792 by ignoring the non-active item. */
793 if (GTK_IS_RADIO_MENU_ITEM (widget)
794 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
795 return;
797 /* When a menu is popped down, X generates a focus event (i.e. focus
798 goes back to the frame below the menu). Since GTK buffers events,
799 we force it out here before the menu selection event. Otherwise
800 sit-for will exit at once if the focus event follows the menu selection
801 event. */
803 block_input ();
804 while (gtk_events_pending ())
805 gtk_main_iteration ();
806 unblock_input ();
808 find_and_call_menu_selection (cb_data->cl_data->f,
809 cb_data->cl_data->menu_bar_items_used,
810 cb_data->cl_data->menu_bar_vector,
811 cb_data->call_data);
814 #else /* not USE_GTK */
816 /* This callback is called from the menu bar pulldown menu
817 when the user makes a selection.
818 Figure out what the user chose
819 and put the appropriate events into the keyboard buffer. */
820 static void
821 menubar_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
823 FRAME_PTR f;
825 f = menubar_id_to_frame (id);
826 if (!f)
827 return;
828 find_and_call_menu_selection (f, f->menu_bar_items_used,
829 f->menu_bar_vector, client_data);
831 #endif /* not USE_GTK */
833 /* Recompute all the widgets of frame F, when the menu bar has been
834 changed. */
836 static void
837 update_frame_menubar (FRAME_PTR f)
839 #ifdef USE_GTK
840 xg_update_frame_menubar (f);
841 #else
842 struct x_output *x;
843 int columns, rows;
845 if (! FRAME_X_P (f))
846 emacs_abort ();
848 x = f->output_data.x;
850 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
851 return;
853 block_input ();
854 /* Save the size of the frame because the pane widget doesn't accept
855 to resize itself. So force it. */
856 columns = FRAME_COLS (f);
857 rows = FRAME_LINES (f);
859 /* Do the voodoo which means "I'm changing lots of things, don't try
860 to refigure sizes until I'm done." */
861 lw_refigure_widget (x->column_widget, False);
863 /* The order in which children are managed is the top to bottom
864 order in which they are displayed in the paned window. First,
865 remove the text-area widget. */
866 XtUnmanageChild (x->edit_widget);
868 /* Remove the menubar that is there now, and put up the menubar that
869 should be there. */
870 XtManageChild (x->menubar_widget);
871 XtMapWidget (x->menubar_widget);
872 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
874 /* Re-manage the text-area widget, and then thrash the sizes. */
875 XtManageChild (x->edit_widget);
876 lw_refigure_widget (x->column_widget, True);
878 /* Force the pane widget to resize itself with the right values. */
879 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
880 unblock_input ();
881 #endif
884 #ifdef USE_LUCID
885 static void
886 apply_systemfont_to_dialog (Widget w)
888 const char *fn = xsettings_get_system_normal_font ();
889 if (fn)
891 XrmDatabase db = XtDatabase (XtDisplay (w));
892 if (db)
893 XrmPutStringResource (&db, "*dialog.font", fn);
897 static void
898 apply_systemfont_to_menu (struct frame *f, Widget w)
900 const char *fn = xsettings_get_system_normal_font ();
902 if (fn)
904 XrmDatabase db = XtDatabase (XtDisplay (w));
905 if (db)
907 XrmPutStringResource (&db, "*menubar*font", fn);
908 XrmPutStringResource (&db, "*popup*font", fn);
913 #endif
915 /* Set the contents of the menubar widgets of frame F.
916 The argument FIRST_TIME is currently ignored;
917 it is set the first time this is called, from initialize_frame_menubar. */
919 void
920 set_frame_menubar (FRAME_PTR f, bool first_time, bool deep_p)
922 xt_or_gtk_widget menubar_widget;
923 #ifdef USE_X_TOOLKIT
924 LWLIB_ID id;
925 #endif
926 Lisp_Object items;
927 widget_value *wv, *first_wv, *prev_wv = 0;
928 int i;
929 int *submenu_start, *submenu_end;
930 bool *submenu_top_level_items;
931 int *submenu_n_panes;
933 if (! FRAME_X_P (f))
934 emacs_abort ();
936 menubar_widget = f->output_data.x->menubar_widget;
938 XSETFRAME (Vmenu_updating_frame, f);
940 #ifdef USE_X_TOOLKIT
941 if (f->output_data.x->id == 0)
942 f->output_data.x->id = next_menubar_widget_id++;
943 id = f->output_data.x->id;
944 #endif
946 if (! menubar_widget)
947 deep_p = 1;
948 /* Make the first call for any given frame always go deep. */
949 else if (!f->output_data.x->saved_menu_event && !deep_p)
951 deep_p = 1;
952 f->output_data.x->saved_menu_event = xmalloc (sizeof (XEvent));
953 f->output_data.x->saved_menu_event->type = 0;
956 #ifdef USE_GTK
957 /* If we have detached menus, we must update deep so detached menus
958 also gets updated. */
959 deep_p = deep_p || xg_have_tear_offs ();
960 #endif
962 if (deep_p)
964 /* Make a widget-value tree representing the entire menu trees. */
966 struct buffer *prev = current_buffer;
967 Lisp_Object buffer;
968 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
969 int previous_menu_items_used = f->menu_bar_items_used;
970 Lisp_Object *previous_items
971 = alloca (previous_menu_items_used * sizeof *previous_items);
972 int subitems;
974 /* If we are making a new widget, its contents are empty,
975 do always reinitialize them. */
976 if (! menubar_widget)
977 previous_menu_items_used = 0;
979 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
980 specbind (Qinhibit_quit, Qt);
981 /* Don't let the debugger step into this code
982 because it is not reentrant. */
983 specbind (Qdebug_on_next_call, Qnil);
985 record_unwind_save_match_data ();
986 if (NILP (Voverriding_local_map_menu_flag))
988 specbind (Qoverriding_terminal_local_map, Qnil);
989 specbind (Qoverriding_local_map, Qnil);
992 set_buffer_internal_1 (XBUFFER (buffer));
994 /* Run the Lucid hook. */
995 safe_run_hooks (Qactivate_menubar_hook);
997 /* If it has changed current-menubar from previous value,
998 really recompute the menubar from the value. */
999 if (! NILP (Vlucid_menu_bar_dirty_flag))
1000 call0 (Qrecompute_lucid_menubar);
1001 safe_run_hooks (Qmenu_bar_update_hook);
1002 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
1004 items = FRAME_MENU_BAR_ITEMS (f);
1006 /* Save the frame's previous menu bar contents data. */
1007 if (previous_menu_items_used)
1008 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents,
1009 previous_menu_items_used * word_size);
1011 /* Fill in menu_items with the current menu bar contents.
1012 This can evaluate Lisp code. */
1013 save_menu_items ();
1015 menu_items = f->menu_bar_vector;
1016 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
1017 subitems = ASIZE (items) / 4;
1018 submenu_start = alloca ((subitems + 1) * sizeof *submenu_start);
1019 submenu_end = alloca (subitems * sizeof *submenu_end);
1020 submenu_n_panes = alloca (subitems * sizeof *submenu_n_panes);
1021 submenu_top_level_items = alloca (subitems
1022 * sizeof *submenu_top_level_items);
1023 init_menu_items ();
1024 for (i = 0; i < subitems; i++)
1026 Lisp_Object key, string, maps;
1028 key = AREF (items, 4 * i);
1029 string = AREF (items, 4 * i + 1);
1030 maps = AREF (items, 4 * i + 2);
1031 if (NILP (string))
1032 break;
1034 submenu_start[i] = menu_items_used;
1036 menu_items_n_panes = 0;
1037 submenu_top_level_items[i]
1038 = parse_single_submenu (key, string, maps);
1039 submenu_n_panes[i] = menu_items_n_panes;
1041 submenu_end[i] = menu_items_used;
1044 submenu_start[i] = -1;
1045 finish_menu_items ();
1047 /* Convert menu_items into widget_value trees
1048 to display the menu. This cannot evaluate Lisp code. */
1050 wv = xmalloc_widget_value ();
1051 wv->name = "menubar";
1052 wv->value = 0;
1053 wv->enabled = 1;
1054 wv->button_type = BUTTON_TYPE_NONE;
1055 wv->help = Qnil;
1056 first_wv = wv;
1058 for (i = 0; 0 <= submenu_start[i]; i++)
1060 menu_items_n_panes = submenu_n_panes[i];
1061 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
1062 submenu_top_level_items[i]);
1063 if (prev_wv)
1064 prev_wv->next = wv;
1065 else
1066 first_wv->contents = wv;
1067 /* Don't set wv->name here; GC during the loop might relocate it. */
1068 wv->enabled = 1;
1069 wv->button_type = BUTTON_TYPE_NONE;
1070 prev_wv = wv;
1073 set_buffer_internal_1 (prev);
1075 /* If there has been no change in the Lisp-level contents
1076 of the menu bar, skip redisplaying it. Just exit. */
1078 /* Compare the new menu items with the ones computed last time. */
1079 for (i = 0; i < previous_menu_items_used; i++)
1080 if (menu_items_used == i
1081 || (!EQ (previous_items[i], AREF (menu_items, i))))
1082 break;
1083 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1085 /* The menu items have not changed. Don't bother updating
1086 the menus in any form, since it would be a no-op. */
1087 free_menubar_widget_value_tree (first_wv);
1088 discard_menu_items ();
1089 unbind_to (specpdl_count, Qnil);
1090 return;
1093 /* The menu items are different, so store them in the frame. */
1094 fset_menu_bar_vector (f, menu_items);
1095 f->menu_bar_items_used = menu_items_used;
1097 /* This undoes save_menu_items. */
1098 unbind_to (specpdl_count, Qnil);
1100 /* Now GC cannot happen during the lifetime of the widget_value,
1101 so it's safe to store data from a Lisp_String. */
1102 wv = first_wv->contents;
1103 for (i = 0; i < ASIZE (items); i += 4)
1105 Lisp_Object string;
1106 string = AREF (items, i + 1);
1107 if (NILP (string))
1108 break;
1109 wv->name = SSDATA (string);
1110 update_submenu_strings (wv->contents);
1111 wv = wv->next;
1115 else
1117 /* Make a widget-value tree containing
1118 just the top level menu bar strings. */
1120 wv = xmalloc_widget_value ();
1121 wv->name = "menubar";
1122 wv->value = 0;
1123 wv->enabled = 1;
1124 wv->button_type = BUTTON_TYPE_NONE;
1125 wv->help = Qnil;
1126 first_wv = wv;
1128 items = FRAME_MENU_BAR_ITEMS (f);
1129 for (i = 0; i < ASIZE (items); i += 4)
1131 Lisp_Object string;
1133 string = AREF (items, i + 1);
1134 if (NILP (string))
1135 break;
1137 wv = xmalloc_widget_value ();
1138 wv->name = SSDATA (string);
1139 wv->value = 0;
1140 wv->enabled = 1;
1141 wv->button_type = BUTTON_TYPE_NONE;
1142 wv->help = Qnil;
1143 /* This prevents lwlib from assuming this
1144 menu item is really supposed to be empty. */
1145 /* The intptr_t cast avoids a warning.
1146 This value just has to be different from small integers. */
1147 wv->call_data = (void *) (intptr_t) (-1);
1149 if (prev_wv)
1150 prev_wv->next = wv;
1151 else
1152 first_wv->contents = wv;
1153 prev_wv = wv;
1156 /* Forget what we thought we knew about what is in the
1157 detailed contents of the menu bar menus.
1158 Changing the top level always destroys the contents. */
1159 f->menu_bar_items_used = 0;
1162 /* Create or update the menu bar widget. */
1164 block_input ();
1166 #ifdef USE_GTK
1167 xg_crazy_callback_abort = 1;
1168 if (menubar_widget)
1170 /* The fourth arg is DEEP_P, which says to consider the entire
1171 menu trees we supply, rather than just the menu bar item names. */
1172 xg_modify_menubar_widgets (menubar_widget,
1174 first_wv,
1175 deep_p,
1176 G_CALLBACK (menubar_selection_callback),
1177 G_CALLBACK (popup_deactivate_callback),
1178 G_CALLBACK (menu_highlight_callback));
1180 else
1182 menubar_widget
1183 = xg_create_widget ("menubar", "menubar", f, first_wv,
1184 G_CALLBACK (menubar_selection_callback),
1185 G_CALLBACK (popup_deactivate_callback),
1186 G_CALLBACK (menu_highlight_callback));
1188 f->output_data.x->menubar_widget = menubar_widget;
1192 #else /* not USE_GTK */
1193 if (menubar_widget)
1195 /* Disable resizing (done for Motif!) */
1196 lw_allow_resizing (f->output_data.x->widget, False);
1198 /* The third arg is DEEP_P, which says to consider the entire
1199 menu trees we supply, rather than just the menu bar item names. */
1200 lw_modify_all_widgets (id, first_wv, deep_p);
1202 /* Re-enable the edit widget to resize. */
1203 lw_allow_resizing (f->output_data.x->widget, True);
1205 else
1207 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
1208 XtTranslations override = XtParseTranslationTable (menuOverride);
1210 #ifdef USE_LUCID
1211 apply_systemfont_to_menu (f, f->output_data.x->column_widget);
1212 #endif
1213 menubar_widget = lw_create_widget ("menubar", "menubar", id,
1214 first_wv,
1215 f->output_data.x->column_widget,
1217 popup_activate_callback,
1218 menubar_selection_callback,
1219 popup_deactivate_callback,
1220 menu_highlight_callback);
1221 f->output_data.x->menubar_widget = menubar_widget;
1223 /* Make menu pop down on C-g. */
1224 XtOverrideTranslations (menubar_widget, override);
1228 int menubar_size;
1229 if (f->output_data.x->menubar_widget)
1230 XtRealizeWidget (f->output_data.x->menubar_widget);
1232 menubar_size
1233 = (f->output_data.x->menubar_widget
1234 ? (f->output_data.x->menubar_widget->core.height
1235 + f->output_data.x->menubar_widget->core.border_width)
1236 : 0);
1238 #if 1 /* Experimentally, we now get the right results
1239 for -geometry -0-0 without this. 24 Aug 96, rms.
1240 Maybe so, but the menu bar size is missing the pixels so the
1241 WM size hints are off by these pixels. Jan D, oct 2009. */
1242 #ifdef USE_LUCID
1243 if (FRAME_EXTERNAL_MENU_BAR (f))
1245 Dimension ibw = 0;
1246 XtVaGetValues (f->output_data.x->column_widget,
1247 XtNinternalBorderWidth, &ibw, NULL);
1248 menubar_size += ibw;
1250 #endif /* USE_LUCID */
1251 #endif /* 1 */
1253 f->output_data.x->menubar_height = menubar_size;
1255 #endif /* not USE_GTK */
1257 free_menubar_widget_value_tree (first_wv);
1258 update_frame_menubar (f);
1260 #ifdef USE_GTK
1261 xg_crazy_callback_abort = 0;
1262 #endif
1264 unblock_input ();
1267 /* Called from Fx_create_frame to create the initial menubar of a frame
1268 before it is mapped, so that the window is mapped with the menubar already
1269 there instead of us tacking it on later and thrashing the window after it
1270 is visible. */
1272 void
1273 initialize_frame_menubar (FRAME_PTR f)
1275 /* This function is called before the first chance to redisplay
1276 the frame. It has to be, so the frame will have the right size. */
1277 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
1278 set_frame_menubar (f, 1, 1);
1282 /* Get rid of the menu bar of frame F, and free its storage.
1283 This is used when deleting a frame, and when turning off the menu bar.
1284 For GTK this function is in gtkutil.c. */
1286 #ifndef USE_GTK
1287 void
1288 free_frame_menubar (FRAME_PTR f)
1290 Widget menubar_widget;
1292 if (! FRAME_X_P (f))
1293 emacs_abort ();
1295 menubar_widget = f->output_data.x->menubar_widget;
1297 f->output_data.x->menubar_height = 0;
1299 if (menubar_widget)
1301 #ifdef USE_MOTIF
1302 /* Removing the menu bar magically changes the shell widget's x
1303 and y position of (0, 0) which, when the menu bar is turned
1304 on again, leads to pull-down menus appearing in strange
1305 positions near the upper-left corner of the display. This
1306 happens only with some window managers like twm and ctwm,
1307 but not with other like Motif's mwm or kwm, because the
1308 latter generate ConfigureNotify events when the menu bar
1309 is switched off, which fixes the shell position. */
1310 Position x0, y0, x1, y1;
1311 #endif
1313 block_input ();
1315 #ifdef USE_MOTIF
1316 if (f->output_data.x->widget)
1317 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
1318 #endif
1320 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1321 f->output_data.x->menubar_widget = NULL;
1323 if (f->output_data.x->widget)
1325 #ifdef USE_MOTIF
1326 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
1327 if (x1 == 0 && y1 == 0)
1328 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
1329 #endif
1330 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1332 unblock_input ();
1335 #endif /* not USE_GTK */
1337 #endif /* USE_X_TOOLKIT || USE_GTK */
1339 /* xmenu_show actually displays a menu using the panes and items in menu_items
1340 and returns the value selected from it.
1341 There are two versions of xmenu_show, one for Xt and one for Xlib.
1342 Both assume input is blocked by the caller. */
1344 /* F is the frame the menu is for.
1345 X and Y are the frame-relative specified position,
1346 relative to the inside upper left corner of the frame F.
1347 FOR_CLICK is true if this menu was invoked for a mouse click.
1348 KEYMAPS is true if this menu was specified with keymaps;
1349 in that case, we return a list containing the chosen item's value
1350 and perhaps also the pane's prefix.
1351 TITLE is the specified menu title.
1352 ERROR is a place to store an error message string in case of failure.
1353 (We return nil on failure, but the value doesn't actually matter.) */
1355 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1357 /* The item selected in the popup menu. */
1358 static Lisp_Object *volatile menu_item_selection;
1360 #ifdef USE_GTK
1362 /* Used when position a popup menu. See menu_position_func and
1363 create_and_show_popup_menu below. */
1364 struct next_popup_x_y
1366 FRAME_PTR f;
1367 int x;
1368 int y;
1371 /* The menu position function to use if we are not putting a popup
1372 menu where the pointer is.
1373 MENU is the menu to pop up.
1374 X and Y shall on exit contain x/y where the menu shall pop up.
1375 PUSH_IN is not documented in the GTK manual.
1376 USER_DATA is any data passed in when calling gtk_menu_popup.
1377 Here it points to a struct next_popup_x_y where the coordinates
1378 to store in *X and *Y are as well as the frame for the popup.
1380 Here only X and Y are used. */
1381 static void
1382 menu_position_func (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer user_data)
1384 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
1385 GtkRequisition req;
1386 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (data->f);
1387 int disp_width = x_display_pixel_width (dpyinfo);
1388 int disp_height = x_display_pixel_height (dpyinfo);
1390 *x = data->x;
1391 *y = data->y;
1393 /* Check if there is room for the menu. If not, adjust x/y so that
1394 the menu is fully visible. */
1395 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1396 if (data->x + req.width > disp_width)
1397 *x -= data->x + req.width - disp_width;
1398 if (data->y + req.height > disp_height)
1399 *y -= data->y + req.height - disp_height;
1402 static void
1403 popup_selection_callback (GtkWidget *widget, gpointer client_data)
1405 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1407 if (xg_crazy_callback_abort) return;
1408 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
1411 static Lisp_Object
1412 pop_down_menu (Lisp_Object arg)
1414 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1416 popup_activated_flag = 0;
1417 block_input ();
1418 gtk_widget_destroy (GTK_WIDGET (p->pointer));
1419 unblock_input ();
1420 return Qnil;
1423 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1424 menu pops down.
1425 menu_item_selection will be set to the selection. */
1426 static void
1427 create_and_show_popup_menu (FRAME_PTR f, widget_value *first_wv, int x, int y,
1428 bool for_click, Time timestamp)
1430 int i;
1431 GtkWidget *menu;
1432 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
1433 struct next_popup_x_y popup_x_y;
1434 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1435 bool use_pos_func = ! for_click;
1437 #ifdef HAVE_GTK3
1438 /* Always use position function for Gtk3. Otherwise menus may become
1439 too small to show anything. */
1440 use_pos_func = 1;
1441 #endif
1443 if (! FRAME_X_P (f))
1444 emacs_abort ();
1446 xg_crazy_callback_abort = 1;
1447 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
1448 G_CALLBACK (popup_selection_callback),
1449 G_CALLBACK (popup_deactivate_callback),
1450 G_CALLBACK (menu_highlight_callback));
1451 xg_crazy_callback_abort = 0;
1453 if (use_pos_func)
1455 /* Not invoked by a click. pop up at x/y. */
1456 pos_func = menu_position_func;
1458 /* Adjust coordinates to be root-window-relative. */
1459 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1460 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1462 popup_x_y.x = x;
1463 popup_x_y.y = y;
1464 popup_x_y.f = f;
1466 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1469 if (for_click)
1471 for (i = 0; i < 5; i++)
1472 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
1473 break;
1476 /* Display the menu. */
1477 gtk_widget_show_all (menu);
1479 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i,
1480 timestamp ? timestamp : gtk_get_current_event_time ());
1482 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
1484 if (gtk_widget_get_mapped (menu))
1486 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1487 two. show_help_echo uses this to detect popup menus. */
1488 popup_activated_flag = 1;
1489 /* Process events that apply to the menu. */
1490 popup_widget_loop (1, menu);
1493 unbind_to (specpdl_count, Qnil);
1495 /* Must reset this manually because the button release event is not passed
1496 to Emacs event loop. */
1497 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
1500 #else /* not USE_GTK */
1502 /* We need a unique id for each widget handled by the Lucid Widget
1503 library.
1505 For the main windows, and popup menus, we use this counter,
1506 which we increment each time after use. This starts from 1<<16.
1508 For menu bars, we use numbers starting at 0, counted in
1509 next_menubar_widget_id. */
1510 LWLIB_ID widget_id_tick;
1512 static void
1513 popup_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1515 menu_item_selection = (Lisp_Object *) client_data;
1518 /* ARG is the LWLIB ID of the dialog box, represented
1519 as a Lisp object as (HIGHPART . LOWPART). */
1521 static Lisp_Object
1522 pop_down_menu (Lisp_Object arg)
1524 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
1525 | XINT (XCDR (arg)));
1527 block_input ();
1528 lw_destroy_all_widgets (id);
1529 unblock_input ();
1530 popup_activated_flag = 0;
1532 return Qnil;
1535 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1536 menu pops down.
1537 menu_item_selection will be set to the selection. */
1538 static void
1539 create_and_show_popup_menu (FRAME_PTR f, widget_value *first_wv,
1540 int x, int y, bool for_click, Time timestamp)
1542 int i;
1543 Arg av[2];
1544 int ac = 0;
1545 XEvent dummy;
1546 XButtonPressedEvent *event = &(dummy.xbutton);
1547 LWLIB_ID menu_id;
1548 Widget menu;
1550 if (! FRAME_X_P (f))
1551 emacs_abort ();
1553 #ifdef USE_LUCID
1554 apply_systemfont_to_menu (f, f->output_data.x->widget);
1555 #endif
1557 menu_id = widget_id_tick++;
1558 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1559 f->output_data.x->widget, 1, 0,
1560 popup_selection_callback,
1561 popup_deactivate_callback,
1562 menu_highlight_callback);
1564 event->type = ButtonPress;
1565 event->serial = 0;
1566 event->send_event = 0;
1567 event->display = FRAME_X_DISPLAY (f);
1568 event->time = CurrentTime;
1569 event->root = FRAME_X_DISPLAY_INFO (f)->root_window;
1570 event->window = event->subwindow = event->root;
1571 event->x = x;
1572 event->y = y;
1574 /* Adjust coordinates to be root-window-relative. */
1575 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1576 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1578 event->x_root = x;
1579 event->y_root = y;
1581 event->state = 0;
1582 event->button = 0;
1583 for (i = 0; i < 5; i++)
1584 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
1585 event->button = i;
1587 /* Don't allow any geometry request from the user. */
1588 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1589 XtSetValues (menu, av, ac);
1591 /* Display the menu. */
1592 lw_popup_menu (menu, &dummy);
1593 popup_activated_flag = 1;
1594 x_activate_timeout_atimer ();
1597 int fact = 4 * sizeof (LWLIB_ID);
1598 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1599 record_unwind_protect (pop_down_menu,
1600 Fcons (make_number (menu_id >> (fact)),
1601 make_number (menu_id & ~(-1 << (fact)))));
1603 /* Process events that apply to the menu. */
1604 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
1606 unbind_to (specpdl_count, Qnil);
1610 #endif /* not USE_GTK */
1612 static Lisp_Object
1613 cleanup_widget_value_tree (Lisp_Object arg)
1615 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1616 widget_value *wv = p->pointer;
1618 free_menubar_widget_value_tree (wv);
1620 return Qnil;
1623 Lisp_Object
1624 xmenu_show (FRAME_PTR f, int x, int y, bool for_click, bool keymaps,
1625 Lisp_Object title, const char **error_name, Time timestamp)
1627 int i;
1628 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1629 widget_value **submenu_stack
1630 = alloca (menu_items_used * sizeof *submenu_stack);
1631 Lisp_Object *subprefix_stack
1632 = alloca (menu_items_used * sizeof *subprefix_stack);
1633 int submenu_depth = 0;
1635 int first_pane;
1637 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1639 if (! FRAME_X_P (f))
1640 emacs_abort ();
1642 *error_name = NULL;
1644 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1646 *error_name = "Empty menu";
1647 return Qnil;
1650 /* Create a tree of widget_value objects
1651 representing the panes and their items. */
1652 wv = xmalloc_widget_value ();
1653 wv->name = "menu";
1654 wv->value = 0;
1655 wv->enabled = 1;
1656 wv->button_type = BUTTON_TYPE_NONE;
1657 wv->help =Qnil;
1658 first_wv = wv;
1659 first_pane = 1;
1661 /* Loop over all panes and items, filling in the tree. */
1662 i = 0;
1663 while (i < menu_items_used)
1665 if (EQ (AREF (menu_items, i), Qnil))
1667 submenu_stack[submenu_depth++] = save_wv;
1668 save_wv = prev_wv;
1669 prev_wv = 0;
1670 first_pane = 1;
1671 i++;
1673 else if (EQ (AREF (menu_items, i), Qlambda))
1675 prev_wv = save_wv;
1676 save_wv = submenu_stack[--submenu_depth];
1677 first_pane = 0;
1678 i++;
1680 else if (EQ (AREF (menu_items, i), Qt)
1681 && submenu_depth != 0)
1682 i += MENU_ITEMS_PANE_LENGTH;
1683 /* Ignore a nil in the item list.
1684 It's meaningful only for dialog boxes. */
1685 else if (EQ (AREF (menu_items, i), Qquote))
1686 i += 1;
1687 else if (EQ (AREF (menu_items, i), Qt))
1689 /* Create a new pane. */
1690 Lisp_Object pane_name, prefix;
1691 const char *pane_string;
1693 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1694 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1696 #ifndef HAVE_MULTILINGUAL_MENU
1697 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1699 pane_name = ENCODE_MENU_STRING (pane_name);
1700 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
1702 #endif
1703 pane_string = (NILP (pane_name)
1704 ? "" : SSDATA (pane_name));
1705 /* If there is just one top-level pane, put all its items directly
1706 under the top-level menu. */
1707 if (menu_items_n_panes == 1)
1708 pane_string = "";
1710 /* If the pane has a meaningful name,
1711 make the pane a top-level menu item
1712 with its items as a submenu beneath it. */
1713 if (!keymaps && strcmp (pane_string, ""))
1715 wv = xmalloc_widget_value ();
1716 if (save_wv)
1717 save_wv->next = wv;
1718 else
1719 first_wv->contents = wv;
1720 wv->name = (char *) pane_string;
1721 if (keymaps && !NILP (prefix))
1722 wv->name++;
1723 wv->value = 0;
1724 wv->enabled = 1;
1725 wv->button_type = BUTTON_TYPE_NONE;
1726 wv->help = Qnil;
1727 save_wv = wv;
1728 prev_wv = 0;
1730 else if (first_pane)
1732 save_wv = wv;
1733 prev_wv = 0;
1735 first_pane = 0;
1736 i += MENU_ITEMS_PANE_LENGTH;
1738 else
1740 /* Create a new item within current pane. */
1741 Lisp_Object item_name, enable, descrip, def, type, selected, help;
1742 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1743 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1744 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1745 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1746 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1747 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1748 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1750 #ifndef HAVE_MULTILINGUAL_MENU
1751 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
1753 item_name = ENCODE_MENU_STRING (item_name);
1754 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
1757 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1759 descrip = ENCODE_MENU_STRING (descrip);
1760 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
1762 #endif /* not HAVE_MULTILINGUAL_MENU */
1764 wv = xmalloc_widget_value ();
1765 if (prev_wv)
1766 prev_wv->next = wv;
1767 else
1768 save_wv->contents = wv;
1769 wv->name = SSDATA (item_name);
1770 if (!NILP (descrip))
1771 wv->key = SSDATA (descrip);
1772 wv->value = 0;
1773 /* If this item has a null value,
1774 make the call_data null so that it won't display a box
1775 when the mouse is on it. */
1776 wv->call_data = !NILP (def) ? aref_addr (menu_items, i) : 0;
1777 wv->enabled = !NILP (enable);
1779 if (NILP (type))
1780 wv->button_type = BUTTON_TYPE_NONE;
1781 else if (EQ (type, QCtoggle))
1782 wv->button_type = BUTTON_TYPE_TOGGLE;
1783 else if (EQ (type, QCradio))
1784 wv->button_type = BUTTON_TYPE_RADIO;
1785 else
1786 emacs_abort ();
1788 wv->selected = !NILP (selected);
1790 if (! STRINGP (help))
1791 help = Qnil;
1793 wv->help = help;
1795 prev_wv = wv;
1797 i += MENU_ITEMS_ITEM_LENGTH;
1801 /* Deal with the title, if it is non-nil. */
1802 if (!NILP (title))
1804 widget_value *wv_title = xmalloc_widget_value ();
1805 widget_value *wv_sep1 = xmalloc_widget_value ();
1806 widget_value *wv_sep2 = xmalloc_widget_value ();
1808 wv_sep2->name = "--";
1809 wv_sep2->next = first_wv->contents;
1810 wv_sep2->help = Qnil;
1812 wv_sep1->name = "--";
1813 wv_sep1->next = wv_sep2;
1814 wv_sep1->help = Qnil;
1816 #ifndef HAVE_MULTILINGUAL_MENU
1817 if (STRING_MULTIBYTE (title))
1818 title = ENCODE_MENU_STRING (title);
1819 #endif
1821 wv_title->name = SSDATA (title);
1822 wv_title->enabled = TRUE;
1823 wv_title->button_type = BUTTON_TYPE_NONE;
1824 wv_title->help = Qnil;
1825 wv_title->next = wv_sep1;
1826 first_wv->contents = wv_title;
1829 /* No selection has been chosen yet. */
1830 menu_item_selection = 0;
1832 /* Make sure to free the widget_value objects we used to specify the
1833 contents even with longjmp. */
1834 record_unwind_protect (cleanup_widget_value_tree,
1835 make_save_value (first_wv, 0));
1837 /* Actually create and show the menu until popped down. */
1838 create_and_show_popup_menu (f, first_wv, x, y, for_click, timestamp);
1840 unbind_to (specpdl_count, Qnil);
1842 /* Find the selected item, and its pane, to return
1843 the proper value. */
1844 if (menu_item_selection != 0)
1846 Lisp_Object prefix, entry;
1848 prefix = entry = Qnil;
1849 i = 0;
1850 while (i < menu_items_used)
1852 if (EQ (AREF (menu_items, i), Qnil))
1854 subprefix_stack[submenu_depth++] = prefix;
1855 prefix = entry;
1856 i++;
1858 else if (EQ (AREF (menu_items, i), Qlambda))
1860 prefix = subprefix_stack[--submenu_depth];
1861 i++;
1863 else if (EQ (AREF (menu_items, i), Qt))
1865 prefix
1866 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1867 i += MENU_ITEMS_PANE_LENGTH;
1869 /* Ignore a nil in the item list.
1870 It's meaningful only for dialog boxes. */
1871 else if (EQ (AREF (menu_items, i), Qquote))
1872 i += 1;
1873 else
1875 entry
1876 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
1877 if (menu_item_selection == aref_addr (menu_items, i))
1879 if (keymaps)
1881 int j;
1883 entry = Fcons (entry, Qnil);
1884 if (!NILP (prefix))
1885 entry = Fcons (prefix, entry);
1886 for (j = submenu_depth - 1; j >= 0; j--)
1887 if (!NILP (subprefix_stack[j]))
1888 entry = Fcons (subprefix_stack[j], entry);
1890 return entry;
1892 i += MENU_ITEMS_ITEM_LENGTH;
1896 else if (!for_click)
1897 /* Make "Cancel" equivalent to C-g. */
1898 Fsignal (Qquit, Qnil);
1900 return Qnil;
1903 #ifdef USE_GTK
1904 static void
1905 dialog_selection_callback (GtkWidget *widget, gpointer client_data)
1907 /* Treat the pointer as an integer. There's no problem
1908 as long as pointers have enough bits to hold small integers. */
1909 if ((intptr_t) client_data != -1)
1910 menu_item_selection = (Lisp_Object *) client_data;
1912 popup_activated_flag = 0;
1915 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1916 dialog pops down.
1917 menu_item_selection will be set to the selection. */
1918 static void
1919 create_and_show_dialog (FRAME_PTR f, widget_value *first_wv)
1921 GtkWidget *menu;
1923 if (! FRAME_X_P (f))
1924 emacs_abort ();
1926 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
1927 G_CALLBACK (dialog_selection_callback),
1928 G_CALLBACK (popup_deactivate_callback),
1931 if (menu)
1933 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1934 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
1936 /* Display the menu. */
1937 gtk_widget_show_all (menu);
1939 /* Process events that apply to the menu. */
1940 popup_widget_loop (1, menu);
1942 unbind_to (specpdl_count, Qnil);
1946 #else /* not USE_GTK */
1947 static void
1948 dialog_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1950 /* Treat the pointer as an integer. There's no problem
1951 as long as pointers have enough bits to hold small integers. */
1952 if ((intptr_t) client_data != -1)
1953 menu_item_selection = (Lisp_Object *) client_data;
1955 block_input ();
1956 lw_destroy_all_widgets (id);
1957 unblock_input ();
1958 popup_activated_flag = 0;
1962 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1963 dialog pops down.
1964 menu_item_selection will be set to the selection. */
1965 static void
1966 create_and_show_dialog (FRAME_PTR f, widget_value *first_wv)
1968 LWLIB_ID dialog_id;
1970 if (!FRAME_X_P (f))
1971 emacs_abort ();
1973 dialog_id = widget_id_tick++;
1974 #ifdef USE_LUCID
1975 apply_systemfont_to_dialog (f->output_data.x->widget);
1976 #endif
1977 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1978 f->output_data.x->widget, 1, 0,
1979 dialog_selection_callback, 0, 0);
1980 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1981 /* Display the dialog box. */
1982 lw_pop_up_all_widgets (dialog_id);
1983 popup_activated_flag = 1;
1984 x_activate_timeout_atimer ();
1986 /* Process events that apply to the dialog box.
1987 Also handle timers. */
1989 ptrdiff_t count = SPECPDL_INDEX ();
1990 int fact = 4 * sizeof (LWLIB_ID);
1992 /* xdialog_show_unwind is responsible for popping the dialog box down. */
1993 record_unwind_protect (pop_down_menu,
1994 Fcons (make_number (dialog_id >> (fact)),
1995 make_number (dialog_id & ~(-1 << (fact)))));
1997 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
1998 dialog_id, 1);
2000 unbind_to (count, Qnil);
2004 #endif /* not USE_GTK */
2006 static const char * button_names [] = {
2007 "button1", "button2", "button3", "button4", "button5",
2008 "button6", "button7", "button8", "button9", "button10" };
2010 static Lisp_Object
2011 xdialog_show (FRAME_PTR f,
2012 bool keymaps,
2013 Lisp_Object title,
2014 Lisp_Object header,
2015 const char **error_name)
2017 int i, nb_buttons=0;
2018 char dialog_name[6];
2020 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2022 /* Number of elements seen so far, before boundary. */
2023 int left_count = 0;
2024 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2025 int boundary_seen = 0;
2027 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
2029 if (! FRAME_X_P (f))
2030 emacs_abort ();
2032 *error_name = NULL;
2034 if (menu_items_n_panes > 1)
2036 *error_name = "Multiple panes in dialog box";
2037 return Qnil;
2040 /* Create a tree of widget_value objects
2041 representing the text label and buttons. */
2043 Lisp_Object pane_name, prefix;
2044 const char *pane_string;
2045 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
2046 prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX);
2047 pane_string = (NILP (pane_name)
2048 ? "" : SSDATA (pane_name));
2049 prev_wv = xmalloc_widget_value ();
2050 prev_wv->value = (char *) pane_string;
2051 if (keymaps && !NILP (prefix))
2052 prev_wv->name++;
2053 prev_wv->enabled = 1;
2054 prev_wv->name = "message";
2055 prev_wv->help = Qnil;
2056 first_wv = prev_wv;
2058 /* Loop over all panes and items, filling in the tree. */
2059 i = MENU_ITEMS_PANE_LENGTH;
2060 while (i < menu_items_used)
2063 /* Create a new item within current pane. */
2064 Lisp_Object item_name, enable, descrip;
2065 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2066 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2067 descrip
2068 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2070 if (NILP (item_name))
2072 free_menubar_widget_value_tree (first_wv);
2073 *error_name = "Submenu in dialog items";
2074 return Qnil;
2076 if (EQ (item_name, Qquote))
2078 /* This is the boundary between left-side elts
2079 and right-side elts. Stop incrementing right_count. */
2080 boundary_seen = 1;
2081 i++;
2082 continue;
2084 if (nb_buttons >= 9)
2086 free_menubar_widget_value_tree (first_wv);
2087 *error_name = "Too many dialog items";
2088 return Qnil;
2091 wv = xmalloc_widget_value ();
2092 prev_wv->next = wv;
2093 wv->name = (char *) button_names[nb_buttons];
2094 if (!NILP (descrip))
2095 wv->key = SSDATA (descrip);
2096 wv->value = SSDATA (item_name);
2097 wv->call_data = aref_addr (menu_items, i);
2098 wv->enabled = !NILP (enable);
2099 wv->help = Qnil;
2100 prev_wv = wv;
2102 if (! boundary_seen)
2103 left_count++;
2105 nb_buttons++;
2106 i += MENU_ITEMS_ITEM_LENGTH;
2109 /* If the boundary was not specified,
2110 by default put half on the left and half on the right. */
2111 if (! boundary_seen)
2112 left_count = nb_buttons - nb_buttons / 2;
2114 wv = xmalloc_widget_value ();
2115 wv->name = dialog_name;
2116 wv->help = Qnil;
2118 /* Frame title: 'Q' = Question, 'I' = Information.
2119 Can also have 'E' = Error if, one day, we want
2120 a popup for errors. */
2121 if (NILP (header))
2122 dialog_name[0] = 'Q';
2123 else
2124 dialog_name[0] = 'I';
2126 /* Dialog boxes use a really stupid name encoding
2127 which specifies how many buttons to use
2128 and how many buttons are on the right. */
2129 dialog_name[1] = '0' + nb_buttons;
2130 dialog_name[2] = 'B';
2131 dialog_name[3] = 'R';
2132 /* Number of buttons to put on the right. */
2133 dialog_name[4] = '0' + nb_buttons - left_count;
2134 dialog_name[5] = 0;
2135 wv->contents = first_wv;
2136 first_wv = wv;
2139 /* No selection has been chosen yet. */
2140 menu_item_selection = 0;
2142 /* Make sure to free the widget_value objects we used to specify the
2143 contents even with longjmp. */
2144 record_unwind_protect (cleanup_widget_value_tree,
2145 make_save_value (first_wv, 0));
2147 /* Actually create and show the dialog. */
2148 create_and_show_dialog (f, first_wv);
2150 unbind_to (specpdl_count, Qnil);
2152 /* Find the selected item, and its pane, to return
2153 the proper value. */
2154 if (menu_item_selection != 0)
2156 Lisp_Object prefix;
2158 prefix = Qnil;
2159 i = 0;
2160 while (i < menu_items_used)
2162 Lisp_Object entry;
2164 if (EQ (AREF (menu_items, i), Qt))
2166 prefix
2167 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2168 i += MENU_ITEMS_PANE_LENGTH;
2170 else if (EQ (AREF (menu_items, i), Qquote))
2172 /* This is the boundary between left-side elts and
2173 right-side elts. */
2174 ++i;
2176 else
2178 entry
2179 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
2180 if (menu_item_selection == aref_addr (menu_items, i))
2182 if (keymaps != 0)
2184 entry = Fcons (entry, Qnil);
2185 if (!NILP (prefix))
2186 entry = Fcons (prefix, entry);
2188 return entry;
2190 i += MENU_ITEMS_ITEM_LENGTH;
2194 else
2195 /* Make "Cancel" equivalent to C-g. */
2196 Fsignal (Qquit, Qnil);
2198 return Qnil;
2201 #else /* not USE_X_TOOLKIT && not USE_GTK */
2203 /* The frame of the last activated non-toolkit menu bar.
2204 Used to generate menu help events. */
2206 static struct frame *menu_help_frame;
2209 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2211 PANE is the pane number, and ITEM is the menu item number in
2212 the menu (currently not used).
2214 This cannot be done with generating a HELP_EVENT because
2215 XMenuActivate contains a loop that doesn't let Emacs process
2216 keyboard events. */
2218 static void
2219 menu_help_callback (char const *help_string, int pane, int item)
2221 Lisp_Object *first_item;
2222 Lisp_Object pane_name;
2223 Lisp_Object menu_object;
2225 first_item = XVECTOR (menu_items)->contents;
2226 if (EQ (first_item[0], Qt))
2227 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2228 else if (EQ (first_item[0], Qquote))
2229 /* This shouldn't happen, see xmenu_show. */
2230 pane_name = empty_unibyte_string;
2231 else
2232 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2234 /* (menu-item MENU-NAME PANE-NUMBER) */
2235 menu_object = Fcons (Qmenu_item,
2236 Fcons (pane_name,
2237 Fcons (make_number (pane), Qnil)));
2238 show_help_echo (help_string ? build_string (help_string) : Qnil,
2239 Qnil, menu_object, make_number (item));
2242 static Lisp_Object
2243 pop_down_menu (Lisp_Object arg)
2245 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
2246 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
2248 FRAME_PTR f = p1->pointer;
2249 XMenu *menu = p2->pointer;
2251 block_input ();
2252 #ifndef MSDOS
2253 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
2254 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
2255 #endif
2256 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2258 #ifdef HAVE_X_WINDOWS
2259 /* Assume the mouse has moved out of the X window.
2260 If it has actually moved in, we will get an EnterNotify. */
2261 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2263 /* State that no mouse buttons are now held.
2264 (The oldXMenu code doesn't track this info for us.)
2265 That is not necessarily true, but the fiction leads to reasonable
2266 results, and it is a pain to ask which are actually held now. */
2267 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2269 #endif /* HAVE_X_WINDOWS */
2271 unblock_input ();
2273 return Qnil;
2277 Lisp_Object
2278 xmenu_show (FRAME_PTR f, int x, int y, bool for_click, bool keymaps,
2279 Lisp_Object title, const char **error_name, Time timestamp)
2281 Window root;
2282 XMenu *menu;
2283 int pane, selidx, lpane, status;
2284 Lisp_Object entry, pane_prefix;
2285 char *datap;
2286 int ulx, uly, width, height;
2287 int dispwidth, dispheight;
2288 int i, j, lines, maxlines;
2289 int maxwidth;
2290 int dummy_int;
2291 unsigned int dummy_uint;
2292 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
2294 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
2295 emacs_abort ();
2297 *error_name = 0;
2298 if (menu_items_n_panes == 0)
2299 return Qnil;
2301 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2303 *error_name = "Empty menu";
2304 return Qnil;
2307 /* Figure out which root window F is on. */
2308 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2309 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2310 &dummy_uint, &dummy_uint);
2312 /* Make the menu on that window. */
2313 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2314 if (menu == NULL)
2316 *error_name = "Can't create menu";
2317 return Qnil;
2320 /* Don't GC while we prepare and show the menu,
2321 because we give the oldxmenu library pointers to the
2322 contents of strings. */
2323 inhibit_garbage_collection ();
2325 #ifdef HAVE_X_WINDOWS
2326 /* Adjust coordinates to relative to the outer (window manager) window. */
2327 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
2328 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
2329 #endif /* HAVE_X_WINDOWS */
2331 /* Adjust coordinates to be root-window-relative. */
2332 x += f->left_pos;
2333 y += f->top_pos;
2335 /* Create all the necessary panes and their items. */
2336 maxwidth = maxlines = lines = i = 0;
2337 lpane = XM_FAILURE;
2338 while (i < menu_items_used)
2340 if (EQ (AREF (menu_items, i), Qt))
2342 /* Create a new pane. */
2343 Lisp_Object pane_name, prefix;
2344 const char *pane_string;
2346 maxlines = max (maxlines, lines);
2347 lines = 0;
2348 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2349 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2350 pane_string = (NILP (pane_name)
2351 ? "" : SSDATA (pane_name));
2352 if (keymaps && !NILP (prefix))
2353 pane_string++;
2355 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2356 if (lpane == XM_FAILURE)
2358 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2359 *error_name = "Can't create pane";
2360 return Qnil;
2362 i += MENU_ITEMS_PANE_LENGTH;
2364 /* Find the width of the widest item in this pane. */
2365 j = i;
2366 while (j < menu_items_used)
2368 Lisp_Object item;
2369 item = AREF (menu_items, j);
2370 if (EQ (item, Qt))
2371 break;
2372 if (NILP (item))
2374 j++;
2375 continue;
2377 width = SBYTES (item);
2378 if (width > maxwidth)
2379 maxwidth = width;
2381 j += MENU_ITEMS_ITEM_LENGTH;
2384 /* Ignore a nil in the item list.
2385 It's meaningful only for dialog boxes. */
2386 else if (EQ (AREF (menu_items, i), Qquote))
2387 i += 1;
2388 else
2390 /* Create a new item within current pane. */
2391 Lisp_Object item_name, enable, descrip, help;
2392 char *item_data;
2393 char const *help_string;
2395 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2396 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2397 descrip
2398 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2399 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2400 help_string = STRINGP (help) ? SSDATA (help) : NULL;
2402 if (!NILP (descrip))
2404 /* if alloca is fast, use that to make the space,
2405 to reduce gc needs. */
2406 item_data = alloca (maxwidth + SBYTES (descrip) + 1);
2407 memcpy (item_data, SSDATA (item_name), SBYTES (item_name));
2408 for (j = SCHARS (item_name); j < maxwidth; j++)
2409 item_data[j] = ' ';
2410 memcpy (item_data + j, SSDATA (descrip), SBYTES (descrip));
2411 item_data[j + SBYTES (descrip)] = 0;
2413 else
2414 item_data = SSDATA (item_name);
2416 if (lpane == XM_FAILURE
2417 || (XMenuAddSelection (FRAME_X_DISPLAY (f),
2418 menu, lpane, 0, item_data,
2419 !NILP (enable), help_string)
2420 == XM_FAILURE))
2422 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2423 *error_name = "Can't add selection to menu";
2424 return Qnil;
2426 i += MENU_ITEMS_ITEM_LENGTH;
2427 lines++;
2431 maxlines = max (maxlines, lines);
2433 /* All set and ready to fly. */
2434 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2435 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2436 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2437 x = min (x, dispwidth);
2438 y = min (y, dispheight);
2439 x = max (x, 1);
2440 y = max (y, 1);
2441 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2442 &ulx, &uly, &width, &height);
2443 if (ulx+width > dispwidth)
2445 x -= (ulx + width) - dispwidth;
2446 ulx = dispwidth - width;
2448 if (uly+height > dispheight)
2450 y -= (uly + height) - dispheight;
2451 uly = dispheight - height;
2453 #ifndef HAVE_X_WINDOWS
2454 if (FRAME_HAS_MINIBUF_P (f) && uly+height > dispheight - 1)
2456 /* Move the menu away of the echo area, to avoid overwriting the
2457 menu with help echo messages or vice versa. */
2458 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
2460 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2461 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2463 else
2465 y--;
2466 uly--;
2469 #endif
2470 if (ulx < 0) x -= ulx;
2471 if (uly < 0) y -= uly;
2473 if (! for_click)
2475 /* If position was not given by a mouse click, adjust so upper left
2476 corner of the menu as a whole ends up at given coordinates. This
2477 is what x-popup-menu says in its documentation. */
2478 x += width/2;
2479 y += 1.5*height/(maxlines+2);
2482 XMenuSetAEQ (menu, TRUE);
2483 XMenuSetFreeze (menu, TRUE);
2484 pane = selidx = 0;
2486 #ifndef MSDOS
2487 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
2488 #endif
2490 record_unwind_protect (pop_down_menu,
2491 Fcons (make_save_value (f, 0),
2492 make_save_value (menu, 0)));
2494 /* Help display under X won't work because XMenuActivate contains
2495 a loop that doesn't give Emacs a chance to process it. */
2496 menu_help_frame = f;
2497 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2498 x, y, ButtonReleaseMask, &datap,
2499 menu_help_callback);
2500 entry = pane_prefix = Qnil;
2502 switch (status)
2504 case XM_SUCCESS:
2505 #ifdef XDEBUG
2506 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2507 #endif
2509 /* Find the item number SELIDX in pane number PANE. */
2510 i = 0;
2511 while (i < menu_items_used)
2513 if (EQ (AREF (menu_items, i), Qt))
2515 if (pane == 0)
2516 pane_prefix
2517 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2518 pane--;
2519 i += MENU_ITEMS_PANE_LENGTH;
2521 else
2523 if (pane == -1)
2525 if (selidx == 0)
2527 entry
2528 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
2529 if (keymaps)
2531 entry = Fcons (entry, Qnil);
2532 if (!NILP (pane_prefix))
2533 entry = Fcons (pane_prefix, entry);
2535 break;
2537 selidx--;
2539 i += MENU_ITEMS_ITEM_LENGTH;
2542 break;
2544 case XM_FAILURE:
2545 *error_name = "Can't activate menu";
2546 case XM_IA_SELECT:
2547 break;
2548 case XM_NO_SELECT:
2549 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2550 the menu was invoked with a mouse event as POSITION). */
2551 if (! for_click)
2552 Fsignal (Qquit, Qnil);
2553 break;
2556 unbind_to (specpdl_count, Qnil);
2558 return entry;
2561 #endif /* not USE_X_TOOLKIT */
2563 #endif /* HAVE_MENUS */
2565 #ifndef MSDOS
2566 /* Detect if a dialog or menu has been posted. MSDOS has its own
2567 implementation on msdos.c. */
2569 int ATTRIBUTE_CONST
2570 popup_activated (void)
2572 return popup_activated_flag;
2574 #endif /* not MSDOS */
2576 /* The following is used by delayed window autoselection. */
2578 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
2579 doc: /* Return t if a menu or popup dialog is active. */)
2580 (void)
2582 #ifdef HAVE_MENUS
2583 return (popup_activated ()) ? Qt : Qnil;
2584 #else
2585 return Qnil;
2586 #endif /* HAVE_MENUS */
2589 void
2590 syms_of_xmenu (void)
2592 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
2594 #ifdef USE_X_TOOLKIT
2595 widget_id_tick = (1<<16);
2596 next_menubar_widget_id = 1;
2597 #endif
2599 defsubr (&Smenu_or_popup_active_p);
2601 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
2602 defsubr (&Sx_menu_bar_open_internal);
2603 Ffset (intern_c_string ("accelerate-menu"),
2604 intern_c_string (Sx_menu_bar_open_internal.symbol_name));
2605 #endif
2607 #ifdef HAVE_MENUS
2608 defsubr (&Sx_popup_dialog);
2609 #endif