Merge from emacs--rel--22
[bpt/emacs.git] / src / xmenu.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* X pop-up deck-of-cards menu facility for GNU Emacs.
21 *
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
24 *
25 */
26
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
29
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
31
32 #include <config.h>
33
34 #if 0 /* Why was this included? And without syssignal.h? */
35 /* On 4.3 this loses if it comes after xterm.h. */
36 #include <signal.h>
37 #endif
38
39 #include <stdio.h>
40
41 #include "lisp.h"
42 #include "keyboard.h"
43 #include "keymap.h"
44 #include "frame.h"
45 #include "termhooks.h"
46 #include "window.h"
47 #include "blockinput.h"
48 #include "buffer.h"
49 #include "charset.h"
50 #include "coding.h"
51 #include "sysselect.h"
52
53 #ifdef MSDOS
54 #include "msdos.h"
55 #endif
56
57 #ifdef HAVE_X_WINDOWS
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"
61 #endif
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
71 #ifdef HAVE_X_WINDOWS
72 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
73 code accepts the Emacs internal encoding. */
74 #undef HAVE_MULTILINGUAL_MENU
75 #ifdef USE_X_TOOLKIT
76 #include "widget.h"
77 #include <X11/Xlib.h>
78 #include <X11/IntrinsicP.h>
79 #include <X11/CoreP.h>
80 #include <X11/StringDefs.h>
81 #include <X11/Shell.h>
82 #ifdef USE_LUCID
83 #ifdef HAVE_XAW3D
84 #include <X11/Xaw3d/Paned.h>
85 #else /* !HAVE_XAW3D */
86 #include <X11/Xaw/Paned.h>
87 #endif /* HAVE_XAW3D */
88 #endif /* USE_LUCID */
89 #include "../lwlib/lwlib.h"
90 #else /* not USE_X_TOOLKIT */
91 #ifndef USE_GTK
92 #include "../oldXMenu/XMenu.h"
93 #endif
94 #endif /* not USE_X_TOOLKIT */
95 #endif /* HAVE_X_WINDOWS */
96
97 #ifdef USE_GTK
98 #include "gtkutil.h"
99 #endif
100
101 #include "menu.h"
102
103 #ifndef TRUE
104 #define TRUE 1
105 #define FALSE 0
106 #endif /* no TRUE */
107
108 Lisp_Object Qdebug_on_next_call;
109
110 extern Lisp_Object Vmenu_updating_frame;
111
112 extern Lisp_Object Qmenu_bar;
113
114 extern Lisp_Object QCtoggle, QCradio;
115
116 extern Lisp_Object Voverriding_local_map;
117 extern Lisp_Object Voverriding_local_map_menu_flag;
118
119 extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
120
121 extern Lisp_Object Qmenu_bar_update_hook;
122
123 #ifdef USE_X_TOOLKIT
124 extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
125 extern XtAppContext Xt_app_con;
126
127 static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, Lisp_Object,
128 char **));
129 static void popup_get_selection P_ ((XEvent *, struct x_display_info *,
130 LWLIB_ID, int));
131 #endif /* USE_X_TOOLKIT */
132
133 #ifdef USE_GTK
134 extern void set_frame_menubar P_ ((FRAME_PTR, int, int));
135 static Lisp_Object xdialog_show P_ ((FRAME_PTR, int, Lisp_Object, Lisp_Object,
136 char **));
137 #endif
138
139 static int update_frame_menubar P_ ((struct frame *));
140 static Lisp_Object xmenu_show P_ ((struct frame *, int, int, int, int,
141 Lisp_Object, char **));
142 \f
143 /* Flag which when set indicates a dialog or menu has been posted by
144 Xt on behalf of one of the widget sets. */
145 static int popup_activated_flag;
146
147 static int next_menubar_widget_id;
148
149 /* For NS and NTGUI, these prototypes are defined in keyboard.h. */
150 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
151 extern widget_value *xmalloc_widget_value P_ ((void));
152 extern widget_value *digest_single_submenu P_ ((int, int, int));
153 #endif
154
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
162 int pending_menu_activation;
163 \f
164 #ifdef USE_X_TOOLKIT
165
166 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
167
168 static struct frame *
169 menubar_id_to_frame (id)
170 LWLIB_ID id;
171 {
172 Lisp_Object tail, frame;
173 FRAME_PTR f;
174
175 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
176 {
177 frame = XCAR (tail);
178 if (!FRAMEP (frame))
179 continue;
180 f = XFRAME (frame);
181 if (!FRAME_WINDOW_P (f))
182 continue;
183 if (f->output_data.x->id == id)
184 return f;
185 }
186 return 0;
187 }
188
189 #endif
190 \f
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. */
199 static void
200 mouse_position_for_popup (f, x, y)
201 FRAME_PTR f;
202 int *x;
203 int *y;
204 {
205 Window root, dummy_window;
206 int dummy;
207
208 if (! FRAME_X_P (f))
209 abort ();
210
211 BLOCK_INPUT;
212
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. */
236 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
237 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
238 }
239
240 #endif /* HAVE_X_WINDOWS */
241
242 DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
243 doc: /* Pop up a deck-of-cards menu and return user's selection.
244 POSITION is a position specification. This is either a mouse button event
245 or a list ((XOFFSET YOFFSET) WINDOW)
246 where XOFFSET and YOFFSET are positions in pixels from the top left
247 corner of WINDOW. (WINDOW may be a window or a frame object.)
248 This controls the position of the top left of the menu as a whole.
249 If POSITION is t, it means to use the current mouse position.
250
251 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
252 The menu items come from key bindings that have a menu string as well as
253 a 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
255 the keymap as a top-level element.
256
257 If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
258 Otherwise, REAL-DEFINITION should be a valid key binding definition.
259
260 You can also use a list of keymaps as MENU.
261 Then each keymap makes a separate pane.
262
263 When MENU is a keymap or a list of keymaps, the return value is the
264 list of events corresponding to the user's choice. Note that
265 `x-popup-menu' does not actually execute the command bound to that
266 sequence of events.
267
268 Alternatively, you can specify a menu of multiple panes
269 with a list of the form (TITLE PANE1 PANE2...),
270 where each pane is a list of form (TITLE ITEM1 ITEM2...).
271 Each ITEM is normally a cons cell (STRING . VALUE);
272 but a string can appear as an item--that makes a nonselectable line
273 in the menu.
274 With this form of menu, the return value is VALUE from the chosen item.
275
276 If POSITION is nil, don't display the menu at all, just precalculate the
277 cached information about equivalent key sequences.
278
279 If the user gets rid of the menu without making a valid choice, for
280 instance by clicking the mouse away from a valid choice or by typing
281 keyboard input, then this normally results in a quit and
282 `x-popup-menu' does not return. But if POSITION is a mouse button
283 event (indicating that the user invoked the menu with the mouse) then
284 no quit occurs and `x-popup-menu' returns nil. */)
285 (position, menu)
286 Lisp_Object position, menu;
287 {
288 Lisp_Object keymap, tem;
289 int xpos = 0, ypos = 0;
290 Lisp_Object title;
291 char *error_name = NULL;
292 Lisp_Object selection = Qnil;
293 FRAME_PTR f = NULL;
294 Lisp_Object x, y, window;
295 int keymaps = 0;
296 int for_click = 0;
297 int specpdl_count = SPECPDL_INDEX ();
298 struct gcpro gcpro1;
299
300 #ifdef HAVE_MENUS
301 if (! NILP (position))
302 {
303 int get_current_pos_p = 0;
304 check_x ();
305
306 /* Decode the first argument: find the window and the coordinates. */
307 if (EQ (position, Qt)
308 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
309 || EQ (XCAR (position), Qtool_bar))))
310 {
311 get_current_pos_p = 1;
312 }
313 else
314 {
315 tem = Fcar (position);
316 if (CONSP (tem))
317 {
318 window = Fcar (Fcdr (position));
319 x = XCAR (tem);
320 y = Fcar (XCDR (tem));
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 {
343 /* Use the mouse's current position. */
344 FRAME_PTR new_f = SELECTED_FRAME ();
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 }
358
359 #else /* not HAVE_X_WINDOWS */
360 Lisp_Object bar_window;
361 enum scroll_bar_part part;
362 unsigned long time;
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;
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 }
384 }
385
386 CHECK_NUMBER (x);
387 CHECK_NUMBER (y);
388
389 /* Decode where to put the menu. */
390
391 if (FRAMEP (window))
392 {
393 f = XFRAME (window);
394 xpos = 0;
395 ypos = 0;
396 }
397 else if (WINDOWP (window))
398 {
399 CHECK_LIVE_WINDOW (window);
400 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
401
402 xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window));
403 ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window));
404 }
405 else
406 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
407 but I don't want to make one now. */
408 CHECK_WINDOW (window);
409
410 xpos += XINT (x);
411 ypos += XINT (y);
412
413 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
414 error ("Can not put X menu on this terminal");
415
416 XSETFRAME (Vmenu_updating_frame, f);
417 }
418 else
419 Vmenu_updating_frame = Qnil;
420 #endif /* HAVE_MENUS */
421
422 record_unwind_protect (unuse_menu_items, Qnil);
423 title = Qnil;
424 GCPRO1 (title);
425
426 /* Decode the menu items from what was specified. */
427
428 keymap = get_keymap (menu, 0, 0);
429 if (CONSP (keymap))
430 {
431 /* We were given a keymap. Extract menu info from the keymap. */
432 Lisp_Object prompt;
433
434 /* Extract the detailed info to make one pane. */
435 keymap_panes (&menu, 1, NILP (position));
436
437 /* Search for a string appearing directly as an element of the keymap.
438 That string is the title of the menu. */
439 prompt = Fkeymap_prompt (keymap);
440 if (NILP (title) && !NILP (prompt))
441 title = prompt;
442
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;
448 }
449 else if (CONSP (menu) && KEYMAPP (XCAR (menu)))
450 {
451 /* We were given a list of keymaps. */
452 int nmaps = XFASTINT (Flength (menu));
453 Lisp_Object *maps
454 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
455 int i;
456
457 title = Qnil;
458
459 /* The first keymap that has a prompt string
460 supplies the menu title. */
461 for (tem = menu, i = 0; CONSP (tem); tem = XCDR (tem))
462 {
463 Lisp_Object prompt;
464
465 maps[i++] = keymap = get_keymap (XCAR (tem), 1, 0);
466
467 prompt = Fkeymap_prompt (keymap);
468 if (NILP (title) && !NILP (prompt))
469 title = prompt;
470 }
471
472 /* Extract the detailed info to make one pane. */
473 keymap_panes (maps, nmaps, NILP (position));
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;
480 }
481 else
482 {
483 /* We were given an old-fashioned menu. */
484 title = Fcar (menu);
485 CHECK_STRING (title);
486
487 list_of_panes (Fcdr (menu));
488
489 keymaps = 0;
490 }
491
492 unbind_to (specpdl_count, Qnil);
493
494 if (NILP (position))
495 {
496 discard_menu_items ();
497 UNGCPRO;
498 return Qnil;
499 }
500
501 #ifdef HAVE_MENUS
502 /* Display them in a menu. */
503 BLOCK_INPUT;
504
505 selection = xmenu_show (f, xpos, ypos, for_click,
506 keymaps, title, &error_name);
507 UNBLOCK_INPUT;
508
509 discard_menu_items ();
510
511 UNGCPRO;
512 #endif /* HAVE_MENUS */
513
514 if (error_name) error (error_name);
515 return selection;
516 }
517
518 #ifdef HAVE_MENUS
519
520 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
521 doc: /* Pop up a dialog box and return user's selection.
522 POSITION specifies which frame to use.
523 This is normally a mouse button event or a window or frame.
524 If POSITION is t, it means to use the frame the mouse is on.
525 The dialog box appears in the middle of the specified frame.
526
527 CONTENTS specifies the alternatives to display in the dialog box.
528 It is a list of the form (DIALOG ITEM1 ITEM2...).
529 Each ITEM is a cons cell (STRING . VALUE).
530 The return value is VALUE from the chosen item.
531
532 An ITEM may also be just a string--that makes a nonselectable item.
533 An ITEM may also be nil--that means to put all preceding items
534 on the left of the dialog box and all following items on the right.
535 \(By default, approximately half appear on each side.)
536
537 If HEADER is non-nil, the frame title for the box is "Information",
538 otherwise it is "Question".
539
540 If the user gets rid of the dialog box without making a valid choice,
541 for instance using the window manager, then this produces a quit and
542 `x-popup-dialog' does not return. */)
543 (position, contents, header)
544 Lisp_Object position, contents, header;
545 {
546 FRAME_PTR f = NULL;
547 Lisp_Object window;
548
549 check_x ();
550
551 /* Decode the first argument: find the window or frame to use. */
552 if (EQ (position, Qt)
553 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
554 || EQ (XCAR (position), Qtool_bar))))
555 {
556 #if 0 /* Using the frame the mouse is on may not be right. */
557 /* Use the mouse's current position. */
558 FRAME_PTR new_f = SELECTED_FRAME ();
559 Lisp_Object bar_window;
560 enum scroll_bar_part part;
561 unsigned long time;
562 Lisp_Object x, y;
563
564 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
565
566 if (new_f != 0)
567 XSETFRAME (window, new_f);
568 else
569 window = selected_window;
570 #endif
571 window = selected_window;
572 }
573 else if (CONSP (position))
574 {
575 Lisp_Object tem;
576 tem = Fcar (position);
577 if (CONSP (tem))
578 window = Fcar (Fcdr (position));
579 else
580 {
581 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
582 window = Fcar (tem); /* POSN_WINDOW (tem) */
583 }
584 }
585 else if (WINDOWP (position) || FRAMEP (position))
586 window = position;
587 else
588 window = Qnil;
589
590 /* Decode where to put the menu. */
591
592 if (FRAMEP (window))
593 f = XFRAME (window);
594 else if (WINDOWP (window))
595 {
596 CHECK_LIVE_WINDOW (window);
597 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
598 }
599 else
600 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
601 but I don't want to make one now. */
602 CHECK_WINDOW (window);
603
604 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
605 error ("Can not put X dialog on this terminal");
606
607 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
608 /* Display a menu with these alternatives
609 in the middle of frame F. */
610 {
611 Lisp_Object x, y, frame, newpos;
612 XSETFRAME (frame, f);
613 XSETINT (x, x_pixel_width (f) / 2);
614 XSETINT (y, x_pixel_height (f) / 2);
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;
625 int specpdl_count = SPECPDL_INDEX ();
626
627 /* Decode the dialog items from what was specified. */
628 title = Fcar (contents);
629 CHECK_STRING (title);
630 record_unwind_protect (unuse_menu_items, Qnil);
631
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));
637
638 list_of_panes (Fcons (contents, Qnil));
639
640 /* Display them in a dialog box. */
641 BLOCK_INPUT;
642 selection = xdialog_show (f, 0, title, header, &error_name);
643 UNBLOCK_INPUT;
644
645 unbind_to (specpdl_count, Qnil);
646 discard_menu_items ();
647
648 if (error_name) error (error_name);
649 return selection;
650 }
651 #endif
652 }
653
654
655 #ifndef MSDOS
656
657 /* Set menu_items_inuse so no other popup menu or dialog is created. */
658
659 void
660 x_menu_set_in_use (in_use)
661 int in_use;
662 {
663 menu_items_inuse = in_use ? Qt : Qnil;
664 popup_activated_flag = in_use;
665 #ifdef USE_X_TOOLKIT
666 if (popup_activated_flag)
667 x_activate_timeout_atimer ();
668 #endif
669 }
670
671 /* Wait for an X event to arrive or for a timer to expire. */
672
673 void
674 x_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
685 ! XtAppPending (Xt_app_con)
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
721 \f
722 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
723
724 #ifdef USE_X_TOOLKIT
725
726 /* Loop in Xt until the menu pulldown or dialog popup has been
727 popped down (deactivated). This is used for x-popup-menu
728 and x-popup-dialog; it is not used for the menu bar.
729
730 NOTE: All calls to popup_get_selection should be protected
731 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
732
733 static void
734 popup_get_selection (initial_event, dpyinfo, id, do_timers)
735 XEvent *initial_event;
736 struct x_display_info *dpyinfo;
737 LWLIB_ID id;
738 int do_timers;
739 {
740 XEvent event;
741
742 while (popup_activated_flag)
743 {
744 if (initial_event)
745 {
746 event = *initial_event;
747 initial_event = 0;
748 }
749 else
750 {
751 if (do_timers) x_menu_wait_for_event (0);
752 XtAppNextEvent (Xt_app_con, &event);
753 }
754
755 /* Make sure we don't consider buttons grabbed after menu goes.
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)
760 {
761 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
762 #ifdef USE_MOTIF /* Pretending that the event came from a
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;
771 #endif
772 }
773 /* Pop down on C-g and Escape. */
774 else if (event.type == KeyPress
775 && dpyinfo->display == event.xbutton.display)
776 {
777 KeySym keysym = XLookupKeysym (&event.xkey, 0);
778
779 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
780 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
781 popup_activated_flag = 0;
782 }
783
784 x_dispatch_event (&event, event.xany.display);
785 }
786 }
787
788 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
789 doc: /* Start key navigation of the menu bar in FRAME.
790 This initially opens the first menu bar item and you can then navigate with the
791 arrow keys, select a menu entry with the return key or cancel with the
792 escape key. If FRAME has no menu bar this function does nothing.
793
794 If 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;
860
861 return Qnil;
862 }
863 #endif /* USE_X_TOOLKIT */
864
865
866 #ifdef USE_GTK
867 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
868 doc: /* Start key navigation of the menu bar in FRAME.
869 This initially opens the first menu bar item and you can then navigate with the
870 arrow keys, select a menu entry with the return key or cancel with the
871 escape key. If FRAME has no menu bar this function does nothing.
872
873 If FRAME is nil or not given, use the selected frame. */)
874 (frame)
875 Lisp_Object frame;
876 {
877 GtkWidget *menubar;
878 FRAME_PTR f;
879
880 /* gcc 2.95 doesn't accept the FRAME_PTR declaration after
881 BLOCK_INPUT. */
882
883 BLOCK_INPUT;
884 f = check_x_frame (frame);
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
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 }
901 }
902 UNBLOCK_INPUT;
903
904 return Qnil;
905 }
906
907 /* Loop util popup_activated_flag is set to zero in a callback.
908 Used for popup menus and dialogs. */
909
910 static void
911 popup_widget_loop (do_timers, widget)
912 int do_timers;
913 GtkWidget *widget;
914 {
915 ++popup_activated_flag;
916
917 /* Process events in the Gtk event loop until done. */
918 while (popup_activated_flag)
919 {
920 if (do_timers) x_menu_wait_for_event (0);
921 gtk_main_iteration ();
922 }
923 }
924 #endif
925
926 /* Activate the menu bar of frame F.
927 This is called from keyboard.c when it gets the
928 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
929
930 To activate the menu bar, we use the X button-press event
931 that was saved in saved_menu_event.
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. */
939
940 void
941 x_activate_menubar (f)
942 FRAME_PTR f;
943 {
944 if (! FRAME_X_P (f))
945 abort ();
946
947 if (!f->output_data.x->saved_menu_event->type)
948 return;
949
950 #ifdef USE_GTK
951 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
952 f->output_data.x->saved_menu_event->xany.window))
953 return;
954 #endif
955
956 set_frame_menubar (f, 0, 1);
957 BLOCK_INPUT;
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
963 XtDispatchEvent (f->output_data.x->saved_menu_event);
964 #endif
965 UNBLOCK_INPUT;
966 #ifdef USE_MOTIF
967 if (f->output_data.x->saved_menu_event->type == ButtonRelease)
968 pending_menu_activation = 1;
969 #endif
970
971 /* Ignore this if we get it a second time. */
972 f->output_data.x->saved_menu_event->type = 0;
973 }
974
975 /* This callback is invoked when the user selects a menubar cascade
976 pushbutton, but before the pulldown menu is posted. */
977
978 #ifndef USE_GTK
979 static void
980 popup_activate_callback (widget, id, client_data)
981 Widget widget;
982 LWLIB_ID id;
983 XtPointer client_data;
984 {
985 popup_activated_flag = 1;
986 #ifdef USE_X_TOOLKIT
987 x_activate_timeout_atimer ();
988 #endif
989 }
990 #endif
991
992 /* This callback is invoked when a dialog or menu is finished being
993 used and has been unposted. */
994
995 #ifdef USE_GTK
996 static void
997 popup_deactivate_callback (widget, client_data)
998 GtkWidget *widget;
999 gpointer client_data;
1000 {
1001 popup_activated_flag = 0;
1002 }
1003 #else
1004 static void
1005 popup_deactivate_callback (widget, id, client_data)
1006 Widget widget;
1007 LWLIB_ID id;
1008 XtPointer client_data;
1009 {
1010 popup_activated_flag = 0;
1011 }
1012 #endif
1013
1014
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. */
1018 static void
1019 show_help_event (f, widget, help)
1020 FRAME_PTR f;
1021 xt_or_gtk_widget widget;
1022 Lisp_Object help;
1023 {
1024 Lisp_Object frame;
1025
1026 if (f)
1027 {
1028 XSETFRAME (frame, f);
1029 kbd_buffer_store_help_event (frame, help);
1030 }
1031 else
1032 {
1033 #if 0 /* This code doesn't do anything useful. ++kfs */
1034 /* WIDGET is the popup menu. It's parent is the frame's
1035 widget. See which frame that is. */
1036 xt_or_gtk_widget frame_widget = XtParent (widget);
1037 Lisp_Object tail;
1038
1039 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1040 {
1041 frame = XCAR (tail);
1042 if (FRAMEP (frame)
1043 && (f = XFRAME (frame),
1044 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
1045 break;
1046 }
1047 #endif
1048 show_help_echo (help, Qnil, Qnil, Qnil, 1);
1049 }
1050 }
1051
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. */
1057
1058 #ifdef USE_GTK
1059 void
1060 menu_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;
1066
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
1080 void
1081 menu_highlight_callback (widget, id, call_data)
1082 Widget widget;
1083 LWLIB_ID id;
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;
1092
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
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 */
1105 static 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. */
1111 static void
1112 menubar_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
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
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
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. */
1156 static void
1157 menubar_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 */
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
1175 static int
1176 update_frame_menubar (f)
1177 FRAME_PTR f;
1178 {
1179 #ifdef USE_GTK
1180 return xg_update_frame_menubar (f);
1181 #else
1182 struct x_output *x;
1183 int columns, rows;
1184
1185 if (! FRAME_X_P (f))
1186 abort ();
1187
1188 x = f->output_data.x;
1189
1190 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
1191 return 0;
1192
1193 BLOCK_INPUT;
1194 /* Save the size of the frame because the pane widget doesn't accept
1195 to resize itself. So force it. */
1196 columns = FRAME_COLS (f);
1197 rows = FRAME_LINES (f);
1198
1199 /* Do the voodoo which means "I'm changing lots of things, don't try
1200 to refigure sizes until I'm done." */
1201 lw_refigure_widget (x->column_widget, False);
1202
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. */
1206 XtUnmanageChild (x->edit_widget);
1207
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);
1213
1214 /* Re-manage the text-area widget, and then thrash the sizes. */
1215 XtManageChild (x->edit_widget);
1216 lw_refigure_widget (x->column_widget, True);
1217
1218 /* Force the pane widget to resize itself with the right values. */
1219 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
1220 UNBLOCK_INPUT;
1221 #endif
1222 return 1;
1223 }
1224
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
1229 void
1230 set_frame_menubar (f, first_time, deep_p)
1231 FRAME_PTR f;
1232 int first_time;
1233 int deep_p;
1234 {
1235 xt_or_gtk_widget menubar_widget;
1236 #ifdef USE_X_TOOLKIT
1237 LWLIB_ID id;
1238 #endif
1239 Lisp_Object items;
1240 widget_value *wv, *first_wv, *prev_wv = 0;
1241 int i, last_i = 0;
1242 int *submenu_start, *submenu_end;
1243 int *submenu_top_level_items, *submenu_n_panes;
1244
1245 if (! FRAME_X_P (f))
1246 abort ();
1247
1248 menubar_widget = f->output_data.x->menubar_widget;
1249
1250 XSETFRAME (Vmenu_updating_frame, f);
1251
1252 #ifdef USE_X_TOOLKIT
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;
1256 #endif
1257
1258 if (! menubar_widget)
1259 deep_p = 1;
1260 else if (pending_menu_activation && !deep_p)
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)
1264 {
1265 deep_p = 1;
1266 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
1267 f->output_data.x->saved_menu_event->type = 0;
1268 }
1269
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
1276 if (deep_p)
1277 {
1278 /* Make a widget-value tree representing the entire menu trees. */
1279
1280 struct buffer *prev = current_buffer;
1281 Lisp_Object buffer;
1282 int specpdl_count = SPECPDL_INDEX ();
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
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
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
1299 record_unwind_save_match_data ();
1300 if (NILP (Voverriding_local_map_menu_flag))
1301 {
1302 specbind (Qoverriding_terminal_local_map, Qnil);
1303 specbind (Qoverriding_local_map, Qnil);
1304 }
1305
1306 set_buffer_internal_1 (XBUFFER (buffer));
1307
1308 /* Run the Lucid hook. */
1309 safe_run_hooks (Qactivate_menubar_hook);
1310
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);
1315 safe_run_hooks (Qmenu_bar_update_hook);
1316 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1317
1318 items = FRAME_MENU_BAR_ITEMS (f);
1319
1320 /* Save the frame's previous menu bar contents data. */
1321 if (previous_menu_items_used)
1322 bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items,
1323 previous_menu_items_used * sizeof (Lisp_Object));
1324
1325 /* Fill in menu_items with the current menu bar contents.
1326 This can evaluate Lisp code. */
1327 save_menu_items ();
1328
1329 menu_items = f->menu_bar_vector;
1330 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
1331 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1332 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1333 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
1334 submenu_top_level_items
1335 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1336 init_menu_items ();
1337 for (i = 0; i < XVECTOR (items)->size; i += 4)
1338 {
1339 Lisp_Object key, string, maps;
1340
1341 last_i = i;
1342
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
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);
1354 submenu_n_panes[i] = menu_items_n_panes;
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 {
1374 menu_items_n_panes = submenu_n_panes[i];
1375 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
1376 submenu_top_level_items[i]);
1377 if (prev_wv)
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;
1383 wv->button_type = BUTTON_TYPE_NONE;
1384 prev_wv = wv;
1385 }
1386
1387 set_buffer_internal_1 (prev);
1388
1389 /* If there has been no change in the Lisp-level contents
1390 of the menu bar, skip redisplaying it. Just exit. */
1391
1392 /* Compare the new menu items with the ones computed last time. */
1393 for (i = 0; i < previous_menu_items_used; i++)
1394 if (menu_items_used == i
1395 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1396 break;
1397 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1398 {
1399 /* The menu items have not changed. Don't bother updating
1400 the menus in any form, since it would be a no-op. */
1401 free_menubar_widget_value_tree (first_wv);
1402 discard_menu_items ();
1403 unbind_to (specpdl_count, Qnil);
1404 return;
1405 }
1406
1407 /* The menu items are different, so store them in the frame. */
1408 f->menu_bar_vector = menu_items;
1409 f->menu_bar_items_used = menu_items_used;
1410
1411 /* This undoes save_menu_items. */
1412 unbind_to (specpdl_count, Qnil);
1413
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;
1417 for (i = 0; i < XVECTOR (items)->size; i += 4)
1418 {
1419 Lisp_Object string;
1420 string = XVECTOR (items)->contents[i + 1];
1421 if (NILP (string))
1422 break;
1423 wv->name = (char *) SDATA (string);
1424 update_submenu_strings (wv->contents);
1425 wv = wv->next;
1426 }
1427
1428 }
1429 else
1430 {
1431 /* Make a widget-value tree containing
1432 just the top level menu bar strings. */
1433
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
1442 items = FRAME_MENU_BAR_ITEMS (f);
1443 for (i = 0; i < XVECTOR (items)->size; i += 4)
1444 {
1445 Lisp_Object string;
1446
1447 string = XVECTOR (items)->contents[i + 1];
1448 if (NILP (string))
1449 break;
1450
1451 wv = xmalloc_widget_value ();
1452 wv->name = (char *) SDATA (string);
1453 wv->value = 0;
1454 wv->enabled = 1;
1455 wv->button_type = BUTTON_TYPE_NONE;
1456 wv->help = Qnil;
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);
1462
1463 if (prev_wv)
1464 prev_wv->next = wv;
1465 else
1466 first_wv->contents = wv;
1467 prev_wv = wv;
1468 }
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;
1474 }
1475
1476 /* Create or update the menu bar widget. */
1477
1478 BLOCK_INPUT;
1479
1480 #ifdef USE_GTK
1481 xg_crazy_callback_abort = 1;
1482 if (menubar_widget)
1483 {
1484 /* The fourth arg is DEEP_P, which says to consider the entire
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;
1497
1498 menubar_widget
1499 = xg_create_widget ("menubar", "menubar", f, first_wv,
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
1507
1508 #else /* not USE_GTK */
1509 if (menubar_widget)
1510 {
1511 /* Disable resizing (done for Motif!) */
1512 lw_allow_resizing (f->output_data.x->widget, False);
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. */
1516 lw_modify_all_widgets (id, first_wv, deep_p);
1517
1518 /* Re-enable the edit widget to resize. */
1519 lw_allow_resizing (f->output_data.x->widget, True);
1520 }
1521 else
1522 {
1523 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
1524 XtTranslations override = XtParseTranslationTable (menuOverride);
1525
1526 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1527 f->output_data.x->column_widget,
1528 0,
1529 popup_activate_callback,
1530 menubar_selection_callback,
1531 popup_deactivate_callback,
1532 menu_highlight_callback);
1533 f->output_data.x->menubar_widget = menubar_widget;
1534
1535 /* Make menu pop down on C-g. */
1536 XtOverrideTranslations (menubar_widget, override);
1537 }
1538
1539 {
1540 int menubar_size
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)
1544 : 0);
1545
1546 #if 0 /* Experimentally, we now get the right results
1547 for -geometry -0-0 without this. 24 Aug 96, rms. */
1548 #ifdef USE_LUCID
1549 if (FRAME_EXTERNAL_MENU_BAR (f))
1550 {
1551 Dimension ibw = 0;
1552 XtVaGetValues (f->output_data.x->column_widget,
1553 XtNinternalBorderWidth, &ibw, NULL);
1554 menubar_size += ibw;
1555 }
1556 #endif /* USE_LUCID */
1557 #endif /* 0 */
1558
1559 f->output_data.x->menubar_height = menubar_size;
1560 }
1561 #endif /* not USE_GTK */
1562
1563 free_menubar_widget_value_tree (first_wv);
1564 update_frame_menubar (f);
1565
1566 #ifdef USE_GTK
1567 xg_crazy_callback_abort = 0;
1568 #endif
1569
1570 UNBLOCK_INPUT;
1571 }
1572
1573 /* Called from Fx_create_frame to create the initial menubar of a frame
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
1578 void
1579 initialize_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));
1585 set_frame_menubar (f, 1, 1);
1586 }
1587
1588
1589 /* Get rid of the menu bar of frame F, and free its storage.
1590 This is used when deleting a frame, and when turning off the menu bar.
1591 For GTK this function is in gtkutil.c. */
1592
1593 #ifndef USE_GTK
1594 void
1595 free_frame_menubar (f)
1596 FRAME_PTR f;
1597 {
1598 Widget menubar_widget;
1599
1600 if (! FRAME_X_P (f))
1601 abort ();
1602
1603 menubar_widget = f->output_data.x->menubar_widget;
1604
1605 f->output_data.x->menubar_height = 0;
1606
1607 if (menubar_widget)
1608 {
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
1620
1621 BLOCK_INPUT;
1622
1623 #ifdef USE_MOTIF
1624 if (f->output_data.x->widget)
1625 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
1626 #endif
1627
1628 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1629 f->output_data.x->menubar_widget = NULL;
1630
1631 #ifdef USE_MOTIF
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 }
1638 #endif
1639
1640 UNBLOCK_INPUT;
1641 }
1642 }
1643 #endif /* not USE_GTK */
1644
1645 #endif /* USE_X_TOOLKIT || USE_GTK */
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.
1655 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
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.) */
1662
1663 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1664
1665 /* The item selected in the popup menu. */
1666 static 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. */
1672 struct next_popup_x_y
1673 {
1674 FRAME_PTR f;
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
1686 to store in *X and *Y are as well as the frame for the popup.
1687
1688 Here only X and Y are used. */
1689 static void
1690 menu_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 {
1697 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
1698 GtkRequisition req;
1699 int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
1700 int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
1701
1702 *x = data->x;
1703 *y = data->y;
1704
1705 /* Check if there is room for the menu. If not, adjust x/y so that
1706 the menu is fully visible. */
1707 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1708 if (data->x + req.width > disp_width)
1709 *x -= data->x + req.width - disp_width;
1710 if (data->y + req.height > disp_height)
1711 *y -= data->y + req.height - disp_height;
1712 }
1713
1714 static void
1715 popup_selection_callback (widget, client_data)
1716 GtkWidget *widget;
1717 gpointer client_data;
1718 {
1719 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1720
1721 if (xg_crazy_callback_abort) return;
1722 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
1723 }
1724
1725 static Lisp_Object
1726 pop_down_menu (arg)
1727 Lisp_Object arg;
1728 {
1729 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1730
1731 popup_activated_flag = 0;
1732 BLOCK_INPUT;
1733 gtk_widget_destroy (GTK_WIDGET (p->pointer));
1734 UNBLOCK_INPUT;
1735 return Qnil;
1736 }
1737
1738 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1739 menu pops down.
1740 menu_item_selection will be set to the selection. */
1741 static void
1742 create_and_show_popup_menu (f, first_wv, x, y, for_click)
1743 FRAME_PTR f;
1744 widget_value *first_wv;
1745 int x;
1746 int y;
1747 int for_click;
1748 {
1749 int i;
1750 GtkWidget *menu;
1751 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
1752 struct next_popup_x_y popup_x_y;
1753 int specpdl_count = SPECPDL_INDEX ();
1754
1755 if (! FRAME_X_P (f))
1756 abort ();
1757
1758 xg_crazy_callback_abort = 1;
1759 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
1760 G_CALLBACK (popup_selection_callback),
1761 G_CALLBACK (popup_deactivate_callback),
1762 G_CALLBACK (menu_highlight_callback));
1763 xg_crazy_callback_abort = 0;
1764
1765 if (! for_click)
1766 {
1767 /* Not invoked by a click. pop up at x/y. */
1768 pos_func = menu_position_func;
1769
1770 /* Adjust coordinates to be root-window-relative. */
1771 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1772 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1773
1774 popup_x_y.x = x;
1775 popup_x_y.y = y;
1776 popup_x_y.f = f;
1777
1778 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1779 }
1780 else
1781 {
1782 for (i = 0; i < 5; i++)
1783 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
1784 break;
1785 }
1786
1787 /* Display the menu. */
1788 gtk_widget_show_all (menu);
1789 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
1790
1791 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
1792
1793 if (GTK_WIDGET_MAPPED (menu))
1794 {
1795 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1796 two. show_help_echo uses this to detect popup menus. */
1797 popup_activated_flag = 1;
1798 /* Process events that apply to the menu. */
1799 popup_widget_loop (1, menu);
1800 }
1801
1802 unbind_to (specpdl_count, Qnil);
1803
1804 /* Must reset this manually because the button release event is not passed
1805 to Emacs event loop. */
1806 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
1807 }
1808
1809 #else /* not USE_GTK */
1810
1811 /* We need a unique id for each widget handled by the Lucid Widget
1812 library.
1813
1814 For the main windows, and popup menus, we use this counter,
1815 which we increment each time after use. This starts from 1<<16.
1816
1817 For menu bars, we use numbers starting at 0, counted in
1818 next_menubar_widget_id. */
1819 LWLIB_ID widget_id_tick;
1820
1821 static void
1822 popup_selection_callback (widget, id, client_data)
1823 Widget widget;
1824 LWLIB_ID id;
1825 XtPointer client_data;
1826 {
1827 menu_item_selection = (Lisp_Object *) client_data;
1828 }
1829
1830 /* ARG is the LWLIB ID of the dialog box, represented
1831 as a Lisp object as (HIGHPART . LOWPART). */
1832
1833 static Lisp_Object
1834 pop_down_menu (arg)
1835 Lisp_Object arg;
1836 {
1837 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
1838 | XINT (XCDR (arg)));
1839
1840 BLOCK_INPUT;
1841 lw_destroy_all_widgets (id);
1842 UNBLOCK_INPUT;
1843 popup_activated_flag = 0;
1844
1845 return Qnil;
1846 }
1847
1848 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1849 menu pops down.
1850 menu_item_selection will be set to the selection. */
1851 static void
1852 create_and_show_popup_menu (f, first_wv, x, y, for_click)
1853 FRAME_PTR f;
1854 widget_value *first_wv;
1855 int x;
1856 int y;
1857 int for_click;
1858 {
1859 int i;
1860 Arg av[2];
1861 int ac = 0;
1862 XButtonPressedEvent dummy;
1863 LWLIB_ID menu_id;
1864 Widget menu;
1865
1866 if (! FRAME_X_P (f))
1867 abort ();
1868
1869 menu_id = widget_id_tick++;
1870 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1871 f->output_data.x->widget, 1, 0,
1872 popup_selection_callback,
1873 popup_deactivate_callback,
1874 menu_highlight_callback);
1875
1876 dummy.type = ButtonPress;
1877 dummy.serial = 0;
1878 dummy.send_event = 0;
1879 dummy.display = FRAME_X_DISPLAY (f);
1880 dummy.time = CurrentTime;
1881 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
1882 dummy.window = dummy.root;
1883 dummy.subwindow = dummy.root;
1884 dummy.x = x;
1885 dummy.y = y;
1886
1887 /* Adjust coordinates to be root-window-relative. */
1888 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1889 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1890
1891 dummy.x_root = x;
1892 dummy.y_root = y;
1893
1894 dummy.state = 0;
1895 dummy.button = 0;
1896 for (i = 0; i < 5; i++)
1897 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
1898 dummy.button = i;
1899
1900 /* Don't allow any geometry request from the user. */
1901 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1902 XtSetValues (menu, av, ac);
1903
1904 /* Display the menu. */
1905 lw_popup_menu (menu, (XEvent *) &dummy);
1906 popup_activated_flag = 1;
1907 x_activate_timeout_atimer ();
1908
1909 {
1910 int fact = 4 * sizeof (LWLIB_ID);
1911 int specpdl_count = SPECPDL_INDEX ();
1912 record_unwind_protect (pop_down_menu,
1913 Fcons (make_number (menu_id >> (fact)),
1914 make_number (menu_id & ~(-1 << (fact)))));
1915
1916 /* Process events that apply to the menu. */
1917 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
1918
1919 unbind_to (specpdl_count, Qnil);
1920 }
1921 }
1922
1923 #endif /* not USE_GTK */
1924
1925 static Lisp_Object
1926 xmenu_show (f, x, y, for_click, keymaps, title, error)
1927 FRAME_PTR f;
1928 int x;
1929 int y;
1930 int for_click;
1931 int keymaps;
1932 Lisp_Object title;
1933 char **error;
1934 {
1935 int i;
1936 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1937 widget_value **submenu_stack
1938 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1939 Lisp_Object *subprefix_stack
1940 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1941 int submenu_depth = 0;
1942
1943 int first_pane;
1944
1945 if (! FRAME_X_P (f))
1946 abort ();
1947
1948 *error = NULL;
1949
1950 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1951 {
1952 *error = "Empty menu";
1953 return Qnil;
1954 }
1955
1956 /* Create a tree of widget_value objects
1957 representing the panes and their items. */
1958 wv = xmalloc_widget_value ();
1959 wv->name = "menu";
1960 wv->value = 0;
1961 wv->enabled = 1;
1962 wv->button_type = BUTTON_TYPE_NONE;
1963 wv->help =Qnil;
1964 first_wv = wv;
1965 first_pane = 1;
1966
1967 /* Loop over all panes and items, filling in the tree. */
1968 i = 0;
1969 while (i < menu_items_used)
1970 {
1971 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1972 {
1973 submenu_stack[submenu_depth++] = save_wv;
1974 save_wv = prev_wv;
1975 prev_wv = 0;
1976 first_pane = 1;
1977 i++;
1978 }
1979 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1980 {
1981 prev_wv = save_wv;
1982 save_wv = submenu_stack[--submenu_depth];
1983 first_pane = 0;
1984 i++;
1985 }
1986 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1987 && submenu_depth != 0)
1988 i += MENU_ITEMS_PANE_LENGTH;
1989 /* Ignore a nil in the item list.
1990 It's meaningful only for dialog boxes. */
1991 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1992 i += 1;
1993 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1994 {
1995 /* Create a new pane. */
1996 Lisp_Object pane_name, prefix;
1997 char *pane_string;
1998
1999 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
2000 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
2001
2002 #ifndef HAVE_MULTILINGUAL_MENU
2003 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
2004 {
2005 pane_name = ENCODE_MENU_STRING (pane_name);
2006 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
2007 }
2008 #endif
2009 pane_string = (NILP (pane_name)
2010 ? "" : (char *) SDATA (pane_name));
2011 /* If there is just one top-level pane, put all its items directly
2012 under the top-level menu. */
2013 if (menu_items_n_panes == 1)
2014 pane_string = "";
2015
2016 /* If the pane has a meaningful name,
2017 make the pane a top-level menu item
2018 with its items as a submenu beneath it. */
2019 if (!keymaps && strcmp (pane_string, ""))
2020 {
2021 wv = xmalloc_widget_value ();
2022 if (save_wv)
2023 save_wv->next = wv;
2024 else
2025 first_wv->contents = wv;
2026 wv->name = pane_string;
2027 if (keymaps && !NILP (prefix))
2028 wv->name++;
2029 wv->value = 0;
2030 wv->enabled = 1;
2031 wv->button_type = BUTTON_TYPE_NONE;
2032 wv->help = Qnil;
2033 save_wv = wv;
2034 prev_wv = 0;
2035 }
2036 else if (first_pane)
2037 {
2038 save_wv = wv;
2039 prev_wv = 0;
2040 }
2041 first_pane = 0;
2042 i += MENU_ITEMS_PANE_LENGTH;
2043 }
2044 else
2045 {
2046 /* Create a new item within current pane. */
2047 Lisp_Object item_name, enable, descrip, def, type, selected, help;
2048 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
2049 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
2050 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
2051 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
2052 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
2053 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
2054 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
2055
2056 #ifndef HAVE_MULTILINGUAL_MENU
2057 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
2058 {
2059 item_name = ENCODE_MENU_STRING (item_name);
2060 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
2061 }
2062
2063 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
2064 {
2065 descrip = ENCODE_MENU_STRING (descrip);
2066 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
2067 }
2068 #endif /* not HAVE_MULTILINGUAL_MENU */
2069
2070 wv = xmalloc_widget_value ();
2071 if (prev_wv)
2072 prev_wv->next = wv;
2073 else
2074 save_wv->contents = wv;
2075 wv->name = (char *) SDATA (item_name);
2076 if (!NILP (descrip))
2077 wv->key = (char *) SDATA (descrip);
2078 wv->value = 0;
2079 /* If this item has a null value,
2080 make the call_data null so that it won't display a box
2081 when the mouse is on it. */
2082 wv->call_data
2083 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
2084 wv->enabled = !NILP (enable);
2085
2086 if (NILP (type))
2087 wv->button_type = BUTTON_TYPE_NONE;
2088 else if (EQ (type, QCtoggle))
2089 wv->button_type = BUTTON_TYPE_TOGGLE;
2090 else if (EQ (type, QCradio))
2091 wv->button_type = BUTTON_TYPE_RADIO;
2092 else
2093 abort ();
2094
2095 wv->selected = !NILP (selected);
2096
2097 if (! STRINGP (help))
2098 help = Qnil;
2099
2100 wv->help = help;
2101
2102 prev_wv = wv;
2103
2104 i += MENU_ITEMS_ITEM_LENGTH;
2105 }
2106 }
2107
2108 /* Deal with the title, if it is non-nil. */
2109 if (!NILP (title))
2110 {
2111 widget_value *wv_title = xmalloc_widget_value ();
2112 widget_value *wv_sep1 = xmalloc_widget_value ();
2113 widget_value *wv_sep2 = xmalloc_widget_value ();
2114
2115 wv_sep2->name = "--";
2116 wv_sep2->next = first_wv->contents;
2117 wv_sep2->help = Qnil;
2118
2119 wv_sep1->name = "--";
2120 wv_sep1->next = wv_sep2;
2121 wv_sep1->help = Qnil;
2122
2123 #ifndef HAVE_MULTILINGUAL_MENU
2124 if (STRING_MULTIBYTE (title))
2125 title = ENCODE_MENU_STRING (title);
2126 #endif
2127
2128 wv_title->name = (char *) SDATA (title);
2129 wv_title->enabled = TRUE;
2130 wv_title->button_type = BUTTON_TYPE_NONE;
2131 wv_title->help = Qnil;
2132 wv_title->next = wv_sep1;
2133 first_wv->contents = wv_title;
2134 }
2135
2136 /* No selection has been chosen yet. */
2137 menu_item_selection = 0;
2138
2139 /* Actually create and show the menu until popped down. */
2140 create_and_show_popup_menu (f, first_wv, x, y, for_click);
2141
2142 /* Free the widget_value objects we used to specify the contents. */
2143 free_menubar_widget_value_tree (first_wv);
2144
2145 /* Find the selected item, and its pane, to return
2146 the proper value. */
2147 if (menu_item_selection != 0)
2148 {
2149 Lisp_Object prefix, entry;
2150
2151 prefix = entry = Qnil;
2152 i = 0;
2153 while (i < menu_items_used)
2154 {
2155 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
2156 {
2157 subprefix_stack[submenu_depth++] = prefix;
2158 prefix = entry;
2159 i++;
2160 }
2161 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
2162 {
2163 prefix = subprefix_stack[--submenu_depth];
2164 i++;
2165 }
2166 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2167 {
2168 prefix
2169 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2170 i += MENU_ITEMS_PANE_LENGTH;
2171 }
2172 /* Ignore a nil in the item list.
2173 It's meaningful only for dialog boxes. */
2174 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2175 i += 1;
2176 else
2177 {
2178 entry
2179 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2180 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2181 {
2182 if (keymaps != 0)
2183 {
2184 int j;
2185
2186 entry = Fcons (entry, Qnil);
2187 if (!NILP (prefix))
2188 entry = Fcons (prefix, entry);
2189 for (j = submenu_depth - 1; j >= 0; j--)
2190 if (!NILP (subprefix_stack[j]))
2191 entry = Fcons (subprefix_stack[j], entry);
2192 }
2193 return entry;
2194 }
2195 i += MENU_ITEMS_ITEM_LENGTH;
2196 }
2197 }
2198 }
2199 else if (!for_click)
2200 /* Make "Cancel" equivalent to C-g. */
2201 Fsignal (Qquit, Qnil);
2202
2203 return Qnil;
2204 }
2205 \f
2206 #ifdef USE_GTK
2207 static void
2208 dialog_selection_callback (widget, client_data)
2209 GtkWidget *widget;
2210 gpointer client_data;
2211 {
2212 /* The EMACS_INT cast avoids a warning. There's no problem
2213 as long as pointers have enough bits to hold small integers. */
2214 if ((int) (EMACS_INT) client_data != -1)
2215 menu_item_selection = (Lisp_Object *) client_data;
2216
2217 popup_activated_flag = 0;
2218 }
2219
2220 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2221 dialog pops down.
2222 menu_item_selection will be set to the selection. */
2223 static void
2224 create_and_show_dialog (f, first_wv)
2225 FRAME_PTR f;
2226 widget_value *first_wv;
2227 {
2228 GtkWidget *menu;
2229
2230 if (! FRAME_X_P (f))
2231 abort ();
2232
2233 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
2234 G_CALLBACK (dialog_selection_callback),
2235 G_CALLBACK (popup_deactivate_callback),
2236 0);
2237
2238 if (menu)
2239 {
2240 int specpdl_count = SPECPDL_INDEX ();
2241 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
2242
2243 /* Display the menu. */
2244 gtk_widget_show_all (menu);
2245
2246 /* Process events that apply to the menu. */
2247 popup_widget_loop (1, menu);
2248
2249 unbind_to (specpdl_count, Qnil);
2250 }
2251 }
2252
2253 #else /* not USE_GTK */
2254 static void
2255 dialog_selection_callback (widget, id, client_data)
2256 Widget widget;
2257 LWLIB_ID id;
2258 XtPointer client_data;
2259 {
2260 /* The EMACS_INT cast avoids a warning. There's no problem
2261 as long as pointers have enough bits to hold small integers. */
2262 if ((int) (EMACS_INT) client_data != -1)
2263 menu_item_selection = (Lisp_Object *) client_data;
2264
2265 BLOCK_INPUT;
2266 lw_destroy_all_widgets (id);
2267 UNBLOCK_INPUT;
2268 popup_activated_flag = 0;
2269 }
2270
2271
2272 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
2273 dialog pops down.
2274 menu_item_selection will be set to the selection. */
2275 static void
2276 create_and_show_dialog (f, first_wv)
2277 FRAME_PTR f;
2278 widget_value *first_wv;
2279 {
2280 LWLIB_ID dialog_id;
2281
2282 if (!FRAME_X_P (f))
2283 abort();
2284
2285 dialog_id = widget_id_tick++;
2286 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
2287 f->output_data.x->widget, 1, 0,
2288 dialog_selection_callback, 0, 0);
2289 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
2290
2291 /* Display the dialog box. */
2292 lw_pop_up_all_widgets (dialog_id);
2293 popup_activated_flag = 1;
2294 x_activate_timeout_atimer ();
2295
2296 /* Process events that apply to the dialog box.
2297 Also handle timers. */
2298 {
2299 int count = SPECPDL_INDEX ();
2300 int fact = 4 * sizeof (LWLIB_ID);
2301
2302 /* xdialog_show_unwind is responsible for popping the dialog box down. */
2303 record_unwind_protect (pop_down_menu,
2304 Fcons (make_number (dialog_id >> (fact)),
2305 make_number (dialog_id & ~(-1 << (fact)))));
2306
2307 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
2308 dialog_id, 1);
2309
2310 unbind_to (count, Qnil);
2311 }
2312 }
2313
2314 #endif /* not USE_GTK */
2315
2316 static char * button_names [] = {
2317 "button1", "button2", "button3", "button4", "button5",
2318 "button6", "button7", "button8", "button9", "button10" };
2319
2320 static Lisp_Object
2321 xdialog_show (f, keymaps, title, header, error_name)
2322 FRAME_PTR f;
2323 int keymaps;
2324 Lisp_Object title, header;
2325 char **error_name;
2326 {
2327 int i, nb_buttons=0;
2328 char dialog_name[6];
2329
2330 widget_value *wv, *first_wv = 0, *prev_wv = 0;
2331
2332 /* Number of elements seen so far, before boundary. */
2333 int left_count = 0;
2334 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2335 int boundary_seen = 0;
2336
2337 if (! FRAME_X_P (f))
2338 abort ();
2339
2340 *error_name = NULL;
2341
2342 if (menu_items_n_panes > 1)
2343 {
2344 *error_name = "Multiple panes in dialog box";
2345 return Qnil;
2346 }
2347
2348 /* Create a tree of widget_value objects
2349 representing the text label and buttons. */
2350 {
2351 Lisp_Object pane_name, prefix;
2352 char *pane_string;
2353 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2354 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2355 pane_string = (NILP (pane_name)
2356 ? "" : (char *) SDATA (pane_name));
2357 prev_wv = xmalloc_widget_value ();
2358 prev_wv->value = pane_string;
2359 if (keymaps && !NILP (prefix))
2360 prev_wv->name++;
2361 prev_wv->enabled = 1;
2362 prev_wv->name = "message";
2363 prev_wv->help = Qnil;
2364 first_wv = prev_wv;
2365
2366 /* Loop over all panes and items, filling in the tree. */
2367 i = MENU_ITEMS_PANE_LENGTH;
2368 while (i < menu_items_used)
2369 {
2370
2371 /* Create a new item within current pane. */
2372 Lisp_Object item_name, enable, descrip;
2373 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2374 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2375 descrip
2376 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2377
2378 if (NILP (item_name))
2379 {
2380 free_menubar_widget_value_tree (first_wv);
2381 *error_name = "Submenu in dialog items";
2382 return Qnil;
2383 }
2384 if (EQ (item_name, Qquote))
2385 {
2386 /* This is the boundary between left-side elts
2387 and right-side elts. Stop incrementing right_count. */
2388 boundary_seen = 1;
2389 i++;
2390 continue;
2391 }
2392 if (nb_buttons >= 9)
2393 {
2394 free_menubar_widget_value_tree (first_wv);
2395 *error_name = "Too many dialog items";
2396 return Qnil;
2397 }
2398
2399 wv = xmalloc_widget_value ();
2400 prev_wv->next = wv;
2401 wv->name = (char *) button_names[nb_buttons];
2402 if (!NILP (descrip))
2403 wv->key = (char *) SDATA (descrip);
2404 wv->value = (char *) SDATA (item_name);
2405 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2406 wv->enabled = !NILP (enable);
2407 wv->help = Qnil;
2408 prev_wv = wv;
2409
2410 if (! boundary_seen)
2411 left_count++;
2412
2413 nb_buttons++;
2414 i += MENU_ITEMS_ITEM_LENGTH;
2415 }
2416
2417 /* If the boundary was not specified,
2418 by default put half on the left and half on the right. */
2419 if (! boundary_seen)
2420 left_count = nb_buttons - nb_buttons / 2;
2421
2422 wv = xmalloc_widget_value ();
2423 wv->name = dialog_name;
2424 wv->help = Qnil;
2425
2426 /* Frame title: 'Q' = Question, 'I' = Information.
2427 Can also have 'E' = Error if, one day, we want
2428 a popup for errors. */
2429 if (NILP(header))
2430 dialog_name[0] = 'Q';
2431 else
2432 dialog_name[0] = 'I';
2433
2434 /* Dialog boxes use a really stupid name encoding
2435 which specifies how many buttons to use
2436 and how many buttons are on the right. */
2437 dialog_name[1] = '0' + nb_buttons;
2438 dialog_name[2] = 'B';
2439 dialog_name[3] = 'R';
2440 /* Number of buttons to put on the right. */
2441 dialog_name[4] = '0' + nb_buttons - left_count;
2442 dialog_name[5] = 0;
2443 wv->contents = first_wv;
2444 first_wv = wv;
2445 }
2446
2447 /* No selection has been chosen yet. */
2448 menu_item_selection = 0;
2449
2450 /* Force a redisplay before showing the dialog. If a frame is created
2451 just before showing the dialog, its contents may not have been fully
2452 drawn, as this depends on timing of events from the X server. Redisplay
2453 is not done when a dialog is shown. If redisplay could be done in the
2454 X event loop (i.e. the X event loop does not run in a signal handler)
2455 this would not be needed. */
2456 Fredisplay (Qt);
2457
2458 /* Actually create and show the dialog. */
2459 create_and_show_dialog (f, first_wv);
2460
2461 /* Free the widget_value objects we used to specify the contents. */
2462 free_menubar_widget_value_tree (first_wv);
2463
2464 /* Find the selected item, and its pane, to return
2465 the proper value. */
2466 if (menu_item_selection != 0)
2467 {
2468 Lisp_Object prefix;
2469
2470 prefix = Qnil;
2471 i = 0;
2472 while (i < menu_items_used)
2473 {
2474 Lisp_Object entry;
2475
2476 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2477 {
2478 prefix
2479 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2480 i += MENU_ITEMS_PANE_LENGTH;
2481 }
2482 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2483 {
2484 /* This is the boundary between left-side elts and
2485 right-side elts. */
2486 ++i;
2487 }
2488 else
2489 {
2490 entry
2491 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2492 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2493 {
2494 if (keymaps != 0)
2495 {
2496 entry = Fcons (entry, Qnil);
2497 if (!NILP (prefix))
2498 entry = Fcons (prefix, entry);
2499 }
2500 return entry;
2501 }
2502 i += MENU_ITEMS_ITEM_LENGTH;
2503 }
2504 }
2505 }
2506 else
2507 /* Make "Cancel" equivalent to C-g. */
2508 Fsignal (Qquit, Qnil);
2509
2510 return Qnil;
2511 }
2512
2513 #else /* not USE_X_TOOLKIT && not USE_GTK */
2514
2515 /* The frame of the last activated non-toolkit menu bar.
2516 Used to generate menu help events. */
2517
2518 static struct frame *menu_help_frame;
2519
2520
2521 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2522
2523 PANE is the pane number, and ITEM is the menu item number in
2524 the menu (currently not used).
2525
2526 This cannot be done with generating a HELP_EVENT because
2527 XMenuActivate contains a loop that doesn't let Emacs process
2528 keyboard events. */
2529
2530 static void
2531 menu_help_callback (help_string, pane, item)
2532 char *help_string;
2533 int pane, item;
2534 {
2535 extern Lisp_Object Qmenu_item;
2536 Lisp_Object *first_item;
2537 Lisp_Object pane_name;
2538 Lisp_Object menu_object;
2539
2540 first_item = XVECTOR (menu_items)->contents;
2541 if (EQ (first_item[0], Qt))
2542 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2543 else if (EQ (first_item[0], Qquote))
2544 /* This shouldn't happen, see xmenu_show. */
2545 pane_name = empty_unibyte_string;
2546 else
2547 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2548
2549 /* (menu-item MENU-NAME PANE-NUMBER) */
2550 menu_object = Fcons (Qmenu_item,
2551 Fcons (pane_name,
2552 Fcons (make_number (pane), Qnil)));
2553 show_help_echo (help_string ? build_string (help_string) : Qnil,
2554 Qnil, menu_object, make_number (item), 1);
2555 }
2556
2557 static Lisp_Object
2558 pop_down_menu (arg)
2559 Lisp_Object arg;
2560 {
2561 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
2562 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
2563
2564 FRAME_PTR f = p1->pointer;
2565 XMenu *menu = p2->pointer;
2566
2567 BLOCK_INPUT;
2568 #ifndef MSDOS
2569 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
2570 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
2571 #endif
2572 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2573
2574 #ifdef HAVE_X_WINDOWS
2575 /* Assume the mouse has moved out of the X window.
2576 If it has actually moved in, we will get an EnterNotify. */
2577 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2578
2579 /* State that no mouse buttons are now held.
2580 (The oldXMenu code doesn't track this info for us.)
2581 That is not necessarily true, but the fiction leads to reasonable
2582 results, and it is a pain to ask which are actually held now. */
2583 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2584
2585 #endif /* HAVE_X_WINDOWS */
2586
2587 UNBLOCK_INPUT;
2588
2589 return Qnil;
2590 }
2591
2592
2593 static Lisp_Object
2594 xmenu_show (f, x, y, for_click, keymaps, title, error)
2595 FRAME_PTR f;
2596 int x, y;
2597 int for_click;
2598 int keymaps;
2599 Lisp_Object title;
2600 char **error;
2601 {
2602 Window root;
2603 XMenu *menu;
2604 int pane, selidx, lpane, status;
2605 Lisp_Object entry, pane_prefix;
2606 char *datap;
2607 int ulx, uly, width, height;
2608 int dispwidth, dispheight;
2609 int i, j, lines, maxlines;
2610 int maxwidth;
2611 int dummy_int;
2612 unsigned int dummy_uint;
2613 int specpdl_count = SPECPDL_INDEX ();
2614
2615 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
2616 abort ();
2617
2618 *error = 0;
2619 if (menu_items_n_panes == 0)
2620 return Qnil;
2621
2622 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2623 {
2624 *error = "Empty menu";
2625 return Qnil;
2626 }
2627
2628 /* Figure out which root window F is on. */
2629 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2630 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2631 &dummy_uint, &dummy_uint);
2632
2633 /* Make the menu on that window. */
2634 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2635 if (menu == NULL)
2636 {
2637 *error = "Can't create menu";
2638 return Qnil;
2639 }
2640
2641 /* Don't GC while we prepare and show the menu,
2642 because we give the oldxmenu library pointers to the
2643 contents of strings. */
2644 inhibit_garbage_collection ();
2645
2646 #ifdef HAVE_X_WINDOWS
2647 /* Adjust coordinates to relative to the outer (window manager) window. */
2648 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
2649 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
2650 #endif /* HAVE_X_WINDOWS */
2651
2652 /* Adjust coordinates to be root-window-relative. */
2653 x += f->left_pos;
2654 y += f->top_pos;
2655
2656 /* Create all the necessary panes and their items. */
2657 maxlines = lines = i = 0;
2658 while (i < menu_items_used)
2659 {
2660 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2661 {
2662 /* Create a new pane. */
2663 Lisp_Object pane_name, prefix;
2664 char *pane_string;
2665
2666 maxlines = max (maxlines, lines);
2667 lines = 0;
2668 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2669 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2670 pane_string = (NILP (pane_name)
2671 ? "" : (char *) SDATA (pane_name));
2672 if (keymaps && !NILP (prefix))
2673 pane_string++;
2674
2675 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2676 if (lpane == XM_FAILURE)
2677 {
2678 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2679 *error = "Can't create pane";
2680 return Qnil;
2681 }
2682 i += MENU_ITEMS_PANE_LENGTH;
2683
2684 /* Find the width of the widest item in this pane. */
2685 maxwidth = 0;
2686 j = i;
2687 while (j < menu_items_used)
2688 {
2689 Lisp_Object item;
2690 item = XVECTOR (menu_items)->contents[j];
2691 if (EQ (item, Qt))
2692 break;
2693 if (NILP (item))
2694 {
2695 j++;
2696 continue;
2697 }
2698 width = SBYTES (item);
2699 if (width > maxwidth)
2700 maxwidth = width;
2701
2702 j += MENU_ITEMS_ITEM_LENGTH;
2703 }
2704 }
2705 /* Ignore a nil in the item list.
2706 It's meaningful only for dialog boxes. */
2707 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2708 i += 1;
2709 else
2710 {
2711 /* Create a new item within current pane. */
2712 Lisp_Object item_name, enable, descrip, help;
2713 unsigned char *item_data;
2714 char *help_string;
2715
2716 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2717 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2718 descrip
2719 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2720 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2721 help_string = STRINGP (help) ? SDATA (help) : NULL;
2722
2723 if (!NILP (descrip))
2724 {
2725 int gap = maxwidth - SBYTES (item_name);
2726 /* if alloca is fast, use that to make the space,
2727 to reduce gc needs. */
2728 item_data
2729 = (unsigned char *) alloca (maxwidth
2730 + SBYTES (descrip) + 1);
2731 bcopy (SDATA (item_name), item_data,
2732 SBYTES (item_name));
2733 for (j = SCHARS (item_name); j < maxwidth; j++)
2734 item_data[j] = ' ';
2735 bcopy (SDATA (descrip), item_data + j,
2736 SBYTES (descrip));
2737 item_data[j + SBYTES (descrip)] = 0;
2738 }
2739 else
2740 item_data = SDATA (item_name);
2741
2742 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2743 menu, lpane, 0, item_data,
2744 !NILP (enable), help_string)
2745 == XM_FAILURE)
2746 {
2747 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2748 *error = "Can't add selection to menu";
2749 return Qnil;
2750 }
2751 i += MENU_ITEMS_ITEM_LENGTH;
2752 lines++;
2753 }
2754 }
2755
2756 maxlines = max (maxlines, lines);
2757
2758 /* All set and ready to fly. */
2759 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2760 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2761 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2762 x = min (x, dispwidth);
2763 y = min (y, dispheight);
2764 x = max (x, 1);
2765 y = max (y, 1);
2766 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2767 &ulx, &uly, &width, &height);
2768 if (ulx+width > dispwidth)
2769 {
2770 x -= (ulx + width) - dispwidth;
2771 ulx = dispwidth - width;
2772 }
2773 if (uly+height > dispheight)
2774 {
2775 y -= (uly + height) - dispheight;
2776 uly = dispheight - height;
2777 }
2778 if (ulx < 0) x -= ulx;
2779 if (uly < 0) y -= uly;
2780
2781 if (! for_click)
2782 {
2783 /* If position was not given by a mouse click, adjust so upper left
2784 corner of the menu as a whole ends up at given coordinates. This
2785 is what x-popup-menu says in its documentation. */
2786 x += width/2;
2787 y += 1.5*height/(maxlines+2);
2788 }
2789
2790 XMenuSetAEQ (menu, TRUE);
2791 XMenuSetFreeze (menu, TRUE);
2792 pane = selidx = 0;
2793
2794 #ifndef MSDOS
2795 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
2796 #endif
2797
2798 record_unwind_protect (pop_down_menu,
2799 Fcons (make_save_value (f, 0),
2800 make_save_value (menu, 0)));
2801
2802 /* Help display under X won't work because XMenuActivate contains
2803 a loop that doesn't give Emacs a chance to process it. */
2804 menu_help_frame = f;
2805 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2806 x, y, ButtonReleaseMask, &datap,
2807 menu_help_callback);
2808
2809 switch (status)
2810 {
2811 case XM_SUCCESS:
2812 #ifdef XDEBUG
2813 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2814 #endif
2815
2816 /* Find the item number SELIDX in pane number PANE. */
2817 i = 0;
2818 while (i < menu_items_used)
2819 {
2820 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2821 {
2822 if (pane == 0)
2823 pane_prefix
2824 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2825 pane--;
2826 i += MENU_ITEMS_PANE_LENGTH;
2827 }
2828 else
2829 {
2830 if (pane == -1)
2831 {
2832 if (selidx == 0)
2833 {
2834 entry
2835 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2836 if (keymaps != 0)
2837 {
2838 entry = Fcons (entry, Qnil);
2839 if (!NILP (pane_prefix))
2840 entry = Fcons (pane_prefix, entry);
2841 }
2842 break;
2843 }
2844 selidx--;
2845 }
2846 i += MENU_ITEMS_ITEM_LENGTH;
2847 }
2848 }
2849 break;
2850
2851 case XM_FAILURE:
2852 *error = "Can't activate menu";
2853 case XM_IA_SELECT:
2854 entry = Qnil;
2855 break;
2856 case XM_NO_SELECT:
2857 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2858 the menu was invoked with a mouse event as POSITION). */
2859 if (! for_click)
2860 Fsignal (Qquit, Qnil);
2861 entry = Qnil;
2862 break;
2863 }
2864
2865 unbind_to (specpdl_count, Qnil);
2866
2867 return entry;
2868 }
2869
2870 #endif /* not USE_X_TOOLKIT */
2871
2872 #endif /* HAVE_MENUS */
2873
2874 /* Detect if a dialog or menu has been posted. */
2875
2876 int
2877 popup_activated ()
2878 {
2879 return popup_activated_flag;
2880 }
2881
2882 /* The following is used by delayed window autoselection. */
2883
2884 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
2885 doc: /* Return t if a menu or popup dialog is active. */)
2886 ()
2887 {
2888 #ifdef HAVE_MENUS
2889 return (popup_activated ()) ? Qt : Qnil;
2890 #else
2891 return Qnil;
2892 #endif /* HAVE_MENUS */
2893 }
2894 \f
2895 void
2896 syms_of_xmenu ()
2897 {
2898 Qdebug_on_next_call = intern ("debug-on-next-call");
2899 staticpro (&Qdebug_on_next_call);
2900
2901 #ifdef USE_X_TOOLKIT
2902 widget_id_tick = (1<<16);
2903 next_menubar_widget_id = 1;
2904 #endif
2905
2906 defsubr (&Sx_popup_menu);
2907 defsubr (&Smenu_or_popup_active_p);
2908
2909 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
2910 defsubr (&Sx_menu_bar_open_internal);
2911 Ffset (intern ("accelerate-menu"),
2912 intern (Sx_menu_bar_open_internal.symbol_name));
2913 #endif
2914
2915 #ifdef HAVE_MENUS
2916 defsubr (&Sx_popup_dialog);
2917 #endif
2918 }
2919
2920 /* arch-tag: 92ea573c-398e-496e-ac73-2436f7d63242
2921 (do not change this comment) */