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