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