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