merge trunk
[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
73931ad1 226 check_window_system ();
165e1749 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
e74aeda8 979 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->contents;
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
908589fd 1058 for (i = 0; submenu_start[i] >= 0; 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
JD
1413{
1414 popup_activated_flag = 0;
4d7e6e51 1415 block_input ();
2b30549c 1416 gtk_widget_destroy (GTK_WIDGET (XSAVE_POINTER (arg, 0)));
4d7e6e51 1417 unblock_input ();
af89e871
JD
1418 return Qnil;
1419}
1420
488dd4c4
JD
1421/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1422 menu pops down.
1423 menu_item_selection will be set to the selection. */
1424static void
08dc5ae6 1425create_and_show_popup_menu (FRAME_PTR f, widget_value *first_wv, int x, int y,
7cded46f 1426 bool for_click, Time timestamp)
488dd4c4
JD
1427{
1428 int i;
1429 GtkWidget *menu;
1430 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
1431 struct next_popup_x_y popup_x_y;
d311d28c 1432 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
7cded46f 1433 bool use_pos_func = ! for_click;
93d5ca1f
JD
1434
1435#ifdef HAVE_GTK3
1436 /* Always use position function for Gtk3. Otherwise menus may become
1437 too small to show anything. */
1438 use_pos_func = 1;
1439#endif
488dd4c4 1440
62af879c 1441 if (! FRAME_X_P (f))
1088b922 1442 emacs_abort ();
62af879c 1443
488dd4c4
JD
1444 xg_crazy_callback_abort = 1;
1445 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
1446 G_CALLBACK (popup_selection_callback),
1447 G_CALLBACK (popup_deactivate_callback),
1448 G_CALLBACK (menu_highlight_callback));
1449 xg_crazy_callback_abort = 0;
177c0ea7 1450
93d5ca1f 1451 if (use_pos_func)
488dd4c4
JD
1452 {
1453 /* Not invoked by a click. pop up at x/y. */
1454 pos_func = menu_position_func;
1455
1456 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
1457 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1458 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
488dd4c4
JD
1459
1460 popup_x_y.x = x;
1461 popup_x_y.y = y;
7b76ca1c 1462 popup_x_y.f = f;
488dd4c4 1463
9b85e63d
JD
1464 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1465 }
93d5ca1f
JD
1466
1467 if (for_click)
9b85e63d
JD
1468 {
1469 for (i = 0; i < 5; i++)
1470 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
1471 break;
1472 }
59cfb104 1473
488dd4c4
JD
1474 /* Display the menu. */
1475 gtk_widget_show_all (menu);
c0df13a6 1476
a20903d0 1477 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i,
08dc5ae6 1478 timestamp ? timestamp : gtk_get_current_event_time ());
177c0ea7 1479
468afbac 1480 record_unwind_protect (pop_down_menu, make_save_pointer (menu));
af89e871 1481
e547b051 1482 if (gtk_widget_get_mapped (menu))
ff18668f
JD
1483 {
1484 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1485 two. show_help_echo uses this to detect popup menus. */
1486 popup_activated_flag = 1;
1487 /* Process events that apply to the menu. */
1488 popup_widget_loop (1, menu);
1489 }
488dd4c4 1490
af89e871 1491 unbind_to (specpdl_count, Qnil);
177c0ea7 1492
488dd4c4
JD
1493 /* Must reset this manually because the button release event is not passed
1494 to Emacs event loop. */
1495 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
1496}
1497
1498#else /* not USE_GTK */
18686d47 1499
8ed87156 1500/* We need a unique id for each widget handled by the Lucid Widget
cc17e9bf
KH
1501 library.
1502
1503 For the main windows, and popup menus, we use this counter,
88766961 1504 which we increment each time after use. This starts from 1<<16.
cc17e9bf 1505
88766961
RS
1506 For menu bars, we use numbers starting at 0, counted in
1507 next_menubar_widget_id. */
8ed87156 1508LWLIB_ID widget_id_tick;
165e1749 1509
4dedbfe0 1510static void
ebd15611 1511popup_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
4dedbfe0
PR
1512{
1513 menu_item_selection = (Lisp_Object *) client_data;
1514}
1515
af89e871
JD
1516/* ARG is the LWLIB ID of the dialog box, represented
1517 as a Lisp object as (HIGHPART . LOWPART). */
1518
1519static Lisp_Object
ebd15611 1520pop_down_menu (Lisp_Object arg)
af89e871
JD
1521{
1522 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
1523 | XINT (XCDR (arg)));
1524
4d7e6e51 1525 block_input ();
af89e871 1526 lw_destroy_all_widgets (id);
4d7e6e51 1527 unblock_input ();
af89e871
JD
1528 popup_activated_flag = 0;
1529
1530 return Qnil;
1531}
1532
488dd4c4
JD
1533/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1534 menu pops down.
1535 menu_item_selection will be set to the selection. */
1536static void
2b23d2a6 1537create_and_show_popup_menu (FRAME_PTR f, widget_value *first_wv,
7cded46f 1538 int x, int y, bool for_click, Time timestamp)
488dd4c4
JD
1539{
1540 int i;
1541 Arg av[2];
1542 int ac = 0;
65969f63
CY
1543 XEvent dummy;
1544 XButtonPressedEvent *event = &(dummy.xbutton);
488dd4c4
JD
1545 LWLIB_ID menu_id;
1546 Widget menu;
488dd4c4 1547
62af879c 1548 if (! FRAME_X_P (f))
1088b922 1549 emacs_abort ();
62af879c 1550
3928f2b6
JD
1551#ifdef USE_LUCID
1552 apply_systemfont_to_menu (f, f->output_data.x->widget);
1553#endif
1554
488dd4c4
JD
1555 menu_id = widget_id_tick++;
1556 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1557 f->output_data.x->widget, 1, 0,
1558 popup_selection_callback,
1559 popup_deactivate_callback,
1560 menu_highlight_callback);
1561
65969f63
CY
1562 event->type = ButtonPress;
1563 event->serial = 0;
1564 event->send_event = 0;
1565 event->display = FRAME_X_DISPLAY (f);
1566 event->time = CurrentTime;
1567 event->root = FRAME_X_DISPLAY_INFO (f)->root_window;
1568 event->window = event->subwindow = event->root;
1569 event->x = x;
1570 event->y = y;
488dd4c4
JD
1571
1572 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
1573 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1574 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
488dd4c4 1575
65969f63
CY
1576 event->x_root = x;
1577 event->y_root = y;
488dd4c4 1578
65969f63
CY
1579 event->state = 0;
1580 event->button = 0;
488dd4c4
JD
1581 for (i = 0; i < 5; i++)
1582 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
65969f63 1583 event->button = i;
488dd4c4
JD
1584
1585 /* Don't allow any geometry request from the user. */
1586 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1587 XtSetValues (menu, av, ac);
1588
1589 /* Display the menu. */
65969f63 1590 lw_popup_menu (menu, &dummy);
488dd4c4 1591 popup_activated_flag = 1;
98a20c65 1592 x_activate_timeout_atimer ();
59cfb104 1593
af89e871
JD
1594 {
1595 int fact = 4 * sizeof (LWLIB_ID);
d311d28c 1596 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
af89e871
JD
1597 record_unwind_protect (pop_down_menu,
1598 Fcons (make_number (menu_id >> (fact)),
1599 make_number (menu_id & ~(-1 << (fact)))));
488dd4c4 1600
af89e871 1601 /* Process events that apply to the menu. */
95bdef2e 1602 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
488dd4c4 1603
af89e871
JD
1604 unbind_to (specpdl_count, Qnil);
1605 }
488dd4c4
JD
1606}
1607
1608#endif /* not USE_GTK */
1609
ba24cea2
YM
1610static Lisp_Object
1611cleanup_widget_value_tree (Lisp_Object arg)
1612{
2b30549c 1613 free_menubar_widget_value_tree (XSAVE_POINTER (arg, 0));
ba24cea2
YM
1614 return Qnil;
1615}
1616
ef7417fd 1617Lisp_Object
7cded46f 1618xmenu_show (FRAME_PTR f, int x, int y, bool for_click, bool keymaps,
08dc5ae6 1619 Lisp_Object title, const char **error_name, Time timestamp)
18686d47 1620{
78589e07 1621 int i;
78589e07 1622 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
101bb4a5 1623 widget_value **submenu_stack
38182d90 1624 = alloca (menu_items_used * sizeof *submenu_stack);
101bb4a5 1625 Lisp_Object *subprefix_stack
38182d90 1626 = alloca (menu_items_used * sizeof *subprefix_stack);
101bb4a5 1627 int submenu_depth = 0;
4e8d3549 1628
78c8278d
RS
1629 int first_pane;
1630
9898bd0e 1631 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
ba24cea2 1632
62af879c 1633 if (! FRAME_X_P (f))
1088b922 1634 emacs_abort ();
62af879c 1635
d4323972 1636 *error_name = NULL;
78589e07 1637
742f715d
KH
1638 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1639 {
d4323972 1640 *error_name = "Empty menu";
742f715d
KH
1641 return Qnil;
1642 }
63c414df 1643
78589e07
RS
1644 /* Create a tree of widget_value objects
1645 representing the panes and their items. */
f7fab165 1646 wv = xmalloc_widget_value ();
78589e07
RS
1647 wv->name = "menu";
1648 wv->value = 0;
1649 wv->enabled = 1;
3427a3db 1650 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 1651 wv->help =Qnil;
78589e07 1652 first_wv = wv;
78c8278d 1653 first_pane = 1;
177c0ea7 1654
78589e07
RS
1655 /* Loop over all panes and items, filling in the tree. */
1656 i = 0;
1657 while (i < menu_items_used)
1658 {
28be1ada 1659 if (EQ (AREF (menu_items, i), Qnil))
101bb4a5
RS
1660 {
1661 submenu_stack[submenu_depth++] = save_wv;
1662 save_wv = prev_wv;
1663 prev_wv = 0;
78c8278d 1664 first_pane = 1;
101bb4a5
RS
1665 i++;
1666 }
28be1ada 1667 else if (EQ (AREF (menu_items, i), Qlambda))
101bb4a5
RS
1668 {
1669 prev_wv = save_wv;
1670 save_wv = submenu_stack[--submenu_depth];
78c8278d 1671 first_pane = 0;
101bb4a5
RS
1672 i++;
1673 }
28be1ada 1674 else if (EQ (AREF (menu_items, i), Qt)
101bb4a5
RS
1675 && submenu_depth != 0)
1676 i += MENU_ITEMS_PANE_LENGTH;
fcaa7665
RS
1677 /* Ignore a nil in the item list.
1678 It's meaningful only for dialog boxes. */
28be1ada 1679 else if (EQ (AREF (menu_items, i), Qquote))
fcaa7665 1680 i += 1;
28be1ada 1681 else if (EQ (AREF (menu_items, i), Qt))
78589e07
RS
1682 {
1683 /* Create a new pane. */
1684 Lisp_Object pane_name, prefix;
6d1f7fee 1685 const char *pane_string;
177c0ea7 1686
4c329aa8
GM
1687 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1688 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
177c0ea7 1689
703dc2a8 1690#ifndef HAVE_MULTILINGUAL_MENU
4c329aa8
GM
1691 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1692 {
8af9fa55 1693 pane_name = ENCODE_MENU_STRING (pane_name);
3ae565b3 1694 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
4c329aa8 1695 }
703dc2a8 1696#endif
78589e07 1697 pane_string = (NILP (pane_name)
51b59d79 1698 ? "" : SSDATA (pane_name));
101bb4a5 1699 /* If there is just one top-level pane, put all its items directly
78589e07
RS
1700 under the top-level menu. */
1701 if (menu_items_n_panes == 1)
1702 pane_string = "";
1703
1704 /* If the pane has a meaningful name,
1705 make the pane a top-level menu item
1706 with its items as a submenu beneath it. */
78c8278d 1707 if (!keymaps && strcmp (pane_string, ""))
78589e07 1708 {
f7fab165 1709 wv = xmalloc_widget_value ();
78589e07
RS
1710 if (save_wv)
1711 save_wv->next = wv;
1712 else
1713 first_wv->contents = wv;
b00876c9 1714 wv->name = (char *) pane_string;
78589e07
RS
1715 if (keymaps && !NILP (prefix))
1716 wv->name++;
1717 wv->value = 0;
1718 wv->enabled = 1;
3427a3db 1719 wv->button_type = BUTTON_TYPE_NONE;
27ad7b52 1720 wv->help = Qnil;
78c8278d
RS
1721 save_wv = wv;
1722 prev_wv = 0;
78589e07 1723 }
78c8278d
RS
1724 else if (first_pane)
1725 {
1726 save_wv = wv;
1727 prev_wv = 0;
1728 }
1729 first_pane = 0;
78589e07
RS
1730 i += MENU_ITEMS_PANE_LENGTH;
1731 }
1732 else
1733 {
1734 /* Create a new item within current pane. */
9cd50434 1735 Lisp_Object item_name, enable, descrip, def, type, selected, help;
4c329aa8
GM
1736 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1737 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1738 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1739 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1740 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1741 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1742 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
3427a3db 1743
703dc2a8 1744#ifndef HAVE_MULTILINGUAL_MENU
3427a3db 1745 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
4c329aa8 1746 {
646f98ec 1747 item_name = ENCODE_MENU_STRING (item_name);
3ae565b3 1748 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
4c329aa8 1749 }
177c0ea7 1750
3427a3db 1751 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
4c329aa8 1752 {
646f98ec 1753 descrip = ENCODE_MENU_STRING (descrip);
3ae565b3 1754 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
4c329aa8
GM
1755 }
1756#endif /* not HAVE_MULTILINGUAL_MENU */
177c0ea7 1757
f7fab165 1758 wv = xmalloc_widget_value ();
177c0ea7 1759 if (prev_wv)
78589e07 1760 prev_wv->next = wv;
177c0ea7 1761 else
78589e07 1762 save_wv->contents = wv;
51b59d79 1763 wv->name = SSDATA (item_name);
78589e07 1764 if (!NILP (descrip))
51b59d79 1765 wv->key = SSDATA (descrip);
78589e07 1766 wv->value = 0;
a352a815
RS
1767 /* If this item has a null value,
1768 make the call_data null so that it won't display a box
1769 when the mouse is on it. */
4939150c 1770 wv->call_data = !NILP (def) ? aref_addr (menu_items, i) : 0;
78589e07 1771 wv->enabled = !NILP (enable);
3427a3db
GM
1772
1773 if (NILP (type))
1774 wv->button_type = BUTTON_TYPE_NONE;
1775 else if (EQ (type, QCtoggle))
1776 wv->button_type = BUTTON_TYPE_TOGGLE;
1777 else if (EQ (type, QCradio))
1778 wv->button_type = BUTTON_TYPE_RADIO;
1779 else
1088b922 1780 emacs_abort ();
3427a3db
GM
1781
1782 wv->selected = !NILP (selected);
cc45fb40 1783
0b1f4572
RS
1784 if (! STRINGP (help))
1785 help = Qnil;
1786
1787 wv->help = help;
cc45fb40 1788
78589e07
RS
1789 prev_wv = wv;
1790
1791 i += MENU_ITEMS_ITEM_LENGTH;
1792 }
1793 }
1794
c98fcf4b 1795 /* Deal with the title, if it is non-nil. */
4dedbfe0
PR
1796 if (!NILP (title))
1797 {
f7fab165
RS
1798 widget_value *wv_title = xmalloc_widget_value ();
1799 widget_value *wv_sep1 = xmalloc_widget_value ();
1800 widget_value *wv_sep2 = xmalloc_widget_value ();
4dedbfe0
PR
1801
1802 wv_sep2->name = "--";
1803 wv_sep2->next = first_wv->contents;
27ad7b52 1804 wv_sep2->help = Qnil;
4dedbfe0
PR
1805
1806 wv_sep1->name = "--";
1807 wv_sep1->next = wv_sep2;
27ad7b52 1808 wv_sep1->help = Qnil;
4dedbfe0 1809
703dc2a8
KH
1810#ifndef HAVE_MULTILINGUAL_MENU
1811 if (STRING_MULTIBYTE (title))
646f98ec 1812 title = ENCODE_MENU_STRING (title);
703dc2a8 1813#endif
177c0ea7 1814
51b59d79 1815 wv_title->name = SSDATA (title);
cc45fb40 1816 wv_title->enabled = TRUE;
3427a3db 1817 wv_title->button_type = BUTTON_TYPE_NONE;
27ad7b52 1818 wv_title->help = Qnil;
a9d8395f 1819 wv_title->next = wv_sep1;
4dedbfe0
PR
1820 first_wv->contents = wv_title;
1821 }
1822
78589e07
RS
1823 /* No selection has been chosen yet. */
1824 menu_item_selection = 0;
1825
ba24cea2
YM
1826 /* Make sure to free the widget_value objects we used to specify the
1827 contents even with longjmp. */
1828 record_unwind_protect (cleanup_widget_value_tree,
468afbac 1829 make_save_pointer (first_wv));
ba24cea2 1830
488dd4c4 1831 /* Actually create and show the menu until popped down. */
a20903d0 1832 create_and_show_popup_menu (f, first_wv, x, y, for_click, timestamp);
18686d47 1833
ba24cea2 1834 unbind_to (specpdl_count, Qnil);
18686d47 1835
78589e07
RS
1836 /* Find the selected item, and its pane, to return
1837 the proper value. */
1838 if (menu_item_selection != 0)
1839 {
c63f6952 1840 Lisp_Object prefix, entry;
78589e07 1841
6bbd7a29 1842 prefix = entry = Qnil;
78589e07
RS
1843 i = 0;
1844 while (i < menu_items_used)
1845 {
28be1ada 1846 if (EQ (AREF (menu_items, i), Qnil))
101bb4a5
RS
1847 {
1848 subprefix_stack[submenu_depth++] = prefix;
1849 prefix = entry;
1850 i++;
1851 }
28be1ada 1852 else if (EQ (AREF (menu_items, i), Qlambda))
101bb4a5
RS
1853 {
1854 prefix = subprefix_stack[--submenu_depth];
1855 i++;
1856 }
28be1ada 1857 else if (EQ (AREF (menu_items, i), Qt))
78589e07
RS
1858 {
1859 prefix
28be1ada 1860 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
78589e07
RS
1861 i += MENU_ITEMS_PANE_LENGTH;
1862 }
d31d42cc
RS
1863 /* Ignore a nil in the item list.
1864 It's meaningful only for dialog boxes. */
28be1ada 1865 else if (EQ (AREF (menu_items, i), Qquote))
d31d42cc 1866 i += 1;
78589e07
RS
1867 else
1868 {
1869 entry
28be1ada 1870 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
4939150c 1871 if (menu_item_selection == aref_addr (menu_items, i))
78589e07 1872 {
7cded46f 1873 if (keymaps)
78589e07 1874 {
101bb4a5
RS
1875 int j;
1876
78589e07
RS
1877 entry = Fcons (entry, Qnil);
1878 if (!NILP (prefix))
1879 entry = Fcons (prefix, entry);
101bb4a5 1880 for (j = submenu_depth - 1; j >= 0; j--)
e48087b7 1881 if (!NILP (subprefix_stack[j]))
5964e450 1882 entry = Fcons (subprefix_stack[j], entry);
78589e07
RS
1883 }
1884 return entry;
1885 }
1886 i += MENU_ITEMS_ITEM_LENGTH;
1887 }
1888 }
1889 }
be6ed24a
RS
1890 else if (!for_click)
1891 /* Make "Cancel" equivalent to C-g. */
1892 Fsignal (Qquit, Qnil);
78589e07
RS
1893
1894 return Qnil;
18686d47 1895}
4dedbfe0 1896\f
488dd4c4
JD
1897#ifdef USE_GTK
1898static void
971de7fb 1899dialog_selection_callback (GtkWidget *widget, gpointer client_data)
488dd4c4 1900{
8ac068ac 1901 /* Treat the pointer as an integer. There's no problem
488dd4c4 1902 as long as pointers have enough bits to hold small integers. */
d01a7826 1903 if ((intptr_t) client_data != -1)
488dd4c4
JD
1904 menu_item_selection = (Lisp_Object *) client_data;
1905
1906 popup_activated_flag = 0;
1907}
1908
1909/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1910 dialog pops down.
1911 menu_item_selection will be set to the selection. */
1912static void
971de7fb 1913create_and_show_dialog (FRAME_PTR f, widget_value *first_wv)
488dd4c4
JD
1914{
1915 GtkWidget *menu;
1916
62af879c 1917 if (! FRAME_X_P (f))
1088b922 1918 emacs_abort ();
62af879c 1919
488dd4c4
JD
1920 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
1921 G_CALLBACK (dialog_selection_callback),
1922 G_CALLBACK (popup_deactivate_callback),
1923 0);
1924
1925 if (menu)
1926 {
d311d28c 1927 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
468afbac 1928 record_unwind_protect (pop_down_menu, make_save_pointer (menu));
af89e871 1929
488dd4c4
JD
1930 /* Display the menu. */
1931 gtk_widget_show_all (menu);
1932
1933 /* Process events that apply to the menu. */
f1d1cd24 1934 popup_widget_loop (1, menu);
177c0ea7 1935
af89e871 1936 unbind_to (specpdl_count, Qnil);
488dd4c4
JD
1937 }
1938}
1939
1940#else /* not USE_GTK */
4dedbfe0 1941static void
ebd15611 1942dialog_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
4dedbfe0 1943{
4475bec4 1944 /* Treat the pointer as an integer. There's no problem
01d5e892 1945 as long as pointers have enough bits to hold small integers. */
4475bec4 1946 if ((intptr_t) client_data != -1)
4dedbfe0 1947 menu_item_selection = (Lisp_Object *) client_data;
488dd4c4 1948
4d7e6e51 1949 block_input ();
4dedbfe0 1950 lw_destroy_all_widgets (id);
4d7e6e51 1951 unblock_input ();
9572375b 1952 popup_activated_flag = 0;
4dedbfe0 1953}
18686d47 1954
488dd4c4 1955
488dd4c4
JD
1956/* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1957 dialog pops down.
1958 menu_item_selection will be set to the selection. */
1959static void
ebd15611 1960create_and_show_dialog (FRAME_PTR f, widget_value *first_wv)
488dd4c4
JD
1961{
1962 LWLIB_ID dialog_id;
1963
62af879c 1964 if (!FRAME_X_P (f))
1088b922 1965 emacs_abort ();
62af879c 1966
488dd4c4 1967 dialog_id = widget_id_tick++;
52214050 1968#ifdef USE_LUCID
1ecb2d3f
JD
1969 apply_systemfont_to_dialog (f->output_data.x->widget);
1970#endif
488dd4c4
JD
1971 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1972 f->output_data.x->widget, 1, 0,
1973 dialog_selection_callback, 0, 0);
1974 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
488dd4c4
JD
1975 /* Display the dialog box. */
1976 lw_pop_up_all_widgets (dialog_id);
1977 popup_activated_flag = 1;
98a20c65 1978 x_activate_timeout_atimer ();
488dd4c4
JD
1979
1980 /* Process events that apply to the dialog box.
1981 Also handle timers. */
1982 {
d311d28c 1983 ptrdiff_t count = SPECPDL_INDEX ();
488dd4c4 1984 int fact = 4 * sizeof (LWLIB_ID);
177c0ea7 1985
488dd4c4 1986 /* xdialog_show_unwind is responsible for popping the dialog box down. */
af89e871 1987 record_unwind_protect (pop_down_menu,
488dd4c4
JD
1988 Fcons (make_number (dialog_id >> (fact)),
1989 make_number (dialog_id & ~(-1 << (fact)))));
1990
df470e3b 1991 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
95bdef2e 1992 dialog_id, 1);
488dd4c4
JD
1993
1994 unbind_to (count, Qnil);
1995 }
1996}
1997
1998#endif /* not USE_GTK */
1999
42ca4633 2000static const char * button_names [] = {
165e1749
FP
2001 "button1", "button2", "button3", "button4", "button5",
2002 "button6", "button7", "button8", "button9", "button10" };
2003
2004static Lisp_Object
42ca4633 2005xdialog_show (FRAME_PTR f,
7cded46f 2006 bool keymaps,
42ca4633
J
2007 Lisp_Object title,
2008 Lisp_Object header,
2009 const char **error_name)
165e1749
FP
2010{
2011 int i, nb_buttons=0;
80670155 2012 char dialog_name[6];
165e1749 2013
faa935b6 2014 widget_value *wv, *first_wv = 0, *prev_wv = 0;
165e1749 2015
fcaa7665
RS
2016 /* Number of elements seen so far, before boundary. */
2017 int left_count = 0;
2018 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2019 int boundary_seen = 0;
2020
9898bd0e 2021 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
ba24cea2 2022
62af879c 2023 if (! FRAME_X_P (f))
1088b922 2024 emacs_abort ();
62af879c 2025
6a040d6a 2026 *error_name = NULL;
165e1749 2027
80670155
RS
2028 if (menu_items_n_panes > 1)
2029 {
6a040d6a 2030 *error_name = "Multiple panes in dialog box";
80670155
RS
2031 return Qnil;
2032 }
2033
165e1749
FP
2034 /* Create a tree of widget_value objects
2035 representing the text label and buttons. */
2036 {
2037 Lisp_Object pane_name, prefix;
6d1f7fee 2038 const char *pane_string;
28be1ada
DA
2039 pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME);
2040 prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX);
165e1749 2041 pane_string = (NILP (pane_name)
51b59d79 2042 ? "" : SSDATA (pane_name));
f7fab165 2043 prev_wv = xmalloc_widget_value ();
b00876c9 2044 prev_wv->value = (char *) pane_string;
165e1749
FP
2045 if (keymaps && !NILP (prefix))
2046 prev_wv->name++;
2047 prev_wv->enabled = 1;
2048 prev_wv->name = "message";
27ad7b52 2049 prev_wv->help = Qnil;
165e1749 2050 first_wv = prev_wv;
177c0ea7 2051
165e1749
FP
2052 /* Loop over all panes and items, filling in the tree. */
2053 i = MENU_ITEMS_PANE_LENGTH;
2054 while (i < menu_items_used)
2055 {
177c0ea7 2056
165e1749
FP
2057 /* Create a new item within current pane. */
2058 Lisp_Object item_name, enable, descrip;
28be1ada
DA
2059 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2060 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
165e1749 2061 descrip
28be1ada 2062 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
177c0ea7 2063
80670155
RS
2064 if (NILP (item_name))
2065 {
2066 free_menubar_widget_value_tree (first_wv);
6a040d6a 2067 *error_name = "Submenu in dialog items";
80670155
RS
2068 return Qnil;
2069 }
fcaa7665
RS
2070 if (EQ (item_name, Qquote))
2071 {
2072 /* This is the boundary between left-side elts
2073 and right-side elts. Stop incrementing right_count. */
2074 boundary_seen = 1;
2075 i++;
2076 continue;
2077 }
86e71abf 2078 if (nb_buttons >= 9)
80670155
RS
2079 {
2080 free_menubar_widget_value_tree (first_wv);
6a040d6a 2081 *error_name = "Too many dialog items";
80670155
RS
2082 return Qnil;
2083 }
2084
f7fab165 2085 wv = xmalloc_widget_value ();
165e1749 2086 prev_wv->next = wv;
80670155 2087 wv->name = (char *) button_names[nb_buttons];
165e1749 2088 if (!NILP (descrip))
51b59d79
PE
2089 wv->key = SSDATA (descrip);
2090 wv->value = SSDATA (item_name);
4939150c 2091 wv->call_data = aref_addr (menu_items, i);
165e1749 2092 wv->enabled = !NILP (enable);
27ad7b52 2093 wv->help = Qnil;
165e1749
FP
2094 prev_wv = wv;
2095
fcaa7665
RS
2096 if (! boundary_seen)
2097 left_count++;
2098
165e1749
FP
2099 nb_buttons++;
2100 i += MENU_ITEMS_ITEM_LENGTH;
2101 }
2102
fcaa7665
RS
2103 /* If the boundary was not specified,
2104 by default put half on the left and half on the right. */
2105 if (! boundary_seen)
2106 left_count = nb_buttons - nb_buttons / 2;
2107
f7fab165 2108 wv = xmalloc_widget_value ();
80670155 2109 wv->name = dialog_name;
27ad7b52 2110 wv->help = Qnil;
6a040d6a
NR
2111
2112 /* Frame title: 'Q' = Question, 'I' = Information.
2113 Can also have 'E' = Error if, one day, we want
2114 a popup for errors. */
5e617bc2 2115 if (NILP (header))
6a040d6a
NR
2116 dialog_name[0] = 'Q';
2117 else
2118 dialog_name[0] = 'I';
2119
80670155
RS
2120 /* Dialog boxes use a really stupid name encoding
2121 which specifies how many buttons to use
6a040d6a 2122 and how many buttons are on the right. */
80670155
RS
2123 dialog_name[1] = '0' + nb_buttons;
2124 dialog_name[2] = 'B';
2125 dialog_name[3] = 'R';
fcaa7665
RS
2126 /* Number of buttons to put on the right. */
2127 dialog_name[4] = '0' + nb_buttons - left_count;
80670155 2128 dialog_name[5] = 0;
165e1749
FP
2129 wv->contents = first_wv;
2130 first_wv = wv;
165e1749
FP
2131 }
2132
165e1749
FP
2133 /* No selection has been chosen yet. */
2134 menu_item_selection = 0;
2135
ba24cea2
YM
2136 /* Make sure to free the widget_value objects we used to specify the
2137 contents even with longjmp. */
2138 record_unwind_protect (cleanup_widget_value_tree,
468afbac 2139 make_save_pointer (first_wv));
ba24cea2 2140
488dd4c4
JD
2141 /* Actually create and show the dialog. */
2142 create_and_show_dialog (f, first_wv);
f02cac82 2143
ba24cea2 2144 unbind_to (specpdl_count, Qnil);
177c0ea7 2145
488dd4c4
JD
2146 /* Find the selected item, and its pane, to return
2147 the proper value. */
165e1749
FP
2148 if (menu_item_selection != 0)
2149 {
2150 Lisp_Object prefix;
2151
2152 prefix = Qnil;
2153 i = 0;
2154 while (i < menu_items_used)
2155 {
2156 Lisp_Object entry;
2157
28be1ada 2158 if (EQ (AREF (menu_items, i), Qt))
165e1749
FP
2159 {
2160 prefix
28be1ada 2161 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
165e1749
FP
2162 i += MENU_ITEMS_PANE_LENGTH;
2163 }
28be1ada 2164 else if (EQ (AREF (menu_items, i), Qquote))
85996cfb
GM
2165 {
2166 /* This is the boundary between left-side elts and
2167 right-side elts. */
2168 ++i;
2169 }
165e1749
FP
2170 else
2171 {
2172 entry
28be1ada 2173 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
4939150c 2174 if (menu_item_selection == aref_addr (menu_items, i))
165e1749
FP
2175 {
2176 if (keymaps != 0)
2177 {
2178 entry = Fcons (entry, Qnil);
2179 if (!NILP (prefix))
2180 entry = Fcons (prefix, entry);
2181 }
2182 return entry;
2183 }
2184 i += MENU_ITEMS_ITEM_LENGTH;
2185 }
2186 }
2187 }
9f6fcdc5
JD
2188 else
2189 /* Make "Cancel" equivalent to C-g. */
2190 Fsignal (Qquit, Qnil);
165e1749
FP
2191
2192 return Qnil;
2193}
ba461919 2194
488dd4c4 2195#else /* not USE_X_TOOLKIT && not USE_GTK */
78589e07 2196
3e703b25
GM
2197/* The frame of the last activated non-toolkit menu bar.
2198 Used to generate menu help events. */
2199
2200static struct frame *menu_help_frame;
2201
2202
62145073
GM
2203/* Show help HELP_STRING, or clear help if HELP_STRING is null.
2204
2205 PANE is the pane number, and ITEM is the menu item number in
2206 the menu (currently not used).
177c0ea7 2207
62145073
GM
2208 This cannot be done with generating a HELP_EVENT because
2209 XMenuActivate contains a loop that doesn't let Emacs process
2210 keyboard events. */
3e703b25
GM
2211
2212static void
eb18f6cc 2213menu_help_callback (char const *help_string, int pane, int item)
3e703b25 2214{
62145073
GM
2215 Lisp_Object *first_item;
2216 Lisp_Object pane_name;
2217 Lisp_Object menu_object;
177c0ea7 2218
62145073
GM
2219 first_item = XVECTOR (menu_items)->contents;
2220 if (EQ (first_item[0], Qt))
2221 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2222 else if (EQ (first_item[0], Qquote))
2223 /* This shouldn't happen, see xmenu_show. */
ce33e8eb 2224 pane_name = empty_unibyte_string;
62145073
GM
2225 else
2226 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
177c0ea7 2227
62145073
GM
2228 /* (menu-item MENU-NAME PANE-NUMBER) */
2229 menu_object = Fcons (Qmenu_item,
2230 Fcons (pane_name,
2231 Fcons (make_number (pane), Qnil)));
ba461919 2232 show_help_echo (help_string ? build_string (help_string) : Qnil,
f868cd8a 2233 Qnil, menu_object, make_number (item));
3e703b25 2234}
177c0ea7 2235
af89e871 2236static Lisp_Object
0521f580 2237pop_down_menu (Lisp_Object arg)
af89e871 2238{
3346c1d0
DA
2239 FRAME_PTR f = XSAVE_POINTER (arg, 0);
2240 XMenu *menu = XSAVE_POINTER (arg, 1);
af89e871 2241
4d7e6e51 2242 block_input ();
af89e871 2243#ifndef MSDOS
a130b901
JD
2244 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
2245 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
af89e871 2246#endif
a130b901 2247 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
af89e871
JD
2248
2249#ifdef HAVE_X_WINDOWS
2250 /* Assume the mouse has moved out of the X window.
2251 If it has actually moved in, we will get an EnterNotify. */
2252 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2253
2254 /* State that no mouse buttons are now held.
2255 (The oldXMenu code doesn't track this info for us.)
2256 That is not necessarily true, but the fiction leads to reasonable
2257 results, and it is a pain to ask which are actually held now. */
2258 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2259
2260#endif /* HAVE_X_WINDOWS */
2261
4d7e6e51 2262 unblock_input ();
af89e871
JD
2263
2264 return Qnil;
2265}
2266
3e703b25 2267
1fb99a3a 2268Lisp_Object
7cded46f 2269xmenu_show (FRAME_PTR f, int x, int y, bool for_click, bool keymaps,
08dc5ae6 2270 Lisp_Object title, const char **error_name, Time timestamp)
dcfdbac7 2271{
177c0ea7 2272 Window root;
78589e07
RS
2273 XMenu *menu;
2274 int pane, selidx, lpane, status;
2275 Lisp_Object entry, pane_prefix;
dcfdbac7
JB
2276 char *datap;
2277 int ulx, uly, width, height;
2278 int dispwidth, dispheight;
453a4f1b 2279 int i, j, lines, maxlines;
4e8d3549 2280 int maxwidth;
78589e07
RS
2281 int dummy_int;
2282 unsigned int dummy_uint;
d311d28c 2283 ptrdiff_t specpdl_count = SPECPDL_INDEX ();
088831f6 2284
a9e1244d 2285 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
1088b922 2286 emacs_abort ();
62af879c 2287
bf501fb9 2288 *error_name = 0;
78589e07
RS
2289 if (menu_items_n_panes == 0)
2290 return Qnil;
088831f6 2291
742f715d
KH
2292 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2293 {
bf501fb9 2294 *error_name = "Empty menu";
742f715d
KH
2295 return Qnil;
2296 }
2297
78589e07 2298 /* Figure out which root window F is on. */
92280f67 2299 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
78589e07
RS
2300 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2301 &dummy_uint, &dummy_uint);
18686d47 2302
78589e07 2303 /* Make the menu on that window. */
92280f67 2304 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
78589e07 2305 if (menu == NULL)
dcfdbac7 2306 {
bf501fb9 2307 *error_name = "Can't create menu";
78589e07 2308 return Qnil;
dcfdbac7 2309 }
78589e07 2310
d130d129
RS
2311 /* Don't GC while we prepare and show the menu,
2312 because we give the oldxmenu library pointers to the
2313 contents of strings. */
2314 inhibit_garbage_collection ();
2315
87485d6f 2316#ifdef HAVE_X_WINDOWS
78589e07 2317 /* Adjust coordinates to relative to the outer (window manager) window. */
453a4f1b
JD
2318 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
2319 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
87485d6f 2320#endif /* HAVE_X_WINDOWS */
78589e07
RS
2321
2322 /* Adjust coordinates to be root-window-relative. */
9882535b
KS
2323 x += f->left_pos;
2324 y += f->top_pos;
177c0ea7 2325
78589e07 2326 /* Create all the necessary panes and their items. */
eef9bc79
PE
2327 maxwidth = maxlines = lines = i = 0;
2328 lpane = XM_FAILURE;
78589e07 2329 while (i < menu_items_used)
dcfdbac7 2330 {
28be1ada 2331 if (EQ (AREF (menu_items, i), Qt))
dcfdbac7 2332 {
78589e07
RS
2333 /* Create a new pane. */
2334 Lisp_Object pane_name, prefix;
6d1f7fee 2335 const char *pane_string;
78589e07 2336
453a4f1b
JD
2337 maxlines = max (maxlines, lines);
2338 lines = 0;
28be1ada
DA
2339 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2340 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
78589e07 2341 pane_string = (NILP (pane_name)
51b59d79 2342 ? "" : SSDATA (pane_name));
78589e07
RS
2343 if (keymaps && !NILP (prefix))
2344 pane_string++;
2345
92280f67 2346 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
78589e07
RS
2347 if (lpane == XM_FAILURE)
2348 {
92280f67 2349 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
bf501fb9 2350 *error_name = "Can't create pane";
78589e07
RS
2351 return Qnil;
2352 }
2353 i += MENU_ITEMS_PANE_LENGTH;
4e8d3549
RS
2354
2355 /* Find the width of the widest item in this pane. */
4e8d3549
RS
2356 j = i;
2357 while (j < menu_items_used)
2358 {
2359 Lisp_Object item;
28be1ada 2360 item = AREF (menu_items, j);
4e8d3549
RS
2361 if (EQ (item, Qt))
2362 break;
2363 if (NILP (item))
2364 {
2365 j++;
2366 continue;
2367 }
d5db4077 2368 width = SBYTES (item);
4e8d3549
RS
2369 if (width > maxwidth)
2370 maxwidth = width;
2371
2372 j += MENU_ITEMS_ITEM_LENGTH;
2373 }
dcfdbac7 2374 }
fcaa7665
RS
2375 /* Ignore a nil in the item list.
2376 It's meaningful only for dialog boxes. */
28be1ada 2377 else if (EQ (AREF (menu_items, i), Qquote))
fcaa7665 2378 i += 1;
78589e07 2379 else
dcfdbac7 2380 {
78589e07 2381 /* Create a new item within current pane. */
3e703b25 2382 Lisp_Object item_name, enable, descrip, help;
eb18f6cc
PE
2383 char *item_data;
2384 char const *help_string;
78589e07 2385
28be1ada
DA
2386 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2387 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
78589e07 2388 descrip
28be1ada
DA
2389 = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2390 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
eb18f6cc 2391 help_string = STRINGP (help) ? SSDATA (help) : NULL;
177c0ea7 2392
78589e07 2393 if (!NILP (descrip))
4e8d3549 2394 {
4e8d3549
RS
2395 /* if alloca is fast, use that to make the space,
2396 to reduce gc needs. */
38182d90 2397 item_data = alloca (maxwidth + SBYTES (descrip) + 1);
eb18f6cc 2398 memcpy (item_data, SSDATA (item_name), SBYTES (item_name));
d5db4077 2399 for (j = SCHARS (item_name); j < maxwidth; j++)
4e8d3549 2400 item_data[j] = ' ';
eb18f6cc 2401 memcpy (item_data + j, SSDATA (descrip), SBYTES (descrip));
d5db4077 2402 item_data[j + SBYTES (descrip)] = 0;
4e8d3549
RS
2403 }
2404 else
eb18f6cc 2405 item_data = SSDATA (item_name);
78589e07 2406
eef9bc79
PE
2407 if (lpane == XM_FAILURE
2408 || (XMenuAddSelection (FRAME_X_DISPLAY (f),
2409 menu, lpane, 0, item_data,
2410 !NILP (enable), help_string)
2411 == XM_FAILURE))
dcfdbac7 2412 {
92280f67 2413 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
bf501fb9 2414 *error_name = "Can't add selection to menu";
78589e07 2415 return Qnil;
dcfdbac7 2416 }
78589e07 2417 i += MENU_ITEMS_ITEM_LENGTH;
453a4f1b 2418 lines++;
dcfdbac7
JB
2419 }
2420 }
4e8d3549 2421
453a4f1b
JD
2422 maxlines = max (maxlines, lines);
2423
78589e07 2424 /* All set and ready to fly. */
92280f67 2425 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
63685b9d
GM
2426 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2427 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
78589e07
RS
2428 x = min (x, dispwidth);
2429 y = min (y, dispheight);
2430 x = max (x, 1);
2431 y = max (y, 1);
92280f67 2432 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
dcfdbac7
JB
2433 &ulx, &uly, &width, &height);
2434 if (ulx+width > dispwidth)
2435 {
78589e07 2436 x -= (ulx + width) - dispwidth;
dcfdbac7
JB
2437 ulx = dispwidth - width;
2438 }
2439 if (uly+height > dispheight)
2440 {
78589e07 2441 y -= (uly + height) - dispheight;
dcfdbac7
JB
2442 uly = dispheight - height;
2443 }
2de7397f
EZ
2444#ifndef HAVE_X_WINDOWS
2445 if (FRAME_HAS_MINIBUF_P (f) && uly+height > dispheight - 1)
2446 {
2447 /* Move the menu away of the echo area, to avoid overwriting the
2448 menu with help echo messages or vice versa. */
2449 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
2450 {
2451 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2452 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2453 }
2454 else
2455 {
2456 y--;
2457 uly--;
2458 }
2459 }
2460#endif
78589e07
RS
2461 if (ulx < 0) x -= ulx;
2462 if (uly < 0) y -= uly;
121e4555 2463
453a4f1b
JD
2464 if (! for_click)
2465 {
2466 /* If position was not given by a mouse click, adjust so upper left
2467 corner of the menu as a whole ends up at given coordinates. This
2468 is what x-popup-menu says in its documentation. */
2469 x += width/2;
2470 y += 1.5*height/(maxlines+2);
2471 }
2472
121e4555 2473 XMenuSetAEQ (menu, TRUE);
78589e07
RS
2474 XMenuSetFreeze (menu, TRUE);
2475 pane = selidx = 0;
3e703b25 2476
c3438661
JD
2477#ifndef MSDOS
2478 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
2479#endif
59cfb104 2480
a130b901 2481 record_unwind_protect (pop_down_menu,
7b1123d8 2482 make_save_value (SAVE_TYPE_PTR_PTR, f, menu));
c3438661 2483
3e703b25
GM
2484 /* Help display under X won't work because XMenuActivate contains
2485 a loop that doesn't give Emacs a chance to process it. */
2486 menu_help_frame = f;
92280f67 2487 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
af89e871
JD
2488 x, y, ButtonReleaseMask, &datap,
2489 menu_help_callback);
eef9bc79 2490 entry = pane_prefix = Qnil;
a352a815 2491
dcfdbac7
JB
2492 switch (status)
2493 {
2494 case XM_SUCCESS:
2495#ifdef XDEBUG
2496 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2497#endif
fa6d54d9 2498
78589e07
RS
2499 /* Find the item number SELIDX in pane number PANE. */
2500 i = 0;
2501 while (i < menu_items_used)
fa6d54d9 2502 {
28be1ada 2503 if (EQ (AREF (menu_items, i), Qt))
088831f6 2504 {
78589e07
RS
2505 if (pane == 0)
2506 pane_prefix
28be1ada 2507 = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
78589e07
RS
2508 pane--;
2509 i += MENU_ITEMS_PANE_LENGTH;
088831f6 2510 }
78589e07 2511 else
ab6ee1a0 2512 {
78589e07 2513 if (pane == -1)
ab6ee1a0 2514 {
78589e07 2515 if (selidx == 0)
ab6ee1a0 2516 {
78589e07 2517 entry
28be1ada 2518 = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE);
7cded46f 2519 if (keymaps)
ab6ee1a0 2520 {
78589e07
RS
2521 entry = Fcons (entry, Qnil);
2522 if (!NILP (pane_prefix))
2523 entry = Fcons (pane_prefix, entry);
ab6ee1a0 2524 }
78589e07 2525 break;
ab6ee1a0 2526 }
78589e07 2527 selidx--;
ab6ee1a0 2528 }
78589e07 2529 i += MENU_ITEMS_ITEM_LENGTH;
ab6ee1a0
RS
2530 }
2531 }
78589e07 2532 break;
dcfdbac7 2533
78589e07 2534 case XM_FAILURE:
bf501fb9 2535 *error_name = "Can't activate menu";
78589e07 2536 case XM_IA_SELECT:
9f6fcdc5 2537 break;
78589e07 2538 case XM_NO_SELECT:
be6ed24a
RS
2539 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2540 the menu was invoked with a mouse event as POSITION). */
9f6fcdc5
JD
2541 if (! for_click)
2542 Fsignal (Qquit, Qnil);
78589e07 2543 break;
dcfdbac7 2544 }
a5285df3 2545
af89e871 2546 unbind_to (specpdl_count, Qnil);
a5285df3 2547
78589e07 2548 return entry;
dcfdbac7 2549}
4dedbfe0 2550
78589e07 2551#endif /* not USE_X_TOOLKIT */
1e659e4c
RS
2552
2553#endif /* HAVE_MENUS */
e3135734 2554
d009ae66
EZ
2555#ifndef MSDOS
2556/* Detect if a dialog or menu has been posted. MSDOS has its own
2557 implementation on msdos.c. */
b79b8a1c 2558
077288cf 2559int ATTRIBUTE_CONST
971de7fb 2560popup_activated (void)
b79b8a1c
CY
2561{
2562 return popup_activated_flag;
2563}
d009ae66 2564#endif /* not MSDOS */
e3135734
CY
2565
2566/* The following is used by delayed window autoselection. */
2567
2568DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
2569 doc: /* Return t if a menu or popup dialog is active. */)
5842a27b 2570 (void)
e3135734
CY
2571{
2572#ifdef HAVE_MENUS
2573 return (popup_activated ()) ? Qt : Qnil;
2574#else
2575 return Qnil;
2576#endif /* HAVE_MENUS */
2577}
088831f6 2578\f
dfcf069d 2579void
971de7fb 2580syms_of_xmenu (void)
dcfdbac7 2581{
cd3520a4 2582 DEFSYM (Qdebug_on_next_call, "debug-on-next-call");
0314aacb 2583
8ed87156 2584#ifdef USE_X_TOOLKIT
177c0ea7 2585 widget_id_tick = (1<<16);
88766961 2586 next_menubar_widget_id = 1;
8ed87156
RS
2587#endif
2588
508fb067 2589 defsubr (&Smenu_or_popup_active_p);
22badffe
JD
2590
2591#if defined (USE_GTK) || defined (USE_X_TOOLKIT)
12b6af5c 2592 defsubr (&Sx_menu_bar_open_internal);
d67b4f80
DN
2593 Ffset (intern_c_string ("accelerate-menu"),
2594 intern_c_string (Sx_menu_bar_open_internal.symbol_name));
22badffe
JD
2595#endif
2596
1e659e4c 2597#ifdef HAVE_MENUS
165e1749 2598 defsubr (&Sx_popup_dialog);
1e659e4c 2599#endif
dcfdbac7 2600}