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