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