SAFE_ALLOCA updated
[bpt/emacs.git] / src / xmenu.c
CommitLineData
dcfdbac7 1/* X Communication module for terminals which understand the X protocol.
95df8112 2
ba318903 3Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2014 Free Software
ab422c4d 4Foundation, Inc.
dcfdbac7
JB
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
dcfdbac7 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
dcfdbac7
JB
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
dcfdbac7 20
c9e60620 21/* X pop-up deck-of-cards menu facility for GNU Emacs.
dcfdbac7
JB
22 *
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
25 *
26 */
27
18686d47
RS
28/* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
30
78589e07
RS
31/* Rewritten for clarity and GC protection by rms in Feb 94. */
32
68c45bf0
PE
33#include <config.h>
34
565620a5 35#include <stdio.h>
7ee72033 36
dcfdbac7 37#include "lisp.h"
02067692 38#include "keyboard.h"
5ee707b8 39#include "keymap.h"
7708e9bd 40#include "frame.h"
428a555e 41#include "termhooks.h"
dcfdbac7 42#include "window.h"
9ac0d9e0 43#include "blockinput.h"
e5560ff7 44#include "character.h"
88766961 45#include "buffer.h"
cc45fb40
SM
46#include "charset.h"
47#include "coding.h"
c3438661 48#include "sysselect.h"
dcfdbac7 49
eeee3112
RS
50#ifdef MSDOS
51#include "msdos.h"
52#endif
53
87485d6f 54#ifdef HAVE_X_WINDOWS
dcfdbac7
JB
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"
87485d6f 58#endif
dcfdbac7
JB
59
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
65
66#include "dispextern.h"
67
87485d6f 68#ifdef HAVE_X_WINDOWS
646f98ec
DL
69/* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
70 code accepts the Emacs internal encoding. */
703dc2a8 71#undef HAVE_MULTILINGUAL_MENU
18686d47 72#ifdef USE_X_TOOLKIT
991786dd 73#include "widget.h"
18686d47
RS
74#include <X11/Xlib.h>
75#include <X11/IntrinsicP.h>
76#include <X11/CoreP.h>
77#include <X11/StringDefs.h>
60f312e2 78#include <X11/Shell.h>
5ee80bd3 79#ifdef USE_LUCID
99852628
JD
80#include "xsettings.h"
81#include "../lwlib/xlwmenu.h"
5262c5c7
CY
82#ifdef HAVE_XAW3D
83#include <X11/Xaw3d/Paned.h>
84#else /* !HAVE_XAW3D */
1d1c1567 85#include <X11/Xaw/Paned.h>
5262c5c7 86#endif /* HAVE_XAW3D */
5ee80bd3 87#endif /* USE_LUCID */
c865c575 88#ifdef USE_MOTIF
dee186b6 89#include "../lwlib/lwlib.h"
c865c575 90#endif
a352a815 91#else /* not USE_X_TOOLKIT */
488dd4c4 92#ifndef USE_GTK
a352a815 93#include "../oldXMenu/XMenu.h"
488dd4c4 94#endif
a352a815
RS
95#endif /* not USE_X_TOOLKIT */
96#endif /* HAVE_X_WINDOWS */
18686d47 97
e7c9048f
AS
98#ifdef USE_GTK
99#include "gtkutil.h"
93d5ca1f
JD
100#ifdef HAVE_GTK3
101#include "xgselect.h"
102#endif
e7c9048f
AS
103#endif
104
105#include "menu.h"
106
dcfdbac7
JB
107#ifndef TRUE
108#define TRUE 1
78589e07 109#endif /* no TRUE */
dcfdbac7 110
955cbe7b 111static Lisp_Object Qdebug_on_next_call;
0314aacb 112
4dedbfe0 113/* Flag which when set indicates a dialog or menu has been posted by
c98fcf4b 114 Xt on behalf of one of the widget sets. */
488dd4c4 115static int popup_activated_flag;
4dedbfe0 116
bd3a4da2 117\f
88766961 118#ifdef USE_X_TOOLKIT
bd3a4da2 119
281585b0
PE
120static int next_menubar_widget_id;
121
7556890b 122/* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
bd3a4da2 123
88766961 124static struct frame *
ebd15611 125menubar_id_to_frame (LWLIB_ID id)
88766961
RS
126{
127 Lisp_Object tail, frame;
a10c8269 128 struct frame *f;
bd3a4da2 129
5b04e9f9 130 FOR_EACH_FRAME (tail, frame)
bd3a4da2 131 {
88766961 132 f = XFRAME (frame);
2d764c78 133 if (!FRAME_WINDOW_P (f))
88766961 134 continue;
7556890b 135 if (f->output_data.x->id == id)
88766961 136 return f;
bd3a4da2 137 }
88766961 138 return 0;
bd3a4da2 139}
88766961
RS
140
141#endif
a894238b 142
c3438661
JD
143#ifndef MSDOS
144
4475bec4
PE
145#if defined USE_GTK || defined USE_MOTIF
146
b3935289
JD
147/* Set menu_items_inuse so no other popup menu or dialog is created. */
148
149void
971de7fb 150x_menu_set_in_use (int in_use)
b3935289
JD
151{
152 menu_items_inuse = in_use ? Qt : Qnil;
5ae53dcf 153 popup_activated_flag = in_use;
73dba342 154#ifdef USE_X_TOOLKIT
98a20c65
CY
155 if (popup_activated_flag)
156 x_activate_timeout_atimer ();
73dba342 157#endif
b3935289
JD
158}
159
4475bec4
PE
160#endif
161
c3438661
JD
162/* Wait for an X event to arrive or for a timer to expire. */
163
b3935289 164void
c3438661
JD
165x_menu_wait_for_event (void *data)
166{
c3438661
JD
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. */
171
172 while (
173#ifdef USE_X_TOOLKIT
f1d1cd24 174 ! XtAppPending (Xt_app_con)
c3438661
JD
175#elif defined USE_GTK
176 ! gtk_events_pending ()
177#else
7d652d97 178 ! XPending (data)
c3438661
JD
179#endif
180 )
181 {
43aac990 182 struct timespec next_time = timer_check (), *ntp;
d486344e 183 fd_set read_fds;
c3438661
JD
184 struct x_display_info *dpyinfo;
185 int n = 0;
186
187 FD_ZERO (&read_fds);
188 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
189 {
190 int fd = ConnectionNumber (dpyinfo->display);
191 FD_SET (fd, &read_fds);
192 if (fd > n) n = fd;
11814156 193 XFlush (dpyinfo->display);
c3438661
JD
194 }
195
43aac990 196 if (! timespec_valid_p (next_time))
e90292a9
JD
197 ntp = 0;
198 else
199 ntp = &next_time;
c3438661 200
8f5f35cc 201#if defined USE_GTK && defined HAVE_GTK3
63def6b6
CY
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);
93d5ca1f 206#else
d35af63c 207 pselect (n + 1, &read_fds, NULL, NULL, ntp, NULL);
93d5ca1f 208#endif
c3438661
JD
209 }
210}
211#endif /* ! MSDOS */
212
78589e07 213\f
488dd4c4 214#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18686d47 215
f1d1cd24
JD
216#ifdef USE_X_TOOLKIT
217
4dedbfe0 218/* Loop in Xt until the menu pulldown or dialog popup has been
f56e7ad2 219 popped down (deactivated). This is used for x-popup-menu
f02cac82
RS
220 and x-popup-dialog; it is not used for the menu bar.
221
2e2b8e22 222 NOTE: All calls to popup_get_selection should be protected
c98fcf4b 223 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
aa669def 224
f02cac82 225static void
ebd15611 226popup_get_selection (XEvent *initial_event, struct x_display_info *dpyinfo, LWLIB_ID id, int do_timers)
78589e07 227{
4dedbfe0 228 XEvent event;
f02cac82 229
8b806a85
JD
230 while (popup_activated_flag)
231 {
c0b0f54b 232 if (initial_event)
8b806a85
JD
233 {
234 event = *initial_event;
235 initial_event = 0;
236 }
237 else
c3438661
JD
238 {
239 if (do_timers) x_menu_wait_for_event (0);
240 XtAppNextEvent (Xt_app_con, &event);
241 }
78589e07 242
10624005 243 /* Make sure we don't consider buttons grabbed after menu goes.
8b806a85
JD
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)
10624005
KH
248 {
249 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
177c0ea7 250#ifdef USE_MOTIF /* Pretending that the event came from a
8b806a85
JD
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;
c8b5aa3d 259#endif
10624005 260 }
33385c6f 261 /* Pop down on C-g and Escape. */
2e2b8e22 262 else if (event.type == KeyPress
10624005 263 && dpyinfo->display == event.xbutton.display)
8b806a85
JD
264 {
265 KeySym keysym = XLookupKeysym (&event.xkey, 0);
9f6fcdc5 266
33385c6f
JD
267 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
268 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
9f6fcdc5 269 popup_activated_flag = 0;
8b806a85 270 }
177c0ea7 271
8b806a85 272 x_dispatch_event (&event, event.xany.display);
aa669def 273 }
78589e07
RS
274}
275
12b6af5c 276DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
22badffe
JD
277 doc: /* Start key navigation of the menu bar in FRAME.
278This initially opens the first menu bar item and you can then navigate with the
279arrow keys, select a menu entry with the return key or cancel with the
280escape key. If FRAME has no menu bar this function does nothing.
281
282If FRAME is nil or not given, use the selected frame. */)
5842a27b 283 (Lisp_Object frame)
22badffe
JD
284{
285 XEvent ev;
a10c8269 286 struct frame *f = decode_window_system_frame (frame);
22badffe 287 Widget menubar;
4d7e6e51 288 block_input ();
22badffe
JD
289
290 if (FRAME_EXTERNAL_MENU_BAR (f))
291 set_frame_menubar (f, 0, 1);
292
293 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
294 if (menubar)
295 {
296 Window child;
d7e6881a 297 bool error_p = 0;
22badffe
JD
298
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);
aad3612f 303 ev.xbutton.root = FRAME_DISPLAY_INFO (f)->root_window;
22badffe
JD
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;
308
309#ifdef USE_MOTIF
310 {
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]);
318 }
319#endif
320
321 XTranslateCoordinates (FRAME_X_DISPLAY (f),
322 /* From-window, to-window. */
323 ev.xbutton.window, ev.xbutton.root,
324
325 /* From-position, to-position. */
326 ev.xbutton.x, ev.xbutton.y,
327 &ev.xbutton.x_root, &ev.xbutton.y_root,
328
329 /* Child of win. */
330 &child);
331 error_p = x_had_errors_p (FRAME_X_DISPLAY (f));
332 x_uncatch_errors ();
333
334 if (! error_p)
335 {
336 ev.type = ButtonPress;
337 ev.xbutton.state = 0;
338
339 XtDispatchEvent (&ev);
340 ev.xbutton.type = ButtonRelease;
341 ev.xbutton.state = Button1Mask;
342 XtDispatchEvent (&ev);
343 }
344 }
345
4d7e6e51 346 unblock_input ();
57d671b4
AS
347
348 return Qnil;
22badffe 349}
488dd4c4
JD
350#endif /* USE_X_TOOLKIT */
351
22badffe 352
488dd4c4 353#ifdef USE_GTK
12b6af5c 354DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
22badffe
JD
355 doc: /* Start key navigation of the menu bar in FRAME.
356This initially opens the first menu bar item and you can then navigate with the
357arrow keys, select a menu entry with the return key or cancel with the
358escape key. If FRAME has no menu bar this function does nothing.
359
360If FRAME is nil or not given, use the selected frame. */)
5842a27b 361 (Lisp_Object frame)
22badffe
JD
362{
363 GtkWidget *menubar;
a10c8269 364 struct frame *f;
718aeeb8 365
4d7e6e51 366 block_input ();
7452b7bd 367 f = decode_window_system_frame (frame);
22badffe
JD
368
369 if (FRAME_EXTERNAL_MENU_BAR (f))
370 set_frame_menubar (f, 0, 1);
371
372 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
373 if (menubar)
374 {
375 /* Activate the first menu. */
376 GList *children = gtk_container_get_children (GTK_CONTAINER (menubar));
377
32d46135
JD
378 if (children)
379 {
380 g_signal_emit_by_name (children->data, "activate_item");
381 popup_activated_flag = 1;
382 g_list_free (children);
383 }
22badffe 384 }
4d7e6e51 385 unblock_input ();
22badffe
JD
386
387 return Qnil;
388}
389
488dd4c4
JD
390/* Loop util popup_activated_flag is set to zero in a callback.
391 Used for popup menus and dialogs. */
f1d1cd24 392
488dd4c4 393static void
971de7fb 394popup_widget_loop (int do_timers, GtkWidget *widget)
488dd4c4
JD
395{
396 ++popup_activated_flag;
397
398 /* Process events in the Gtk event loop until done. */
399 while (popup_activated_flag)
400 {
c3438661 401 if (do_timers) x_menu_wait_for_event (0);
488dd4c4
JD
402 gtk_main_iteration ();
403 }
404}
405#endif
406
88766961
RS
407/* Activate the menu bar of frame F.
408 This is called from keyboard.c when it gets the
3b8f9651 409 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
88766961
RS
410
411 To activate the menu bar, we use the X button-press event
ac78b144 412 that was saved in saved_menu_event.
88766961
RS
413 That makes the toolkit do its thing.
414
415 But first we recompute the menu bar contents (the whole tree).
416
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. */
177c0ea7 420
dfcf069d 421void
a10c8269 422x_activate_menubar (struct frame *f)
88766961 423{
65b02bb9 424 eassert (FRAME_X_P (f));
62af879c 425
ac78b144 426 if (!f->output_data.x->saved_menu_event->type)
88766961
RS
427 return;
428
177c0ea7 429#ifdef USE_GTK
6b61353c
KH
430 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
431 f->output_data.x->saved_menu_event->xany.window))
488dd4c4
JD
432 return;
433#endif
177c0ea7 434
a9be6839 435 set_frame_menubar (f, 0, 1);
4d7e6e51 436 block_input ();
cf28cebc 437 popup_activated_flag = 1;
488dd4c4
JD
438#ifdef USE_GTK
439 XPutBackEvent (f->output_data.x->display_info->display,
440 f->output_data.x->saved_menu_event);
488dd4c4 441#else
86fad4ec 442 XtDispatchEvent (f->output_data.x->saved_menu_event);
488dd4c4 443#endif
4d7e6e51 444 unblock_input ();
177c0ea7 445
88766961 446 /* Ignore this if we get it a second time. */
ac78b144 447 f->output_data.x->saved_menu_event->type = 0;
88766961
RS
448}
449
4dedbfe0
PR
450/* This callback is invoked when the user selects a menubar cascade
451 pushbutton, but before the pulldown menu is posted. */
78589e07 452
488dd4c4 453#ifndef USE_GTK
78589e07 454static void
ebd15611 455popup_activate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
78589e07 456{
4dedbfe0 457 popup_activated_flag = 1;
98a20c65 458 x_activate_timeout_atimer ();
7555d825 459}
488dd4c4 460#endif
7555d825
GM
461
462/* This callback is invoked when a dialog or menu is finished being
463 used and has been unposted. */
464
488dd4c4 465static void
5db81e33
SM
466popup_deactivate_callback (
467#ifdef USE_GTK
468 GtkWidget *widget, gpointer client_data
488dd4c4 469#else
5db81e33
SM
470 Widget widget, LWLIB_ID id, XtPointer client_data
471#endif
472 )
7555d825 473{
7555d825 474 popup_activated_flag = 0;
78589e07
RS
475}
476
850df50b 477
488dd4c4
JD
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. */
481static void
a10c8269 482show_help_event (struct frame *f, xt_or_gtk_widget widget, Lisp_Object help)
850df50b 483{
488dd4c4 484 Lisp_Object frame;
850df50b 485
850df50b 486 if (f)
9cd50434
GM
487 {
488 XSETFRAME (frame, f);
489 kbd_buffer_store_help_event (frame, help);
490 }
850df50b
GM
491 else
492 {
6b61353c 493#if 0 /* This code doesn't do anything useful. ++kfs */
177c0ea7 494 /* WIDGET is the popup menu. It's parent is the frame's
850df50b 495 widget. See which frame that is. */
488dd4c4 496 xt_or_gtk_widget frame_widget = XtParent (widget);
850df50b
GM
497 Lisp_Object tail;
498
8e50cc2d 499 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
850df50b
GM
500 {
501 frame = XCAR (tail);
8e50cc2d 502 if (FRAMEP (frame)
850df50b
GM
503 && (f = XFRAME (frame),
504 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
505 break;
506 }
6b61353c 507#endif
f868cd8a 508 show_help_echo (help, Qnil, Qnil, Qnil);
9cd50434 509 }
850df50b
GM
510}
511
488dd4c4
JD
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. */
4dedbfe0 517
488dd4c4 518#ifdef USE_GTK
032f1620 519static void
971de7fb 520menu_highlight_callback (GtkWidget *widget, gpointer call_data)
488dd4c4
JD
521{
522 xg_menu_item_cb_data *cb_data;
523 Lisp_Object help;
177c0ea7 524
7d652d97 525 cb_data = g_object_get_data (G_OBJECT (widget), XG_ITEM_DATA);
488dd4c4
JD
526 if (! cb_data) return;
527
528 help = call_data ? cb_data->help : Qnil;
529
530 /* If popup_activated_flag is greater than 1 we are in a popup menu.
a560d974
JD
531 Don't pass the frame to show_help_event for those.
532 Passing frame creates an Emacs event. As we are looping in
8d5ed899 533 popup_widget_loop, it won't be handled. Passing NULL shows the tip
a560d974
JD
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);
488dd4c4
JD
538}
539#else
032f1620 540static void
ebd15611 541menu_highlight_callback (Widget widget, LWLIB_ID id, void *call_data)
488dd4c4 542{
7d652d97
PE
543 widget_value *wv = call_data;
544 Lisp_Object help = wv ? wv->help : Qnil;
177c0ea7 545
488dd4c4 546 /* Determine the frame for the help event. */
7d652d97 547 struct frame *f = menubar_id_to_frame (id);
488dd4c4
JD
548
549 show_help_event (f, widget, help);
550}
551#endif
552
488dd4c4
JD
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.
557*/
558static int xg_crazy_callback_abort;
559
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. */
564static void
971de7fb 565menubar_selection_callback (GtkWidget *widget, gpointer client_data)
488dd4c4 566{
7d652d97 567 xg_menu_item_cb_data *cb_data = client_data;
488dd4c4
JD
568
569 if (xg_crazy_callback_abort)
570 return;
571
572 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
573 return;
574
b79c8219
JD
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;
583
81f09877
JD
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. */
589
4d7e6e51 590 block_input ();
81f09877
JD
591 while (gtk_events_pending ())
592 gtk_main_iteration ();
4d7e6e51 593 unblock_input ();
81f09877 594
488dd4c4
JD
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);
599}
600
601#else /* not USE_GTK */
602
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. */
607static void
ebd15611 608menubar_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
488dd4c4 609{
a10c8269 610 struct frame *f;
488dd4c4
JD
611
612 f = menubar_id_to_frame (id);
613 if (!f)
614 return;
615 find_and_call_menu_selection (f, f->menu_bar_items_used,
e69b0960 616 f->menu_bar_vector, client_data);
488dd4c4
JD
617}
618#endif /* not USE_GTK */
f61a541b
GM
619\f
620/* Recompute all the widgets of frame F, when the menu bar has been
18e27ea8 621 changed. */
f61a541b 622
18e27ea8 623static void
a10c8269 624update_frame_menubar (struct frame *f)
18686d47 625{
488dd4c4 626#ifdef USE_GTK
18e27ea8 627 xg_update_frame_menubar (f);
488dd4c4 628#else
62af879c 629 struct x_output *x;
cffa74ea 630 int columns, rows;
177c0ea7 631
65b02bb9 632 eassert (FRAME_X_P (f));
62af879c
KL
633
634 x = f->output_data.x;
635
f61a541b 636 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
18e27ea8 637 return;
18686d47 638
4d7e6e51 639 block_input ();
f61a541b
GM
640 /* Save the size of the frame because the pane widget doesn't accept
641 to resize itself. So force it. */
9882535b
KS
642 columns = FRAME_COLS (f);
643 rows = FRAME_LINES (f);
cffa74ea 644
f61a541b
GM
645 /* Do the voodoo which means "I'm changing lots of things, don't try
646 to refigure sizes until I'm done." */
4dedbfe0 647 lw_refigure_widget (x->column_widget, False);
cffa74ea 648
f61a541b
GM
649 /* The order in which children are managed is the top to bottom
650 order in which they are displayed in the paned window. First,
651 remove the text-area widget. */
18686d47
RS
652 XtUnmanageChild (x->edit_widget);
653
f61a541b
GM
654 /* Remove the menubar that is there now, and put up the menubar that
655 should be there. */
656 XtManageChild (x->menubar_widget);
657 XtMapWidget (x->menubar_widget);
658 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
18686d47 659
c98fcf4b 660 /* Re-manage the text-area widget, and then thrash the sizes. */
18686d47 661 XtManageChild (x->edit_widget);
4dedbfe0 662 lw_refigure_widget (x->column_widget, True);
cffa74ea
FP
663
664 /* Force the pane widget to resize itself with the right values. */
665 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
4d7e6e51 666 unblock_input ();
488dd4c4 667#endif
18686d47
RS
668}
669
99852628 670#ifdef USE_LUCID
1ecb2d3f 671static void
ebd15611 672apply_systemfont_to_dialog (Widget w)
1ecb2d3f
JD
673{
674 const char *fn = xsettings_get_system_normal_font ();
51b59d79 675 if (fn)
1ecb2d3f
JD
676 {
677 XrmDatabase db = XtDatabase (XtDisplay (w));
678 if (db)
3928f2b6 679 XrmPutStringResource (&db, "*dialog.font", fn);
1ecb2d3f
JD
680 }
681}
682
99852628 683static void
3928f2b6 684apply_systemfont_to_menu (struct frame *f, Widget w)
99852628
JD
685{
686 const char *fn = xsettings_get_system_normal_font ();
99852628 687
3928f2b6 688 if (fn)
99852628 689 {
3928f2b6
JD
690 XrmDatabase db = XtDatabase (XtDisplay (w));
691 if (db)
692 {
693 XrmPutStringResource (&db, "*menubar*font", fn);
694 XrmPutStringResource (&db, "*popup*font", fn);
695 }
99852628 696 }
99852628 697}
3928f2b6 698
99852628
JD
699#endif
700
4bcdbab1
KH
701/* Set the contents of the menubar widgets of frame F.
702 The argument FIRST_TIME is currently ignored;
703 it is set the first time this is called, from initialize_frame_menubar. */
704
18686d47 705void
a10c8269 706set_frame_menubar (struct frame *f, bool first_time, bool deep_p)
18686d47 707{
62af879c 708 xt_or_gtk_widget menubar_widget;
488dd4c4
JD
709#ifdef USE_X_TOOLKIT
710 LWLIB_ID id;
711#endif
faa935b6 712 Lisp_Object items;
4d19cb8e 713 widget_value *wv, *first_wv, *prev_wv = 0;
3e26f69c 714 int i;
ced89c24 715 int *submenu_start, *submenu_end;
7cded46f
PE
716 bool *submenu_top_level_items;
717 int *submenu_n_panes;
cc45fb40 718
65b02bb9 719 eassert (FRAME_X_P (f));
62af879c
KL
720
721 menubar_widget = f->output_data.x->menubar_widget;
18686d47 722
bfc524bc
RS
723 XSETFRAME (Vmenu_updating_frame, f);
724
488dd4c4 725#ifdef USE_X_TOOLKIT
7556890b
RS
726 if (f->output_data.x->id == 0)
727 f->output_data.x->id = next_menubar_widget_id++;
728 id = f->output_data.x->id;
488dd4c4 729#endif
177c0ea7 730
88766961
RS
731 if (! menubar_widget)
732 deep_p = 1;
a9be6839
RS
733 /* Make the first call for any given frame always go deep. */
734 else if (!f->output_data.x->saved_menu_event && !deep_p)
745c34fb
RS
735 {
736 deep_p = 1;
23f86fce 737 f->output_data.x->saved_menu_event = xmalloc (sizeof (XEvent));
a9be6839 738 f->output_data.x->saved_menu_event->type = 0;
745c34fb 739 }
18686d47 740
6b61353c
KH
741#ifdef USE_GTK
742 /* If we have detached menus, we must update deep so detached menus
743 also gets updated. */
7a1b473c 744 deep_p = deep_p || xg_have_tear_offs (f);
6b61353c
KH
745#endif
746
88766961 747 if (deep_p)
18686d47 748 {
88766961
RS
749 /* Make a widget-value tree representing the entire menu trees. */
750
751 struct buffer *prev = current_buffer;
752 Lisp_Object buffer;
d311d28c 753 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
88766961
RS
754 int previous_menu_items_used = f->menu_bar_items_used;
755 Lisp_Object *previous_items
38182d90 756 = alloca (previous_menu_items_used * sizeof *previous_items);
3e26f69c 757 int subitems;
88766961 758
0b1cf399
RS
759 /* If we are making a new widget, its contents are empty,
760 do always reinitialize them. */
761 if (! menubar_widget)
762 previous_menu_items_used = 0;
763
e74aeda8 764 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->contents;
88766961
RS
765 specbind (Qinhibit_quit, Qt);
766 /* Don't let the debugger step into this code
767 because it is not reentrant. */
768 specbind (Qdebug_on_next_call, Qnil);
769
89f2614d 770 record_unwind_save_match_data ();
88766961
RS
771 if (NILP (Voverriding_local_map_menu_flag))
772 {
773 specbind (Qoverriding_terminal_local_map, Qnil);
774 specbind (Qoverriding_local_map, Qnil);
775 }
18686d47 776
88766961 777 set_buffer_internal_1 (XBUFFER (buffer));
18686d47 778
88766961 779 /* Run the Lucid hook. */
950eaee7 780 safe_run_hooks (Qactivate_menubar_hook);
177c0ea7 781
88766961
RS
782 /* If it has changed current-menubar from previous value,
783 really recompute the menubar from the value. */
784 if (! NILP (Vlucid_menu_bar_dirty_flag))
785 call0 (Qrecompute_lucid_menubar);
a57634d4 786 safe_run_hooks (Qmenu_bar_update_hook);
f00af5b1 787 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
18686d47 788
88766961 789 items = FRAME_MENU_BAR_ITEMS (f);
8d8a3494 790
88766961 791 /* Save the frame's previous menu bar contents data. */
86c04183 792 if (previous_menu_items_used)
91f2d272 793 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents,
663e2b3f 794 previous_menu_items_used * word_size);
8d8a3494 795
ced89c24
RS
796 /* Fill in menu_items with the current menu bar contents.
797 This can evaluate Lisp code. */
1fc4d463
RS
798 save_menu_items ();
799
e69b0960 800 menu_items = f->menu_bar_vector;
86c04183 801 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
77b37c05 802 subitems = ASIZE (items) / 4;
38182d90
PE
803 submenu_start = alloca ((subitems + 1) * sizeof *submenu_start);
804 submenu_end = alloca (subitems * sizeof *submenu_end);
805 submenu_n_panes = alloca (subitems * sizeof *submenu_n_panes);
806 submenu_top_level_items = alloca (subitems
807 * sizeof *submenu_top_level_items);
88766961 808 init_menu_items ();
e3019616 809 for (i = 0; i < subitems; i++)
88766961
RS
810 {
811 Lisp_Object key, string, maps;
812
28be1ada
DA
813 key = AREF (items, 4 * i);
814 string = AREF (items, 4 * i + 1);
815 maps = AREF (items, 4 * i + 2);
88766961
RS
816 if (NILP (string))
817 break;
818
ced89c24
RS
819 submenu_start[i] = menu_items_used;
820
821 menu_items_n_panes = 0;
822 submenu_top_level_items[i]
823 = parse_single_submenu (key, string, maps);
37dc84ff 824 submenu_n_panes[i] = menu_items_n_panes;
ced89c24
RS
825
826 submenu_end[i] = menu_items_used;
827 }
828
3e26f69c 829 submenu_start[i] = -1;
ced89c24
RS
830 finish_menu_items ();
831
832 /* Convert menu_items into widget_value trees
833 to display the menu. This cannot evaluate Lisp code. */
834
5668fb88 835 wv = make_widget_value ("menubar", NULL, true, Qnil);
ced89c24 836 wv->button_type = BUTTON_TYPE_NONE;
ced89c24
RS
837 first_wv = wv;
838
908589fd 839 for (i = 0; submenu_start[i] >= 0; i++)
ced89c24 840 {
37dc84ff 841 menu_items_n_panes = submenu_n_panes[i];
ced89c24
RS
842 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
843 submenu_top_level_items[i]);
177c0ea7 844 if (prev_wv)
88766961
RS
845 prev_wv->next = wv;
846 else
847 first_wv->contents = wv;
848 /* Don't set wv->name here; GC during the loop might relocate it. */
849 wv->enabled = 1;
3427a3db 850 wv->button_type = BUTTON_TYPE_NONE;
88766961
RS
851 prev_wv = wv;
852 }
853
88766961 854 set_buffer_internal_1 (prev);
8d8a3494 855
88766961
RS
856 /* If there has been no change in the Lisp-level contents
857 of the menu bar, skip redisplaying it. Just exit. */
858
510d5bf6 859 /* Compare the new menu items with the ones computed last time. */
88766961
RS
860 for (i = 0; i < previous_menu_items_used; i++)
861 if (menu_items_used == i
28be1ada 862 || (!EQ (previous_items[i], AREF (menu_items, i))))
88766961 863 break;
62555c22 864 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
88766961 865 {
510d5bf6
RS
866 /* The menu items have not changed. Don't bother updating
867 the menus in any form, since it would be a no-op. */
88766961 868 free_menubar_widget_value_tree (first_wv);
86fad4ec 869 discard_menu_items ();
1fc4d463 870 unbind_to (specpdl_count, Qnil);
88766961
RS
871 return;
872 }
873
510d5bf6 874 /* The menu items are different, so store them in the frame. */
f00af5b1 875 fset_menu_bar_vector (f, menu_items);
1fc4d463
RS
876 f->menu_bar_items_used = menu_items_used;
877
bf732638 878 /* This undoes save_menu_items. */
1fc4d463
RS
879 unbind_to (specpdl_count, Qnil);
880
88766961
RS
881 /* Now GC cannot happen during the lifetime of the widget_value,
882 so it's safe to store data from a Lisp_String. */
883 wv = first_wv->contents;
77b37c05 884 for (i = 0; i < ASIZE (items); i += 4)
88766961
RS
885 {
886 Lisp_Object string;
28be1ada 887 string = AREF (items, i + 1);
88766961 888 if (NILP (string))
2bf436c3 889 break;
51b59d79 890 wv->name = SSDATA (string);
2bf436c3
JD
891 update_submenu_strings (wv->contents);
892 wv = wv->next;
88766961
RS
893 }
894
4d19cb8e 895 }
88766961
RS
896 else
897 {
898 /* Make a widget-value tree containing
899 just the top level menu bar strings. */
4d19cb8e 900
5668fb88 901 wv = make_widget_value ("menubar", NULL, true, Qnil);
ced89c24 902 wv->button_type = BUTTON_TYPE_NONE;
ced89c24
RS
903 first_wv = wv;
904
88766961 905 items = FRAME_MENU_BAR_ITEMS (f);
77b37c05 906 for (i = 0; i < ASIZE (items); i += 4)
88766961
RS
907 {
908 Lisp_Object string;
909
28be1ada 910 string = AREF (items, i + 1);
88766961
RS
911 if (NILP (string))
912 break;
913
5668fb88 914 wv = make_widget_value (SSDATA (string), NULL, true, Qnil);
3427a3db 915 wv->button_type = BUTTON_TYPE_NONE;
fe8fa62f
RS
916 /* This prevents lwlib from assuming this
917 menu item is really supposed to be empty. */
d01a7826 918 /* The intptr_t cast avoids a warning.
fe8fa62f 919 This value just has to be different from small integers. */
d01a7826 920 wv->call_data = (void *) (intptr_t) (-1);
88766961 921
177c0ea7 922 if (prev_wv)
88766961
RS
923 prev_wv->next = wv;
924 else
925 first_wv->contents = wv;
926 prev_wv = wv;
927 }
62555c22
RS
928
929 /* Forget what we thought we knew about what is in the
930 detailed contents of the menu bar menus.
931 Changing the top level always destroys the contents. */
932 f->menu_bar_items_used = 0;
88766961 933 }
4dedbfe0 934
88766961 935 /* Create or update the menu bar widget. */
aa669def 936
4d7e6e51 937 block_input ();
aa669def 938
488dd4c4
JD
939#ifdef USE_GTK
940 xg_crazy_callback_abort = 1;
941 if (menubar_widget)
942 {
6b61353c 943 /* The fourth arg is DEEP_P, which says to consider the entire
488dd4c4
JD
944 menu trees we supply, rather than just the menu bar item names. */
945 xg_modify_menubar_widgets (menubar_widget,
946 f,
947 first_wv,
948 deep_p,
949 G_CALLBACK (menubar_selection_callback),
950 G_CALLBACK (popup_deactivate_callback),
951 G_CALLBACK (menu_highlight_callback));
952 }
953 else
954 {
488dd4c4 955 menubar_widget
177c0ea7 956 = xg_create_widget ("menubar", "menubar", f, first_wv,
488dd4c4
JD
957 G_CALLBACK (menubar_selection_callback),
958 G_CALLBACK (popup_deactivate_callback),
959 G_CALLBACK (menu_highlight_callback));
960
961 f->output_data.x->menubar_widget = menubar_widget;
962 }
963
177c0ea7 964
488dd4c4 965#else /* not USE_GTK */
18686d47 966 if (menubar_widget)
4dedbfe0
PR
967 {
968 /* Disable resizing (done for Motif!) */
7556890b 969 lw_allow_resizing (f->output_data.x->widget, False);
4dedbfe0
PR
970
971 /* The third arg is DEEP_P, which says to consider the entire
972 menu trees we supply, rather than just the menu bar item names. */
88766961 973 lw_modify_all_widgets (id, first_wv, deep_p);
4dedbfe0 974
c98fcf4b 975 /* Re-enable the edit widget to resize. */
7556890b 976 lw_allow_resizing (f->output_data.x->widget, True);
4dedbfe0 977 }
18686d47
RS
978 else
979 {
9f6fcdc5
JD
980 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
981 XtTranslations override = XtParseTranslationTable (menuOverride);
982
3928f2b6
JD
983#ifdef USE_LUCID
984 apply_systemfont_to_menu (f, f->output_data.x->column_widget);
985#endif
986 menubar_widget = lw_create_widget ("menubar", "menubar", id,
987 first_wv,
7556890b 988 f->output_data.x->column_widget,
4dedbfe0
PR
989 0,
990 popup_activate_callback,
991 menubar_selection_callback,
850df50b
GM
992 popup_deactivate_callback,
993 menu_highlight_callback);
7556890b 994 f->output_data.x->menubar_widget = menubar_widget;
9f6fcdc5
JD
995
996 /* Make menu pop down on C-g. */
997 XtOverrideTranslations (menubar_widget, override);
18686d47 998 }
1d1c1567
KH
999
1000 {
cf28cebc 1001 int menubar_size;
723f5a07
J
1002 if (f->output_data.x->menubar_widget)
1003 XtRealizeWidget (f->output_data.x->menubar_widget);
1004
cf28cebc 1005 menubar_size
7556890b
RS
1006 = (f->output_data.x->menubar_widget
1007 ? (f->output_data.x->menubar_widget->core.height
1008 + f->output_data.x->menubar_widget->core.border_width)
1d1c1567
KH
1009 : 0);
1010
5c646d5a
JD
1011#if 1 /* Experimentally, we now get the right results
1012 for -geometry -0-0 without this. 24 Aug 96, rms.
1013 Maybe so, but the menu bar size is missing the pixels so the
cf28cebc 1014 WM size hints are off by these pixels. Jan D, oct 2009. */
5ee80bd3 1015#ifdef USE_LUCID
1d1c1567
KH
1016 if (FRAME_EXTERNAL_MENU_BAR (f))
1017 {
1018 Dimension ibw = 0;
7556890b 1019 XtVaGetValues (f->output_data.x->column_widget,
1d1c1567
KH
1020 XtNinternalBorderWidth, &ibw, NULL);
1021 menubar_size += ibw;
1022 }
5ee80bd3 1023#endif /* USE_LUCID */
5c646d5a 1024#endif /* 1 */
1d1c1567 1025
7556890b 1026 f->output_data.x->menubar_height = menubar_size;
1d1c1567 1027 }
488dd4c4 1028#endif /* not USE_GTK */
177c0ea7 1029
18686d47 1030 free_menubar_widget_value_tree (first_wv);
364cd450 1031 update_frame_menubar (f);
18686d47 1032
488dd4c4
JD
1033#ifdef USE_GTK
1034 xg_crazy_callback_abort = 0;
1035#endif
1036
4d7e6e51 1037 unblock_input ();
18686d47 1038}
85f487d1 1039
8e6208c5 1040/* Called from Fx_create_frame to create the initial menubar of a frame
4dedbfe0
PR
1041 before it is mapped, so that the window is mapped with the menubar already
1042 there instead of us tacking it on later and thrashing the window after it
1043 is visible. */
1044
1045void
a10c8269 1046initialize_frame_menubar (struct frame *f)
4dedbfe0
PR
1047{
1048 /* This function is called before the first chance to redisplay
1049 the frame. It has to be, so the frame will have the right size. */
f00af5b1 1050 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
88766961 1051 set_frame_menubar (f, 1, 1);
4dedbfe0
PR
1052}
1053
aba25348 1054
4dedbfe0 1055/* Get rid of the menu bar of frame F, and free its storage.
488dd4c4
JD
1056 This is used when deleting a frame, and when turning off the menu bar.
1057 For GTK this function is in gtkutil.c. */
4dedbfe0 1058
488dd4c4 1059#ifndef USE_GTK
85f487d1 1060void
a10c8269 1061free_frame_menubar (struct frame *f)
85f487d1
FP
1062{
1063 Widget menubar_widget;
85f487d1 1064
65b02bb9 1065 eassert (FRAME_X_P (f));
62af879c 1066
7556890b 1067 menubar_widget = f->output_data.x->menubar_widget;
a45bad2a
RS
1068
1069 f->output_data.x->menubar_height = 0;
177c0ea7 1070
85f487d1
FP
1071 if (menubar_widget)
1072 {
aba25348
GM
1073#ifdef USE_MOTIF
1074 /* Removing the menu bar magically changes the shell widget's x
1075 and y position of (0, 0) which, when the menu bar is turned
53964682 1076 on again, leads to pull-down menus appearing in strange
aba25348
GM
1077 positions near the upper-left corner of the display. This
1078 happens only with some window managers like twm and ctwm,
1079 but not with other like Motif's mwm or kwm, because the
1080 latter generate ConfigureNotify events when the menu bar
1081 is switched off, which fixes the shell position. */
1082 Position x0, y0, x1, y1;
1083#endif
177c0ea7 1084
4d7e6e51 1085 block_input ();
aba25348
GM
1086
1087#ifdef USE_MOTIF
ae556422
GM
1088 if (f->output_data.x->widget)
1089 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
aba25348 1090#endif
177c0ea7 1091
7556890b 1092 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
0c05dcd3 1093 f->output_data.x->menubar_widget = NULL;
aba25348 1094
ae556422
GM
1095 if (f->output_data.x->widget)
1096 {
23243f29 1097#ifdef USE_MOTIF
ae556422
GM
1098 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
1099 if (x1 == 0 && y1 == 0)
1100 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
aba25348 1101#endif
880e6158 1102 x_set_window_size (f, 0, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 1);
23243f29 1103 }
4d7e6e51 1104 unblock_input ();
85f487d1
FP
1105 }
1106}
488dd4c4 1107#endif /* not USE_GTK */
78589e07 1108
488dd4c4 1109#endif /* USE_X_TOOLKIT || USE_GTK */
78589e07 1110\f
cfd794af 1111/* x_menu_show actually displays a menu using the panes and items in menu_items
78589e07 1112 and returns the value selected from it.
cfd794af 1113 There are two versions of x_menu_show, one for Xt and one for Xlib.
78589e07
RS
1114 Both assume input is blocked by the caller. */
1115
1116/* F is the frame the menu is for.
1117 X and Y are the frame-relative specified position,
1118 relative to the inside upper left corner of the frame F.
cfd794af
DA
1119 Bitfield MENUFLAGS bits are:
1120 MENU_FOR_CLICK is set if this menu was invoked for a mouse click.
1121 MENU_KEYMAPS is set if this menu was specified with keymaps;
78589e07
RS
1122 in that case, we return a list containing the chosen item's value
1123 and perhaps also the pane's prefix.
1124 TITLE is the specified menu title.
1125 ERROR is a place to store an error message string in case of failure.
1126 (We return nil on failure, but the value doesn't actually matter.) */
18686d47 1127
488dd4c4
JD
1128#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1129
1130/* The item selected in the popup menu. */
1131static Lisp_Object *volatile menu_item_selection;
1132
1133#ifdef USE_GTK
1134
1135/* Used when position a popup menu. See menu_position_func and
1136 create_and_show_popup_menu below. */
1137struct next_popup_x_y
1138{
a10c8269 1139 struct frame *f;
488dd4c4
JD
1140 int x;
1141 int y;
1142};
1143
1144/* The menu position function to use if we are not putting a popup
1145 menu where the pointer is.
1146 MENU is the menu to pop up.
1147 X and Y shall on exit contain x/y where the menu shall pop up.
1148 PUSH_IN is not documented in the GTK manual.
1149 USER_DATA is any data passed in when calling gtk_menu_popup.
1150 Here it points to a struct next_popup_x_y where the coordinates
7b76ca1c 1151 to store in *X and *Y are as well as the frame for the popup.
488dd4c4
JD
1152
1153 Here only X and Y are used. */
1154static void
971de7fb 1155menu_position_func (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer user_data)
488dd4c4 1156{
7d652d97 1157 struct next_popup_x_y *data = user_data;
7b76ca1c 1158 GtkRequisition req;
aad3612f 1159 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (data->f);
fd2f80c6
CY
1160 int disp_width = x_display_pixel_width (dpyinfo);
1161 int disp_height = x_display_pixel_height (dpyinfo);
b9de078a 1162
7b76ca1c
JD
1163 *x = data->x;
1164 *y = data->y;
1165
1166 /* Check if there is room for the menu. If not, adjust x/y so that
1167 the menu is fully visible. */
0afb4571 1168 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &req);
7b76ca1c
JD
1169 if (data->x + req.width > disp_width)
1170 *x -= data->x + req.width - disp_width;
1171 if (data->y + req.height > disp_height)
1172 *y -= data->y + req.height - disp_height;
488dd4c4
JD
1173}
1174
1175static void
971de7fb 1176popup_selection_callback (GtkWidget *widget, gpointer client_data)
488dd4c4 1177{
7d652d97 1178 xg_menu_item_cb_data *cb_data = client_data;
488dd4c4
JD
1179
1180 if (xg_crazy_callback_abort) return;
7d652d97 1181 if (cb_data) menu_item_selection = cb_data->call_data;
488dd4c4
JD
1182}
1183
27e498e6
PE
1184static void
1185pop_down_menu (void *arg)
af89e871
JD
1186{
1187 popup_activated_flag = 0;
4d7e6e51 1188 block_input ();
27e498e6 1189 gtk_widget_destroy (GTK_WIDGET (arg));
4d7e6e51 1190 unblock_input ();
af89e871
JD
1191}
1192
488dd4c4
JD
1193/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1194 menu pops down.
1195 menu_item_selection will be set to the selection. */
1196static void
6bbe6da8
DA
1197create_and_show_popup_menu (struct frame *f, widget_value *first_wv,
1198 int x, int y, bool for_click)
488dd4c4
JD
1199{
1200 int i;
1201 GtkWidget *menu;
1202 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
1203 struct next_popup_x_y popup_x_y;
d311d28c 1204 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
7cded46f 1205 bool use_pos_func = ! for_click;
93d5ca1f
JD
1206
1207#ifdef HAVE_GTK3
1208 /* Always use position function for Gtk3. Otherwise menus may become
1209 too small to show anything. */
1210 use_pos_func = 1;
1211#endif
488dd4c4 1212
65b02bb9 1213 eassert (FRAME_X_P (f));
62af879c 1214
488dd4c4
JD
1215 xg_crazy_callback_abort = 1;
1216 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
1217 G_CALLBACK (popup_selection_callback),
1218 G_CALLBACK (popup_deactivate_callback),
1219 G_CALLBACK (menu_highlight_callback));
1220 xg_crazy_callback_abort = 0;
177c0ea7 1221
93d5ca1f 1222 if (use_pos_func)
488dd4c4
JD
1223 {
1224 /* Not invoked by a click. pop up at x/y. */
1225 pos_func = menu_position_func;
1226
1227 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
1228 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1229 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
488dd4c4
JD
1230
1231 popup_x_y.x = x;
1232 popup_x_y.y = y;
7b76ca1c 1233 popup_x_y.f = f;
488dd4c4 1234
9b85e63d
JD
1235 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1236 }
93d5ca1f
JD
1237
1238 if (for_click)
9b85e63d
JD
1239 {
1240 for (i = 0; i < 5; i++)
aad3612f 1241 if (FRAME_DISPLAY_INFO (f)->grabbed & (1 << i))
9b85e63d 1242 break;
e9abb8a8 1243 /* If keys aren't grabbed (i.e., a mouse up event), use 0. */
61c2b0b3 1244 if (i == 5) i = 0;
9b85e63d 1245 }
59cfb104 1246
488dd4c4
JD
1247 /* Display the menu. */
1248 gtk_widget_show_all (menu);
c0df13a6 1249
a20903d0 1250 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i,
6bbe6da8 1251 FRAME_DISPLAY_INFO (f)->last_user_time);
177c0ea7 1252
27e498e6 1253 record_unwind_protect_ptr (pop_down_menu, menu);
af89e871 1254
e547b051 1255 if (gtk_widget_get_mapped (menu))
ff18668f
JD
1256 {
1257 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1258 two. show_help_echo uses this to detect popup menus. */
1259 popup_activated_flag = 1;
1260 /* Process events that apply to the menu. */
1261 popup_widget_loop (1, menu);
1262 }
488dd4c4 1263
af89e871 1264 unbind_to (specpdl_count, Qnil);
177c0ea7 1265
488dd4c4
JD
1266 /* Must reset this manually because the button release event is not passed
1267 to Emacs event loop. */
aad3612f 1268 FRAME_DISPLAY_INFO (f)->grabbed = 0;
488dd4c4
JD
1269}
1270
1271#else /* not USE_GTK */
18686d47 1272
8ed87156 1273/* We need a unique id for each widget handled by the Lucid Widget
cc17e9bf
KH
1274 library.
1275
1276 For the main windows, and popup menus, we use this counter,
88766961 1277 which we increment each time after use. This starts from 1<<16.
cc17e9bf 1278
88766961
RS
1279 For menu bars, we use numbers starting at 0, counted in
1280 next_menubar_widget_id. */
8ed87156 1281LWLIB_ID widget_id_tick;
165e1749 1282
4dedbfe0 1283static void
ebd15611 1284popup_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
4dedbfe0 1285{
7d652d97 1286 menu_item_selection = client_data;
4dedbfe0
PR
1287}
1288
af89e871
JD
1289/* ARG is the LWLIB ID of the dialog box, represented
1290 as a Lisp object as (HIGHPART . LOWPART). */
1291
27e498e6 1292static void
ebd15611 1293pop_down_menu (Lisp_Object arg)
af89e871
JD
1294{
1295 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
1296 | XINT (XCDR (arg)));
1297
4d7e6e51 1298 block_input ();
af89e871 1299 lw_destroy_all_widgets (id);
4d7e6e51 1300 unblock_input ();
af89e871 1301 popup_activated_flag = 0;
af89e871
JD
1302}
1303
488dd4c4
JD
1304/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1305 menu pops down.
1306 menu_item_selection will be set to the selection. */
1307static void
a10c8269 1308create_and_show_popup_menu (struct frame *f, widget_value *first_wv,
6bbe6da8 1309 int x, int y, bool for_click)
488dd4c4
JD
1310{
1311 int i;
1312 Arg av[2];
1313 int ac = 0;
65969f63
CY
1314 XEvent dummy;
1315 XButtonPressedEvent *event = &(dummy.xbutton);
488dd4c4
JD
1316 LWLIB_ID menu_id;
1317 Widget menu;
488dd4c4 1318
65b02bb9 1319 eassert (FRAME_X_P (f));
62af879c 1320
3928f2b6
JD
1321#ifdef USE_LUCID
1322 apply_systemfont_to_menu (f, f->output_data.x->widget);
1323#endif
1324
488dd4c4
JD
1325 menu_id = widget_id_tick++;
1326 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1327 f->output_data.x->widget, 1, 0,
1328 popup_selection_callback,
1329 popup_deactivate_callback,
1330 menu_highlight_callback);
1331
65969f63
CY
1332 event->type = ButtonPress;
1333 event->serial = 0;
1334 event->send_event = 0;
1335 event->display = FRAME_X_DISPLAY (f);
1336 event->time = CurrentTime;
aad3612f 1337 event->root = FRAME_DISPLAY_INFO (f)->root_window;
65969f63
CY
1338 event->window = event->subwindow = event->root;
1339 event->x = x;
1340 event->y = y;
488dd4c4
JD
1341
1342 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
1343 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1344 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
488dd4c4 1345
65969f63
CY
1346 event->x_root = x;
1347 event->y_root = y;
488dd4c4 1348
65969f63
CY
1349 event->state = 0;
1350 event->button = 0;
488dd4c4 1351 for (i = 0; i < 5; i++)
aad3612f 1352 if (FRAME_DISPLAY_INFO (f)->grabbed & (1 << i))
65969f63 1353 event->button = i;
488dd4c4
JD
1354
1355 /* Don't allow any geometry request from the user. */
1356 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1357 XtSetValues (menu, av, ac);
1358
1359 /* Display the menu. */
65969f63 1360 lw_popup_menu (menu, &dummy);
488dd4c4 1361 popup_activated_flag = 1;
98a20c65 1362 x_activate_timeout_atimer ();
59cfb104 1363
af89e871
JD
1364 {
1365 int fact = 4 * sizeof (LWLIB_ID);
d311d28c 1366 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
af89e871
JD
1367 record_unwind_protect (pop_down_menu,
1368 Fcons (make_number (menu_id >> (fact)),
1369 make_number (menu_id & ~(-1 << (fact)))));
488dd4c4 1370
af89e871 1371 /* Process events that apply to the menu. */
aad3612f 1372 popup_get_selection (0, FRAME_DISPLAY_INFO (f), menu_id, 1);
488dd4c4 1373
af89e871
JD
1374 unbind_to (specpdl_count, Qnil);
1375 }
488dd4c4
JD
1376}
1377
1378#endif /* not USE_GTK */
1379
27e498e6
PE
1380static void
1381cleanup_widget_value_tree (void *arg)
ba24cea2 1382{
27e498e6 1383 free_menubar_widget_value_tree (arg);
ba24cea2
YM
1384}
1385
ef7417fd 1386Lisp_Object
cfd794af
DA
1387x_menu_show (struct frame *f, int x, int y, int menuflags,
1388 Lisp_Object title, const char **error_name)
18686d47 1389{
78589e07 1390 int i;
78589e07 1391 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
101bb4a5 1392 widget_value **submenu_stack
38182d90 1393 = alloca (menu_items_used * sizeof *submenu_stack);
101bb4a5 1394 Lisp_Object *subprefix_stack
38182d90 1395 = alloca (menu_items_used * sizeof *subprefix_stack);
101bb4a5 1396 int submenu_depth = 0;
4e8d3549 1397
78c8278d
RS
1398 int first_pane;
1399
9898bd0e 1400 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
ba24cea2 1401
65b02bb9 1402 eassert (FRAME_X_P (f));
62af879c 1403
d4323972 1404 *error_name = NULL;
78589e07 1405
742f715d
KH
1406 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1407 {
d4323972 1408 *error_name = "Empty menu";
742f715d
KH
1409 return Qnil;
1410 }
63c414df 1411
f0177f86
EZ
1412 block_input ();
1413
78589e07
RS
1414 /* Create a tree of widget_value objects
1415 representing the panes and their items. */
5668fb88 1416 wv = make_widget_value ("menu", NULL, true, Qnil);
3427a3db 1417 wv->button_type = BUTTON_TYPE_NONE;
78589e07 1418 first_wv = wv;
78c8278d 1419 first_pane = 1;
177c0ea7 1420
78589e07
RS
1421 /* Loop over all panes and items, filling in the tree. */
1422 i = 0;
1423 while (i < menu_items_used)
1424 {
28be1ada 1425 if (EQ (AREF (menu_items, i), Qnil))
101bb4a5
RS
1426 {
1427 submenu_stack[submenu_depth++] = save_wv;
1428 save_wv = prev_wv;
1429 prev_wv = 0;
78c8278d 1430 first_pane = 1;
101bb4a5
RS
1431 i++;
1432 }
28be1ada 1433 else if (EQ (AREF (menu_items, i), Qlambda))
101bb4a5
RS
1434 {
1435 prev_wv = save_wv;
1436 save_wv = submenu_stack[--submenu_depth];
78c8278d 1437 first_pane = 0;
101bb4a5
RS
1438 i++;
1439 }
28be1ada 1440 else if (EQ (AREF (menu_items, i), Qt)
101bb4a5
RS
1441 && submenu_depth != 0)
1442 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
1443 /* Ignore a nil in the item list.
1444 It's meaningful only for dialog boxes. */
28be1ada 1445 else if (EQ (AREF (menu_items, i), Qquote))
fcaa7665 1446 i += 1;
28be1ada 1447 else if (EQ (AREF (menu_items, i), Qt))
78589e07
RS
1448 {
1449 /* Create a new pane. */
1450 Lisp_Object pane_name, prefix;
6d1f7fee 1451 const char *pane_string;
177c0ea7 1452
4c329aa8
GM
1453 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1454 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
177c0ea7 1455
703dc2a8 1456#ifndef HAVE_MULTILINGUAL_MENU
4c329aa8
GM
1457 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1458 {
8af9fa55 1459 pane_name = ENCODE_MENU_STRING (pane_name);
3ae565b3 1460 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
4c329aa8 1461 }
703dc2a8 1462#endif
78589e07 1463 pane_string = (NILP (pane_name)
51b59d79 1464 ? "" : SSDATA (pane_name));
101bb4a5 1465 /* If there is just one top-level pane, put all its items directly
78589e07
RS
1466 under the top-level menu. */
1467 if (menu_items_n_panes == 1)
1468 pane_string = "";
1469
1470 /* If the pane has a meaningful name,
1471 make the pane a top-level menu item
1472 with its items as a submenu beneath it. */
cfd794af 1473 if (!(menuflags & MENU_KEYMAPS) && strcmp (pane_string, ""))
78589e07 1474 {
5668fb88 1475 wv = make_widget_value (pane_string, NULL, true, Qnil);
78589e07
RS
1476 if (save_wv)
1477 save_wv->next = wv;
1478 else
1479 first_wv->contents = wv;
cfd794af 1480 if ((menuflags & MENU_KEYMAPS) && !NILP (prefix))
78589e07 1481 wv->name++;
3427a3db 1482 wv->button_type = BUTTON_TYPE_NONE;
78c8278d
RS
1483 save_wv = wv;
1484 prev_wv = 0;
78589e07 1485 }
78c8278d
RS
1486 else if (first_pane)
1487 {
1488 save_wv = wv;
1489 prev_wv = 0;
1490 }
1491 first_pane = 0;
78589e07
RS
1492 i += MENU_ITEMS_PANE_LENGTH;
1493 }
1494 else
1495 {
1496 /* Create a new item within current pane. */
9cd50434 1497 Lisp_Object item_name, enable, descrip, def, type, selected, help;
4c329aa8
GM
1498 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1499 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1500 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1501 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1502 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1503 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1504 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3427a3db 1505
703dc2a8 1506#ifndef HAVE_MULTILINGUAL_MENU
3427a3db 1507 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
4c329aa8 1508 {
646f98ec 1509 item_name = ENCODE_MENU_STRING (item_name);
3ae565b3 1510 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
4c329aa8 1511 }
177c0ea7 1512
3427a3db 1513 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
4c329aa8 1514 {
646f98ec 1515 descrip = ENCODE_MENU_STRING (descrip);
3ae565b3 1516 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
4c329aa8
GM
1517 }
1518#endif /* not HAVE_MULTILINGUAL_MENU */
177c0ea7 1519
5668fb88
DA
1520 wv = make_widget_value (SSDATA (item_name), NULL, !NILP (enable),
1521 STRINGP (help) ? help : Qnil);
177c0ea7 1522 if (prev_wv)
78589e07 1523 prev_wv->next = wv;
177c0ea7 1524 else
78589e07 1525 save_wv->contents = wv;
78589e07 1526 if (!NILP (descrip))
51b59d79 1527 wv->key = SSDATA (descrip);
a352a815
RS
1528 /* If this item has a null value,
1529 make the call_data null so that it won't display a box
1530 when the mouse is on it. */
4939150c 1531 wv->call_data = !NILP (def) ? aref_addr (menu_items, i) : 0;
3427a3db
GM
1532
1533 if (NILP (type))
1534 wv->button_type = BUTTON_TYPE_NONE;
1535 else if (EQ (type, QCtoggle))
1536 wv->button_type = BUTTON_TYPE_TOGGLE;
1537 else if (EQ (type, QCradio))
1538 wv->button_type = BUTTON_TYPE_RADIO;
1539 else
1088b922 1540 emacs_abort ();
3427a3db
GM
1541
1542 wv->selected = !NILP (selected);
cc45fb40 1543
78589e07
RS
1544 prev_wv = wv;
1545
1546 i += MENU_ITEMS_ITEM_LENGTH;
1547 }
1548 }
1549
c98fcf4b 1550 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
1551 if (!NILP (title))
1552 {
5668fb88
DA
1553 widget_value *wv_title;
1554 widget_value *wv_sep1 = make_widget_value ("--", NULL, false, Qnil);
1555 widget_value *wv_sep2 = make_widget_value ("--", NULL, false, Qnil);
4dedbfe0 1556
4dedbfe0 1557 wv_sep2->next = first_wv->contents;
4dedbfe0
PR
1558 wv_sep1->next = wv_sep2;
1559
703dc2a8
KH
1560#ifndef HAVE_MULTILINGUAL_MENU
1561 if (STRING_MULTIBYTE (title))
646f98ec 1562 title = ENCODE_MENU_STRING (title);
703dc2a8 1563#endif
177c0ea7 1564
5668fb88 1565 wv_title = make_widget_value (SSDATA (title), NULL, true, Qnil);
3427a3db 1566 wv_title->button_type = BUTTON_TYPE_NONE;
a9d8395f 1567 wv_title->next = wv_sep1;
4dedbfe0
PR
1568 first_wv->contents = wv_title;
1569 }
1570
78589e07
RS
1571 /* No selection has been chosen yet. */
1572 menu_item_selection = 0;
1573
ba24cea2
YM
1574 /* Make sure to free the widget_value objects we used to specify the
1575 contents even with longjmp. */
27e498e6 1576 record_unwind_protect_ptr (cleanup_widget_value_tree, first_wv);
ba24cea2 1577
488dd4c4 1578 /* Actually create and show the menu until popped down. */
cfd794af
DA
1579 create_and_show_popup_menu (f, first_wv, x, y,
1580 menuflags & MENU_FOR_CLICK);
18686d47 1581
ba24cea2 1582 unbind_to (specpdl_count, Qnil);
18686d47 1583
78589e07
RS
1584 /* Find the selected item, and its pane, to return
1585 the proper value. */
1586 if (menu_item_selection != 0)
1587 {
c63f6952 1588 Lisp_Object prefix, entry;
78589e07 1589
6bbd7a29 1590 prefix = entry = Qnil;
78589e07
RS
1591 i = 0;
1592 while (i < menu_items_used)
1593 {
28be1ada 1594 if (EQ (AREF (menu_items, i), Qnil))
101bb4a5
RS
1595 {
1596 subprefix_stack[submenu_depth++] = prefix;
1597 prefix = entry;
1598 i++;
1599 }
28be1ada 1600 else if (EQ (AREF (menu_items, i), Qlambda))
101bb4a5
RS
1601 {
1602 prefix = subprefix_stack[--submenu_depth];
1603 i++;
1604 }
28be1ada 1605 else if (EQ (AREF (menu_items, i), Qt))
78589e07
RS
1606 {
1607 prefix
28be1ada 1608 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
78589e07
RS
1609 i += MENU_ITEMS_PANE_LENGTH;
1610 }
d31d42cc
RS
1611 /* Ignore a nil in the item list.
1612 It's meaningful only for dialog boxes. */
28be1ada 1613 else if (EQ (AREF (menu_items, i), Qquote))
d31d42cc 1614 i += 1;
78589e07
RS
1615 else
1616 {
1617 entry
28be1ada 1618 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
4939150c 1619 if (menu_item_selection == aref_addr (menu_items, i))
78589e07 1620 {
cfd794af 1621 if (menuflags & MENU_KEYMAPS)
78589e07 1622 {
101bb4a5
RS
1623 int j;
1624
6c6f1994 1625 entry = list1 (entry);
78589e07
RS
1626 if (!NILP (prefix))
1627 entry = Fcons (prefix, entry);
101bb4a5 1628 for (j = submenu_depth - 1; j >= 0; j--)
e48087b7 1629 if (!NILP (subprefix_stack[j]))
5964e450 1630 entry = Fcons (subprefix_stack[j], entry);
78589e07 1631 }
f0177f86 1632 unblock_input ();
78589e07
RS
1633 return entry;
1634 }
1635 i += MENU_ITEMS_ITEM_LENGTH;
1636 }
1637 }
1638 }
cfd794af 1639 else if (!(menuflags & MENU_FOR_CLICK))
f0177f86
EZ
1640 {
1641 unblock_input ();
1642 /* Make "Cancel" equivalent to C-g. */
1643 Fsignal (Qquit, Qnil);
1644 }
78589e07 1645
f0177f86 1646 unblock_input ();
78589e07 1647 return Qnil;
18686d47 1648}
4dedbfe0 1649\f
488dd4c4
JD
1650#ifdef USE_GTK
1651static void
971de7fb 1652dialog_selection_callback (GtkWidget *widget, gpointer client_data)
488dd4c4 1653{
8ac068ac 1654 /* Treat the pointer as an integer. There's no problem
488dd4c4 1655 as long as pointers have enough bits to hold small integers. */
d01a7826 1656 if ((intptr_t) client_data != -1)
7d652d97 1657 menu_item_selection = client_data;
488dd4c4
JD
1658
1659 popup_activated_flag = 0;
1660}
1661
1662/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1663 dialog pops down.
1664 menu_item_selection will be set to the selection. */
1665static void
a10c8269 1666create_and_show_dialog (struct frame *f, widget_value *first_wv)
488dd4c4
JD
1667{
1668 GtkWidget *menu;
1669
65b02bb9 1670 eassert (FRAME_X_P (f));
62af879c 1671
488dd4c4
JD
1672 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
1673 G_CALLBACK (dialog_selection_callback),
1674 G_CALLBACK (popup_deactivate_callback),
1675 0);
1676
1677 if (menu)
1678 {
d311d28c 1679 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
27e498e6 1680 record_unwind_protect_ptr (pop_down_menu, menu);
af89e871 1681
488dd4c4
JD
1682 /* Display the menu. */
1683 gtk_widget_show_all (menu);
1684
1685 /* Process events that apply to the menu. */
f1d1cd24 1686 popup_widget_loop (1, menu);
177c0ea7 1687
af89e871 1688 unbind_to (specpdl_count, Qnil);
488dd4c4
JD
1689 }
1690}
1691
1692#else /* not USE_GTK */
4dedbfe0 1693static void
ebd15611 1694dialog_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
4dedbfe0 1695{
4475bec4 1696 /* Treat the pointer as an integer. There's no problem
01d5e892 1697 as long as pointers have enough bits to hold small integers. */
4475bec4 1698 if ((intptr_t) client_data != -1)
7d652d97 1699 menu_item_selection = client_data;
488dd4c4 1700
4d7e6e51 1701 block_input ();
4dedbfe0 1702 lw_destroy_all_widgets (id);
4d7e6e51 1703 unblock_input ();
9572375b 1704 popup_activated_flag = 0;
4dedbfe0 1705}
18686d47 1706
488dd4c4 1707
488dd4c4
JD
1708/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1709 dialog pops down.
1710 menu_item_selection will be set to the selection. */
1711static void
a10c8269 1712create_and_show_dialog (struct frame *f, widget_value *first_wv)
488dd4c4
JD
1713{
1714 LWLIB_ID dialog_id;
1715
65b02bb9 1716 eassert (FRAME_X_P (f));
62af879c 1717
488dd4c4 1718 dialog_id = widget_id_tick++;
52214050 1719#ifdef USE_LUCID
1ecb2d3f
JD
1720 apply_systemfont_to_dialog (f->output_data.x->widget);
1721#endif
488dd4c4
JD
1722 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1723 f->output_data.x->widget, 1, 0,
1724 dialog_selection_callback, 0, 0);
1725 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
488dd4c4
JD
1726 /* Display the dialog box. */
1727 lw_pop_up_all_widgets (dialog_id);
1728 popup_activated_flag = 1;
98a20c65 1729 x_activate_timeout_atimer ();
488dd4c4
JD
1730
1731 /* Process events that apply to the dialog box.
1732 Also handle timers. */
1733 {
d311d28c 1734 ptrdiff_t count = SPECPDL_INDEX ();
488dd4c4 1735 int fact = 4 * sizeof (LWLIB_ID);
177c0ea7 1736
488dd4c4 1737 /* xdialog_show_unwind is responsible for popping the dialog box down. */
af89e871 1738 record_unwind_protect (pop_down_menu,
488dd4c4
JD
1739 Fcons (make_number (dialog_id >> (fact)),
1740 make_number (dialog_id & ~(-1 << (fact)))));
1741
aad3612f 1742 popup_get_selection (0, FRAME_DISPLAY_INFO (f), dialog_id, 1);
488dd4c4
JD
1743
1744 unbind_to (count, Qnil);
1745 }
1746}
1747
1748#endif /* not USE_GTK */
1749
42ca4633 1750static const char * button_names [] = {
165e1749
FP
1751 "button1", "button2", "button3", "button4", "button5",
1752 "button6", "button7", "button8", "button9", "button10" };
1753
1754static Lisp_Object
f1b44793
DA
1755x_dialog_show (struct frame *f, Lisp_Object title,
1756 Lisp_Object header, const char **error_name)
165e1749
FP
1757{
1758 int i, nb_buttons=0;
80670155 1759 char dialog_name[6];
165e1749 1760
faa935b6 1761 widget_value *wv, *first_wv = 0, *prev_wv = 0;
165e1749 1762
fcaa7665
RS
1763 /* Number of elements seen so far, before boundary. */
1764 int left_count = 0;
1765 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1766 int boundary_seen = 0;
1767
9898bd0e 1768 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
ba24cea2 1769
65b02bb9 1770 eassert (FRAME_X_P (f));
62af879c 1771
6a040d6a 1772 *error_name = NULL;
165e1749 1773
80670155
RS
1774 if (menu_items_n_panes > 1)
1775 {
6a040d6a 1776 *error_name = "Multiple panes in dialog box";
80670155
RS
1777 return Qnil;
1778 }
1779
165e1749
FP
1780 /* Create a tree of widget_value objects
1781 representing the text label and buttons. */
1782 {
f1b44793 1783 Lisp_Object pane_name;
6d1f7fee 1784 const char *pane_string;
28be1ada 1785 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
165e1749 1786 pane_string = (NILP (pane_name)
51b59d79 1787 ? "" : SSDATA (pane_name));
5668fb88 1788 prev_wv = make_widget_value ("message", (char *) pane_string, true, Qnil);
165e1749 1789 first_wv = prev_wv;
177c0ea7 1790
165e1749
FP
1791 /* Loop over all panes and items, filling in the tree. */
1792 i = MENU_ITEMS_PANE_LENGTH;
1793 while (i < menu_items_used)
1794 {
177c0ea7 1795
165e1749
FP
1796 /* Create a new item within current pane. */
1797 Lisp_Object item_name, enable, descrip;
28be1ada
DA
1798 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1799 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
165e1749 1800 descrip
28be1ada 1801 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
177c0ea7 1802
80670155
RS
1803 if (NILP (item_name))
1804 {
1805 free_menubar_widget_value_tree (first_wv);
6a040d6a 1806 *error_name = "Submenu in dialog items";
80670155
RS
1807 return Qnil;
1808 }
fcaa7665
RS
1809 if (EQ (item_name, Qquote))
1810 {
1811 /* This is the boundary between left-side elts
1812 and right-side elts. Stop incrementing right_count. */
1813 boundary_seen = 1;
1814 i++;
1815 continue;
1816 }
86e71abf 1817 if (nb_buttons >= 9)
80670155
RS
1818 {
1819 free_menubar_widget_value_tree (first_wv);
6a040d6a 1820 *error_name = "Too many dialog items";
80670155
RS
1821 return Qnil;
1822 }
1823
5668fb88
DA
1824 wv = make_widget_value (button_names[nb_buttons],
1825 SSDATA (item_name),
1826 !NILP (enable), Qnil);
165e1749 1827 prev_wv->next = wv;
165e1749 1828 if (!NILP (descrip))
51b59d79 1829 wv->key = SSDATA (descrip);
4939150c 1830 wv->call_data = aref_addr (menu_items, i);
165e1749
FP
1831 prev_wv = wv;
1832
fcaa7665
RS
1833 if (! boundary_seen)
1834 left_count++;
1835
165e1749
FP
1836 nb_buttons++;
1837 i += MENU_ITEMS_ITEM_LENGTH;
1838 }
1839
fcaa7665
RS
1840 /* If the boundary was not specified,
1841 by default put half on the left and half on the right. */
1842 if (! boundary_seen)
1843 left_count = nb_buttons - nb_buttons / 2;
1844
5668fb88 1845 wv = make_widget_value (dialog_name, NULL, false, Qnil);
6a040d6a
NR
1846
1847 /* Frame title: 'Q' = Question, 'I' = Information.
1848 Can also have 'E' = Error if, one day, we want
1849 a popup for errors. */
5e617bc2 1850 if (NILP (header))
6a040d6a
NR
1851 dialog_name[0] = 'Q';
1852 else
1853 dialog_name[0] = 'I';
1854
80670155
RS
1855 /* Dialog boxes use a really stupid name encoding
1856 which specifies how many buttons to use
6a040d6a 1857 and how many buttons are on the right. */
80670155
RS
1858 dialog_name[1] = '0' + nb_buttons;
1859 dialog_name[2] = 'B';
1860 dialog_name[3] = 'R';
fcaa7665
RS
1861 /* Number of buttons to put on the right. */
1862 dialog_name[4] = '0' + nb_buttons - left_count;
80670155 1863 dialog_name[5] = 0;
165e1749
FP
1864 wv->contents = first_wv;
1865 first_wv = wv;
165e1749
FP
1866 }
1867
165e1749
FP
1868 /* No selection has been chosen yet. */
1869 menu_item_selection = 0;
1870
ba24cea2
YM
1871 /* Make sure to free the widget_value objects we used to specify the
1872 contents even with longjmp. */
27e498e6 1873 record_unwind_protect_ptr (cleanup_widget_value_tree, first_wv);
ba24cea2 1874
488dd4c4
JD
1875 /* Actually create and show the dialog. */
1876 create_and_show_dialog (f, first_wv);
f02cac82 1877
ba24cea2 1878 unbind_to (specpdl_count, Qnil);
177c0ea7 1879
488dd4c4
JD
1880 /* Find the selected item, and its pane, to return
1881 the proper value. */
165e1749
FP
1882 if (menu_item_selection != 0)
1883 {
165e1749
FP
1884 i = 0;
1885 while (i < menu_items_used)
1886 {
1887 Lisp_Object entry;
1888
28be1ada 1889 if (EQ (AREF (menu_items, i), Qt))
f1b44793 1890 i += MENU_ITEMS_PANE_LENGTH;
28be1ada 1891 else if (EQ (AREF (menu_items, i), Qquote))
85996cfb
GM
1892 {
1893 /* This is the boundary between left-side elts and
1894 right-side elts. */
1895 ++i;
1896 }
165e1749
FP
1897 else
1898 {
1899 entry
28be1ada 1900 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
4939150c 1901 if (menu_item_selection == aref_addr (menu_items, i))
f1b44793 1902 return entry;
165e1749
FP
1903 i += MENU_ITEMS_ITEM_LENGTH;
1904 }
1905 }
1906 }
9f6fcdc5
JD
1907 else
1908 /* Make "Cancel" equivalent to C-g. */
1909 Fsignal (Qquit, Qnil);
165e1749
FP
1910
1911 return Qnil;
1912}
ba461919 1913
0afa0aab
EZ
1914Lisp_Object
1915xw_popup_dialog (struct frame *f, Lisp_Object header, Lisp_Object contents)
1916{
1917 Lisp_Object title;
1918 const char *error_name;
1919 Lisp_Object selection;
1920 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1921
1922 check_window_system (f);
1923
1924 /* Decode the dialog items from what was specified. */
1925 title = Fcar (contents);
1926 CHECK_STRING (title);
1927 record_unwind_protect_void (unuse_menu_items);
1928
1929 if (NILP (Fcar (Fcdr (contents))))
1930 /* No buttons specified, add an "Ok" button so users can pop down
1931 the dialog. Also, the lesstif/motif version crashes if there are
1932 no buttons. */
1933 contents = list2 (title, Fcons (build_string ("Ok"), Qt));
1934
1935 list_of_panes (list1 (contents));
1936
1937 /* Display them in a dialog box. */
1938 block_input ();
f1b44793 1939 selection = x_dialog_show (f, title, header, &error_name);
0afa0aab
EZ
1940 unblock_input ();
1941
1942 unbind_to (specpdl_count, Qnil);
1943 discard_menu_items ();
1944
1945 if (error_name) error ("%s", error_name);
1946 return selection;
1947}
1948
488dd4c4 1949#else /* not USE_X_TOOLKIT && not USE_GTK */
78589e07 1950
3e703b25
GM
1951/* The frame of the last activated non-toolkit menu bar.
1952 Used to generate menu help events. */
1953
1954static struct frame *menu_help_frame;
1955
1956
62145073
GM
1957/* Show help HELP_STRING, or clear help if HELP_STRING is null.
1958
1959 PANE is the pane number, and ITEM is the menu item number in
1960 the menu (currently not used).
177c0ea7 1961
62145073
GM
1962 This cannot be done with generating a HELP_EVENT because
1963 XMenuActivate contains a loop that doesn't let Emacs process
1964 keyboard events. */
3e703b25
GM
1965
1966static void
eb18f6cc 1967menu_help_callback (char const *help_string, int pane, int item)
3e703b25 1968{
62145073
GM
1969 Lisp_Object *first_item;
1970 Lisp_Object pane_name;
1971 Lisp_Object menu_object;
177c0ea7 1972
91f2d272 1973 first_item = XVECTOR (menu_items)->contents;
62145073
GM
1974 if (EQ (first_item[0], Qt))
1975 pane_name = first_item[MENU_ITEMS_PANE_NAME];
1976 else if (EQ (first_item[0], Qquote))
cfd794af 1977 /* This shouldn't happen, see x_menu_show. */
ce33e8eb 1978 pane_name = empty_unibyte_string;
62145073
GM
1979 else
1980 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
177c0ea7 1981
62145073 1982 /* (menu-item MENU-NAME PANE-NUMBER) */
6c6f1994 1983 menu_object = list3 (Qmenu_item, pane_name, make_number (pane));
ba461919 1984 show_help_echo (help_string ? build_string (help_string) : Qnil,
f868cd8a 1985 Qnil, menu_object, make_number (item));
3e703b25 1986}
177c0ea7 1987
27e498e6 1988static void
0521f580 1989pop_down_menu (Lisp_Object arg)
af89e871 1990{
a10c8269 1991 struct frame *f = XSAVE_POINTER (arg, 0);
3346c1d0 1992 XMenu *menu = XSAVE_POINTER (arg, 1);
af89e871 1993
4d7e6e51 1994 block_input ();
af89e871 1995#ifndef MSDOS
a130b901
JD
1996 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
1997 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
af89e871 1998#endif
a130b901 1999 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
af89e871
JD
2000
2001#ifdef HAVE_X_WINDOWS
2002 /* Assume the mouse has moved out of the X window.
2003 If it has actually moved in, we will get an EnterNotify. */
aad3612f 2004 x_mouse_leave (FRAME_DISPLAY_INFO (f));
af89e871
JD
2005
2006 /* State that no mouse buttons are now held.
2007 (The oldXMenu code doesn't track this info for us.)
2008 That is not necessarily true, but the fiction leads to reasonable
2009 results, and it is a pain to ask which are actually held now. */
aad3612f 2010 FRAME_DISPLAY_INFO (f)->grabbed = 0;
af89e871
JD
2011
2012#endif /* HAVE_X_WINDOWS */
2013
4d7e6e51 2014 unblock_input ();
af89e871
JD
2015}
2016
3e703b25 2017
1fb99a3a 2018Lisp_Object
cfd794af
DA
2019x_menu_show (struct frame *f, int x, int y, int menuflags,
2020 Lisp_Object title, const char **error_name)
dcfdbac7 2021{
177c0ea7 2022 Window root;
78589e07
RS
2023 XMenu *menu;
2024 int pane, selidx, lpane, status;
2025 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
2026 char *datap;
2027 int ulx, uly, width, height;
2028 int dispwidth, dispheight;
453a4f1b 2029 int i, j, lines, maxlines;
4e8d3549 2030 int maxwidth;
78589e07
RS
2031 int dummy_int;
2032 unsigned int dummy_uint;
d311d28c 2033 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
088831f6 2034
65b02bb9 2035 eassert (FRAME_X_P (f) || FRAME_MSDOS_P (f));
62af879c 2036
bf501fb9 2037 *error_name = 0;
78589e07
RS
2038 if (menu_items_n_panes == 0)
2039 return Qnil;
088831f6 2040
742f715d
KH
2041 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2042 {
bf501fb9 2043 *error_name = "Empty menu";
742f715d
KH
2044 return Qnil;
2045 }
2046
f0177f86
EZ
2047 block_input ();
2048
78589e07 2049 /* Figure out which root window F is on. */
92280f67 2050 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
2051 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2052 &dummy_uint, &dummy_uint);
18686d47 2053
78589e07 2054 /* Make the menu on that window. */
92280f67 2055 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 2056 if (menu == NULL)
dcfdbac7 2057 {
bf501fb9 2058 *error_name = "Can't create menu";
f0177f86 2059 unblock_input ();
78589e07 2060 return Qnil;
dcfdbac7 2061 }
78589e07 2062
87485d6f 2063#ifdef HAVE_X_WINDOWS
78589e07 2064 /* Adjust coordinates to relative to the outer (window manager) window. */
453a4f1b
JD
2065 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
2066 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
87485d6f 2067#endif /* HAVE_X_WINDOWS */
78589e07
RS
2068
2069 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
2070 x += f->left_pos;
2071 y += f->top_pos;
177c0ea7 2072
78589e07 2073 /* Create all the necessary panes and their items. */
eef9bc79
PE
2074 maxwidth = maxlines = lines = i = 0;
2075 lpane = XM_FAILURE;
78589e07 2076 while (i < menu_items_used)
dcfdbac7 2077 {
28be1ada 2078 if (EQ (AREF (menu_items, i), Qt))
dcfdbac7 2079 {
78589e07
RS
2080 /* Create a new pane. */
2081 Lisp_Object pane_name, prefix;
6d1f7fee 2082 const char *pane_string;
78589e07 2083
453a4f1b
JD
2084 maxlines = max (maxlines, lines);
2085 lines = 0;
28be1ada
DA
2086 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2087 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
78589e07 2088 pane_string = (NILP (pane_name)
51b59d79 2089 ? "" : SSDATA (pane_name));
cfd794af 2090 if ((menuflags & MENU_KEYMAPS) && !NILP (prefix))
78589e07
RS
2091 pane_string++;
2092
92280f67 2093 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
2094 if (lpane == XM_FAILURE)
2095 {
92280f67 2096 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
bf501fb9 2097 *error_name = "Can't create pane";
f0177f86 2098 unblock_input ();
78589e07
RS
2099 return Qnil;
2100 }
2101 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
2102
2103 /* Find the width of the widest item in this pane. */
4e8d3549
RS
2104 j = i;
2105 while (j < menu_items_used)
2106 {
2107 Lisp_Object item;
28be1ada 2108 item = AREF (menu_items, j);
4e8d3549
RS
2109 if (EQ (item, Qt))
2110 break;
2111 if (NILP (item))
2112 {
2113 j++;
2114 continue;
2115 }
d5db4077 2116 width = SBYTES (item);
4e8d3549
RS
2117 if (width > maxwidth)
2118 maxwidth = width;
2119
2120 j += MENU_ITEMS_ITEM_LENGTH;
2121 }
dcfdbac7 2122 }
fcaa7665
RS
2123 /* Ignore a nil in the item list.
2124 It's meaningful only for dialog boxes. */
28be1ada 2125 else if (EQ (AREF (menu_items, i), Qquote))
fcaa7665 2126 i += 1;
78589e07 2127 else
dcfdbac7 2128 {
78589e07 2129 /* Create a new item within current pane. */
3e703b25 2130 Lisp_Object item_name, enable, descrip, help;
eb18f6cc
PE
2131 char *item_data;
2132 char const *help_string;
78589e07 2133
28be1ada
DA
2134 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2135 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
78589e07 2136 descrip
28be1ada
DA
2137 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2138 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
eb18f6cc 2139 help_string = STRINGP (help) ? SSDATA (help) : NULL;
177c0ea7 2140
78589e07 2141 if (!NILP (descrip))
4e8d3549 2142 {
4e8d3549
RS
2143 /* if alloca is fast, use that to make the space,
2144 to reduce gc needs. */
38182d90 2145 item_data = alloca (maxwidth + SBYTES (descrip) + 1);
eb18f6cc 2146 memcpy (item_data, SSDATA (item_name), SBYTES (item_name));
d5db4077 2147 for (j = SCHARS (item_name); j < maxwidth; j++)
4e8d3549 2148 item_data[j] = ' ';
eb18f6cc 2149 memcpy (item_data + j, SSDATA (descrip), SBYTES (descrip));
d5db4077 2150 item_data[j + SBYTES (descrip)] = 0;
4e8d3549
RS
2151 }
2152 else
eb18f6cc 2153 item_data = SSDATA (item_name);
78589e07 2154
eef9bc79
PE
2155 if (lpane == XM_FAILURE
2156 || (XMenuAddSelection (FRAME_X_DISPLAY (f),
2157 menu, lpane, 0, item_data,
2158 !NILP (enable), help_string)
2159 == XM_FAILURE))
dcfdbac7 2160 {
92280f67 2161 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
bf501fb9 2162 *error_name = "Can't add selection to menu";
f0177f86 2163 unblock_input ();
78589e07 2164 return Qnil;
dcfdbac7 2165 }
78589e07 2166 i += MENU_ITEMS_ITEM_LENGTH;
453a4f1b 2167 lines++;
dcfdbac7
JB
2168 }
2169 }
4e8d3549 2170
453a4f1b
JD
2171 maxlines = max (maxlines, lines);
2172
78589e07 2173 /* All set and ready to fly. */
92280f67 2174 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
63685b9d
GM
2175 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2176 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
78589e07
RS
2177 x = min (x, dispwidth);
2178 y = min (y, dispheight);
2179 x = max (x, 1);
2180 y = max (y, 1);
92280f67 2181 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
2182 &ulx, &uly, &width, &height);
2183 if (ulx+width > dispwidth)
2184 {
78589e07 2185 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
2186 ulx = dispwidth - width;
2187 }
2188 if (uly+height > dispheight)
2189 {
78589e07 2190 y -= (uly + height) - dispheight;
dcfdbac7
JB
2191 uly = dispheight - height;
2192 }
2de7397f
EZ
2193#ifndef HAVE_X_WINDOWS
2194 if (FRAME_HAS_MINIBUF_P (f) && uly+height > dispheight - 1)
2195 {
2196 /* Move the menu away of the echo area, to avoid overwriting the
2197 menu with help echo messages or vice versa. */
2198 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
2199 {
2200 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2201 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2202 }
2203 else
2204 {
2205 y--;
2206 uly--;
2207 }
2208 }
2209#endif
78589e07
RS
2210 if (ulx < 0) x -= ulx;
2211 if (uly < 0) y -= uly;
121e4555 2212
cfd794af 2213 if (!(menuflags & MENU_FOR_CLICK))
453a4f1b
JD
2214 {
2215 /* If position was not given by a mouse click, adjust so upper left
2216 corner of the menu as a whole ends up at given coordinates. This
2217 is what x-popup-menu says in its documentation. */
2218 x += width/2;
2219 y += 1.5*height/(maxlines+2);
2220 }
2221
121e4555 2222 XMenuSetAEQ (menu, TRUE);
78589e07
RS
2223 XMenuSetFreeze (menu, TRUE);
2224 pane = selidx = 0;
3e703b25 2225
c3438661
JD
2226#ifndef MSDOS
2227 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
2228#endif
59cfb104 2229
1396ac86 2230 record_unwind_protect (pop_down_menu, make_save_ptr_ptr (f, menu));
c3438661 2231
3e703b25
GM
2232 /* Help display under X won't work because XMenuActivate contains
2233 a loop that doesn't give Emacs a chance to process it. */
2234 menu_help_frame = f;
92280f67 2235 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
af89e871
JD
2236 x, y, ButtonReleaseMask, &datap,
2237 menu_help_callback);
eef9bc79 2238 entry = pane_prefix = Qnil;
a352a815 2239
dcfdbac7
JB
2240 switch (status)
2241 {
2242 case XM_SUCCESS:
2243#ifdef XDEBUG
2244 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2245#endif
fa6d54d9 2246
78589e07
RS
2247 /* Find the item number SELIDX in pane number PANE. */
2248 i = 0;
2249 while (i < menu_items_used)
fa6d54d9 2250 {
28be1ada 2251 if (EQ (AREF (menu_items, i), Qt))
088831f6 2252 {
78589e07
RS
2253 if (pane == 0)
2254 pane_prefix
28be1ada 2255 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
78589e07
RS
2256 pane--;
2257 i += MENU_ITEMS_PANE_LENGTH;
088831f6 2258 }
78589e07 2259 else
ab6ee1a0 2260 {
78589e07 2261 if (pane == -1)
ab6ee1a0 2262 {
78589e07 2263 if (selidx == 0)
ab6ee1a0 2264 {
78589e07 2265 entry
28be1ada 2266 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
cfd794af 2267 if (menuflags & MENU_KEYMAPS)
ab6ee1a0 2268 {
6c6f1994 2269 entry = list1 (entry);
78589e07
RS
2270 if (!NILP (pane_prefix))
2271 entry = Fcons (pane_prefix, entry);
ab6ee1a0 2272 }
78589e07 2273 break;
ab6ee1a0 2274 }
78589e07 2275 selidx--;
ab6ee1a0 2276 }
78589e07 2277 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
2278 }
2279 }
78589e07 2280 break;
dcfdbac7 2281
78589e07 2282 case XM_FAILURE:
bf501fb9 2283 *error_name = "Can't activate menu";
78589e07 2284 case XM_IA_SELECT:
9f6fcdc5 2285 break;
78589e07 2286 case XM_NO_SELECT:
be6ed24a
RS
2287 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2288 the menu was invoked with a mouse event as POSITION). */
cfd794af 2289 if (!(menuflags & MENU_FOR_CLICK))
f0177f86
EZ
2290 {
2291 unblock_input ();
2292 Fsignal (Qquit, Qnil);
2293 }
78589e07 2294 break;
dcfdbac7 2295 }
a5285df3 2296
f0177f86 2297 unblock_input ();
af89e871 2298 unbind_to (specpdl_count, Qnil);
a5285df3 2299
78589e07 2300 return entry;
dcfdbac7 2301}
4dedbfe0 2302
78589e07 2303#endif /* not USE_X_TOOLKIT */
1e659e4c 2304
d009ae66
EZ
2305#ifndef MSDOS
2306/* Detect if a dialog or menu has been posted. MSDOS has its own
2307 implementation on msdos.c. */
b79b8a1c 2308
077288cf 2309int ATTRIBUTE_CONST
971de7fb 2310popup_activated (void)
b79b8a1c
CY
2311{
2312 return popup_activated_flag;
2313}
d009ae66 2314#endif /* not MSDOS */
e3135734
CY
2315
2316/* The following is used by delayed window autoselection. */
2317
2318DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
2319 doc: /* Return t if a menu or popup dialog is active. */)
5842a27b 2320 (void)
e3135734 2321{
e3135734 2322 return (popup_activated ()) ? Qt : Qnil;
e3135734 2323}
088831f6 2324\f
dfcf069d 2325void
971de7fb 2326syms_of_xmenu (void)
dcfdbac7 2327{
fe6aa7a1
BT
2328#include "xmenu.x"
2329
cd3520a4 2330 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
0314aacb 2331
8ed87156 2332#ifdef USE_X_TOOLKIT
177c0ea7 2333 widget_id_tick = (1<<16);
88766961 2334 next_menubar_widget_id = 1;
8ed87156
RS
2335#endif
2336
22badffe 2337#if defined (USE_GTK) || defined (USE_X_TOOLKIT)
d67b4f80
DN
2338 Ffset (intern_c_string ("accelerate-menu"),
2339 intern_c_string (Sx_menu_bar_open_internal.symbol_name));
22badffe 2340#endif
dcfdbac7 2341}