Fix problem with images referenced within svg files. (bug#19373)
[emacs.git] / src / xmenu.c
blob0f69ee28e847dc307d45ba7362ea9e5c5b539379
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2014 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 /* Flag which when set indicates a dialog or menu has been posted by
114 Xt on behalf of one of the widget sets. */
115 static int popup_activated_flag;
118 #ifdef USE_X_TOOLKIT
120 static LWLIB_ID next_menubar_widget_id;
122 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
124 static struct frame *
125 menubar_id_to_frame (LWLIB_ID id)
127 Lisp_Object tail, frame;
128 struct frame *f;
130 FOR_EACH_FRAME (tail, frame)
132 f = XFRAME (frame);
133 if (!FRAME_WINDOW_P (f))
134 continue;
135 if (f->output_data.x->id == id)
136 return f;
138 return 0;
141 #endif
143 #ifndef MSDOS
145 #if defined USE_GTK || defined USE_MOTIF
147 /* Set menu_items_inuse so no other popup menu or dialog is created. */
149 void
150 x_menu_set_in_use (int in_use)
152 menu_items_inuse = in_use ? Qt : Qnil;
153 popup_activated_flag = in_use;
154 #ifdef USE_X_TOOLKIT
155 if (popup_activated_flag)
156 x_activate_timeout_atimer ();
157 #endif
160 #endif
162 /* Wait for an X event to arrive or for a timer to expire. */
164 void
165 x_menu_wait_for_event (void *data)
167 /* Another way to do this is to register a timer callback, that can be
168 done in GTK and Xt. But we have to do it like this when using only X
169 anyway, and with callbacks we would have three variants for timer handling
170 instead of the small ifdefs below. */
172 while (
173 #ifdef USE_X_TOOLKIT
174 ! XtAppPending (Xt_app_con)
175 #elif defined USE_GTK
176 ! gtk_events_pending ()
177 #else
178 ! XPending (data)
179 #endif
182 struct timespec next_time = timer_check (), *ntp;
183 fd_set read_fds;
184 struct x_display_info *dpyinfo;
185 int n = 0;
187 FD_ZERO (&read_fds);
188 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
190 int fd = ConnectionNumber (dpyinfo->display);
191 FD_SET (fd, &read_fds);
192 if (fd > n) n = fd;
193 XFlush (dpyinfo->display);
196 if (! timespec_valid_p (next_time))
197 ntp = 0;
198 else
199 ntp = &next_time;
201 #if defined USE_GTK && defined HAVE_GTK3
202 /* Gtk3 have arrows on menus when they don't fit. When the
203 pointer is over an arrow, a timeout scrolls it a bit. Use
204 xg_select so that timeout gets triggered. */
205 xg_select (n + 1, &read_fds, NULL, NULL, ntp, NULL);
206 #else
207 pselect (n + 1, &read_fds, NULL, NULL, ntp, NULL);
208 #endif
211 #endif /* ! MSDOS */
214 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
216 #ifdef USE_X_TOOLKIT
218 /* Loop in Xt until the menu pulldown or dialog popup has been
219 popped down (deactivated). This is used for x-popup-menu
220 and x-popup-dialog; it is not used for the menu bar.
222 NOTE: All calls to popup_get_selection should be protected
223 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
225 static void
226 popup_get_selection (XEvent *initial_event, struct x_display_info *dpyinfo, LWLIB_ID id, int do_timers)
228 XEvent event;
230 while (popup_activated_flag)
232 if (initial_event)
234 event = *initial_event;
235 initial_event = 0;
237 else
239 if (do_timers) x_menu_wait_for_event (0);
240 XtAppNextEvent (Xt_app_con, &event);
243 /* Make sure we don't consider buttons grabbed after menu goes.
244 And make sure to deactivate for any ButtonRelease,
245 even if XtDispatchEvent doesn't do that. */
246 if (event.type == ButtonRelease
247 && dpyinfo->display == event.xbutton.display)
249 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
250 #ifdef USE_MOTIF /* Pretending that the event came from a
251 Btn1Down seems the only way to convince Motif to
252 activate its callbacks; setting the XmNmenuPost
253 isn't working. --marcus@sysc.pdx.edu. */
254 event.xbutton.button = 1;
255 /* Motif only pops down menus when no Ctrl, Alt or Mod
256 key is pressed and the button is released. So reset key state
257 so Motif thinks this is the case. */
258 event.xbutton.state = 0;
259 #endif
261 /* Pop down on C-g and Escape. */
262 else if (event.type == KeyPress
263 && dpyinfo->display == event.xbutton.display)
265 KeySym keysym = XLookupKeysym (&event.xkey, 0);
267 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
268 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
269 popup_activated_flag = 0;
272 x_dispatch_event (&event, event.xany.display);
276 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
277 doc: /* Start key navigation of the menu bar in FRAME.
278 This initially opens the first menu bar item and you can then navigate with the
279 arrow keys, select a menu entry with the return key or cancel with the
280 escape key. If FRAME has no menu bar this function does nothing.
282 If FRAME is nil or not given, use the selected frame. */)
283 (Lisp_Object frame)
285 XEvent ev;
286 struct frame *f = decode_window_system_frame (frame);
287 Widget menubar;
288 block_input ();
290 if (FRAME_EXTERNAL_MENU_BAR (f))
291 set_frame_menubar (f, 0, 1);
293 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
294 if (menubar)
296 Window child;
297 bool error_p = 0;
299 x_catch_errors (FRAME_X_DISPLAY (f));
300 memset (&ev, 0, sizeof ev);
301 ev.xbutton.display = FRAME_X_DISPLAY (f);
302 ev.xbutton.window = XtWindow (menubar);
303 ev.xbutton.root = FRAME_DISPLAY_INFO (f)->root_window;
304 ev.xbutton.time = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
305 ev.xbutton.button = Button1;
306 ev.xbutton.x = ev.xbutton.y = FRAME_MENUBAR_HEIGHT (f) / 2;
307 ev.xbutton.same_screen = True;
309 #ifdef USE_MOTIF
311 Arg al[2];
312 WidgetList list;
313 Cardinal nr;
314 XtSetArg (al[0], XtNchildren, &list);
315 XtSetArg (al[1], XtNnumChildren, &nr);
316 XtGetValues (menubar, al, 2);
317 ev.xbutton.window = XtWindow (list[0]);
319 #endif
321 XTranslateCoordinates (FRAME_X_DISPLAY (f),
322 /* From-window, to-window. */
323 ev.xbutton.window, ev.xbutton.root,
325 /* From-position, to-position. */
326 ev.xbutton.x, ev.xbutton.y,
327 &ev.xbutton.x_root, &ev.xbutton.y_root,
329 /* Child of win. */
330 &child);
331 error_p = x_had_errors_p (FRAME_X_DISPLAY (f));
332 x_uncatch_errors ();
334 if (! error_p)
336 ev.type = ButtonPress;
337 ev.xbutton.state = 0;
339 XtDispatchEvent (&ev);
340 ev.xbutton.type = ButtonRelease;
341 ev.xbutton.state = Button1Mask;
342 XtDispatchEvent (&ev);
346 unblock_input ();
348 return Qnil;
350 #endif /* USE_X_TOOLKIT */
353 #ifdef USE_GTK
354 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
355 doc: /* Start key navigation of the menu bar in FRAME.
356 This initially opens the first menu bar item and you can then navigate with the
357 arrow keys, select a menu entry with the return key or cancel with the
358 escape key. If FRAME has no menu bar this function does nothing.
360 If FRAME is nil or not given, use the selected frame. */)
361 (Lisp_Object frame)
363 GtkWidget *menubar;
364 struct frame *f;
366 block_input ();
367 f = decode_window_system_frame (frame);
369 if (FRAME_EXTERNAL_MENU_BAR (f))
370 set_frame_menubar (f, 0, 1);
372 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
373 if (menubar)
375 /* Activate the first menu. */
376 GList *children = gtk_container_get_children (GTK_CONTAINER (menubar));
378 if (children)
380 g_signal_emit_by_name (children->data, "activate_item");
381 popup_activated_flag = 1;
382 g_list_free (children);
385 unblock_input ();
387 return Qnil;
390 /* Loop util popup_activated_flag is set to zero in a callback.
391 Used for popup menus and dialogs. */
393 static void
394 popup_widget_loop (int do_timers, GtkWidget *widget)
396 ++popup_activated_flag;
398 /* Process events in the Gtk event loop until done. */
399 while (popup_activated_flag)
401 if (do_timers) x_menu_wait_for_event (0);
402 gtk_main_iteration ();
405 #endif
407 /* Activate the menu bar of frame F.
408 This is called from keyboard.c when it gets the
409 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
411 To activate the menu bar, we use the X button-press event
412 that was saved in saved_menu_event.
413 That makes the toolkit do its thing.
415 But first we recompute the menu bar contents (the whole tree).
417 The reason for saving the button event until here, instead of
418 passing it to the toolkit right away, is that we can safely
419 execute Lisp code. */
421 void
422 x_activate_menubar (struct frame *f)
424 eassert (FRAME_X_P (f));
426 if (!f->output_data.x->saved_menu_event->type)
427 return;
429 #ifdef USE_GTK
430 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
431 f->output_data.x->saved_menu_event->xany.window))
432 return;
433 #endif
435 set_frame_menubar (f, 0, 1);
436 block_input ();
437 popup_activated_flag = 1;
438 #ifdef USE_GTK
439 XPutBackEvent (f->output_data.x->display_info->display,
440 f->output_data.x->saved_menu_event);
441 #else
442 XtDispatchEvent (f->output_data.x->saved_menu_event);
443 #endif
444 unblock_input ();
446 /* Ignore this if we get it a second time. */
447 f->output_data.x->saved_menu_event->type = 0;
450 /* This callback is invoked when the user selects a menubar cascade
451 pushbutton, but before the pulldown menu is posted. */
453 #ifndef USE_GTK
454 static void
455 popup_activate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
457 popup_activated_flag = 1;
458 x_activate_timeout_atimer ();
460 #endif
462 /* This callback is invoked when a dialog or menu is finished being
463 used and has been unposted. */
465 static void
466 popup_deactivate_callback (
467 #ifdef USE_GTK
468 GtkWidget *widget, gpointer client_data
469 #else
470 Widget widget, LWLIB_ID id, XtPointer client_data
471 #endif
474 popup_activated_flag = 0;
478 /* Function that finds the frame for WIDGET and shows the HELP text
479 for that widget.
480 F is the frame if known, or NULL if not known. */
481 static void
482 show_help_event (struct frame *f, xt_or_gtk_widget widget, Lisp_Object help)
484 Lisp_Object frame;
486 if (f)
488 XSETFRAME (frame, f);
489 kbd_buffer_store_help_event (frame, help);
491 else
493 #if 0 /* This code doesn't do anything useful. ++kfs */
494 /* WIDGET is the popup menu. It's parent is the frame's
495 widget. See which frame that is. */
496 xt_or_gtk_widget frame_widget = XtParent (widget);
497 Lisp_Object tail;
499 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
501 frame = XCAR (tail);
502 if (FRAMEP (frame)
503 && (f = XFRAME (frame),
504 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
505 break;
507 #endif
508 show_help_echo (help, Qnil, Qnil, Qnil);
512 /* Callback called when menu items are highlighted/unhighlighted
513 while moving the mouse over them. WIDGET is the menu bar or menu
514 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
515 the data structure for the menu item, or null in case of
516 unhighlighting. */
518 #ifdef USE_GTK
519 static void
520 menu_highlight_callback (GtkWidget *widget, gpointer call_data)
522 xg_menu_item_cb_data *cb_data;
523 Lisp_Object help;
525 cb_data = g_object_get_data (G_OBJECT (widget), XG_ITEM_DATA);
526 if (! cb_data) return;
528 help = call_data ? cb_data->help : Qnil;
530 /* If popup_activated_flag is greater than 1 we are in a popup menu.
531 Don't pass the frame to show_help_event for those.
532 Passing frame creates an Emacs event. As we are looping in
533 popup_widget_loop, it won't be handled. Passing NULL shows the tip
534 directly without using an Emacs event. This is what the Lucid code
535 does below. */
536 show_help_event (popup_activated_flag <= 1 ? cb_data->cl_data->f : NULL,
537 widget, help);
539 #else
540 static void
541 menu_highlight_callback (Widget widget, LWLIB_ID id, void *call_data)
543 widget_value *wv = call_data;
544 Lisp_Object help = wv ? wv->help : Qnil;
546 /* Determine the frame for the help event. */
547 struct frame *f = menubar_id_to_frame (id);
549 show_help_event (f, widget, help);
551 #endif
553 #ifdef USE_GTK
554 /* Gtk calls callbacks just because we tell it what item should be
555 selected in a radio group. If this variable is set to a non-zero
556 value, we are creating menus and don't want callbacks right now.
558 static int xg_crazy_callback_abort;
560 /* This callback is called from the menu bar pulldown menu
561 when the user makes a selection.
562 Figure out what the user chose
563 and put the appropriate events into the keyboard buffer. */
564 static void
565 menubar_selection_callback (GtkWidget *widget, gpointer client_data)
567 xg_menu_item_cb_data *cb_data = client_data;
569 if (xg_crazy_callback_abort)
570 return;
572 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
573 return;
575 /* For a group of radio buttons, GTK calls the selection callback first
576 for the item that was active before the selection and then for the one that
577 is active after the selection. For C-h k this means we get the help on
578 the deselected item and then the selected item is executed. Prevent that
579 by ignoring the non-active item. */
580 if (GTK_IS_RADIO_MENU_ITEM (widget)
581 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
582 return;
584 /* When a menu is popped down, X generates a focus event (i.e. focus
585 goes back to the frame below the menu). Since GTK buffers events,
586 we force it out here before the menu selection event. Otherwise
587 sit-for will exit at once if the focus event follows the menu selection
588 event. */
590 block_input ();
591 while (gtk_events_pending ())
592 gtk_main_iteration ();
593 unblock_input ();
595 find_and_call_menu_selection (cb_data->cl_data->f,
596 cb_data->cl_data->menu_bar_items_used,
597 cb_data->cl_data->menu_bar_vector,
598 cb_data->call_data);
601 #else /* not USE_GTK */
603 /* This callback is called from the menu bar pulldown menu
604 when the user makes a selection.
605 Figure out what the user chose
606 and put the appropriate events into the keyboard buffer. */
607 static void
608 menubar_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
610 struct frame *f;
612 f = menubar_id_to_frame (id);
613 if (!f)
614 return;
615 find_and_call_menu_selection (f, f->menu_bar_items_used,
616 f->menu_bar_vector, client_data);
618 #endif /* not USE_GTK */
620 /* Recompute all the widgets of frame F, when the menu bar has been
621 changed. */
623 static void
624 update_frame_menubar (struct frame *f)
626 #ifdef USE_GTK
627 xg_update_frame_menubar (f);
628 #else
629 struct x_output *x;
631 eassert (FRAME_X_P (f));
633 x = f->output_data.x;
635 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
636 return;
638 block_input ();
640 /* Do the voodoo which means "I'm changing lots of things, don't try
641 to refigure sizes until I'm done." */
642 lw_refigure_widget (x->column_widget, False);
644 /* The order in which children are managed is the top to bottom
645 order in which they are displayed in the paned window. First,
646 remove the text-area widget. */
647 XtUnmanageChild (x->edit_widget);
649 /* Remove the menubar that is there now, and put up the menubar that
650 should be there. */
651 XtManageChild (x->menubar_widget);
652 XtMapWidget (x->menubar_widget);
653 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
655 /* Re-manage the text-area widget, and then thrash the sizes. */
656 XtManageChild (x->edit_widget);
657 lw_refigure_widget (x->column_widget, True);
659 /* Force the pane widget to resize itself. */
660 #ifdef USE_LUCID
661 /* For reasons I don't know Lucid wants to add one pixel to the frame
662 height when adding the menu bar. Compensate that here. */
663 adjust_frame_size (f, -1, FRAME_TEXT_HEIGHT (f) - 1, 2, 0, Qmenu_bar_lines);
664 #else
665 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
666 #endif /* USE_LUCID */
667 unblock_input ();
668 #endif /* USE_GTK */
671 #ifdef USE_LUCID
672 static void
673 apply_systemfont_to_dialog (Widget w)
675 const char *fn = xsettings_get_system_normal_font ();
676 if (fn)
678 XrmDatabase db = XtDatabase (XtDisplay (w));
679 if (db)
680 XrmPutStringResource (&db, "*dialog.font", fn);
684 static void
685 apply_systemfont_to_menu (struct frame *f, Widget w)
687 const char *fn = xsettings_get_system_normal_font ();
689 if (fn)
691 XrmDatabase db = XtDatabase (XtDisplay (w));
692 if (db)
694 XrmPutStringResource (&db, "*menubar*font", fn);
695 XrmPutStringResource (&db, "*popup*font", fn);
700 #endif
702 /* Set the contents of the menubar widgets of frame F.
703 The argument FIRST_TIME is currently ignored;
704 it is set the first time this is called, from initialize_frame_menubar. */
706 void
707 set_frame_menubar (struct frame *f, bool first_time, bool deep_p)
709 xt_or_gtk_widget menubar_widget, old_widget;
710 #ifdef USE_X_TOOLKIT
711 LWLIB_ID id;
712 #endif
713 Lisp_Object items;
714 widget_value *wv, *first_wv, *prev_wv = 0;
715 int i;
716 int *submenu_start, *submenu_end;
717 bool *submenu_top_level_items;
718 int *submenu_n_panes;
720 eassert (FRAME_X_P (f));
722 menubar_widget = old_widget = f->output_data.x->menubar_widget;
724 XSETFRAME (Vmenu_updating_frame, f);
726 #ifdef USE_X_TOOLKIT
727 if (f->output_data.x->id == 0)
728 f->output_data.x->id = next_menubar_widget_id++;
729 id = f->output_data.x->id;
730 #endif
732 if (! menubar_widget)
733 deep_p = 1;
734 /* Make the first call for any given frame always go deep. */
735 else if (!f->output_data.x->saved_menu_event && !deep_p)
737 deep_p = 1;
738 f->output_data.x->saved_menu_event = xmalloc (sizeof (XEvent));
739 f->output_data.x->saved_menu_event->type = 0;
742 if (deep_p)
744 /* Make a widget-value tree representing the entire menu trees. */
746 struct buffer *prev = current_buffer;
747 Lisp_Object buffer;
748 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
749 int previous_menu_items_used = f->menu_bar_items_used;
750 Lisp_Object *previous_items
751 = alloca (previous_menu_items_used * sizeof *previous_items);
752 int subitems;
754 /* If we are making a new widget, its contents are empty,
755 do always reinitialize them. */
756 if (! menubar_widget)
757 previous_menu_items_used = 0;
759 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->contents;
760 specbind (Qinhibit_quit, Qt);
761 /* Don't let the debugger step into this code
762 because it is not reentrant. */
763 specbind (Qdebug_on_next_call, Qnil);
765 record_unwind_save_match_data ();
766 if (NILP (Voverriding_local_map_menu_flag))
768 specbind (Qoverriding_terminal_local_map, Qnil);
769 specbind (Qoverriding_local_map, Qnil);
772 set_buffer_internal_1 (XBUFFER (buffer));
774 /* Run the Lucid hook. */
775 safe_run_hooks (Qactivate_menubar_hook);
777 /* If it has changed current-menubar from previous value,
778 really recompute the menubar from the value. */
779 if (! NILP (Vlucid_menu_bar_dirty_flag))
780 call0 (Qrecompute_lucid_menubar);
781 safe_run_hooks (Qmenu_bar_update_hook);
782 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
784 items = FRAME_MENU_BAR_ITEMS (f);
786 /* Save the frame's previous menu bar contents data. */
787 if (previous_menu_items_used)
788 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents,
789 previous_menu_items_used * word_size);
791 /* Fill in menu_items with the current menu bar contents.
792 This can evaluate Lisp code. */
793 save_menu_items ();
795 menu_items = f->menu_bar_vector;
796 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
797 subitems = ASIZE (items) / 4;
798 submenu_start = alloca ((subitems + 1) * sizeof *submenu_start);
799 submenu_end = alloca (subitems * sizeof *submenu_end);
800 submenu_n_panes = alloca (subitems * sizeof *submenu_n_panes);
801 submenu_top_level_items = alloca (subitems
802 * sizeof *submenu_top_level_items);
803 init_menu_items ();
804 for (i = 0; i < subitems; i++)
806 Lisp_Object key, string, maps;
808 key = AREF (items, 4 * i);
809 string = AREF (items, 4 * i + 1);
810 maps = AREF (items, 4 * i + 2);
811 if (NILP (string))
812 break;
814 submenu_start[i] = menu_items_used;
816 menu_items_n_panes = 0;
817 submenu_top_level_items[i]
818 = parse_single_submenu (key, string, maps);
819 submenu_n_panes[i] = menu_items_n_panes;
821 submenu_end[i] = menu_items_used;
824 submenu_start[i] = -1;
825 finish_menu_items ();
827 /* Convert menu_items into widget_value trees
828 to display the menu. This cannot evaluate Lisp code. */
830 wv = make_widget_value ("menubar", NULL, true, Qnil);
831 wv->button_type = BUTTON_TYPE_NONE;
832 first_wv = wv;
834 for (i = 0; submenu_start[i] >= 0; i++)
836 menu_items_n_panes = submenu_n_panes[i];
837 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
838 submenu_top_level_items[i]);
839 if (prev_wv)
840 prev_wv->next = wv;
841 else
842 first_wv->contents = wv;
843 /* Don't set wv->name here; GC during the loop might relocate it. */
844 wv->enabled = 1;
845 wv->button_type = BUTTON_TYPE_NONE;
846 prev_wv = wv;
849 set_buffer_internal_1 (prev);
851 /* If there has been no change in the Lisp-level contents
852 of the menu bar, skip redisplaying it. Just exit. */
854 /* Compare the new menu items with the ones computed last time. */
855 for (i = 0; i < previous_menu_items_used; i++)
856 if (menu_items_used == i
857 || (!EQ (previous_items[i], AREF (menu_items, i))))
858 break;
859 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
861 /* The menu items have not changed. Don't bother updating
862 the menus in any form, since it would be a no-op. */
863 free_menubar_widget_value_tree (first_wv);
864 discard_menu_items ();
865 unbind_to (specpdl_count, Qnil);
866 return;
869 /* The menu items are different, so store them in the frame. */
870 fset_menu_bar_vector (f, menu_items);
871 f->menu_bar_items_used = menu_items_used;
873 /* This undoes save_menu_items. */
874 unbind_to (specpdl_count, Qnil);
876 /* Now GC cannot happen during the lifetime of the widget_value,
877 so it's safe to store data from a Lisp_String. */
878 wv = first_wv->contents;
879 for (i = 0; i < ASIZE (items); i += 4)
881 Lisp_Object string;
882 string = AREF (items, i + 1);
883 if (NILP (string))
884 break;
885 wv->name = SSDATA (string);
886 update_submenu_strings (wv->contents);
887 wv = wv->next;
891 else
893 /* Make a widget-value tree containing
894 just the top level menu bar strings. */
896 wv = make_widget_value ("menubar", NULL, true, Qnil);
897 wv->button_type = BUTTON_TYPE_NONE;
898 first_wv = wv;
900 items = FRAME_MENU_BAR_ITEMS (f);
901 for (i = 0; i < ASIZE (items); i += 4)
903 Lisp_Object string;
905 string = AREF (items, i + 1);
906 if (NILP (string))
907 break;
909 wv = make_widget_value (SSDATA (string), NULL, true, Qnil);
910 wv->button_type = BUTTON_TYPE_NONE;
911 /* This prevents lwlib from assuming this
912 menu item is really supposed to be empty. */
913 /* The intptr_t cast avoids a warning.
914 This value just has to be different from small integers. */
915 wv->call_data = (void *) (intptr_t) (-1);
917 if (prev_wv)
918 prev_wv->next = wv;
919 else
920 first_wv->contents = wv;
921 prev_wv = wv;
924 /* Forget what we thought we knew about what is in the
925 detailed contents of the menu bar menus.
926 Changing the top level always destroys the contents. */
927 f->menu_bar_items_used = 0;
930 /* Create or update the menu bar widget. */
932 block_input ();
934 #ifdef USE_GTK
935 xg_crazy_callback_abort = 1;
936 if (menubar_widget)
938 /* The fourth arg is DEEP_P, which says to consider the entire
939 menu trees we supply, rather than just the menu bar item names. */
940 xg_modify_menubar_widgets (menubar_widget,
942 first_wv,
943 deep_p,
944 G_CALLBACK (menubar_selection_callback),
945 G_CALLBACK (popup_deactivate_callback),
946 G_CALLBACK (menu_highlight_callback));
948 else
950 menubar_widget
951 = xg_create_widget ("menubar", "menubar", f, first_wv,
952 G_CALLBACK (menubar_selection_callback),
953 G_CALLBACK (popup_deactivate_callback),
954 G_CALLBACK (menu_highlight_callback));
956 f->output_data.x->menubar_widget = menubar_widget;
960 #else /* not USE_GTK */
961 if (menubar_widget)
963 /* Disable resizing (done for Motif!) */
964 lw_allow_resizing (f->output_data.x->widget, False);
966 /* The third arg is DEEP_P, which says to consider the entire
967 menu trees we supply, rather than just the menu bar item names. */
968 lw_modify_all_widgets (id, first_wv, deep_p);
970 /* Re-enable the edit widget to resize. */
971 lw_allow_resizing (f->output_data.x->widget, True);
973 else
975 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
976 XtTranslations override = XtParseTranslationTable (menuOverride);
978 #ifdef USE_LUCID
979 apply_systemfont_to_menu (f, f->output_data.x->column_widget);
980 #endif
981 menubar_widget = lw_create_widget ("menubar", "menubar", id,
982 first_wv,
983 f->output_data.x->column_widget,
985 popup_activate_callback,
986 menubar_selection_callback,
987 popup_deactivate_callback,
988 menu_highlight_callback);
989 f->output_data.x->menubar_widget = menubar_widget;
991 /* Make menu pop down on C-g. */
992 XtOverrideTranslations (menubar_widget, override);
996 int menubar_size;
997 if (f->output_data.x->menubar_widget)
998 XtRealizeWidget (f->output_data.x->menubar_widget);
1000 menubar_size
1001 = (f->output_data.x->menubar_widget
1002 ? (f->output_data.x->menubar_widget->core.height
1003 + f->output_data.x->menubar_widget->core.border_width)
1004 : 0);
1006 #if 1 /* Experimentally, we now get the right results
1007 for -geometry -0-0 without this. 24 Aug 96, rms.
1008 Maybe so, but the menu bar size is missing the pixels so the
1009 WM size hints are off by these pixels. Jan D, oct 2009. */
1010 #ifdef USE_LUCID
1011 if (FRAME_EXTERNAL_MENU_BAR (f))
1013 Dimension ibw = 0;
1014 XtVaGetValues (f->output_data.x->column_widget,
1015 XtNinternalBorderWidth, &ibw, NULL);
1016 menubar_size += ibw;
1018 #endif /* USE_LUCID */
1019 #endif /* 1 */
1021 FRAME_MENUBAR_HEIGHT (f) = menubar_size;
1023 #endif /* not USE_GTK */
1025 free_menubar_widget_value_tree (first_wv);
1026 update_frame_menubar (f);
1028 #ifdef USE_GTK
1029 xg_crazy_callback_abort = 0;
1030 #endif
1032 unblock_input ();
1035 /* Called from Fx_create_frame to create the initial menubar of a frame
1036 before it is mapped, so that the window is mapped with the menubar already
1037 there instead of us tacking it on later and thrashing the window after it
1038 is visible. */
1040 void
1041 initialize_frame_menubar (struct frame *f)
1043 /* This function is called before the first chance to redisplay
1044 the frame. It has to be, so the frame will have the right size. */
1045 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
1046 set_frame_menubar (f, 1, 1);
1050 /* Get rid of the menu bar of frame F, and free its storage.
1051 This is used when deleting a frame, and when turning off the menu bar.
1052 For GTK this function is in gtkutil.c. */
1054 #ifndef USE_GTK
1055 void
1056 free_frame_menubar (struct frame *f)
1058 Widget menubar_widget;
1059 #ifdef USE_MOTIF
1060 /* Motif automatically shrinks the frame in lw_destroy_all_widgets.
1061 If we want to preserve the old height, calculate it now so we can
1062 restore it below. */
1063 int old_height = FRAME_TEXT_HEIGHT (f) + FRAME_MENUBAR_HEIGHT (f);
1064 #endif
1066 eassert (FRAME_X_P (f));
1068 menubar_widget = f->output_data.x->menubar_widget;
1070 FRAME_MENUBAR_HEIGHT (f) = 0;
1072 if (menubar_widget)
1074 #ifdef USE_MOTIF
1075 /* Removing the menu bar magically changes the shell widget's x
1076 and y position of (0, 0) which, when the menu bar is turned
1077 on again, leads to pull-down menus appearing in strange
1078 positions near the upper-left corner of the display. This
1079 happens only with some window managers like twm and ctwm,
1080 but not with other like Motif's mwm or kwm, because the
1081 latter generate ConfigureNotify events when the menu bar
1082 is switched off, which fixes the shell position. */
1083 Position x0, y0, x1, y1;
1084 #endif
1086 block_input ();
1088 #ifdef USE_MOTIF
1089 if (f->output_data.x->widget)
1090 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
1091 #endif
1093 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1094 f->output_data.x->menubar_widget = NULL;
1096 if (f->output_data.x->widget)
1098 #ifdef USE_MOTIF
1099 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
1100 if (x1 == 0 && y1 == 0)
1101 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
1102 if (frame_inhibit_resize (f, 0, Qmenu_bar_lines))
1103 adjust_frame_size (f, -1, old_height, 1, 0, Qmenu_bar_lines);
1104 else
1105 #endif /* USE_MOTIF */
1106 adjust_frame_size (f, -1, -1, 2, 0, Qmenu_bar_lines);
1108 else
1110 #ifdef USE_MOTIF
1111 if (frame_inhibit_resize (f, 0, Qmenu_bar_lines))
1112 adjust_frame_size (f, -1, old_height, 1, 0, Qmenu_bar_lines);
1113 #endif
1116 unblock_input ();
1119 #endif /* not USE_GTK */
1121 #endif /* USE_X_TOOLKIT || USE_GTK */
1123 /* x_menu_show actually displays a menu using the panes and items in menu_items
1124 and returns the value selected from it.
1125 There are two versions of x_menu_show, one for Xt and one for Xlib.
1126 Both assume input is blocked by the caller. */
1128 /* F is the frame the menu is for.
1129 X and Y are the frame-relative specified position,
1130 relative to the inside upper left corner of the frame F.
1131 Bitfield MENUFLAGS bits are:
1132 MENU_FOR_CLICK is set if this menu was invoked for a mouse click.
1133 MENU_KEYMAPS is set if this menu was specified with keymaps;
1134 in that case, we return a list containing the chosen item's value
1135 and perhaps also the pane's prefix.
1136 TITLE is the specified menu title.
1137 ERROR is a place to store an error message string in case of failure.
1138 (We return nil on failure, but the value doesn't actually matter.) */
1140 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1142 /* The item selected in the popup menu. */
1143 static Lisp_Object *volatile menu_item_selection;
1145 #ifdef USE_GTK
1147 /* Used when position a popup menu. See menu_position_func and
1148 create_and_show_popup_menu below. */
1149 struct next_popup_x_y
1151 struct frame *f;
1152 int x;
1153 int y;
1156 /* The menu position function to use if we are not putting a popup
1157 menu where the pointer is.
1158 MENU is the menu to pop up.
1159 X and Y shall on exit contain x/y where the menu shall pop up.
1160 PUSH_IN is not documented in the GTK manual.
1161 USER_DATA is any data passed in when calling gtk_menu_popup.
1162 Here it points to a struct next_popup_x_y where the coordinates
1163 to store in *X and *Y are as well as the frame for the popup.
1165 Here only X and Y are used. */
1166 static void
1167 menu_position_func (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer user_data)
1169 struct next_popup_x_y *data = user_data;
1170 GtkRequisition req;
1171 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (data->f);
1172 int disp_width = x_display_pixel_width (dpyinfo);
1173 int disp_height = x_display_pixel_height (dpyinfo);
1175 *x = data->x;
1176 *y = data->y;
1178 /* Check if there is room for the menu. If not, adjust x/y so that
1179 the menu is fully visible. */
1180 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
1181 if (data->x + req.width > disp_width)
1182 *x -= data->x + req.width - disp_width;
1183 if (data->y + req.height > disp_height)
1184 *y -= data->y + req.height - disp_height;
1187 static void
1188 popup_selection_callback (GtkWidget *widget, gpointer client_data)
1190 xg_menu_item_cb_data *cb_data = client_data;
1192 if (xg_crazy_callback_abort) return;
1193 if (cb_data) menu_item_selection = cb_data->call_data;
1196 static void
1197 pop_down_menu (void *arg)
1199 popup_activated_flag = 0;
1200 block_input ();
1201 gtk_widget_destroy (GTK_WIDGET (arg));
1202 unblock_input ();
1205 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1206 menu pops down.
1207 menu_item_selection will be set to the selection. */
1208 static void
1209 create_and_show_popup_menu (struct frame *f, widget_value *first_wv,
1210 int x, int y, bool for_click)
1212 int i;
1213 GtkWidget *menu;
1214 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
1215 struct next_popup_x_y popup_x_y;
1216 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1217 bool use_pos_func = ! for_click;
1219 #ifdef HAVE_GTK3
1220 /* Always use position function for Gtk3. Otherwise menus may become
1221 too small to show anything. */
1222 use_pos_func = 1;
1223 #endif
1225 eassert (FRAME_X_P (f));
1227 xg_crazy_callback_abort = 1;
1228 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
1229 G_CALLBACK (popup_selection_callback),
1230 G_CALLBACK (popup_deactivate_callback),
1231 G_CALLBACK (menu_highlight_callback));
1232 xg_crazy_callback_abort = 0;
1234 if (use_pos_func)
1236 /* Not invoked by a click. pop up at x/y. */
1237 pos_func = menu_position_func;
1239 /* Adjust coordinates to be root-window-relative. */
1240 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1241 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1243 popup_x_y.x = x;
1244 popup_x_y.y = y;
1245 popup_x_y.f = f;
1247 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1250 if (for_click)
1252 for (i = 0; i < 5; i++)
1253 if (FRAME_DISPLAY_INFO (f)->grabbed & (1 << i))
1254 break;
1255 /* If keys aren't grabbed (i.e., a mouse up event), use 0. */
1256 if (i == 5) i = 0;
1259 /* Display the menu. */
1260 gtk_widget_show_all (menu);
1262 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i,
1263 FRAME_DISPLAY_INFO (f)->last_user_time);
1265 record_unwind_protect_ptr (pop_down_menu, menu);
1267 if (gtk_widget_get_mapped (menu))
1269 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1270 two. show_help_echo uses this to detect popup menus. */
1271 popup_activated_flag = 1;
1272 /* Process events that apply to the menu. */
1273 popup_widget_loop (1, menu);
1276 unbind_to (specpdl_count, Qnil);
1278 /* Must reset this manually because the button release event is not passed
1279 to Emacs event loop. */
1280 FRAME_DISPLAY_INFO (f)->grabbed = 0;
1283 #else /* not USE_GTK */
1285 /* We need a unique id for each widget handled by the Lucid Widget
1286 library.
1288 For the main windows, and popup menus, we use this counter, which we
1289 increment each time after use. This starts from WIDGET_ID_TICK_START.
1291 For menu bars, we use numbers starting at 0, counted in
1292 next_menubar_widget_id. */
1293 LWLIB_ID widget_id_tick;
1295 static void
1296 popup_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1298 menu_item_selection = client_data;
1301 /* ID is the LWLIB ID of the dialog box. */
1303 static void
1304 pop_down_menu (int id)
1306 block_input ();
1307 lw_destroy_all_widgets ((LWLIB_ID) id);
1308 unblock_input ();
1309 popup_activated_flag = 0;
1312 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1313 menu pops down.
1314 menu_item_selection will be set to the selection. */
1315 static void
1316 create_and_show_popup_menu (struct frame *f, widget_value *first_wv,
1317 int x, int y, bool for_click)
1319 int i;
1320 Arg av[2];
1321 int ac = 0;
1322 XEvent dummy;
1323 XButtonPressedEvent *event = &(dummy.xbutton);
1324 LWLIB_ID menu_id;
1325 Widget menu;
1327 eassert (FRAME_X_P (f));
1329 #ifdef USE_LUCID
1330 apply_systemfont_to_menu (f, f->output_data.x->widget);
1331 #endif
1333 menu_id = widget_id_tick++;
1334 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1335 f->output_data.x->widget, 1, 0,
1336 popup_selection_callback,
1337 popup_deactivate_callback,
1338 menu_highlight_callback);
1340 event->type = ButtonPress;
1341 event->serial = 0;
1342 event->send_event = 0;
1343 event->display = FRAME_X_DISPLAY (f);
1344 event->time = CurrentTime;
1345 event->root = FRAME_DISPLAY_INFO (f)->root_window;
1346 event->window = event->subwindow = event->root;
1347 event->x = x;
1348 event->y = y;
1350 /* Adjust coordinates to be root-window-relative. */
1351 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1352 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1354 event->x_root = x;
1355 event->y_root = y;
1357 event->state = 0;
1358 event->button = 0;
1359 for (i = 0; i < 5; i++)
1360 if (FRAME_DISPLAY_INFO (f)->grabbed & (1 << i))
1361 event->button = i;
1363 /* Don't allow any geometry request from the user. */
1364 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1365 XtSetValues (menu, av, ac);
1367 /* Display the menu. */
1368 lw_popup_menu (menu, &dummy);
1369 popup_activated_flag = 1;
1370 x_activate_timeout_atimer ();
1373 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1375 record_unwind_protect_int (pop_down_menu, (int) menu_id);
1377 /* Process events that apply to the menu. */
1378 popup_get_selection (0, FRAME_DISPLAY_INFO (f), menu_id, 1);
1380 unbind_to (specpdl_count, Qnil);
1384 #endif /* not USE_GTK */
1386 static void
1387 cleanup_widget_value_tree (void *arg)
1389 free_menubar_widget_value_tree (arg);
1392 Lisp_Object
1393 x_menu_show (struct frame *f, int x, int y, int menuflags,
1394 Lisp_Object title, const char **error_name)
1396 int i;
1397 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1398 widget_value **submenu_stack
1399 = alloca (menu_items_used * sizeof *submenu_stack);
1400 Lisp_Object *subprefix_stack
1401 = alloca (menu_items_used * sizeof *subprefix_stack);
1402 int submenu_depth = 0;
1404 int first_pane;
1406 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1408 eassert (FRAME_X_P (f));
1410 *error_name = NULL;
1412 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1414 *error_name = "Empty menu";
1415 return Qnil;
1418 block_input ();
1420 /* Create a tree of widget_value objects
1421 representing the panes and their items. */
1422 wv = make_widget_value ("menu", NULL, true, Qnil);
1423 wv->button_type = BUTTON_TYPE_NONE;
1424 first_wv = wv;
1425 first_pane = 1;
1427 /* Loop over all panes and items, filling in the tree. */
1428 i = 0;
1429 while (i < menu_items_used)
1431 if (EQ (AREF (menu_items, i), Qnil))
1433 submenu_stack[submenu_depth++] = save_wv;
1434 save_wv = prev_wv;
1435 prev_wv = 0;
1436 first_pane = 1;
1437 i++;
1439 else if (EQ (AREF (menu_items, i), Qlambda))
1441 prev_wv = save_wv;
1442 save_wv = submenu_stack[--submenu_depth];
1443 first_pane = 0;
1444 i++;
1446 else if (EQ (AREF (menu_items, i), Qt)
1447 && submenu_depth != 0)
1448 i += MENU_ITEMS_PANE_LENGTH;
1449 /* Ignore a nil in the item list.
1450 It's meaningful only for dialog boxes. */
1451 else if (EQ (AREF (menu_items, i), Qquote))
1452 i += 1;
1453 else if (EQ (AREF (menu_items, i), Qt))
1455 /* Create a new pane. */
1456 Lisp_Object pane_name, prefix;
1457 const char *pane_string;
1459 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1460 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1462 #ifndef HAVE_MULTILINGUAL_MENU
1463 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1465 pane_name = ENCODE_MENU_STRING (pane_name);
1466 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
1468 #endif
1469 pane_string = (NILP (pane_name)
1470 ? "" : SSDATA (pane_name));
1471 /* If there is just one top-level pane, put all its items directly
1472 under the top-level menu. */
1473 if (menu_items_n_panes == 1)
1474 pane_string = "";
1476 /* If the pane has a meaningful name,
1477 make the pane a top-level menu item
1478 with its items as a submenu beneath it. */
1479 if (!(menuflags & MENU_KEYMAPS) && strcmp (pane_string, ""))
1481 wv = make_widget_value (pane_string, NULL, true, Qnil);
1482 if (save_wv)
1483 save_wv->next = wv;
1484 else
1485 first_wv->contents = wv;
1486 if ((menuflags & MENU_KEYMAPS) && !NILP (prefix))
1487 wv->name++;
1488 wv->button_type = BUTTON_TYPE_NONE;
1489 save_wv = wv;
1490 prev_wv = 0;
1492 else if (first_pane)
1494 save_wv = wv;
1495 prev_wv = 0;
1497 first_pane = 0;
1498 i += MENU_ITEMS_PANE_LENGTH;
1500 else
1502 /* Create a new item within current pane. */
1503 Lisp_Object item_name, enable, descrip, def, type, selected, help;
1504 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1505 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1506 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1507 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1508 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1509 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1510 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1512 #ifndef HAVE_MULTILINGUAL_MENU
1513 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
1515 item_name = ENCODE_MENU_STRING (item_name);
1516 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
1519 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1521 descrip = ENCODE_MENU_STRING (descrip);
1522 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
1524 #endif /* not HAVE_MULTILINGUAL_MENU */
1526 wv = make_widget_value (SSDATA (item_name), NULL, !NILP (enable),
1527 STRINGP (help) ? help : Qnil);
1528 if (prev_wv)
1529 prev_wv->next = wv;
1530 else
1531 save_wv->contents = wv;
1532 if (!NILP (descrip))
1533 wv->key = SSDATA (descrip);
1534 /* If this item has a null value,
1535 make the call_data null so that it won't display a box
1536 when the mouse is on it. */
1537 wv->call_data = !NILP (def) ? aref_addr (menu_items, i) : 0;
1539 if (NILP (type))
1540 wv->button_type = BUTTON_TYPE_NONE;
1541 else if (EQ (type, QCtoggle))
1542 wv->button_type = BUTTON_TYPE_TOGGLE;
1543 else if (EQ (type, QCradio))
1544 wv->button_type = BUTTON_TYPE_RADIO;
1545 else
1546 emacs_abort ();
1548 wv->selected = !NILP (selected);
1550 prev_wv = wv;
1552 i += MENU_ITEMS_ITEM_LENGTH;
1556 /* Deal with the title, if it is non-nil. */
1557 if (!NILP (title))
1559 widget_value *wv_title;
1560 widget_value *wv_sep1 = make_widget_value ("--", NULL, false, Qnil);
1561 widget_value *wv_sep2 = make_widget_value ("--", NULL, false, Qnil);
1563 wv_sep2->next = first_wv->contents;
1564 wv_sep1->next = wv_sep2;
1566 #ifndef HAVE_MULTILINGUAL_MENU
1567 if (STRING_MULTIBYTE (title))
1568 title = ENCODE_MENU_STRING (title);
1569 #endif
1571 wv_title = make_widget_value (SSDATA (title), NULL, true, Qnil);
1572 wv_title->button_type = BUTTON_TYPE_NONE;
1573 wv_title->next = wv_sep1;
1574 first_wv->contents = wv_title;
1577 /* No selection has been chosen yet. */
1578 menu_item_selection = 0;
1580 /* Make sure to free the widget_value objects we used to specify the
1581 contents even with longjmp. */
1582 record_unwind_protect_ptr (cleanup_widget_value_tree, first_wv);
1584 /* Actually create and show the menu until popped down. */
1585 create_and_show_popup_menu (f, first_wv, x, y,
1586 menuflags & MENU_FOR_CLICK);
1588 unbind_to (specpdl_count, Qnil);
1590 /* Find the selected item, and its pane, to return
1591 the proper value. */
1592 if (menu_item_selection != 0)
1594 Lisp_Object prefix, entry;
1596 prefix = entry = Qnil;
1597 i = 0;
1598 while (i < menu_items_used)
1600 if (EQ (AREF (menu_items, i), Qnil))
1602 subprefix_stack[submenu_depth++] = prefix;
1603 prefix = entry;
1604 i++;
1606 else if (EQ (AREF (menu_items, i), Qlambda))
1608 prefix = subprefix_stack[--submenu_depth];
1609 i++;
1611 else if (EQ (AREF (menu_items, i), Qt))
1613 prefix
1614 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1615 i += MENU_ITEMS_PANE_LENGTH;
1617 /* Ignore a nil in the item list.
1618 It's meaningful only for dialog boxes. */
1619 else if (EQ (AREF (menu_items, i), Qquote))
1620 i += 1;
1621 else
1623 entry
1624 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
1625 if (menu_item_selection == aref_addr (menu_items, i))
1627 if (menuflags & MENU_KEYMAPS)
1629 int j;
1631 entry = list1 (entry);
1632 if (!NILP (prefix))
1633 entry = Fcons (prefix, entry);
1634 for (j = submenu_depth - 1; j >= 0; j--)
1635 if (!NILP (subprefix_stack[j]))
1636 entry = Fcons (subprefix_stack[j], entry);
1638 unblock_input ();
1639 return entry;
1641 i += MENU_ITEMS_ITEM_LENGTH;
1645 else if (!(menuflags & MENU_FOR_CLICK))
1647 unblock_input ();
1648 /* Make "Cancel" equivalent to C-g. */
1649 Fsignal (Qquit, Qnil);
1652 unblock_input ();
1653 return Qnil;
1656 #ifdef USE_GTK
1657 static void
1658 dialog_selection_callback (GtkWidget *widget, gpointer client_data)
1660 /* Treat the pointer as an integer. There's no problem
1661 as long as pointers have enough bits to hold small integers. */
1662 if ((intptr_t) client_data != -1)
1663 menu_item_selection = client_data;
1665 popup_activated_flag = 0;
1668 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1669 dialog pops down.
1670 menu_item_selection will be set to the selection. */
1671 static void
1672 create_and_show_dialog (struct frame *f, widget_value *first_wv)
1674 GtkWidget *menu;
1676 eassert (FRAME_X_P (f));
1678 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
1679 G_CALLBACK (dialog_selection_callback),
1680 G_CALLBACK (popup_deactivate_callback),
1683 if (menu)
1685 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1686 record_unwind_protect_ptr (pop_down_menu, menu);
1688 /* Display the menu. */
1689 gtk_widget_show_all (menu);
1691 /* Process events that apply to the menu. */
1692 popup_widget_loop (1, menu);
1694 unbind_to (specpdl_count, Qnil);
1698 #else /* not USE_GTK */
1699 static void
1700 dialog_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1702 /* Treat the pointer as an integer. There's no problem
1703 as long as pointers have enough bits to hold small integers. */
1704 if ((intptr_t) client_data != -1)
1705 menu_item_selection = client_data;
1707 block_input ();
1708 lw_destroy_all_widgets (id);
1709 unblock_input ();
1710 popup_activated_flag = 0;
1714 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1715 dialog pops down.
1716 menu_item_selection will be set to the selection. */
1717 static void
1718 create_and_show_dialog (struct frame *f, widget_value *first_wv)
1720 LWLIB_ID dialog_id;
1722 eassert (FRAME_X_P (f));
1724 dialog_id = widget_id_tick++;
1725 #ifdef USE_LUCID
1726 apply_systemfont_to_dialog (f->output_data.x->widget);
1727 #endif
1728 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1729 f->output_data.x->widget, 1, 0,
1730 dialog_selection_callback, 0, 0);
1731 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1732 /* Display the dialog box. */
1733 lw_pop_up_all_widgets (dialog_id);
1734 popup_activated_flag = 1;
1735 x_activate_timeout_atimer ();
1737 /* Process events that apply to the dialog box.
1738 Also handle timers. */
1740 ptrdiff_t count = SPECPDL_INDEX ();
1742 /* xdialog_show_unwind is responsible for popping the dialog box down. */
1744 record_unwind_protect_int (pop_down_menu, (int) dialog_id);
1746 popup_get_selection (0, FRAME_DISPLAY_INFO (f), dialog_id, 1);
1748 unbind_to (count, Qnil);
1752 #endif /* not USE_GTK */
1754 static const char * button_names [] = {
1755 "button1", "button2", "button3", "button4", "button5",
1756 "button6", "button7", "button8", "button9", "button10" };
1758 static Lisp_Object
1759 x_dialog_show (struct frame *f, Lisp_Object title,
1760 Lisp_Object header, const char **error_name)
1762 int i, nb_buttons=0;
1763 char dialog_name[6];
1765 widget_value *wv, *first_wv = 0, *prev_wv = 0;
1767 /* Number of elements seen so far, before boundary. */
1768 int left_count = 0;
1769 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1770 int boundary_seen = 0;
1772 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1774 eassert (FRAME_X_P (f));
1776 *error_name = NULL;
1778 if (menu_items_n_panes > 1)
1780 *error_name = "Multiple panes in dialog box";
1781 return Qnil;
1784 /* Create a tree of widget_value objects
1785 representing the text label and buttons. */
1787 Lisp_Object pane_name;
1788 const char *pane_string;
1789 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
1790 pane_string = (NILP (pane_name)
1791 ? "" : SSDATA (pane_name));
1792 prev_wv = make_widget_value ("message", (char *) pane_string, true, Qnil);
1793 first_wv = prev_wv;
1795 /* Loop over all panes and items, filling in the tree. */
1796 i = MENU_ITEMS_PANE_LENGTH;
1797 while (i < menu_items_used)
1800 /* Create a new item within current pane. */
1801 Lisp_Object item_name, enable, descrip;
1802 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1803 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1804 descrip
1805 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1807 if (NILP (item_name))
1809 free_menubar_widget_value_tree (first_wv);
1810 *error_name = "Submenu in dialog items";
1811 return Qnil;
1813 if (EQ (item_name, Qquote))
1815 /* This is the boundary between left-side elts
1816 and right-side elts. Stop incrementing right_count. */
1817 boundary_seen = 1;
1818 i++;
1819 continue;
1821 if (nb_buttons >= 9)
1823 free_menubar_widget_value_tree (first_wv);
1824 *error_name = "Too many dialog items";
1825 return Qnil;
1828 wv = make_widget_value (button_names[nb_buttons],
1829 SSDATA (item_name),
1830 !NILP (enable), Qnil);
1831 prev_wv->next = wv;
1832 if (!NILP (descrip))
1833 wv->key = SSDATA (descrip);
1834 wv->call_data = aref_addr (menu_items, i);
1835 prev_wv = wv;
1837 if (! boundary_seen)
1838 left_count++;
1840 nb_buttons++;
1841 i += MENU_ITEMS_ITEM_LENGTH;
1844 /* If the boundary was not specified,
1845 by default put half on the left and half on the right. */
1846 if (! boundary_seen)
1847 left_count = nb_buttons - nb_buttons / 2;
1849 wv = make_widget_value (dialog_name, NULL, false, Qnil);
1851 /* Frame title: 'Q' = Question, 'I' = Information.
1852 Can also have 'E' = Error if, one day, we want
1853 a popup for errors. */
1854 if (NILP (header))
1855 dialog_name[0] = 'Q';
1856 else
1857 dialog_name[0] = 'I';
1859 /* Dialog boxes use a really stupid name encoding
1860 which specifies how many buttons to use
1861 and how many buttons are on the right. */
1862 dialog_name[1] = '0' + nb_buttons;
1863 dialog_name[2] = 'B';
1864 dialog_name[3] = 'R';
1865 /* Number of buttons to put on the right. */
1866 dialog_name[4] = '0' + nb_buttons - left_count;
1867 dialog_name[5] = 0;
1868 wv->contents = first_wv;
1869 first_wv = wv;
1872 /* No selection has been chosen yet. */
1873 menu_item_selection = 0;
1875 /* Make sure to free the widget_value objects we used to specify the
1876 contents even with longjmp. */
1877 record_unwind_protect_ptr (cleanup_widget_value_tree, first_wv);
1879 /* Actually create and show the dialog. */
1880 create_and_show_dialog (f, first_wv);
1882 unbind_to (specpdl_count, Qnil);
1884 /* Find the selected item, and its pane, to return
1885 the proper value. */
1886 if (menu_item_selection != 0)
1888 i = 0;
1889 while (i < menu_items_used)
1891 Lisp_Object entry;
1893 if (EQ (AREF (menu_items, i), Qt))
1894 i += MENU_ITEMS_PANE_LENGTH;
1895 else if (EQ (AREF (menu_items, i), Qquote))
1897 /* This is the boundary between left-side elts and
1898 right-side elts. */
1899 ++i;
1901 else
1903 entry
1904 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
1905 if (menu_item_selection == aref_addr (menu_items, i))
1906 return entry;
1907 i += MENU_ITEMS_ITEM_LENGTH;
1911 else
1912 /* Make "Cancel" equivalent to C-g. */
1913 Fsignal (Qquit, Qnil);
1915 return Qnil;
1918 Lisp_Object
1919 xw_popup_dialog (struct frame *f, Lisp_Object header, Lisp_Object contents)
1921 Lisp_Object title;
1922 const char *error_name;
1923 Lisp_Object selection;
1924 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1926 check_window_system (f);
1928 /* Decode the dialog items from what was specified. */
1929 title = Fcar (contents);
1930 CHECK_STRING (title);
1931 record_unwind_protect_void (unuse_menu_items);
1933 if (NILP (Fcar (Fcdr (contents))))
1934 /* No buttons specified, add an "Ok" button so users can pop down
1935 the dialog. Also, the lesstif/motif version crashes if there are
1936 no buttons. */
1937 contents = list2 (title, Fcons (build_string ("Ok"), Qt));
1939 list_of_panes (list1 (contents));
1941 /* Display them in a dialog box. */
1942 block_input ();
1943 selection = x_dialog_show (f, title, header, &error_name);
1944 unblock_input ();
1946 unbind_to (specpdl_count, Qnil);
1947 discard_menu_items ();
1949 if (error_name) error ("%s", error_name);
1950 return selection;
1953 #else /* not USE_X_TOOLKIT && not USE_GTK */
1955 /* The frame of the last activated non-toolkit menu bar.
1956 Used to generate menu help events. */
1958 static struct frame *menu_help_frame;
1961 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
1963 PANE is the pane number, and ITEM is the menu item number in
1964 the menu (currently not used).
1966 This cannot be done with generating a HELP_EVENT because
1967 XMenuActivate contains a loop that doesn't let Emacs process
1968 keyboard events. */
1970 static void
1971 menu_help_callback (char const *help_string, int pane, int item)
1973 Lisp_Object *first_item;
1974 Lisp_Object pane_name;
1975 Lisp_Object menu_object;
1977 first_item = XVECTOR (menu_items)->contents;
1978 if (EQ (first_item[0], Qt))
1979 pane_name = first_item[MENU_ITEMS_PANE_NAME];
1980 else if (EQ (first_item[0], Qquote))
1981 /* This shouldn't happen, see x_menu_show. */
1982 pane_name = empty_unibyte_string;
1983 else
1984 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
1986 /* (menu-item MENU-NAME PANE-NUMBER) */
1987 menu_object = list3 (Qmenu_item, pane_name, make_number (pane));
1988 show_help_echo (help_string ? build_string (help_string) : Qnil,
1989 Qnil, menu_object, make_number (item));
1992 static void
1993 pop_down_menu (Lisp_Object arg)
1995 struct frame *f = XSAVE_POINTER (arg, 0);
1996 XMenu *menu = XSAVE_POINTER (arg, 1);
1998 block_input ();
1999 #ifndef MSDOS
2000 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
2001 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
2002 #endif
2003 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2005 #ifdef HAVE_X_WINDOWS
2006 /* Assume the mouse has moved out of the X window.
2007 If it has actually moved in, we will get an EnterNotify. */
2008 x_mouse_leave (FRAME_DISPLAY_INFO (f));
2010 /* State that no mouse buttons are now held.
2011 (The oldXMenu code doesn't track this info for us.)
2012 That is not necessarily true, but the fiction leads to reasonable
2013 results, and it is a pain to ask which are actually held now. */
2014 FRAME_DISPLAY_INFO (f)->grabbed = 0;
2016 #endif /* HAVE_X_WINDOWS */
2018 unblock_input ();
2022 Lisp_Object
2023 x_menu_show (struct frame *f, int x, int y, int menuflags,
2024 Lisp_Object title, const char **error_name)
2026 Window root;
2027 XMenu *menu;
2028 int pane, selidx, lpane, status;
2029 Lisp_Object entry = Qnil;
2030 Lisp_Object pane_prefix;
2031 char *datap;
2032 int ulx, uly, width, height;
2033 int dispwidth, dispheight;
2034 int i, j, lines, maxlines;
2035 int maxwidth;
2036 int dummy_int;
2037 unsigned int dummy_uint;
2038 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
2040 eassert (FRAME_X_P (f) || FRAME_MSDOS_P (f));
2042 *error_name = 0;
2043 if (menu_items_n_panes == 0)
2044 return Qnil;
2046 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2048 *error_name = "Empty menu";
2049 return Qnil;
2052 USE_SAFE_ALLOCA;
2053 block_input ();
2055 /* Figure out which root window F is on. */
2056 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2057 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2058 &dummy_uint, &dummy_uint);
2060 /* Make the menu on that window. */
2061 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2062 if (menu == NULL)
2064 *error_name = "Can't create menu";
2065 goto return_entry;
2068 /* Don't GC while we prepare and show the menu,
2069 because we give the oldxmenu library pointers to the
2070 contents of strings. */
2071 inhibit_garbage_collection ();
2073 #ifdef HAVE_X_WINDOWS
2074 /* Adjust coordinates to relative to the outer (window manager) window. */
2075 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
2076 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
2077 #endif /* HAVE_X_WINDOWS */
2079 /* Adjust coordinates to be root-window-relative. */
2080 x += f->left_pos;
2081 y += f->top_pos;
2083 /* Create all the necessary panes and their items. */
2084 maxwidth = maxlines = lines = i = 0;
2085 lpane = XM_FAILURE;
2086 while (i < menu_items_used)
2088 if (EQ (AREF (menu_items, i), Qt))
2090 /* Create a new pane. */
2091 Lisp_Object pane_name, prefix;
2092 const char *pane_string;
2094 maxlines = max (maxlines, lines);
2095 lines = 0;
2096 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2097 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2098 pane_string = (NILP (pane_name)
2099 ? "" : SSDATA (pane_name));
2100 if ((menuflags & MENU_KEYMAPS) && !NILP (prefix))
2101 pane_string++;
2103 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2104 if (lpane == XM_FAILURE)
2106 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2107 *error_name = "Can't create pane";
2108 goto return_entry;
2110 i += MENU_ITEMS_PANE_LENGTH;
2112 /* Find the width of the widest item in this pane. */
2113 j = i;
2114 while (j < menu_items_used)
2116 Lisp_Object item;
2117 item = AREF (menu_items, j);
2118 if (EQ (item, Qt))
2119 break;
2120 if (NILP (item))
2122 j++;
2123 continue;
2125 width = SBYTES (item);
2126 if (width > maxwidth)
2127 maxwidth = width;
2129 j += MENU_ITEMS_ITEM_LENGTH;
2132 /* Ignore a nil in the item list.
2133 It's meaningful only for dialog boxes. */
2134 else if (EQ (AREF (menu_items, i), Qquote))
2135 i += 1;
2136 else
2138 /* Create a new item within current pane. */
2139 Lisp_Object item_name, enable, descrip, help;
2140 char *item_data;
2141 char const *help_string;
2143 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2144 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2145 descrip
2146 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2147 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2148 help_string = STRINGP (help) ? SSDATA (help) : NULL;
2150 if (!NILP (descrip))
2152 item_data = SAFE_ALLOCA (maxwidth + SBYTES (descrip) + 1);
2153 memcpy (item_data, SSDATA (item_name), SBYTES (item_name));
2154 for (j = SCHARS (item_name); j < maxwidth; j++)
2155 item_data[j] = ' ';
2156 memcpy (item_data + j, SSDATA (descrip), SBYTES (descrip));
2157 item_data[j + SBYTES (descrip)] = 0;
2159 else
2160 item_data = SSDATA (item_name);
2162 if (lpane == XM_FAILURE
2163 || (XMenuAddSelection (FRAME_X_DISPLAY (f),
2164 menu, lpane, 0, item_data,
2165 !NILP (enable), help_string)
2166 == XM_FAILURE))
2168 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2169 *error_name = "Can't add selection to menu";
2170 goto return_entry;
2172 i += MENU_ITEMS_ITEM_LENGTH;
2173 lines++;
2177 maxlines = max (maxlines, lines);
2179 /* All set and ready to fly. */
2180 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2181 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2182 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2183 x = min (x, dispwidth);
2184 y = min (y, dispheight);
2185 x = max (x, 1);
2186 y = max (y, 1);
2187 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2188 &ulx, &uly, &width, &height);
2189 if (ulx+width > dispwidth)
2191 x -= (ulx + width) - dispwidth;
2192 ulx = dispwidth - width;
2194 if (uly+height > dispheight)
2196 y -= (uly + height) - dispheight;
2197 uly = dispheight - height;
2199 #ifndef HAVE_X_WINDOWS
2200 if (FRAME_HAS_MINIBUF_P (f) && uly+height > dispheight - 1)
2202 /* Move the menu away of the echo area, to avoid overwriting the
2203 menu with help echo messages or vice versa. */
2204 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
2206 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2207 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2209 else
2211 y--;
2212 uly--;
2215 #endif
2216 if (ulx < 0) x -= ulx;
2217 if (uly < 0) y -= uly;
2219 if (!(menuflags & MENU_FOR_CLICK))
2221 /* If position was not given by a mouse click, adjust so upper left
2222 corner of the menu as a whole ends up at given coordinates. This
2223 is what x-popup-menu says in its documentation. */
2224 x += width/2;
2225 y += 1.5*height/(maxlines+2);
2228 XMenuSetAEQ (menu, TRUE);
2229 XMenuSetFreeze (menu, TRUE);
2230 pane = selidx = 0;
2232 #ifndef MSDOS
2233 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
2234 #endif
2236 record_unwind_protect (pop_down_menu, make_save_ptr_ptr (f, menu));
2238 /* Help display under X won't work because XMenuActivate contains
2239 a loop that doesn't give Emacs a chance to process it. */
2240 menu_help_frame = f;
2241 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2242 x, y, ButtonReleaseMask, &datap,
2243 menu_help_callback);
2244 pane_prefix = Qnil;
2246 switch (status)
2248 case XM_SUCCESS:
2249 #ifdef XDEBUG
2250 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2251 #endif
2253 /* Find the item number SELIDX in pane number PANE. */
2254 i = 0;
2255 while (i < menu_items_used)
2257 if (EQ (AREF (menu_items, i), Qt))
2259 if (pane == 0)
2260 pane_prefix
2261 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2262 pane--;
2263 i += MENU_ITEMS_PANE_LENGTH;
2265 else
2267 if (pane == -1)
2269 if (selidx == 0)
2271 entry
2272 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
2273 if (menuflags & MENU_KEYMAPS)
2275 entry = list1 (entry);
2276 if (!NILP (pane_prefix))
2277 entry = Fcons (pane_prefix, entry);
2279 break;
2281 selidx--;
2283 i += MENU_ITEMS_ITEM_LENGTH;
2286 break;
2288 case XM_FAILURE:
2289 *error_name = "Can't activate menu";
2290 case XM_IA_SELECT:
2291 break;
2292 case XM_NO_SELECT:
2293 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2294 the menu was invoked with a mouse event as POSITION). */
2295 if (!(menuflags & MENU_FOR_CLICK))
2297 unblock_input ();
2298 Fsignal (Qquit, Qnil);
2300 break;
2303 return_entry:
2304 unblock_input ();
2305 SAFE_FREE ();
2306 return unbind_to (specpdl_count, entry);
2309 #endif /* not USE_X_TOOLKIT */
2311 #ifndef MSDOS
2312 /* Detect if a dialog or menu has been posted. MSDOS has its own
2313 implementation on msdos.c. */
2315 int ATTRIBUTE_CONST
2316 popup_activated (void)
2318 return popup_activated_flag;
2320 #endif /* not MSDOS */
2322 /* The following is used by delayed window autoselection. */
2324 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
2325 doc: /* Return t if a menu or popup dialog is active. */)
2326 (void)
2328 return (popup_activated ()) ? Qt : Qnil;
2331 void
2332 syms_of_xmenu (void)
2334 #ifdef USE_X_TOOLKIT
2335 enum { WIDGET_ID_TICK_START = 1 << 16 };
2336 widget_id_tick = WIDGET_ID_TICK_START;
2337 next_menubar_widget_id = 1;
2338 #endif
2340 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
2341 defsubr (&Smenu_or_popup_active_p);
2343 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
2344 defsubr (&Sx_menu_bar_open_internal);
2345 Ffset (intern_c_string ("accelerate-menu"),
2346 intern_c_string (Sx_menu_bar_open_internal.symbol_name));
2347 #endif