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