use dynwind_begin and dynwind_end
[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;
2bfa3d3e 753 dynwind_begin ();
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 ();
2bfa3d3e 870 dynwind_end ();
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. */
2bfa3d3e 879 dynwind_end ();
1fc4d463 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;
2bfa3d3e 1204 dynwind_begin ();
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
2bfa3d3e 1264 dynwind_end ();
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);
2bfa3d3e 1366 dynwind_begin ();
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
2bfa3d3e 1374 dynwind_end ();
af89e871 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
2bfa3d3e 1400 dynwind_begin ();
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";
2bfa3d3e 1409 dynwind_end ();
742f715d
KH
1410 return Qnil;
1411 }
63c414df 1412
f0177f86
EZ
1413 block_input ();
1414
78589e07
RS
1415 /* Create a tree of widget_value objects
1416 representing the panes and their items. */
5668fb88 1417 wv = make_widget_value ("menu", NULL, true, Qnil);
3427a3db 1418 wv->button_type = BUTTON_TYPE_NONE;
78589e07 1419 first_wv = wv;
78c8278d 1420 first_pane = 1;
177c0ea7 1421
78589e07
RS
1422 /* Loop over all panes and items, filling in the tree. */
1423 i = 0;
1424 while (i < menu_items_used)
1425 {
28be1ada 1426 if (EQ (AREF (menu_items, i), Qnil))
101bb4a5
RS
1427 {
1428 submenu_stack[submenu_depth++] = save_wv;
1429 save_wv = prev_wv;
1430 prev_wv = 0;
78c8278d 1431 first_pane = 1;
101bb4a5
RS
1432 i++;
1433 }
28be1ada 1434 else if (EQ (AREF (menu_items, i), Qlambda))
101bb4a5
RS
1435 {
1436 prev_wv = save_wv;
1437 save_wv = submenu_stack[--submenu_depth];
78c8278d 1438 first_pane = 0;
101bb4a5
RS
1439 i++;
1440 }
28be1ada 1441 else if (EQ (AREF (menu_items, i), Qt)
101bb4a5
RS
1442 && submenu_depth != 0)
1443 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
1444 /* Ignore a nil in the item list.
1445 It's meaningful only for dialog boxes. */
28be1ada 1446 else if (EQ (AREF (menu_items, i), Qquote))
fcaa7665 1447 i += 1;
28be1ada 1448 else if (EQ (AREF (menu_items, i), Qt))
78589e07
RS
1449 {
1450 /* Create a new pane. */
1451 Lisp_Object pane_name, prefix;
6d1f7fee 1452 const char *pane_string;
177c0ea7 1453
4c329aa8
GM
1454 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1455 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
177c0ea7 1456
703dc2a8 1457#ifndef HAVE_MULTILINGUAL_MENU
4c329aa8
GM
1458 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1459 {
8af9fa55 1460 pane_name = ENCODE_MENU_STRING (pane_name);
3ae565b3 1461 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
4c329aa8 1462 }
703dc2a8 1463#endif
78589e07 1464 pane_string = (NILP (pane_name)
51b59d79 1465 ? "" : SSDATA (pane_name));
101bb4a5 1466 /* If there is just one top-level pane, put all its items directly
78589e07
RS
1467 under the top-level menu. */
1468 if (menu_items_n_panes == 1)
1469 pane_string = "";
1470
1471 /* If the pane has a meaningful name,
1472 make the pane a top-level menu item
1473 with its items as a submenu beneath it. */
cfd794af 1474 if (!(menuflags & MENU_KEYMAPS) && strcmp (pane_string, ""))
78589e07 1475 {
5668fb88 1476 wv = make_widget_value (pane_string, NULL, true, Qnil);
78589e07
RS
1477 if (save_wv)
1478 save_wv->next = wv;
1479 else
1480 first_wv->contents = wv;
cfd794af 1481 if ((menuflags & MENU_KEYMAPS) && !NILP (prefix))
78589e07 1482 wv->name++;
3427a3db 1483 wv->button_type = BUTTON_TYPE_NONE;
78c8278d
RS
1484 save_wv = wv;
1485 prev_wv = 0;
78589e07 1486 }
78c8278d
RS
1487 else if (first_pane)
1488 {
1489 save_wv = wv;
1490 prev_wv = 0;
1491 }
1492 first_pane = 0;
78589e07
RS
1493 i += MENU_ITEMS_PANE_LENGTH;
1494 }
1495 else
1496 {
1497 /* Create a new item within current pane. */
9cd50434 1498 Lisp_Object item_name, enable, descrip, def, type, selected, help;
4c329aa8
GM
1499 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1500 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1501 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1502 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1503 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1504 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1505 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3427a3db 1506
703dc2a8 1507#ifndef HAVE_MULTILINGUAL_MENU
3427a3db 1508 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
4c329aa8 1509 {
646f98ec 1510 item_name = ENCODE_MENU_STRING (item_name);
3ae565b3 1511 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
4c329aa8 1512 }
177c0ea7 1513
3427a3db 1514 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
4c329aa8 1515 {
646f98ec 1516 descrip = ENCODE_MENU_STRING (descrip);
3ae565b3 1517 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
4c329aa8
GM
1518 }
1519#endif /* not HAVE_MULTILINGUAL_MENU */
177c0ea7 1520
5668fb88
DA
1521 wv = make_widget_value (SSDATA (item_name), NULL, !NILP (enable),
1522 STRINGP (help) ? help : Qnil);
177c0ea7 1523 if (prev_wv)
78589e07 1524 prev_wv->next = wv;
177c0ea7 1525 else
78589e07 1526 save_wv->contents = wv;
78589e07 1527 if (!NILP (descrip))
51b59d79 1528 wv->key = SSDATA (descrip);
a352a815
RS
1529 /* If this item has a null value,
1530 make the call_data null so that it won't display a box
1531 when the mouse is on it. */
4939150c 1532 wv->call_data = !NILP (def) ? aref_addr (menu_items, i) : 0;
3427a3db
GM
1533
1534 if (NILP (type))
1535 wv->button_type = BUTTON_TYPE_NONE;
1536 else if (EQ (type, QCtoggle))
1537 wv->button_type = BUTTON_TYPE_TOGGLE;
1538 else if (EQ (type, QCradio))
1539 wv->button_type = BUTTON_TYPE_RADIO;
1540 else
1088b922 1541 emacs_abort ();
3427a3db
GM
1542
1543 wv->selected = !NILP (selected);
cc45fb40 1544
78589e07
RS
1545 prev_wv = wv;
1546
1547 i += MENU_ITEMS_ITEM_LENGTH;
1548 }
1549 }
1550
c98fcf4b 1551 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
1552 if (!NILP (title))
1553 {
5668fb88
DA
1554 widget_value *wv_title;
1555 widget_value *wv_sep1 = make_widget_value ("--", NULL, false, Qnil);
1556 widget_value *wv_sep2 = make_widget_value ("--", NULL, false, Qnil);
4dedbfe0 1557
4dedbfe0 1558 wv_sep2->next = first_wv->contents;
4dedbfe0
PR
1559 wv_sep1->next = wv_sep2;
1560
703dc2a8
KH
1561#ifndef HAVE_MULTILINGUAL_MENU
1562 if (STRING_MULTIBYTE (title))
646f98ec 1563 title = ENCODE_MENU_STRING (title);
703dc2a8 1564#endif
177c0ea7 1565
5668fb88 1566 wv_title = make_widget_value (SSDATA (title), NULL, true, Qnil);
3427a3db 1567 wv_title->button_type = BUTTON_TYPE_NONE;
a9d8395f 1568 wv_title->next = wv_sep1;
4dedbfe0
PR
1569 first_wv->contents = wv_title;
1570 }
1571
78589e07
RS
1572 /* No selection has been chosen yet. */
1573 menu_item_selection = 0;
1574
ba24cea2
YM
1575 /* Make sure to free the widget_value objects we used to specify the
1576 contents even with longjmp. */
27e498e6 1577 record_unwind_protect_ptr (cleanup_widget_value_tree, first_wv);
ba24cea2 1578
488dd4c4 1579 /* Actually create and show the menu until popped down. */
cfd794af
DA
1580 create_and_show_popup_menu (f, first_wv, x, y,
1581 menuflags & MENU_FOR_CLICK);
18686d47 1582
2bfa3d3e 1583 dynwind_end ();
18686d47 1584
78589e07
RS
1585 /* Find the selected item, and its pane, to return
1586 the proper value. */
1587 if (menu_item_selection != 0)
1588 {
c63f6952 1589 Lisp_Object prefix, entry;
78589e07 1590
6bbd7a29 1591 prefix = entry = Qnil;
78589e07
RS
1592 i = 0;
1593 while (i < menu_items_used)
1594 {
28be1ada 1595 if (EQ (AREF (menu_items, i), Qnil))
101bb4a5
RS
1596 {
1597 subprefix_stack[submenu_depth++] = prefix;
1598 prefix = entry;
1599 i++;
1600 }
28be1ada 1601 else if (EQ (AREF (menu_items, i), Qlambda))
101bb4a5
RS
1602 {
1603 prefix = subprefix_stack[--submenu_depth];
1604 i++;
1605 }
28be1ada 1606 else if (EQ (AREF (menu_items, i), Qt))
78589e07
RS
1607 {
1608 prefix
28be1ada 1609 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
78589e07
RS
1610 i += MENU_ITEMS_PANE_LENGTH;
1611 }
d31d42cc
RS
1612 /* Ignore a nil in the item list.
1613 It's meaningful only for dialog boxes. */
28be1ada 1614 else if (EQ (AREF (menu_items, i), Qquote))
d31d42cc 1615 i += 1;
78589e07
RS
1616 else
1617 {
1618 entry
28be1ada 1619 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
4939150c 1620 if (menu_item_selection == aref_addr (menu_items, i))
78589e07 1621 {
cfd794af 1622 if (menuflags & MENU_KEYMAPS)
78589e07 1623 {
101bb4a5
RS
1624 int j;
1625
6c6f1994 1626 entry = list1 (entry);
78589e07
RS
1627 if (!NILP (prefix))
1628 entry = Fcons (prefix, entry);
101bb4a5 1629 for (j = submenu_depth - 1; j >= 0; j--)
e48087b7 1630 if (!NILP (subprefix_stack[j]))
5964e450 1631 entry = Fcons (subprefix_stack[j], entry);
78589e07 1632 }
f0177f86 1633 unblock_input ();
78589e07
RS
1634 return entry;
1635 }
1636 i += MENU_ITEMS_ITEM_LENGTH;
1637 }
1638 }
1639 }
cfd794af 1640 else if (!(menuflags & MENU_FOR_CLICK))
f0177f86
EZ
1641 {
1642 unblock_input ();
1643 /* Make "Cancel" equivalent to C-g. */
1644 Fsignal (Qquit, Qnil);
1645 }
78589e07 1646
f0177f86 1647 unblock_input ();
78589e07 1648 return Qnil;
18686d47 1649}
4dedbfe0 1650\f
488dd4c4
JD
1651#ifdef USE_GTK
1652static void
971de7fb 1653dialog_selection_callback (GtkWidget *widget, gpointer client_data)
488dd4c4 1654{
8ac068ac 1655 /* Treat the pointer as an integer. There's no problem
488dd4c4 1656 as long as pointers have enough bits to hold small integers. */
d01a7826 1657 if ((intptr_t) client_data != -1)
7d652d97 1658 menu_item_selection = client_data;
488dd4c4
JD
1659
1660 popup_activated_flag = 0;
1661}
1662
1663/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1664 dialog pops down.
1665 menu_item_selection will be set to the selection. */
1666static void
a10c8269 1667create_and_show_dialog (struct frame *f, widget_value *first_wv)
488dd4c4
JD
1668{
1669 GtkWidget *menu;
1670
65b02bb9 1671 eassert (FRAME_X_P (f));
62af879c 1672
488dd4c4
JD
1673 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
1674 G_CALLBACK (dialog_selection_callback),
1675 G_CALLBACK (popup_deactivate_callback),
1676 0);
1677
1678 if (menu)
1679 {
2bfa3d3e 1680 dynwind_begin ();
27e498e6 1681 record_unwind_protect_ptr (pop_down_menu, menu);
af89e871 1682
488dd4c4
JD
1683 /* Display the menu. */
1684 gtk_widget_show_all (menu);
1685
1686 /* Process events that apply to the menu. */
f1d1cd24 1687 popup_widget_loop (1, menu);
177c0ea7 1688
2bfa3d3e 1689 dynwind_end ();
488dd4c4
JD
1690 }
1691}
1692
1693#else /* not USE_GTK */
4dedbfe0 1694static void
ebd15611 1695dialog_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
4dedbfe0 1696{
4475bec4 1697 /* Treat the pointer as an integer. There's no problem
01d5e892 1698 as long as pointers have enough bits to hold small integers. */
4475bec4 1699 if ((intptr_t) client_data != -1)
7d652d97 1700 menu_item_selection = client_data;
488dd4c4 1701
4d7e6e51 1702 block_input ();
4dedbfe0 1703 lw_destroy_all_widgets (id);
4d7e6e51 1704 unblock_input ();
9572375b 1705 popup_activated_flag = 0;
4dedbfe0 1706}
18686d47 1707
488dd4c4 1708
488dd4c4
JD
1709/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1710 dialog pops down.
1711 menu_item_selection will be set to the selection. */
1712static void
a10c8269 1713create_and_show_dialog (struct frame *f, widget_value *first_wv)
488dd4c4
JD
1714{
1715 LWLIB_ID dialog_id;
1716
65b02bb9 1717 eassert (FRAME_X_P (f));
62af879c 1718
488dd4c4 1719 dialog_id = widget_id_tick++;
52214050 1720#ifdef USE_LUCID
1ecb2d3f
JD
1721 apply_systemfont_to_dialog (f->output_data.x->widget);
1722#endif
488dd4c4
JD
1723 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1724 f->output_data.x->widget, 1, 0,
1725 dialog_selection_callback, 0, 0);
1726 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
488dd4c4
JD
1727 /* Display the dialog box. */
1728 lw_pop_up_all_widgets (dialog_id);
1729 popup_activated_flag = 1;
98a20c65 1730 x_activate_timeout_atimer ();
488dd4c4
JD
1731
1732 /* Process events that apply to the dialog box.
1733 Also handle timers. */
1734 {
2bfa3d3e 1735 dynwind_begin ();
488dd4c4 1736 int fact = 4 * sizeof (LWLIB_ID);
177c0ea7 1737
488dd4c4 1738 /* xdialog_show_unwind is responsible for popping the dialog box down. */
af89e871 1739 record_unwind_protect (pop_down_menu,
488dd4c4
JD
1740 Fcons (make_number (dialog_id >> (fact)),
1741 make_number (dialog_id & ~(-1 << (fact)))));
1742
aad3612f 1743 popup_get_selection (0, FRAME_DISPLAY_INFO (f), dialog_id, 1);
488dd4c4 1744
2bfa3d3e 1745 dynwind_end ();
488dd4c4
JD
1746 }
1747}
1748
1749#endif /* not USE_GTK */
1750
42ca4633 1751static const char * button_names [] = {
165e1749
FP
1752 "button1", "button2", "button3", "button4", "button5",
1753 "button6", "button7", "button8", "button9", "button10" };
1754
1755static Lisp_Object
f1b44793
DA
1756x_dialog_show (struct frame *f, Lisp_Object title,
1757 Lisp_Object header, const char **error_name)
165e1749
FP
1758{
1759 int i, nb_buttons=0;
80670155 1760 char dialog_name[6];
165e1749 1761
faa935b6 1762 widget_value *wv, *first_wv = 0, *prev_wv = 0;
165e1749 1763
fcaa7665
RS
1764 /* Number of elements seen so far, before boundary. */
1765 int left_count = 0;
1766 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
1767 int boundary_seen = 0;
1768
2bfa3d3e 1769 dynwind_begin ();
ba24cea2 1770
65b02bb9 1771 eassert (FRAME_X_P (f));
62af879c 1772
6a040d6a 1773 *error_name = NULL;
165e1749 1774
80670155
RS
1775 if (menu_items_n_panes > 1)
1776 {
6a040d6a 1777 *error_name = "Multiple panes in dialog box";
2bfa3d3e 1778 dynwind_end ();
80670155
RS
1779 return Qnil;
1780 }
1781
165e1749
FP
1782 /* Create a tree of widget_value objects
1783 representing the text label and buttons. */
1784 {
f1b44793 1785 Lisp_Object pane_name;
6d1f7fee 1786 const char *pane_string;
28be1ada 1787 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
165e1749 1788 pane_string = (NILP (pane_name)
51b59d79 1789 ? "" : SSDATA (pane_name));
5668fb88 1790 prev_wv = make_widget_value ("message", (char *) pane_string, true, Qnil);
165e1749 1791 first_wv = prev_wv;
177c0ea7 1792
165e1749
FP
1793 /* Loop over all panes and items, filling in the tree. */
1794 i = MENU_ITEMS_PANE_LENGTH;
1795 while (i < menu_items_used)
1796 {
177c0ea7 1797
165e1749
FP
1798 /* Create a new item within current pane. */
1799 Lisp_Object item_name, enable, descrip;
28be1ada
DA
1800 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1801 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
165e1749 1802 descrip
28be1ada 1803 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
177c0ea7 1804
80670155
RS
1805 if (NILP (item_name))
1806 {
1807 free_menubar_widget_value_tree (first_wv);
6a040d6a 1808 *error_name = "Submenu in dialog items";
2bfa3d3e 1809 dynwind_end ();
80670155
RS
1810 return Qnil;
1811 }
fcaa7665
RS
1812 if (EQ (item_name, Qquote))
1813 {
1814 /* This is the boundary between left-side elts
1815 and right-side elts. Stop incrementing right_count. */
1816 boundary_seen = 1;
1817 i++;
1818 continue;
1819 }
86e71abf 1820 if (nb_buttons >= 9)
80670155
RS
1821 {
1822 free_menubar_widget_value_tree (first_wv);
6a040d6a 1823 *error_name = "Too many dialog items";
2bfa3d3e 1824 dynwind_end ();
80670155
RS
1825 return Qnil;
1826 }
1827
5668fb88
DA
1828 wv = make_widget_value (button_names[nb_buttons],
1829 SSDATA (item_name),
1830 !NILP (enable), Qnil);
165e1749 1831 prev_wv->next = wv;
165e1749 1832 if (!NILP (descrip))
51b59d79 1833 wv->key = SSDATA (descrip);
4939150c 1834 wv->call_data = aref_addr (menu_items, i);
165e1749
FP
1835 prev_wv = wv;
1836
fcaa7665
RS
1837 if (! boundary_seen)
1838 left_count++;
1839
165e1749
FP
1840 nb_buttons++;
1841 i += MENU_ITEMS_ITEM_LENGTH;
1842 }
1843
fcaa7665
RS
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;
1848
5668fb88 1849 wv = make_widget_value (dialog_name, NULL, false, Qnil);
6a040d6a
NR
1850
1851 /* Frame title: 'Q' = Question, 'I' = Information.
1852 Can also have 'E' = Error if, one day, we want
1853 a popup for errors. */
5e617bc2 1854 if (NILP (header))
6a040d6a
NR
1855 dialog_name[0] = 'Q';
1856 else
1857 dialog_name[0] = 'I';
1858
80670155
RS
1859 /* Dialog boxes use a really stupid name encoding
1860 which specifies how many buttons to use
6a040d6a 1861 and how many buttons are on the right. */
80670155
RS
1862 dialog_name[1] = '0' + nb_buttons;
1863 dialog_name[2] = 'B';
1864 dialog_name[3] = 'R';
fcaa7665
RS
1865 /* Number of buttons to put on the right. */
1866 dialog_name[4] = '0' + nb_buttons - left_count;
80670155 1867 dialog_name[5] = 0;
165e1749
FP
1868 wv->contents = first_wv;
1869 first_wv = wv;
165e1749
FP
1870 }
1871
165e1749
FP
1872 /* No selection has been chosen yet. */
1873 menu_item_selection = 0;
1874
ba24cea2
YM
1875 /* Make sure to free the widget_value objects we used to specify the
1876 contents even with longjmp. */
27e498e6 1877 record_unwind_protect_ptr (cleanup_widget_value_tree, first_wv);
ba24cea2 1878
488dd4c4
JD
1879 /* Actually create and show the dialog. */
1880 create_and_show_dialog (f, first_wv);
f02cac82 1881
2bfa3d3e 1882 dynwind_end ();
177c0ea7 1883
488dd4c4
JD
1884 /* Find the selected item, and its pane, to return
1885 the proper value. */
165e1749
FP
1886 if (menu_item_selection != 0)
1887 {
165e1749
FP
1888 i = 0;
1889 while (i < menu_items_used)
1890 {
1891 Lisp_Object entry;
1892
28be1ada 1893 if (EQ (AREF (menu_items, i), Qt))
f1b44793 1894 i += MENU_ITEMS_PANE_LENGTH;
28be1ada 1895 else if (EQ (AREF (menu_items, i), Qquote))
85996cfb
GM
1896 {
1897 /* This is the boundary between left-side elts and
1898 right-side elts. */
1899 ++i;
1900 }
165e1749
FP
1901 else
1902 {
1903 entry
28be1ada 1904 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
4939150c 1905 if (menu_item_selection == aref_addr (menu_items, i))
f1b44793 1906 return entry;
165e1749
FP
1907 i += MENU_ITEMS_ITEM_LENGTH;
1908 }
1909 }
1910 }
9f6fcdc5
JD
1911 else
1912 /* Make "Cancel" equivalent to C-g. */
1913 Fsignal (Qquit, Qnil);
165e1749
FP
1914
1915 return Qnil;
1916}
ba461919 1917
0afa0aab
EZ
1918Lisp_Object
1919xw_popup_dialog (struct frame *f, Lisp_Object header, Lisp_Object contents)
1920{
1921 Lisp_Object title;
1922 const char *error_name;
1923 Lisp_Object selection;
1924 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
1925
1926 check_window_system (f);
1927
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);
1932
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));
1938
1939 list_of_panes (list1 (contents));
1940
1941 /* Display them in a dialog box. */
1942 block_input ();
f1b44793 1943 selection = x_dialog_show (f, title, header, &error_name);
0afa0aab
EZ
1944 unblock_input ();
1945
1946 unbind_to (specpdl_count, Qnil);
1947 discard_menu_items ();
1948
1949 if (error_name) error ("%s", error_name);
1950 return selection;
1951}
1952
488dd4c4 1953#else /* not USE_X_TOOLKIT && not USE_GTK */
78589e07 1954
3e703b25
GM
1955/* The frame of the last activated non-toolkit menu bar.
1956 Used to generate menu help events. */
1957
1958static struct frame *menu_help_frame;
1959
1960
62145073
GM
1961/* Show help HELP_STRING, or clear help if HELP_STRING is null.
1962
1963 PANE is the pane number, and ITEM is the menu item number in
1964 the menu (currently not used).
177c0ea7 1965
62145073
GM
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. */
3e703b25
GM
1969
1970static void
eb18f6cc 1971menu_help_callback (char const *help_string, int pane, int item)
3e703b25 1972{
62145073
GM
1973 Lisp_Object *first_item;
1974 Lisp_Object pane_name;
1975 Lisp_Object menu_object;
177c0ea7 1976
91f2d272 1977 first_item = XVECTOR (menu_items)->contents;
62145073
GM
1978 if (EQ (first_item[0], Qt))
1979 pane_name = first_item[MENU_ITEMS_PANE_NAME];
1980 else if (EQ (first_item[0], Qquote))
cfd794af 1981 /* This shouldn't happen, see x_menu_show. */
ce33e8eb 1982 pane_name = empty_unibyte_string;
62145073
GM
1983 else
1984 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
177c0ea7 1985
62145073 1986 /* (menu-item MENU-NAME PANE-NUMBER) */
6c6f1994 1987 menu_object = list3 (Qmenu_item, pane_name, make_number (pane));
ba461919 1988 show_help_echo (help_string ? build_string (help_string) : Qnil,
f868cd8a 1989 Qnil, menu_object, make_number (item));
3e703b25 1990}
177c0ea7 1991
27e498e6 1992static void
0521f580 1993pop_down_menu (Lisp_Object arg)
af89e871 1994{
a10c8269 1995 struct frame *f = XSAVE_POINTER (arg, 0);
3346c1d0 1996 XMenu *menu = XSAVE_POINTER (arg, 1);
af89e871 1997
4d7e6e51 1998 block_input ();
af89e871 1999#ifndef MSDOS
a130b901
JD
2000 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
2001 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
af89e871 2002#endif
a130b901 2003 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
af89e871
JD
2004
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. */
aad3612f 2008 x_mouse_leave (FRAME_DISPLAY_INFO (f));
af89e871
JD
2009
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. */
aad3612f 2014 FRAME_DISPLAY_INFO (f)->grabbed = 0;
af89e871
JD
2015
2016#endif /* HAVE_X_WINDOWS */
2017
4d7e6e51 2018 unblock_input ();
af89e871
JD
2019}
2020
3e703b25 2021
1fb99a3a 2022Lisp_Object
cfd794af
DA
2023x_menu_show (struct frame *f, int x, int y, int menuflags,
2024 Lisp_Object title, const char **error_name)
dcfdbac7 2025{
177c0ea7 2026 Window root;
78589e07
RS
2027 XMenu *menu;
2028 int pane, selidx, lpane, status;
2029 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
2030 char *datap;
2031 int ulx, uly, width, height;
2032 int dispwidth, dispheight;
453a4f1b 2033 int i, j, lines, maxlines;
4e8d3549 2034 int maxwidth;
78589e07
RS
2035 int dummy_int;
2036 unsigned int dummy_uint;
2bfa3d3e 2037 dynwind_begin ();
088831f6 2038
65b02bb9 2039 eassert (FRAME_X_P (f) || FRAME_MSDOS_P (f));
62af879c 2040
bf501fb9 2041 *error_name = 0;
2bfa3d3e
BT
2042 if (menu_items_n_panes == 0) {
2043 dynwind_end ();
78589e07 2044 return Qnil;
2bfa3d3e 2045 }
088831f6 2046
742f715d
KH
2047 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2048 {
bf501fb9 2049 *error_name = "Empty menu";
2bfa3d3e 2050 dynwind_end ();
742f715d
KH
2051 return Qnil;
2052 }
2053
f0177f86
EZ
2054 block_input ();
2055
78589e07 2056 /* Figure out which root window F is on. */
92280f67 2057 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
2058 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2059 &dummy_uint, &dummy_uint);
18686d47 2060
78589e07 2061 /* Make the menu on that window. */
92280f67 2062 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 2063 if (menu == NULL)
dcfdbac7 2064 {
bf501fb9 2065 *error_name = "Can't create menu";
f0177f86 2066 unblock_input ();
2bfa3d3e 2067 dynwind_end ();
78589e07 2068 return Qnil;
dcfdbac7 2069 }
78589e07 2070
87485d6f 2071#ifdef HAVE_X_WINDOWS
78589e07 2072 /* Adjust coordinates to relative to the outer (window manager) window. */
453a4f1b
JD
2073 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
2074 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
87485d6f 2075#endif /* HAVE_X_WINDOWS */
78589e07
RS
2076
2077 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
2078 x += f->left_pos;
2079 y += f->top_pos;
177c0ea7 2080
78589e07 2081 /* Create all the necessary panes and their items. */
eef9bc79
PE
2082 maxwidth = maxlines = lines = i = 0;
2083 lpane = XM_FAILURE;
78589e07 2084 while (i < menu_items_used)
dcfdbac7 2085 {
28be1ada 2086 if (EQ (AREF (menu_items, i), Qt))
dcfdbac7 2087 {
78589e07
RS
2088 /* Create a new pane. */
2089 Lisp_Object pane_name, prefix;
6d1f7fee 2090 const char *pane_string;
78589e07 2091
453a4f1b
JD
2092 maxlines = max (maxlines, lines);
2093 lines = 0;
28be1ada
DA
2094 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2095 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
78589e07 2096 pane_string = (NILP (pane_name)
51b59d79 2097 ? "" : SSDATA (pane_name));
cfd794af 2098 if ((menuflags & MENU_KEYMAPS) && !NILP (prefix))
78589e07
RS
2099 pane_string++;
2100
92280f67 2101 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
2102 if (lpane == XM_FAILURE)
2103 {
92280f67 2104 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
bf501fb9 2105 *error_name = "Can't create pane";
f0177f86 2106 unblock_input ();
2bfa3d3e 2107 dynwind_end ();
78589e07
RS
2108 return Qnil;
2109 }
2110 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
2111
2112 /* Find the width of the widest item in this pane. */
4e8d3549
RS
2113 j = i;
2114 while (j < menu_items_used)
2115 {
2116 Lisp_Object item;
28be1ada 2117 item = AREF (menu_items, j);
4e8d3549
RS
2118 if (EQ (item, Qt))
2119 break;
2120 if (NILP (item))
2121 {
2122 j++;
2123 continue;
2124 }
d5db4077 2125 width = SBYTES (item);
4e8d3549
RS
2126 if (width > maxwidth)
2127 maxwidth = width;
2128
2129 j += MENU_ITEMS_ITEM_LENGTH;
2130 }
dcfdbac7 2131 }
fcaa7665
RS
2132 /* Ignore a nil in the item list.
2133 It's meaningful only for dialog boxes. */
28be1ada 2134 else if (EQ (AREF (menu_items, i), Qquote))
fcaa7665 2135 i += 1;
78589e07 2136 else
dcfdbac7 2137 {
78589e07 2138 /* Create a new item within current pane. */
3e703b25 2139 Lisp_Object item_name, enable, descrip, help;
eb18f6cc
PE
2140 char *item_data;
2141 char const *help_string;
78589e07 2142
28be1ada
DA
2143 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2144 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
78589e07 2145 descrip
28be1ada
DA
2146 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2147 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
eb18f6cc 2148 help_string = STRINGP (help) ? SSDATA (help) : NULL;
177c0ea7 2149
78589e07 2150 if (!NILP (descrip))
4e8d3549 2151 {
4e8d3549
RS
2152 /* if alloca is fast, use that to make the space,
2153 to reduce gc needs. */
38182d90 2154 item_data = alloca (maxwidth + SBYTES (descrip) + 1);
eb18f6cc 2155 memcpy (item_data, SSDATA (item_name), SBYTES (item_name));
d5db4077 2156 for (j = SCHARS (item_name); j < maxwidth; j++)
4e8d3549 2157 item_data[j] = ' ';
eb18f6cc 2158 memcpy (item_data + j, SSDATA (descrip), SBYTES (descrip));
d5db4077 2159 item_data[j + SBYTES (descrip)] = 0;
4e8d3549
RS
2160 }
2161 else
eb18f6cc 2162 item_data = SSDATA (item_name);
78589e07 2163
eef9bc79
PE
2164 if (lpane == XM_FAILURE
2165 || (XMenuAddSelection (FRAME_X_DISPLAY (f),
2166 menu, lpane, 0, item_data,
2167 !NILP (enable), help_string)
2168 == XM_FAILURE))
dcfdbac7 2169 {
92280f67 2170 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
bf501fb9 2171 *error_name = "Can't add selection to menu";
f0177f86 2172 unblock_input ();
2bfa3d3e 2173 dynwind_end ();
78589e07 2174 return Qnil;
dcfdbac7 2175 }
78589e07 2176 i += MENU_ITEMS_ITEM_LENGTH;
453a4f1b 2177 lines++;
dcfdbac7
JB
2178 }
2179 }
4e8d3549 2180
453a4f1b
JD
2181 maxlines = max (maxlines, lines);
2182
78589e07 2183 /* All set and ready to fly. */
92280f67 2184 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
63685b9d
GM
2185 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2186 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
78589e07
RS
2187 x = min (x, dispwidth);
2188 y = min (y, dispheight);
2189 x = max (x, 1);
2190 y = max (y, 1);
92280f67 2191 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
2192 &ulx, &uly, &width, &height);
2193 if (ulx+width > dispwidth)
2194 {
78589e07 2195 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
2196 ulx = dispwidth - width;
2197 }
2198 if (uly+height > dispheight)
2199 {
78589e07 2200 y -= (uly + height) - dispheight;
dcfdbac7
JB
2201 uly = dispheight - height;
2202 }
2de7397f
EZ
2203#ifndef HAVE_X_WINDOWS
2204 if (FRAME_HAS_MINIBUF_P (f) && uly+height > dispheight - 1)
2205 {
2206 /* Move the menu away of the echo area, to avoid overwriting the
2207 menu with help echo messages or vice versa. */
2208 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
2209 {
2210 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2211 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2212 }
2213 else
2214 {
2215 y--;
2216 uly--;
2217 }
2218 }
2219#endif
78589e07
RS
2220 if (ulx < 0) x -= ulx;
2221 if (uly < 0) y -= uly;
121e4555 2222
cfd794af 2223 if (!(menuflags & MENU_FOR_CLICK))
453a4f1b
JD
2224 {
2225 /* If position was not given by a mouse click, adjust so upper left
2226 corner of the menu as a whole ends up at given coordinates. This
2227 is what x-popup-menu says in its documentation. */
2228 x += width/2;
2229 y += 1.5*height/(maxlines+2);
2230 }
2231
121e4555 2232 XMenuSetAEQ (menu, TRUE);
78589e07
RS
2233 XMenuSetFreeze (menu, TRUE);
2234 pane = selidx = 0;
3e703b25 2235
c3438661
JD
2236#ifndef MSDOS
2237 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
2238#endif
59cfb104 2239
1396ac86 2240 record_unwind_protect (pop_down_menu, make_save_ptr_ptr (f, menu));
c3438661 2241
3e703b25
GM
2242 /* Help display under X won't work because XMenuActivate contains
2243 a loop that doesn't give Emacs a chance to process it. */
2244 menu_help_frame = f;
92280f67 2245 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
af89e871
JD
2246 x, y, ButtonReleaseMask, &datap,
2247 menu_help_callback);
eef9bc79 2248 entry = pane_prefix = Qnil;
a352a815 2249
dcfdbac7
JB
2250 switch (status)
2251 {
2252 case XM_SUCCESS:
2253#ifdef XDEBUG
2254 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2255#endif
fa6d54d9 2256
78589e07
RS
2257 /* Find the item number SELIDX in pane number PANE. */
2258 i = 0;
2259 while (i < menu_items_used)
fa6d54d9 2260 {
28be1ada 2261 if (EQ (AREF (menu_items, i), Qt))
088831f6 2262 {
78589e07
RS
2263 if (pane == 0)
2264 pane_prefix
28be1ada 2265 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
78589e07
RS
2266 pane--;
2267 i += MENU_ITEMS_PANE_LENGTH;
088831f6 2268 }
78589e07 2269 else
ab6ee1a0 2270 {
78589e07 2271 if (pane == -1)
ab6ee1a0 2272 {
78589e07 2273 if (selidx == 0)
ab6ee1a0 2274 {
78589e07 2275 entry
28be1ada 2276 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
cfd794af 2277 if (menuflags & MENU_KEYMAPS)
ab6ee1a0 2278 {
6c6f1994 2279 entry = list1 (entry);
78589e07
RS
2280 if (!NILP (pane_prefix))
2281 entry = Fcons (pane_prefix, entry);
ab6ee1a0 2282 }
78589e07 2283 break;
ab6ee1a0 2284 }
78589e07 2285 selidx--;
ab6ee1a0 2286 }
78589e07 2287 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
2288 }
2289 }
78589e07 2290 break;
dcfdbac7 2291
78589e07 2292 case XM_FAILURE:
bf501fb9 2293 *error_name = "Can't activate menu";
78589e07 2294 case XM_IA_SELECT:
9f6fcdc5 2295 break;
78589e07 2296 case XM_NO_SELECT:
be6ed24a
RS
2297 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2298 the menu was invoked with a mouse event as POSITION). */
cfd794af 2299 if (!(menuflags & MENU_FOR_CLICK))
f0177f86
EZ
2300 {
2301 unblock_input ();
2302 Fsignal (Qquit, Qnil);
2303 }
78589e07 2304 break;
dcfdbac7 2305 }
a5285df3 2306
f0177f86 2307 unblock_input ();
2bfa3d3e 2308 dynwind_end ();
a5285df3 2309
78589e07 2310 return entry;
dcfdbac7 2311}
4dedbfe0 2312
78589e07 2313#endif /* not USE_X_TOOLKIT */
1e659e4c 2314
d009ae66
EZ
2315#ifndef MSDOS
2316/* Detect if a dialog or menu has been posted. MSDOS has its own
2317 implementation on msdos.c. */
b79b8a1c 2318
077288cf 2319int ATTRIBUTE_CONST
971de7fb 2320popup_activated (void)
b79b8a1c
CY
2321{
2322 return popup_activated_flag;
2323}
d009ae66 2324#endif /* not MSDOS */
e3135734
CY
2325
2326/* The following is used by delayed window autoselection. */
2327
2328DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
2329 doc: /* Return t if a menu or popup dialog is active. */)
5842a27b 2330 (void)
e3135734 2331{
e3135734 2332 return (popup_activated ()) ? Qt : Qnil;
e3135734 2333}
088831f6 2334\f
dfcf069d 2335void
971de7fb 2336syms_of_xmenu (void)
dcfdbac7 2337{
fe6aa7a1
BT
2338#include "xmenu.x"
2339
cd3520a4 2340 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
0314aacb 2341
8ed87156 2342#ifdef USE_X_TOOLKIT
177c0ea7 2343 widget_id_tick = (1<<16);
88766961 2344 next_menubar_widget_id = 1;
8ed87156
RS
2345#endif
2346
22badffe 2347#if defined (USE_GTK) || defined (USE_X_TOOLKIT)
d67b4f80
DN
2348 Ffset (intern_c_string ("accelerate-menu"),
2349 intern_c_string (Sx_menu_bar_open_internal.symbol_name));
22badffe 2350#endif
dcfdbac7 2351}