Merge changes from emacs-23 branch
[emacs.git] / src / xmenu.c
blobad1a764eab8e31ecb39741eb15bb018b8972e035
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1986, 1988, 1993-1994, 1996, 1999-2011
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* X pop-up deck-of-cards menu facility for GNU Emacs.
23 * Written by Jon Arnold and Roman Budzianowski
24 * Mods and rewrite by Robert Krawitz
28 /* Modified by Fred Pierresteguy on December 93
29 to make the popup menus and menubar use the Xt. */
31 /* Rewritten for clarity and GC protection by rms in Feb 94. */
33 #include <config.h>
35 #if 0 /* Why was this included? And without syssignal.h? */
36 /* On 4.3 this loses if it comes after xterm.h. */
37 #include <signal.h>
38 #endif
40 #include <stdio.h>
41 #include <setjmp.h>
43 #include "lisp.h"
44 #include "keyboard.h"
45 #include "keymap.h"
46 #include "frame.h"
47 #include "termhooks.h"
48 #include "window.h"
49 #include "blockinput.h"
50 #include "buffer.h"
51 #include "charset.h"
52 #include "coding.h"
53 #include "sysselect.h"
55 #ifdef MSDOS
56 #include "msdos.h"
57 #endif
59 #ifdef HAVE_X_WINDOWS
60 /* This may include sys/types.h, and that somehow loses
61 if this is not done before the other system files. */
62 #include "xterm.h"
63 #endif
65 /* Load sys/types.h if not already loaded.
66 In some systems loading it twice is suicidal. */
67 #ifndef makedev
68 #include <sys/types.h>
69 #endif
71 #include "dispextern.h"
73 #ifdef HAVE_X_WINDOWS
74 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
75 code accepts the Emacs internal encoding. */
76 #undef HAVE_MULTILINGUAL_MENU
77 #ifdef USE_X_TOOLKIT
78 #include "widget.h"
79 #include <X11/Xlib.h>
80 #include <X11/IntrinsicP.h>
81 #include <X11/CoreP.h>
82 #include <X11/StringDefs.h>
83 #include <X11/Shell.h>
84 #ifdef USE_LUCID
85 #include "xsettings.h"
86 #include "../lwlib/xlwmenu.h"
87 #ifdef HAVE_XAW3D
88 #include <X11/Xaw3d/Paned.h>
89 #else /* !HAVE_XAW3D */
90 #include <X11/Xaw/Paned.h>
91 #endif /* HAVE_XAW3D */
92 #endif /* USE_LUCID */
93 #ifdef USE_MOTIF
94 #include "../lwlib/lwlib.h"
95 #endif
96 #else /* not USE_X_TOOLKIT */
97 #ifndef USE_GTK
98 #include "../oldXMenu/XMenu.h"
99 #endif
100 #endif /* not USE_X_TOOLKIT */
101 #endif /* HAVE_X_WINDOWS */
103 #ifdef USE_GTK
104 #include "gtkutil.h"
105 #endif
107 #include "menu.h"
109 #ifndef TRUE
110 #define TRUE 1
111 #define FALSE 0
112 #endif /* no TRUE */
114 Lisp_Object Qdebug_on_next_call;
116 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
117 static Lisp_Object xdialog_show (FRAME_PTR, int, Lisp_Object, Lisp_Object,
118 const char **);
119 #endif
121 static int update_frame_menubar (struct frame *);
123 /* Flag which when set indicates a dialog or menu has been posted by
124 Xt on behalf of one of the widget sets. */
125 static int popup_activated_flag;
127 static int next_menubar_widget_id;
130 #ifdef USE_X_TOOLKIT
132 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
134 static struct frame *
135 menubar_id_to_frame (LWLIB_ID id)
137 Lisp_Object tail, frame;
138 FRAME_PTR f;
140 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
142 frame = XCAR (tail);
143 if (!FRAMEP (frame))
144 continue;
145 f = XFRAME (frame);
146 if (!FRAME_WINDOW_P (f))
147 continue;
148 if (f->output_data.x->id == id)
149 return f;
151 return 0;
154 #endif
156 #ifdef HAVE_X_WINDOWS
157 /* Return the mouse position in *X and *Y. The coordinates are window
158 relative for the edit window in frame F.
159 This is for Fx_popup_menu. The mouse_position_hook can not
160 be used for X, as it returns window relative coordinates
161 for the window where the mouse is in. This could be the menu bar,
162 the scroll bar or the edit window. Fx_popup_menu needs to be
163 sure it is the edit window. */
164 void
165 mouse_position_for_popup (FRAME_PTR f, int *x, int *y)
167 Window root, dummy_window;
168 int dummy;
170 if (! FRAME_X_P (f))
171 abort ();
173 BLOCK_INPUT;
175 XQueryPointer (FRAME_X_DISPLAY (f),
176 DefaultRootWindow (FRAME_X_DISPLAY (f)),
178 /* The root window which contains the pointer. */
179 &root,
181 /* Window pointer is on, not used */
182 &dummy_window,
184 /* The position on that root window. */
185 x, y,
187 /* x/y in dummy_window coordinates, not used. */
188 &dummy, &dummy,
190 /* Modifier keys and pointer buttons, about which
191 we don't care. */
192 (unsigned int *) &dummy);
194 UNBLOCK_INPUT;
196 /* xmenu_show expects window coordinates, not root window
197 coordinates. Translate. */
198 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
199 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
202 #endif /* HAVE_X_WINDOWS */
204 #ifdef HAVE_MENUS
206 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
207 doc: /* Pop up a dialog box and return user's selection.
208 POSITION specifies which frame to use.
209 This is normally a mouse button event or a window or frame.
210 If POSITION is t, it means to use the frame the mouse is on.
211 The dialog box appears in the middle of the specified frame.
213 CONTENTS specifies the alternatives to display in the dialog box.
214 It is a list of the form (DIALOG ITEM1 ITEM2...).
215 Each ITEM is a cons cell (STRING . VALUE).
216 The return value is VALUE from the chosen item.
218 An ITEM may also be just a string--that makes a nonselectable item.
219 An ITEM may also be nil--that means to put all preceding items
220 on the left of the dialog box and all following items on the right.
221 \(By default, approximately half appear on each side.)
223 If HEADER is non-nil, the frame title for the box is "Information",
224 otherwise it is "Question".
226 If the user gets rid of the dialog box without making a valid choice,
227 for instance using the window manager, then this produces a quit and
228 `x-popup-dialog' does not return. */)
229 (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
231 FRAME_PTR f = NULL;
232 Lisp_Object window;
234 check_x ();
236 /* Decode the first argument: find the window or frame to use. */
237 if (EQ (position, Qt)
238 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
239 || EQ (XCAR (position), Qtool_bar))))
241 #if 0 /* Using the frame the mouse is on may not be right. */
242 /* Use the mouse's current position. */
243 FRAME_PTR new_f = SELECTED_FRAME ();
244 Lisp_Object bar_window;
245 enum scroll_bar_part part;
246 unsigned long time;
247 Lisp_Object x, y;
249 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
251 if (new_f != 0)
252 XSETFRAME (window, new_f);
253 else
254 window = selected_window;
255 #endif
256 window = selected_window;
258 else if (CONSP (position))
260 Lisp_Object tem;
261 tem = Fcar (position);
262 if (CONSP (tem))
263 window = Fcar (Fcdr (position));
264 else
266 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
267 window = Fcar (tem); /* POSN_WINDOW (tem) */
270 else if (WINDOWP (position) || FRAMEP (position))
271 window = position;
272 else
273 window = Qnil;
275 /* Decode where to put the menu. */
277 if (FRAMEP (window))
278 f = XFRAME (window);
279 else if (WINDOWP (window))
281 CHECK_LIVE_WINDOW (window);
282 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
284 else
285 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
286 but I don't want to make one now. */
287 CHECK_WINDOW (window);
289 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
290 error ("Can not put X dialog on this terminal");
292 /* Force a redisplay before showing the dialog. If a frame is created
293 just before showing the dialog, its contents may not have been fully
294 drawn, as this depends on timing of events from the X server. Redisplay
295 is not done when a dialog is shown. If redisplay could be done in the
296 X event loop (i.e. the X event loop does not run in a signal handler)
297 this would not be needed.
299 Do this before creating the widget value that points to Lisp
300 string contents, because Fredisplay may GC and relocate them. */
301 Fredisplay (Qt);
303 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
304 /* Display a menu with these alternatives
305 in the middle of frame F. */
307 Lisp_Object x, y, frame, newpos;
308 XSETFRAME (frame, f);
309 XSETINT (x, x_pixel_width (f) / 2);
310 XSETINT (y, x_pixel_height (f) / 2);
311 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
313 return Fx_popup_menu (newpos,
314 Fcons (Fcar (contents), Fcons (contents, Qnil)));
316 #else
318 Lisp_Object title;
319 const char *error_name;
320 Lisp_Object selection;
321 int specpdl_count = SPECPDL_INDEX ();
323 /* Decode the dialog items from what was specified. */
324 title = Fcar (contents);
325 CHECK_STRING (title);
326 record_unwind_protect (unuse_menu_items, Qnil);
328 if (NILP (Fcar (Fcdr (contents))))
329 /* No buttons specified, add an "Ok" button so users can pop down
330 the dialog. Also, the lesstif/motif version crashes if there are
331 no buttons. */
332 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
334 list_of_panes (Fcons (contents, Qnil));
336 /* Display them in a dialog box. */
337 BLOCK_INPUT;
338 selection = xdialog_show (f, 0, title, header, &error_name);
339 UNBLOCK_INPUT;
341 unbind_to (specpdl_count, Qnil);
342 discard_menu_items ();
344 if (error_name) error (error_name);
345 return selection;
347 #endif
351 #ifndef MSDOS
353 /* Set menu_items_inuse so no other popup menu or dialog is created. */
355 void
356 x_menu_set_in_use (int in_use)
358 menu_items_inuse = in_use ? Qt : Qnil;
359 popup_activated_flag = in_use;
360 #ifdef USE_X_TOOLKIT
361 if (popup_activated_flag)
362 x_activate_timeout_atimer ();
363 #endif
366 /* Wait for an X event to arrive or for a timer to expire. */
368 void
369 x_menu_wait_for_event (void *data)
371 /* Another way to do this is to register a timer callback, that can be
372 done in GTK and Xt. But we have to do it like this when using only X
373 anyway, and with callbacks we would have three variants for timer handling
374 instead of the small ifdefs below. */
376 while (
377 #ifdef USE_X_TOOLKIT
378 ! XtAppPending (Xt_app_con)
379 #elif defined USE_GTK
380 ! gtk_events_pending ()
381 #else
382 ! XPending ((Display*) data)
383 #endif
386 EMACS_TIME next_time = timer_check (1), *ntp;
387 long secs = EMACS_SECS (next_time);
388 long usecs = EMACS_USECS (next_time);
389 SELECT_TYPE read_fds;
390 struct x_display_info *dpyinfo;
391 int n = 0;
393 FD_ZERO (&read_fds);
394 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
396 int fd = ConnectionNumber (dpyinfo->display);
397 FD_SET (fd, &read_fds);
398 if (fd > n) n = fd;
399 XFlush (dpyinfo->display);
402 if (secs < 0 && usecs < 0)
403 ntp = 0;
404 else
405 ntp = &next_time;
407 select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, ntp);
410 #endif /* ! MSDOS */
413 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
415 #ifdef USE_X_TOOLKIT
417 /* Loop in Xt until the menu pulldown or dialog popup has been
418 popped down (deactivated). This is used for x-popup-menu
419 and x-popup-dialog; it is not used for the menu bar.
421 NOTE: All calls to popup_get_selection should be protected
422 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
424 static void
425 popup_get_selection (XEvent *initial_event, struct x_display_info *dpyinfo, LWLIB_ID id, int do_timers)
427 XEvent event;
429 while (popup_activated_flag)
431 if (initial_event)
433 event = *initial_event;
434 initial_event = 0;
436 else
438 if (do_timers) x_menu_wait_for_event (0);
439 XtAppNextEvent (Xt_app_con, &event);
442 /* Make sure we don't consider buttons grabbed after menu goes.
443 And make sure to deactivate for any ButtonRelease,
444 even if XtDispatchEvent doesn't do that. */
445 if (event.type == ButtonRelease
446 && dpyinfo->display == event.xbutton.display)
448 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
449 #ifdef USE_MOTIF /* Pretending that the event came from a
450 Btn1Down seems the only way to convince Motif to
451 activate its callbacks; setting the XmNmenuPost
452 isn't working. --marcus@sysc.pdx.edu. */
453 event.xbutton.button = 1;
454 /* Motif only pops down menus when no Ctrl, Alt or Mod
455 key is pressed and the button is released. So reset key state
456 so Motif thinks this is the case. */
457 event.xbutton.state = 0;
458 #endif
460 /* Pop down on C-g and Escape. */
461 else if (event.type == KeyPress
462 && dpyinfo->display == event.xbutton.display)
464 KeySym keysym = XLookupKeysym (&event.xkey, 0);
466 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
467 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
468 popup_activated_flag = 0;
471 x_dispatch_event (&event, event.xany.display);
475 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
476 doc: /* Start key navigation of the menu bar in FRAME.
477 This initially opens the first menu bar item and you can then navigate with the
478 arrow keys, select a menu entry with the return key or cancel with the
479 escape key. If FRAME has no menu bar this function does nothing.
481 If FRAME is nil or not given, use the selected frame. */)
482 (Lisp_Object frame)
484 XEvent ev;
485 FRAME_PTR f = check_x_frame (frame);
486 Widget menubar;
487 BLOCK_INPUT;
489 if (FRAME_EXTERNAL_MENU_BAR (f))
490 set_frame_menubar (f, 0, 1);
492 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
493 if (menubar)
495 Window child;
496 int error_p = 0;
498 x_catch_errors (FRAME_X_DISPLAY (f));
499 memset (&ev, 0, sizeof ev);
500 ev.xbutton.display = FRAME_X_DISPLAY (f);
501 ev.xbutton.window = XtWindow (menubar);
502 ev.xbutton.root = FRAME_X_DISPLAY_INFO (f)->root_window;
503 ev.xbutton.time = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
504 ev.xbutton.button = Button1;
505 ev.xbutton.x = ev.xbutton.y = FRAME_MENUBAR_HEIGHT (f) / 2;
506 ev.xbutton.same_screen = True;
508 #ifdef USE_MOTIF
510 Arg al[2];
511 WidgetList list;
512 Cardinal nr;
513 XtSetArg (al[0], XtNchildren, &list);
514 XtSetArg (al[1], XtNnumChildren, &nr);
515 XtGetValues (menubar, al, 2);
516 ev.xbutton.window = XtWindow (list[0]);
518 #endif
520 XTranslateCoordinates (FRAME_X_DISPLAY (f),
521 /* From-window, to-window. */
522 ev.xbutton.window, ev.xbutton.root,
524 /* From-position, to-position. */
525 ev.xbutton.x, ev.xbutton.y,
526 &ev.xbutton.x_root, &ev.xbutton.y_root,
528 /* Child of win. */
529 &child);
530 error_p = x_had_errors_p (FRAME_X_DISPLAY (f));
531 x_uncatch_errors ();
533 if (! error_p)
535 ev.type = ButtonPress;
536 ev.xbutton.state = 0;
538 XtDispatchEvent (&ev);
539 ev.xbutton.type = ButtonRelease;
540 ev.xbutton.state = Button1Mask;
541 XtDispatchEvent (&ev);
545 UNBLOCK_INPUT;
547 return Qnil;
549 #endif /* USE_X_TOOLKIT */
552 #ifdef USE_GTK
553 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
554 doc: /* Start key navigation of the menu bar in FRAME.
555 This initially opens the first menu bar item and you can then navigate with the
556 arrow keys, select a menu entry with the return key or cancel with the
557 escape key. If FRAME has no menu bar this function does nothing.
559 If FRAME is nil or not given, use the selected frame. */)
560 (Lisp_Object frame)
562 GtkWidget *menubar;
563 FRAME_PTR f;
565 /* gcc 2.95 doesn't accept the FRAME_PTR declaration after
566 BLOCK_INPUT. */
568 BLOCK_INPUT;
569 f = check_x_frame (frame);
571 if (FRAME_EXTERNAL_MENU_BAR (f))
572 set_frame_menubar (f, 0, 1);
574 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
575 if (menubar)
577 /* Activate the first menu. */
578 GList *children = gtk_container_get_children (GTK_CONTAINER (menubar));
580 if (children)
582 g_signal_emit_by_name (children->data, "activate_item");
583 popup_activated_flag = 1;
584 g_list_free (children);
587 UNBLOCK_INPUT;
589 return Qnil;
592 /* Loop util popup_activated_flag is set to zero in a callback.
593 Used for popup menus and dialogs. */
595 static void
596 popup_widget_loop (int do_timers, GtkWidget *widget)
598 ++popup_activated_flag;
600 /* Process events in the Gtk event loop until done. */
601 while (popup_activated_flag)
603 if (do_timers) x_menu_wait_for_event (0);
604 gtk_main_iteration ();
607 #endif
609 /* Activate the menu bar of frame F.
610 This is called from keyboard.c when it gets the
611 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
613 To activate the menu bar, we use the X button-press event
614 that was saved in saved_menu_event.
615 That makes the toolkit do its thing.
617 But first we recompute the menu bar contents (the whole tree).
619 The reason for saving the button event until here, instead of
620 passing it to the toolkit right away, is that we can safely
621 execute Lisp code. */
623 void
624 x_activate_menubar (FRAME_PTR f)
626 if (! FRAME_X_P (f))
627 abort ();
629 if (!f->output_data.x->saved_menu_event->type)
630 return;
632 #ifdef USE_GTK
633 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
634 f->output_data.x->saved_menu_event->xany.window))
635 return;
636 #endif
638 set_frame_menubar (f, 0, 1);
639 BLOCK_INPUT;
640 popup_activated_flag = 1;
641 #ifdef USE_GTK
642 XPutBackEvent (f->output_data.x->display_info->display,
643 f->output_data.x->saved_menu_event);
644 #else
645 XtDispatchEvent (f->output_data.x->saved_menu_event);
646 #endif
647 UNBLOCK_INPUT;
649 /* Ignore this if we get it a second time. */
650 f->output_data.x->saved_menu_event->type = 0;
653 /* This callback is invoked when the user selects a menubar cascade
654 pushbutton, but before the pulldown menu is posted. */
656 #ifndef USE_GTK
657 static void
658 popup_activate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
660 popup_activated_flag = 1;
661 #ifdef USE_X_TOOLKIT
662 x_activate_timeout_atimer ();
663 #endif
665 #endif
667 /* This callback is invoked when a dialog or menu is finished being
668 used and has been unposted. */
670 #ifdef USE_GTK
671 static void
672 popup_deactivate_callback (GtkWidget *widget, gpointer client_data)
674 popup_activated_flag = 0;
676 #else
677 static void
678 popup_deactivate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
680 popup_activated_flag = 0;
682 #endif
685 /* Function that finds the frame for WIDGET and shows the HELP text
686 for that widget.
687 F is the frame if known, or NULL if not known. */
688 static void
689 show_help_event (FRAME_PTR f, xt_or_gtk_widget widget, Lisp_Object help)
691 Lisp_Object frame;
693 if (f)
695 XSETFRAME (frame, f);
696 kbd_buffer_store_help_event (frame, help);
698 else
700 #if 0 /* This code doesn't do anything useful. ++kfs */
701 /* WIDGET is the popup menu. It's parent is the frame's
702 widget. See which frame that is. */
703 xt_or_gtk_widget frame_widget = XtParent (widget);
704 Lisp_Object tail;
706 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
708 frame = XCAR (tail);
709 if (FRAMEP (frame)
710 && (f = XFRAME (frame),
711 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
712 break;
714 #endif
715 show_help_echo (help, Qnil, Qnil, Qnil, 1);
719 /* Callback called when menu items are highlighted/unhighlighted
720 while moving the mouse over them. WIDGET is the menu bar or menu
721 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
722 the data structure for the menu item, or null in case of
723 unhighlighting. */
725 #ifdef USE_GTK
726 void
727 menu_highlight_callback (GtkWidget *widget, gpointer call_data)
729 xg_menu_item_cb_data *cb_data;
730 Lisp_Object help;
732 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
733 XG_ITEM_DATA);
734 if (! cb_data) return;
736 help = call_data ? cb_data->help : Qnil;
738 /* If popup_activated_flag is greater than 1 we are in a popup menu.
739 Don't show help for them, they won't appear before the
740 popup is popped down. */
741 if (popup_activated_flag <= 1)
742 show_help_event (cb_data->cl_data->f, widget, help);
744 #else
745 void
746 menu_highlight_callback (Widget widget, LWLIB_ID id, void *call_data)
748 struct frame *f;
749 Lisp_Object help;
751 widget_value *wv = (widget_value *) call_data;
753 help = wv ? wv->help : Qnil;
755 /* Determine the frame for the help event. */
756 f = menubar_id_to_frame (id);
758 show_help_event (f, widget, help);
760 #endif
762 #ifdef USE_GTK
763 /* Gtk calls callbacks just because we tell it what item should be
764 selected in a radio group. If this variable is set to a non-zero
765 value, we are creating menus and don't want callbacks right now.
767 static int xg_crazy_callback_abort;
769 /* This callback is called from the menu bar pulldown menu
770 when the user makes a selection.
771 Figure out what the user chose
772 and put the appropriate events into the keyboard buffer. */
773 static void
774 menubar_selection_callback (GtkWidget *widget, gpointer client_data)
776 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
778 if (xg_crazy_callback_abort)
779 return;
781 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
782 return;
784 /* For a group of radio buttons, GTK calls the selection callback first
785 for the item that was active before the selection and then for the one that
786 is active after the selection. For C-h k this means we get the help on
787 the deselected item and then the selected item is executed. Prevent that
788 by ignoring the non-active item. */
789 if (GTK_IS_RADIO_MENU_ITEM (widget)
790 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
791 return;
793 /* When a menu is popped down, X generates a focus event (i.e. focus
794 goes back to the frame below the menu). Since GTK buffers events,
795 we force it out here before the menu selection event. Otherwise
796 sit-for will exit at once if the focus event follows the menu selection
797 event. */
799 BLOCK_INPUT;
800 while (gtk_events_pending ())
801 gtk_main_iteration ();
802 UNBLOCK_INPUT;
804 find_and_call_menu_selection (cb_data->cl_data->f,
805 cb_data->cl_data->menu_bar_items_used,
806 cb_data->cl_data->menu_bar_vector,
807 cb_data->call_data);
810 #else /* not USE_GTK */
812 /* This callback is called from the menu bar pulldown menu
813 when the user makes a selection.
814 Figure out what the user chose
815 and put the appropriate events into the keyboard buffer. */
816 static void
817 menubar_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
819 FRAME_PTR f;
821 f = menubar_id_to_frame (id);
822 if (!f)
823 return;
824 find_and_call_menu_selection (f, f->menu_bar_items_used,
825 f->menu_bar_vector, client_data);
827 #endif /* not USE_GTK */
829 /* Recompute all the widgets of frame F, when the menu bar has been
830 changed. Value is non-zero if widgets were updated. */
832 static int
833 update_frame_menubar (FRAME_PTR f)
835 #ifdef USE_GTK
836 return xg_update_frame_menubar (f);
837 #else
838 struct x_output *x;
839 int columns, rows;
841 if (! FRAME_X_P (f))
842 abort ();
844 x = f->output_data.x;
846 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
847 return 0;
849 BLOCK_INPUT;
850 /* Save the size of the frame because the pane widget doesn't accept
851 to resize itself. So force it. */
852 columns = FRAME_COLS (f);
853 rows = FRAME_LINES (f);
855 /* Do the voodoo which means "I'm changing lots of things, don't try
856 to refigure sizes until I'm done." */
857 lw_refigure_widget (x->column_widget, False);
859 /* The order in which children are managed is the top to bottom
860 order in which they are displayed in the paned window. First,
861 remove the text-area widget. */
862 XtUnmanageChild (x->edit_widget);
864 /* Remove the menubar that is there now, and put up the menubar that
865 should be there. */
866 XtManageChild (x->menubar_widget);
867 XtMapWidget (x->menubar_widget);
868 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
870 /* Re-manage the text-area widget, and then thrash the sizes. */
871 XtManageChild (x->edit_widget);
872 lw_refigure_widget (x->column_widget, True);
874 /* Force the pane widget to resize itself with the right values. */
875 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
876 UNBLOCK_INPUT;
877 #endif
878 return 1;
881 #ifdef USE_LUCID
882 static void
883 apply_systemfont_to_dialog (Widget w)
885 const char *fn = xsettings_get_system_normal_font ();
886 if (fn)
888 XrmDatabase db = XtDatabase (XtDisplay (w));
889 if (db)
890 XrmPutStringResource (&db, "*dialog.faceName", fn);
894 static void
895 apply_systemfont_to_menu (Widget w)
897 const char *fn = xsettings_get_system_normal_font ();
898 int defflt;
900 if (!fn) return;
902 if (XtIsShell (w)) /* popup menu */
904 Widget *childs = NULL;
906 XtVaGetValues (w, XtNchildren, &childs, NULL);
907 if (*childs) w = *childs;
910 /* Only use system font if the default is used for the menu. */
911 XtVaGetValues (w, XtNdefaultFace, &defflt, NULL);
912 if (defflt)
913 XtVaSetValues (w, XtNfaceName, fn, NULL);
915 #endif
917 /* Set the contents of the menubar widgets of frame F.
918 The argument FIRST_TIME is currently ignored;
919 it is set the first time this is called, from initialize_frame_menubar. */
921 void
922 set_frame_menubar (FRAME_PTR f, int first_time, int deep_p)
924 xt_or_gtk_widget menubar_widget;
925 #ifdef USE_X_TOOLKIT
926 LWLIB_ID id;
927 #endif
928 Lisp_Object items;
929 widget_value *wv, *first_wv, *prev_wv = 0;
930 int i, last_i = 0;
931 int *submenu_start, *submenu_end;
932 int *submenu_top_level_items, *submenu_n_panes;
934 if (! FRAME_X_P (f))
935 abort ();
937 menubar_widget = f->output_data.x->menubar_widget;
939 XSETFRAME (Vmenu_updating_frame, f);
941 #ifdef USE_X_TOOLKIT
942 if (f->output_data.x->id == 0)
943 f->output_data.x->id = next_menubar_widget_id++;
944 id = f->output_data.x->id;
945 #endif
947 if (! menubar_widget)
948 deep_p = 1;
949 /* Make the first call for any given frame always go deep. */
950 else if (!f->output_data.x->saved_menu_event && !deep_p)
952 deep_p = 1;
953 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
954 f->output_data.x->saved_menu_event->type = 0;
957 #ifdef USE_GTK
958 /* If we have detached menus, we must update deep so detached menus
959 also gets updated. */
960 deep_p = deep_p || xg_have_tear_offs ();
961 #endif
963 if (deep_p)
965 /* Make a widget-value tree representing the entire menu trees. */
967 struct buffer *prev = current_buffer;
968 Lisp_Object buffer;
969 int specpdl_count = SPECPDL_INDEX ();
970 int previous_menu_items_used = f->menu_bar_items_used;
971 Lisp_Object *previous_items
972 = (Lisp_Object *) alloca (previous_menu_items_used
973 * sizeof (Lisp_Object));
975 /* If we are making a new widget, its contents are empty,
976 do always reinitialize them. */
977 if (! menubar_widget)
978 previous_menu_items_used = 0;
980 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
981 specbind (Qinhibit_quit, Qt);
982 /* Don't let the debugger step into this code
983 because it is not reentrant. */
984 specbind (Qdebug_on_next_call, Qnil);
986 record_unwind_save_match_data ();
987 if (NILP (Voverriding_local_map_menu_flag))
989 specbind (Qoverriding_terminal_local_map, Qnil);
990 specbind (Qoverriding_local_map, Qnil);
993 set_buffer_internal_1 (XBUFFER (buffer));
995 /* Run the Lucid hook. */
996 safe_run_hooks (Qactivate_menubar_hook);
998 /* If it has changed current-menubar from previous value,
999 really recompute the menubar from the value. */
1000 if (! NILP (Vlucid_menu_bar_dirty_flag))
1001 call0 (Qrecompute_lucid_menubar);
1002 safe_run_hooks (Qmenu_bar_update_hook);
1003 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1005 items = FRAME_MENU_BAR_ITEMS (f);
1007 /* Save the frame's previous menu bar contents data. */
1008 if (previous_menu_items_used)
1009 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents,
1010 previous_menu_items_used * sizeof (Lisp_Object));
1012 /* Fill in menu_items with the current menu bar contents.
1013 This can evaluate Lisp code. */
1014 save_menu_items ();
1016 menu_items = f->menu_bar_vector;
1017 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
1018 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1019 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1020 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
1021 submenu_top_level_items
1022 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1023 init_menu_items ();
1024 for (i = 0; i < XVECTOR (items)->size; i += 4)
1026 Lisp_Object key, string, maps;
1028 last_i = i;
1030 key = XVECTOR (items)->contents[i];
1031 string = XVECTOR (items)->contents[i + 1];
1032 maps = XVECTOR (items)->contents[i + 2];
1033 if (NILP (string))
1034 break;
1036 submenu_start[i] = menu_items_used;
1038 menu_items_n_panes = 0;
1039 submenu_top_level_items[i]
1040 = parse_single_submenu (key, string, maps);
1041 submenu_n_panes[i] = menu_items_n_panes;
1043 submenu_end[i] = menu_items_used;
1046 finish_menu_items ();
1048 /* Convert menu_items into widget_value trees
1049 to display the menu. This cannot evaluate Lisp code. */
1051 wv = xmalloc_widget_value ();
1052 wv->name = "menubar";
1053 wv->value = 0;
1054 wv->enabled = 1;
1055 wv->button_type = BUTTON_TYPE_NONE;
1056 wv->help = Qnil;
1057 first_wv = wv;
1059 for (i = 0; i < last_i; i += 4)
1061 menu_items_n_panes = submenu_n_panes[i];
1062 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
1063 submenu_top_level_items[i]);
1064 if (prev_wv)
1065 prev_wv->next = wv;
1066 else
1067 first_wv->contents = wv;
1068 /* Don't set wv->name here; GC during the loop might relocate it. */
1069 wv->enabled = 1;
1070 wv->button_type = BUTTON_TYPE_NONE;
1071 prev_wv = wv;
1074 set_buffer_internal_1 (prev);
1076 /* If there has been no change in the Lisp-level contents
1077 of the menu bar, skip redisplaying it. Just exit. */
1079 /* Compare the new menu items with the ones computed last time. */
1080 for (i = 0; i < previous_menu_items_used; i++)
1081 if (menu_items_used == i
1082 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1083 break;
1084 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1086 /* The menu items have not changed. Don't bother updating
1087 the menus in any form, since it would be a no-op. */
1088 free_menubar_widget_value_tree (first_wv);
1089 discard_menu_items ();
1090 unbind_to (specpdl_count, Qnil);
1091 return;
1094 /* The menu items are different, so store them in the frame. */
1095 f->menu_bar_vector = menu_items;
1096 f->menu_bar_items_used = menu_items_used;
1098 /* This undoes save_menu_items. */
1099 unbind_to (specpdl_count, Qnil);
1101 /* Now GC cannot happen during the lifetime of the widget_value,
1102 so it's safe to store data from a Lisp_String. */
1103 wv = first_wv->contents;
1104 for (i = 0; i < XVECTOR (items)->size; i += 4)
1106 Lisp_Object string;
1107 string = XVECTOR (items)->contents[i + 1];
1108 if (NILP (string))
1109 break;
1110 wv->name = SSDATA (string);
1111 update_submenu_strings (wv->contents);
1112 wv = wv->next;
1116 else
1118 /* Make a widget-value tree containing
1119 just the top level menu bar strings. */
1121 wv = xmalloc_widget_value ();
1122 wv->name = "menubar";
1123 wv->value = 0;
1124 wv->enabled = 1;
1125 wv->button_type = BUTTON_TYPE_NONE;
1126 wv->help = Qnil;
1127 first_wv = wv;
1129 items = FRAME_MENU_BAR_ITEMS (f);
1130 for (i = 0; i < XVECTOR (items)->size; i += 4)
1132 Lisp_Object string;
1134 string = XVECTOR (items)->contents[i + 1];
1135 if (NILP (string))
1136 break;
1138 wv = xmalloc_widget_value ();
1139 wv->name = SSDATA (string);
1140 wv->value = 0;
1141 wv->enabled = 1;
1142 wv->button_type = BUTTON_TYPE_NONE;
1143 wv->help = Qnil;
1144 /* This prevents lwlib from assuming this
1145 menu item is really supposed to be empty. */
1146 /* The EMACS_INT cast avoids a warning.
1147 This value just has to be different from small integers. */
1148 wv->call_data = (void *) (EMACS_INT) (-1);
1150 if (prev_wv)
1151 prev_wv->next = wv;
1152 else
1153 first_wv->contents = wv;
1154 prev_wv = wv;
1157 /* Forget what we thought we knew about what is in the
1158 detailed contents of the menu bar menus.
1159 Changing the top level always destroys the contents. */
1160 f->menu_bar_items_used = 0;
1163 /* Create or update the menu bar widget. */
1165 BLOCK_INPUT;
1167 #ifdef USE_GTK
1168 xg_crazy_callback_abort = 1;
1169 if (menubar_widget)
1171 /* The fourth arg is DEEP_P, which says to consider the entire
1172 menu trees we supply, rather than just the menu bar item names. */
1173 xg_modify_menubar_widgets (menubar_widget,
1175 first_wv,
1176 deep_p,
1177 G_CALLBACK (menubar_selection_callback),
1178 G_CALLBACK (popup_deactivate_callback),
1179 G_CALLBACK (menu_highlight_callback));
1181 else
1183 GtkWidget *wvbox = f->output_data.x->vbox_widget;
1185 menubar_widget
1186 = xg_create_widget ("menubar", "menubar", f, first_wv,
1187 G_CALLBACK (menubar_selection_callback),
1188 G_CALLBACK (popup_deactivate_callback),
1189 G_CALLBACK (menu_highlight_callback));
1191 f->output_data.x->menubar_widget = menubar_widget;
1195 #else /* not USE_GTK */
1196 if (menubar_widget)
1198 /* Disable resizing (done for Motif!) */
1199 lw_allow_resizing (f->output_data.x->widget, False);
1201 /* The third arg is DEEP_P, which says to consider the entire
1202 menu trees we supply, rather than just the menu bar item names. */
1203 lw_modify_all_widgets (id, first_wv, deep_p);
1205 /* Re-enable the edit widget to resize. */
1206 lw_allow_resizing (f->output_data.x->widget, True);
1208 else
1210 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
1211 XtTranslations override = XtParseTranslationTable (menuOverride);
1213 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1214 f->output_data.x->column_widget,
1216 popup_activate_callback,
1217 menubar_selection_callback,
1218 popup_deactivate_callback,
1219 menu_highlight_callback);
1220 f->output_data.x->menubar_widget = menubar_widget;
1222 /* Make menu pop down on C-g. */
1223 XtOverrideTranslations (menubar_widget, override);
1224 #ifdef USE_LUCID
1225 apply_systemfont_to_menu (menubar_widget);
1226 #endif
1230 int menubar_size;
1231 if (f->output_data.x->menubar_widget)
1232 XtRealizeWidget (f->output_data.x->menubar_widget);
1234 menubar_size
1235 = (f->output_data.x->menubar_widget
1236 ? (f->output_data.x->menubar_widget->core.height
1237 + f->output_data.x->menubar_widget->core.border_width)
1238 : 0);
1240 #if 1 /* Experimentally, we now get the right results
1241 for -geometry -0-0 without this. 24 Aug 96, rms.
1242 Maybe so, but the menu bar size is missing the pixels so the
1243 WM size hints are off by these pixels. Jan D, oct 2009. */
1244 #ifdef USE_LUCID
1245 if (FRAME_EXTERNAL_MENU_BAR (f))
1247 Dimension ibw = 0;
1248 XtVaGetValues (f->output_data.x->column_widget,
1249 XtNinternalBorderWidth, &ibw, NULL);
1250 menubar_size += ibw;
1252 #endif /* USE_LUCID */
1253 #endif /* 1 */
1255 f->output_data.x->menubar_height = menubar_size;
1257 #endif /* not USE_GTK */
1259 free_menubar_widget_value_tree (first_wv);
1260 update_frame_menubar (f);
1262 #ifdef USE_GTK
1263 xg_crazy_callback_abort = 0;
1264 #endif
1266 UNBLOCK_INPUT;
1269 /* Called from Fx_create_frame to create the initial menubar of a frame
1270 before it is mapped, so that the window is mapped with the menubar already
1271 there instead of us tacking it on later and thrashing the window after it
1272 is visible. */
1274 void
1275 initialize_frame_menubar (FRAME_PTR f)
1277 /* This function is called before the first chance to redisplay
1278 the frame. It has to be, so the frame will have the right size. */
1279 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1280 set_frame_menubar (f, 1, 1);
1284 /* Get rid of the menu bar of frame F, and free its storage.
1285 This is used when deleting a frame, and when turning off the menu bar.
1286 For GTK this function is in gtkutil.c. */
1288 #ifndef USE_GTK
1289 void
1290 free_frame_menubar (FRAME_PTR f)
1292 Widget menubar_widget;
1294 if (! FRAME_X_P (f))
1295 abort ();
1297 menubar_widget = f->output_data.x->menubar_widget;
1299 f->output_data.x->menubar_height = 0;
1301 if (menubar_widget)
1303 #ifdef USE_MOTIF
1304 /* Removing the menu bar magically changes the shell widget's x
1305 and y position of (0, 0) which, when the menu bar is turned
1306 on again, leads to pull-down menuss appearing in strange
1307 positions near the upper-left corner of the display. This
1308 happens only with some window managers like twm and ctwm,
1309 but not with other like Motif's mwm or kwm, because the
1310 latter generate ConfigureNotify events when the menu bar
1311 is switched off, which fixes the shell position. */
1312 Position x0, y0, x1, y1;
1313 #endif
1315 BLOCK_INPUT;
1317 #ifdef USE_MOTIF
1318 if (f->output_data.x->widget)
1319 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
1320 #endif
1322 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1323 f->output_data.x->menubar_widget = NULL;
1325 if (f->output_data.x->widget)
1327 #ifdef USE_MOTIF
1328 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
1329 if (x1 == 0 && y1 == 0)
1330 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
1331 #endif
1332 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1334 UNBLOCK_INPUT;
1337 #endif /* not USE_GTK */
1339 #endif /* USE_X_TOOLKIT || USE_GTK */
1341 /* xmenu_show actually displays a menu using the panes and items in menu_items
1342 and returns the value selected from it.
1343 There are two versions of xmenu_show, one for Xt and one for Xlib.
1344 Both assume input is blocked by the caller. */
1346 /* F is the frame the menu is for.
1347 X and Y are the frame-relative specified position,
1348 relative to the inside upper left corner of the frame F.
1349 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1350 KEYMAPS is 1 if this menu was specified with keymaps;
1351 in that case, we return a list containing the chosen item's value
1352 and perhaps also the pane's prefix.
1353 TITLE is the specified menu title.
1354 ERROR is a place to store an error message string in case of failure.
1355 (We return nil on failure, but the value doesn't actually matter.) */
1357 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1359 /* The item selected in the popup menu. */
1360 static Lisp_Object *volatile menu_item_selection;
1362 #ifdef USE_GTK
1364 /* Used when position a popup menu. See menu_position_func and
1365 create_and_show_popup_menu below. */
1366 struct next_popup_x_y
1368 FRAME_PTR f;
1369 int x;
1370 int y;
1373 /* The menu position function to use if we are not putting a popup
1374 menu where the pointer is.
1375 MENU is the menu to pop up.
1376 X and Y shall on exit contain x/y where the menu shall pop up.
1377 PUSH_IN is not documented in the GTK manual.
1378 USER_DATA is any data passed in when calling gtk_menu_popup.
1379 Here it points to a struct next_popup_x_y where the coordinates
1380 to store in *X and *Y are as well as the frame for the popup.
1382 Here only X and Y are used. */
1383 static void
1384 menu_position_func (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer user_data)
1386 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
1387 GtkRequisition req;
1388 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (data->f);
1389 int disp_width = x_display_pixel_width (dpyinfo);
1390 int disp_height = x_display_pixel_height (dpyinfo);
1392 *x = data->x;
1393 *y = data->y;
1395 /* Check if there is room for the menu. If not, adjust x/y so that
1396 the menu is fully visible. */
1397 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1398 if (data->x + req.width > disp_width)
1399 *x -= data->x + req.width - disp_width;
1400 if (data->y + req.height > disp_height)
1401 *y -= data->y + req.height - disp_height;
1404 static void
1405 popup_selection_callback (GtkWidget *widget, gpointer client_data)
1407 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1409 if (xg_crazy_callback_abort) return;
1410 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
1413 static Lisp_Object
1414 pop_down_menu (Lisp_Object arg)
1416 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1418 popup_activated_flag = 0;
1419 BLOCK_INPUT;
1420 gtk_widget_destroy (GTK_WIDGET (p->pointer));
1421 UNBLOCK_INPUT;
1422 return Qnil;
1425 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1426 menu pops down.
1427 menu_item_selection will be set to the selection. */
1428 static void
1429 create_and_show_popup_menu (FRAME_PTR f, widget_value *first_wv, int x, int y, int for_click, EMACS_UINT timestamp)
1431 int i;
1432 GtkWidget *menu;
1433 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
1434 struct next_popup_x_y popup_x_y;
1435 int specpdl_count = SPECPDL_INDEX ();
1437 if (! FRAME_X_P (f))
1438 abort ();
1440 xg_crazy_callback_abort = 1;
1441 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
1442 G_CALLBACK (popup_selection_callback),
1443 G_CALLBACK (popup_deactivate_callback),
1444 G_CALLBACK (menu_highlight_callback));
1445 xg_crazy_callback_abort = 0;
1447 if (! for_click)
1449 /* Not invoked by a click. pop up at x/y. */
1450 pos_func = menu_position_func;
1452 /* Adjust coordinates to be root-window-relative. */
1453 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1454 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1456 popup_x_y.x = x;
1457 popup_x_y.y = y;
1458 popup_x_y.f = f;
1460 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1462 else
1464 for (i = 0; i < 5; i++)
1465 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
1466 break;
1469 /* Display the menu. */
1470 gtk_widget_show_all (menu);
1472 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i,
1473 timestamp > 0 ? timestamp : gtk_get_current_event_time());
1475 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
1477 if (gtk_widget_get_mapped (menu))
1479 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1480 two. show_help_echo uses this to detect popup menus. */
1481 popup_activated_flag = 1;
1482 /* Process events that apply to the menu. */
1483 popup_widget_loop (1, menu);
1486 unbind_to (specpdl_count, Qnil);
1488 /* Must reset this manually because the button release event is not passed
1489 to Emacs event loop. */
1490 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
1493 #else /* not USE_GTK */
1495 /* We need a unique id for each widget handled by the Lucid Widget
1496 library.
1498 For the main windows, and popup menus, we use this counter,
1499 which we increment each time after use. This starts from 1<<16.
1501 For menu bars, we use numbers starting at 0, counted in
1502 next_menubar_widget_id. */
1503 LWLIB_ID widget_id_tick;
1505 static void
1506 popup_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1508 menu_item_selection = (Lisp_Object *) client_data;
1511 /* ARG is the LWLIB ID of the dialog box, represented
1512 as a Lisp object as (HIGHPART . LOWPART). */
1514 static Lisp_Object
1515 pop_down_menu (Lisp_Object arg)
1517 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
1518 | XINT (XCDR (arg)));
1520 BLOCK_INPUT;
1521 lw_destroy_all_widgets (id);
1522 UNBLOCK_INPUT;
1523 popup_activated_flag = 0;
1525 return Qnil;
1528 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1529 menu pops down.
1530 menu_item_selection will be set to the selection. */
1531 static void
1532 create_and_show_popup_menu (FRAME_PTR f, widget_value *first_wv,
1533 int x, int y, int for_click, EMACS_UINT timestamp)
1535 int i;
1536 Arg av[2];
1537 int ac = 0;
1538 XButtonPressedEvent dummy;
1539 LWLIB_ID menu_id;
1540 Widget menu;
1542 if (! FRAME_X_P (f))
1543 abort ();
1545 menu_id = widget_id_tick++;
1546 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1547 f->output_data.x->widget, 1, 0,
1548 popup_selection_callback,
1549 popup_deactivate_callback,
1550 menu_highlight_callback);
1552 #ifdef USE_LUCID
1553 apply_systemfont_to_menu (menu);
1554 #endif
1556 dummy.type = ButtonPress;
1557 dummy.serial = 0;
1558 dummy.send_event = 0;
1559 dummy.display = FRAME_X_DISPLAY (f);
1560 dummy.time = CurrentTime;
1561 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
1562 dummy.window = dummy.root;
1563 dummy.subwindow = dummy.root;
1564 dummy.x = x;
1565 dummy.y = y;
1567 /* Adjust coordinates to be root-window-relative. */
1568 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1569 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1571 dummy.x_root = x;
1572 dummy.y_root = y;
1574 dummy.state = 0;
1575 dummy.button = 0;
1576 for (i = 0; i < 5; i++)
1577 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
1578 dummy.button = i;
1580 /* Don't allow any geometry request from the user. */
1581 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1582 XtSetValues (menu, av, ac);
1584 /* Display the menu. */
1585 lw_popup_menu (menu, (XEvent *) &dummy);
1586 popup_activated_flag = 1;
1587 x_activate_timeout_atimer ();
1590 int fact = 4 * sizeof (LWLIB_ID);
1591 int specpdl_count = SPECPDL_INDEX ();
1592 record_unwind_protect (pop_down_menu,
1593 Fcons (make_number (menu_id >> (fact)),
1594 make_number (menu_id & ~(-1 << (fact)))));
1596 /* Process events that apply to the menu. */
1597 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
1599 unbind_to (specpdl_count, Qnil);
1603 #endif /* not USE_GTK */
1605 Lisp_Object
1606 xmenu_show (FRAME_PTR f, int x, int y, int for_click, int keymaps,
1607 Lisp_Object title, const char **error, EMACS_UINT timestamp)
1609 int i;
1610 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1611 widget_value **submenu_stack
1612 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1613 Lisp_Object *subprefix_stack
1614 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1615 int submenu_depth = 0;
1617 int first_pane;
1619 if (! FRAME_X_P (f))
1620 abort ();
1622 *error = NULL;
1624 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1626 *error = "Empty menu";
1627 return Qnil;
1630 /* Create a tree of widget_value objects
1631 representing the panes and their items. */
1632 wv = xmalloc_widget_value ();
1633 wv->name = "menu";
1634 wv->value = 0;
1635 wv->enabled = 1;
1636 wv->button_type = BUTTON_TYPE_NONE;
1637 wv->help =Qnil;
1638 first_wv = wv;
1639 first_pane = 1;
1641 /* Loop over all panes and items, filling in the tree. */
1642 i = 0;
1643 while (i < menu_items_used)
1645 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1647 submenu_stack[submenu_depth++] = save_wv;
1648 save_wv = prev_wv;
1649 prev_wv = 0;
1650 first_pane = 1;
1651 i++;
1653 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1655 prev_wv = save_wv;
1656 save_wv = submenu_stack[--submenu_depth];
1657 first_pane = 0;
1658 i++;
1660 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1661 && submenu_depth != 0)
1662 i += MENU_ITEMS_PANE_LENGTH;
1663 /* Ignore a nil in the item list.
1664 It's meaningful only for dialog boxes. */
1665 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1666 i += 1;
1667 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1669 /* Create a new pane. */
1670 Lisp_Object pane_name, prefix;
1671 char *pane_string;
1673 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1674 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1676 #ifndef HAVE_MULTILINGUAL_MENU
1677 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1679 pane_name = ENCODE_MENU_STRING (pane_name);
1680 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
1682 #endif
1683 pane_string = (NILP (pane_name)
1684 ? "" : SSDATA (pane_name));
1685 /* If there is just one top-level pane, put all its items directly
1686 under the top-level menu. */
1687 if (menu_items_n_panes == 1)
1688 pane_string = "";
1690 /* If the pane has a meaningful name,
1691 make the pane a top-level menu item
1692 with its items as a submenu beneath it. */
1693 if (!keymaps && strcmp (pane_string, ""))
1695 wv = xmalloc_widget_value ();
1696 if (save_wv)
1697 save_wv->next = wv;
1698 else
1699 first_wv->contents = wv;
1700 wv->name = pane_string;
1701 if (keymaps && !NILP (prefix))
1702 wv->name++;
1703 wv->value = 0;
1704 wv->enabled = 1;
1705 wv->button_type = BUTTON_TYPE_NONE;
1706 wv->help = Qnil;
1707 save_wv = wv;
1708 prev_wv = 0;
1710 else if (first_pane)
1712 save_wv = wv;
1713 prev_wv = 0;
1715 first_pane = 0;
1716 i += MENU_ITEMS_PANE_LENGTH;
1718 else
1720 /* Create a new item within current pane. */
1721 Lisp_Object item_name, enable, descrip, def, type, selected, help;
1722 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1723 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1724 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1725 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1726 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1727 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1728 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1730 #ifndef HAVE_MULTILINGUAL_MENU
1731 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
1733 item_name = ENCODE_MENU_STRING (item_name);
1734 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
1737 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1739 descrip = ENCODE_MENU_STRING (descrip);
1740 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
1742 #endif /* not HAVE_MULTILINGUAL_MENU */
1744 wv = xmalloc_widget_value ();
1745 if (prev_wv)
1746 prev_wv->next = wv;
1747 else
1748 save_wv->contents = wv;
1749 wv->name = SSDATA (item_name);
1750 if (!NILP (descrip))
1751 wv->key = SSDATA (descrip);
1752 wv->value = 0;
1753 /* If this item has a null value,
1754 make the call_data null so that it won't display a box
1755 when the mouse is on it. */
1756 wv->call_data
1757 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
1758 wv->enabled = !NILP (enable);
1760 if (NILP (type))
1761 wv->button_type = BUTTON_TYPE_NONE;
1762 else if (EQ (type, QCtoggle))
1763 wv->button_type = BUTTON_TYPE_TOGGLE;
1764 else if (EQ (type, QCradio))
1765 wv->button_type = BUTTON_TYPE_RADIO;
1766 else
1767 abort ();
1769 wv->selected = !NILP (selected);
1771 if (! STRINGP (help))
1772 help = Qnil;
1774 wv->help = help;
1776 prev_wv = wv;
1778 i += MENU_ITEMS_ITEM_LENGTH;
1782 /* Deal with the title, if it is non-nil. */
1783 if (!NILP (title))
1785 widget_value *wv_title = xmalloc_widget_value ();
1786 widget_value *wv_sep1 = xmalloc_widget_value ();
1787 widget_value *wv_sep2 = xmalloc_widget_value ();
1789 wv_sep2->name = "--";
1790 wv_sep2->next = first_wv->contents;
1791 wv_sep2->help = Qnil;
1793 wv_sep1->name = "--";
1794 wv_sep1->next = wv_sep2;
1795 wv_sep1->help = Qnil;
1797 #ifndef HAVE_MULTILINGUAL_MENU
1798 if (STRING_MULTIBYTE (title))
1799 title = ENCODE_MENU_STRING (title);
1800 #endif
1802 wv_title->name = SSDATA (title);
1803 wv_title->enabled = TRUE;
1804 wv_title->button_type = BUTTON_TYPE_NONE;
1805 wv_title->help = Qnil;
1806 wv_title->next = wv_sep1;
1807 first_wv->contents = wv_title;
1810 /* No selection has been chosen yet. */
1811 menu_item_selection = 0;
1813 /* Actually create and show the menu until popped down. */
1814 create_and_show_popup_menu (f, first_wv, x, y, for_click, timestamp);
1816 /* Free the widget_value objects we used to specify the contents. */
1817 free_menubar_widget_value_tree (first_wv);
1819 /* Find the selected item, and its pane, to return
1820 the proper value. */
1821 if (menu_item_selection != 0)
1823 Lisp_Object prefix, entry;
1825 prefix = entry = Qnil;
1826 i = 0;
1827 while (i < menu_items_used)
1829 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1831 subprefix_stack[submenu_depth++] = prefix;
1832 prefix = entry;
1833 i++;
1835 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1837 prefix = subprefix_stack[--submenu_depth];
1838 i++;
1840 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1842 prefix
1843 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1844 i += MENU_ITEMS_PANE_LENGTH;
1846 /* Ignore a nil in the item list.
1847 It's meaningful only for dialog boxes. */
1848 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1849 i += 1;
1850 else
1852 entry
1853 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1854 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1856 if (keymaps != 0)
1858 int j;
1860 entry = Fcons (entry, Qnil);
1861 if (!NILP (prefix))
1862 entry = Fcons (prefix, entry);
1863 for (j = submenu_depth - 1; j >= 0; j--)
1864 if (!NILP (subprefix_stack[j]))
1865 entry = Fcons (subprefix_stack[j], entry);
1867 return entry;
1869 i += MENU_ITEMS_ITEM_LENGTH;
1873 else if (!for_click)
1874 /* Make "Cancel" equivalent to C-g. */
1875 Fsignal (Qquit, Qnil);
1877 return Qnil;
1880 #ifdef USE_GTK
1881 static void
1882 dialog_selection_callback (GtkWidget *widget, gpointer client_data)
1884 /* The EMACS_INT cast avoids a warning. There's no problem
1885 as long as pointers have enough bits to hold small integers. */
1886 if ((int) (EMACS_INT) client_data != -1)
1887 menu_item_selection = (Lisp_Object *) client_data;
1889 popup_activated_flag = 0;
1892 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1893 dialog pops down.
1894 menu_item_selection will be set to the selection. */
1895 static void
1896 create_and_show_dialog (FRAME_PTR f, widget_value *first_wv)
1898 GtkWidget *menu;
1900 if (! FRAME_X_P (f))
1901 abort ();
1903 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
1904 G_CALLBACK (dialog_selection_callback),
1905 G_CALLBACK (popup_deactivate_callback),
1908 if (menu)
1910 int specpdl_count = SPECPDL_INDEX ();
1911 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
1913 /* Display the menu. */
1914 gtk_widget_show_all (menu);
1916 /* Process events that apply to the menu. */
1917 popup_widget_loop (1, menu);
1919 unbind_to (specpdl_count, Qnil);
1923 #else /* not USE_GTK */
1924 static void
1925 dialog_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1927 /* The EMACS_INT cast avoids a warning. There's no problem
1928 as long as pointers have enough bits to hold small integers. */
1929 if ((int) (EMACS_INT) client_data != -1)
1930 menu_item_selection = (Lisp_Object *) client_data;
1932 BLOCK_INPUT;
1933 lw_destroy_all_widgets (id);
1934 UNBLOCK_INPUT;
1935 popup_activated_flag = 0;
1939 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1940 dialog pops down.
1941 menu_item_selection will be set to the selection. */
1942 static void
1943 create_and_show_dialog (FRAME_PTR f, widget_value *first_wv)
1945 LWLIB_ID dialog_id;
1947 if (!FRAME_X_P (f))
1948 abort();
1950 dialog_id = widget_id_tick++;
1951 #ifdef USE_LUCID
1952 apply_systemfont_to_dialog (f->output_data.x->widget);
1953 #endif
1954 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1955 f->output_data.x->widget, 1, 0,
1956 dialog_selection_callback, 0, 0);
1957 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1958 /* Display the dialog box. */
1959 lw_pop_up_all_widgets (dialog_id);
1960 popup_activated_flag = 1;
1961 x_activate_timeout_atimer ();
1963 /* Process events that apply to the dialog box.
1964 Also handle timers. */
1966 int count = SPECPDL_INDEX ();
1967 int fact = 4 * sizeof (LWLIB_ID);
1969 /* xdialog_show_unwind is responsible for popping the dialog box down. */
1970 record_unwind_protect (pop_down_menu,
1971 Fcons (make_number (dialog_id >> (fact)),
1972 make_number (dialog_id & ~(-1 << (fact)))));
1974 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
1975 dialog_id, 1);
1977 unbind_to (count, Qnil);
1981 #endif /* not USE_GTK */
1983 static const char * button_names [] = {
1984 "button1", "button2", "button3", "button4", "button5",
1985 "button6", "button7", "button8", "button9", "button10" };
1987 static Lisp_Object
1988 xdialog_show (FRAME_PTR f,
1989 int keymaps,
1990 Lisp_Object title,
1991 Lisp_Object header,
1992 const char **error_name)
1994 int i, nb_buttons=0;
1995 char dialog_name[6];
1997 widget_value *wv, *first_wv = 0, *prev_wv = 0;
1999 /* Number of elements seen so far, before boundary. */
2000 int left_count = 0;
2001 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2002 int boundary_seen = 0;
2004 if (! FRAME_X_P (f))
2005 abort ();
2007 *error_name = NULL;
2009 if (menu_items_n_panes > 1)
2011 *error_name = "Multiple panes in dialog box";
2012 return Qnil;
2015 /* Create a tree of widget_value objects
2016 representing the text label and buttons. */
2018 Lisp_Object pane_name, prefix;
2019 char *pane_string;
2020 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2021 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2022 pane_string = (NILP (pane_name)
2023 ? "" : SSDATA (pane_name));
2024 prev_wv = xmalloc_widget_value ();
2025 prev_wv->value = pane_string;
2026 if (keymaps && !NILP (prefix))
2027 prev_wv->name++;
2028 prev_wv->enabled = 1;
2029 prev_wv->name = "message";
2030 prev_wv->help = Qnil;
2031 first_wv = prev_wv;
2033 /* Loop over all panes and items, filling in the tree. */
2034 i = MENU_ITEMS_PANE_LENGTH;
2035 while (i < menu_items_used)
2038 /* Create a new item within current pane. */
2039 Lisp_Object item_name, enable, descrip;
2040 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2041 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2042 descrip
2043 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2045 if (NILP (item_name))
2047 free_menubar_widget_value_tree (first_wv);
2048 *error_name = "Submenu in dialog items";
2049 return Qnil;
2051 if (EQ (item_name, Qquote))
2053 /* This is the boundary between left-side elts
2054 and right-side elts. Stop incrementing right_count. */
2055 boundary_seen = 1;
2056 i++;
2057 continue;
2059 if (nb_buttons >= 9)
2061 free_menubar_widget_value_tree (first_wv);
2062 *error_name = "Too many dialog items";
2063 return Qnil;
2066 wv = xmalloc_widget_value ();
2067 prev_wv->next = wv;
2068 wv->name = (char *) button_names[nb_buttons];
2069 if (!NILP (descrip))
2070 wv->key = SSDATA (descrip);
2071 wv->value = SSDATA (item_name);
2072 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2073 wv->enabled = !NILP (enable);
2074 wv->help = Qnil;
2075 prev_wv = wv;
2077 if (! boundary_seen)
2078 left_count++;
2080 nb_buttons++;
2081 i += MENU_ITEMS_ITEM_LENGTH;
2084 /* If the boundary was not specified,
2085 by default put half on the left and half on the right. */
2086 if (! boundary_seen)
2087 left_count = nb_buttons - nb_buttons / 2;
2089 wv = xmalloc_widget_value ();
2090 wv->name = dialog_name;
2091 wv->help = Qnil;
2093 /* Frame title: 'Q' = Question, 'I' = Information.
2094 Can also have 'E' = Error if, one day, we want
2095 a popup for errors. */
2096 if (NILP(header))
2097 dialog_name[0] = 'Q';
2098 else
2099 dialog_name[0] = 'I';
2101 /* Dialog boxes use a really stupid name encoding
2102 which specifies how many buttons to use
2103 and how many buttons are on the right. */
2104 dialog_name[1] = '0' + nb_buttons;
2105 dialog_name[2] = 'B';
2106 dialog_name[3] = 'R';
2107 /* Number of buttons to put on the right. */
2108 dialog_name[4] = '0' + nb_buttons - left_count;
2109 dialog_name[5] = 0;
2110 wv->contents = first_wv;
2111 first_wv = wv;
2114 /* No selection has been chosen yet. */
2115 menu_item_selection = 0;
2117 /* Actually create and show the dialog. */
2118 create_and_show_dialog (f, first_wv);
2120 /* Free the widget_value objects we used to specify the contents. */
2121 free_menubar_widget_value_tree (first_wv);
2123 /* Find the selected item, and its pane, to return
2124 the proper value. */
2125 if (menu_item_selection != 0)
2127 Lisp_Object prefix;
2129 prefix = Qnil;
2130 i = 0;
2131 while (i < menu_items_used)
2133 Lisp_Object entry;
2135 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2137 prefix
2138 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2139 i += MENU_ITEMS_PANE_LENGTH;
2141 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2143 /* This is the boundary between left-side elts and
2144 right-side elts. */
2145 ++i;
2147 else
2149 entry
2150 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2151 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2153 if (keymaps != 0)
2155 entry = Fcons (entry, Qnil);
2156 if (!NILP (prefix))
2157 entry = Fcons (prefix, entry);
2159 return entry;
2161 i += MENU_ITEMS_ITEM_LENGTH;
2165 else
2166 /* Make "Cancel" equivalent to C-g. */
2167 Fsignal (Qquit, Qnil);
2169 return Qnil;
2172 #else /* not USE_X_TOOLKIT && not USE_GTK */
2174 /* The frame of the last activated non-toolkit menu bar.
2175 Used to generate menu help events. */
2177 static struct frame *menu_help_frame;
2180 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2182 PANE is the pane number, and ITEM is the menu item number in
2183 the menu (currently not used).
2185 This cannot be done with generating a HELP_EVENT because
2186 XMenuActivate contains a loop that doesn't let Emacs process
2187 keyboard events. */
2189 static void
2190 menu_help_callback (char *help_string, int pane, int item)
2192 Lisp_Object *first_item;
2193 Lisp_Object pane_name;
2194 Lisp_Object menu_object;
2196 first_item = XVECTOR (menu_items)->contents;
2197 if (EQ (first_item[0], Qt))
2198 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2199 else if (EQ (first_item[0], Qquote))
2200 /* This shouldn't happen, see xmenu_show. */
2201 pane_name = empty_unibyte_string;
2202 else
2203 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2205 /* (menu-item MENU-NAME PANE-NUMBER) */
2206 menu_object = Fcons (Qmenu_item,
2207 Fcons (pane_name,
2208 Fcons (make_number (pane), Qnil)));
2209 show_help_echo (help_string ? build_string (help_string) : Qnil,
2210 Qnil, menu_object, make_number (item), 1);
2213 static Lisp_Object
2214 pop_down_menu (Lisp_Object arg)
2216 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
2217 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
2219 FRAME_PTR f = p1->pointer;
2220 XMenu *menu = p2->pointer;
2222 BLOCK_INPUT;
2223 #ifndef MSDOS
2224 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
2225 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
2226 #endif
2227 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2229 #ifdef HAVE_X_WINDOWS
2230 /* Assume the mouse has moved out of the X window.
2231 If it has actually moved in, we will get an EnterNotify. */
2232 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2234 /* State that no mouse buttons are now held.
2235 (The oldXMenu code doesn't track this info for us.)
2236 That is not necessarily true, but the fiction leads to reasonable
2237 results, and it is a pain to ask which are actually held now. */
2238 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2240 #endif /* HAVE_X_WINDOWS */
2242 UNBLOCK_INPUT;
2244 return Qnil;
2248 Lisp_Object
2249 xmenu_show (FRAME_PTR f, int x, int y, int for_click, int keymaps,
2250 Lisp_Object title, const char **error, EMACS_UINT timestamp)
2252 Window root;
2253 XMenu *menu;
2254 int pane, selidx, lpane, status;
2255 Lisp_Object entry, pane_prefix;
2256 char *datap;
2257 int ulx, uly, width, height;
2258 int dispwidth, dispheight;
2259 int i, j, lines, maxlines;
2260 int maxwidth;
2261 int dummy_int;
2262 unsigned int dummy_uint;
2263 int specpdl_count = SPECPDL_INDEX ();
2265 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
2266 abort ();
2268 *error = 0;
2269 if (menu_items_n_panes == 0)
2270 return Qnil;
2272 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2274 *error = "Empty menu";
2275 return Qnil;
2278 /* Figure out which root window F is on. */
2279 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2280 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2281 &dummy_uint, &dummy_uint);
2283 /* Make the menu on that window. */
2284 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2285 if (menu == NULL)
2287 *error = "Can't create menu";
2288 return Qnil;
2291 /* Don't GC while we prepare and show the menu,
2292 because we give the oldxmenu library pointers to the
2293 contents of strings. */
2294 inhibit_garbage_collection ();
2296 #ifdef HAVE_X_WINDOWS
2297 /* Adjust coordinates to relative to the outer (window manager) window. */
2298 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
2299 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
2300 #endif /* HAVE_X_WINDOWS */
2302 /* Adjust coordinates to be root-window-relative. */
2303 x += f->left_pos;
2304 y += f->top_pos;
2306 /* Create all the necessary panes and their items. */
2307 maxlines = lines = i = 0;
2308 while (i < menu_items_used)
2310 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2312 /* Create a new pane. */
2313 Lisp_Object pane_name, prefix;
2314 char *pane_string;
2316 maxlines = max (maxlines, lines);
2317 lines = 0;
2318 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2319 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2320 pane_string = (NILP (pane_name)
2321 ? "" : SSDATA (pane_name));
2322 if (keymaps && !NILP (prefix))
2323 pane_string++;
2325 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2326 if (lpane == XM_FAILURE)
2328 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2329 *error = "Can't create pane";
2330 return Qnil;
2332 i += MENU_ITEMS_PANE_LENGTH;
2334 /* Find the width of the widest item in this pane. */
2335 maxwidth = 0;
2336 j = i;
2337 while (j < menu_items_used)
2339 Lisp_Object item;
2340 item = XVECTOR (menu_items)->contents[j];
2341 if (EQ (item, Qt))
2342 break;
2343 if (NILP (item))
2345 j++;
2346 continue;
2348 width = SBYTES (item);
2349 if (width > maxwidth)
2350 maxwidth = width;
2352 j += MENU_ITEMS_ITEM_LENGTH;
2355 /* Ignore a nil in the item list.
2356 It's meaningful only for dialog boxes. */
2357 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2358 i += 1;
2359 else
2361 /* Create a new item within current pane. */
2362 Lisp_Object item_name, enable, descrip, help;
2363 unsigned char *item_data;
2364 char *help_string;
2366 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2367 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2368 descrip
2369 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2370 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2371 help_string = STRINGP (help) ? SDATA (help) : NULL;
2373 if (!NILP (descrip))
2375 int gap = maxwidth - SBYTES (item_name);
2376 /* if alloca is fast, use that to make the space,
2377 to reduce gc needs. */
2378 item_data
2379 = (unsigned char *) alloca (maxwidth
2380 + SBYTES (descrip) + 1);
2381 memcpy (item_data, SDATA (item_name), SBYTES (item_name));
2382 for (j = SCHARS (item_name); j < maxwidth; j++)
2383 item_data[j] = ' ';
2384 memcpy (item_data + j, SDATA (descrip), SBYTES (descrip));
2385 item_data[j + SBYTES (descrip)] = 0;
2387 else
2388 item_data = SDATA (item_name);
2390 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2391 menu, lpane, 0, item_data,
2392 !NILP (enable), help_string)
2393 == XM_FAILURE)
2395 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2396 *error = "Can't add selection to menu";
2397 return Qnil;
2399 i += MENU_ITEMS_ITEM_LENGTH;
2400 lines++;
2404 maxlines = max (maxlines, lines);
2406 /* All set and ready to fly. */
2407 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2408 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2409 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2410 x = min (x, dispwidth);
2411 y = min (y, dispheight);
2412 x = max (x, 1);
2413 y = max (y, 1);
2414 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2415 &ulx, &uly, &width, &height);
2416 if (ulx+width > dispwidth)
2418 x -= (ulx + width) - dispwidth;
2419 ulx = dispwidth - width;
2421 if (uly+height > dispheight)
2423 y -= (uly + height) - dispheight;
2424 uly = dispheight - height;
2426 #ifndef HAVE_X_WINDOWS
2427 if (FRAME_HAS_MINIBUF_P (f) && uly+height > dispheight - 1)
2429 /* Move the menu away of the echo area, to avoid overwriting the
2430 menu with help echo messages or vice versa. */
2431 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
2433 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2434 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2436 else
2438 y--;
2439 uly--;
2442 #endif
2443 if (ulx < 0) x -= ulx;
2444 if (uly < 0) y -= uly;
2446 if (! for_click)
2448 /* If position was not given by a mouse click, adjust so upper left
2449 corner of the menu as a whole ends up at given coordinates. This
2450 is what x-popup-menu says in its documentation. */
2451 x += width/2;
2452 y += 1.5*height/(maxlines+2);
2455 XMenuSetAEQ (menu, TRUE);
2456 XMenuSetFreeze (menu, TRUE);
2457 pane = selidx = 0;
2459 #ifndef MSDOS
2460 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
2461 #endif
2463 record_unwind_protect (pop_down_menu,
2464 Fcons (make_save_value (f, 0),
2465 make_save_value (menu, 0)));
2467 /* Help display under X won't work because XMenuActivate contains
2468 a loop that doesn't give Emacs a chance to process it. */
2469 menu_help_frame = f;
2470 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2471 x, y, ButtonReleaseMask, &datap,
2472 menu_help_callback);
2474 switch (status)
2476 case XM_SUCCESS:
2477 #ifdef XDEBUG
2478 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2479 #endif
2481 /* Find the item number SELIDX in pane number PANE. */
2482 i = 0;
2483 while (i < menu_items_used)
2485 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2487 if (pane == 0)
2488 pane_prefix
2489 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2490 pane--;
2491 i += MENU_ITEMS_PANE_LENGTH;
2493 else
2495 if (pane == -1)
2497 if (selidx == 0)
2499 entry
2500 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2501 if (keymaps != 0)
2503 entry = Fcons (entry, Qnil);
2504 if (!NILP (pane_prefix))
2505 entry = Fcons (pane_prefix, entry);
2507 break;
2509 selidx--;
2511 i += MENU_ITEMS_ITEM_LENGTH;
2514 break;
2516 case XM_FAILURE:
2517 *error = "Can't activate menu";
2518 case XM_IA_SELECT:
2519 entry = Qnil;
2520 break;
2521 case XM_NO_SELECT:
2522 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2523 the menu was invoked with a mouse event as POSITION). */
2524 if (! for_click)
2525 Fsignal (Qquit, Qnil);
2526 entry = Qnil;
2527 break;
2530 unbind_to (specpdl_count, Qnil);
2532 return entry;
2535 #endif /* not USE_X_TOOLKIT */
2537 #endif /* HAVE_MENUS */
2539 #ifndef MSDOS
2540 /* Detect if a dialog or menu has been posted. MSDOS has its own
2541 implementation on msdos.c. */
2544 popup_activated (void)
2546 return popup_activated_flag;
2548 #endif /* not MSDOS */
2550 /* The following is used by delayed window autoselection. */
2552 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
2553 doc: /* Return t if a menu or popup dialog is active. */)
2554 (void)
2556 #ifdef HAVE_MENUS
2557 return (popup_activated ()) ? Qt : Qnil;
2558 #else
2559 return Qnil;
2560 #endif /* HAVE_MENUS */
2563 void
2564 syms_of_xmenu (void)
2566 Qdebug_on_next_call = intern_c_string ("debug-on-next-call");
2567 staticpro (&Qdebug_on_next_call);
2569 #ifdef USE_X_TOOLKIT
2570 widget_id_tick = (1<<16);
2571 next_menubar_widget_id = 1;
2572 #endif
2574 defsubr (&Smenu_or_popup_active_p);
2576 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
2577 defsubr (&Sx_menu_bar_open_internal);
2578 Ffset (intern_c_string ("accelerate-menu"),
2579 intern_c_string (Sx_menu_bar_open_internal.symbol_name));
2580 #endif
2582 #ifdef HAVE_MENUS
2583 defsubr (&Sx_popup_dialog);
2584 #endif