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