gnus-util.el (gnus-macroexpand-all): Fix last change.
[emacs.git] / src / xmenu.c
blobe8be9c6ad4c4d7ff55282252684558bd412e3ea6
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, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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/>. */
20 /* X pop-up deck-of-cards menu facility for GNU Emacs.
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
27 /* Modified by Fred Pierresteguy on December 93
28 to make the popup menus and menubar use the Xt. */
30 /* Rewritten for clarity and GC protection by rms in Feb 94. */
32 #include <config.h>
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
39 #include <stdio.h>
40 #include <setjmp.h>
42 #include "lisp.h"
43 #include "keyboard.h"
44 #include "keymap.h"
45 #include "frame.h"
46 #include "termhooks.h"
47 #include "window.h"
48 #include "blockinput.h"
49 #include "buffer.h"
50 #include "charset.h"
51 #include "coding.h"
52 #include "sysselect.h"
54 #ifdef MSDOS
55 #include "msdos.h"
56 #endif
58 #ifdef HAVE_X_WINDOWS
59 /* This may include sys/types.h, and that somehow loses
60 if this is not done before the other system files. */
61 #include "xterm.h"
62 #endif
64 /* Load sys/types.h if not already loaded.
65 In some systems loading it twice is suicidal. */
66 #ifndef makedev
67 #include <sys/types.h>
68 #endif
70 #include "dispextern.h"
72 #ifdef HAVE_X_WINDOWS
73 /* Defining HAVE_MULTILINGUAL_MENU would mean that the toolkit menu
74 code accepts the Emacs internal encoding. */
75 #undef HAVE_MULTILINGUAL_MENU
76 #ifdef USE_X_TOOLKIT
77 #include "widget.h"
78 #include <X11/Xlib.h>
79 #include <X11/IntrinsicP.h>
80 #include <X11/CoreP.h>
81 #include <X11/StringDefs.h>
82 #include <X11/Shell.h>
83 #ifdef USE_LUCID
84 #include "xsettings.h"
85 #include "../lwlib/xlwmenu.h"
86 #ifdef HAVE_XAW3D
87 #include <X11/Xaw3d/Paned.h>
88 #else /* !HAVE_XAW3D */
89 #include <X11/Xaw/Paned.h>
90 #endif /* HAVE_XAW3D */
91 #endif /* USE_LUCID */
92 #ifdef USE_MOTIF
93 #include "../lwlib/lwlib.h"
94 #endif
95 #else /* not USE_X_TOOLKIT */
96 #ifndef USE_GTK
97 #include "../oldXMenu/XMenu.h"
98 #endif
99 #endif /* not USE_X_TOOLKIT */
100 #endif /* HAVE_X_WINDOWS */
102 #ifdef USE_GTK
103 #include "gtkutil.h"
104 #endif
106 #include "menu.h"
108 #ifndef TRUE
109 #define TRUE 1
110 #define FALSE 0
111 #endif /* no TRUE */
113 Lisp_Object Qdebug_on_next_call;
115 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
116 static Lisp_Object xdialog_show (FRAME_PTR, int, Lisp_Object, Lisp_Object,
117 const char **);
118 #endif
120 static int update_frame_menubar (struct frame *);
122 /* Flag which when set indicates a dialog or menu has been posted by
123 Xt on behalf of one of the widget sets. */
124 static int popup_activated_flag;
126 static int next_menubar_widget_id;
129 #ifdef USE_X_TOOLKIT
131 /* Return the frame whose ->output_data.x->id equals ID, or 0 if none. */
133 static struct frame *
134 menubar_id_to_frame (LWLIB_ID id)
136 Lisp_Object tail, frame;
137 FRAME_PTR f;
139 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
141 frame = XCAR (tail);
142 if (!FRAMEP (frame))
143 continue;
144 f = XFRAME (frame);
145 if (!FRAME_WINDOW_P (f))
146 continue;
147 if (f->output_data.x->id == id)
148 return f;
150 return 0;
153 #endif
155 #ifdef HAVE_X_WINDOWS
156 /* Return the mouse position in *X and *Y. The coordinates are window
157 relative for the edit window in frame F.
158 This is for Fx_popup_menu. The mouse_position_hook can not
159 be used for X, as it returns window relative coordinates
160 for the window where the mouse is in. This could be the menu bar,
161 the scroll bar or the edit window. Fx_popup_menu needs to be
162 sure it is the edit window. */
163 void
164 mouse_position_for_popup (FRAME_PTR f, int *x, int *y)
166 Window root, dummy_window;
167 int dummy;
169 if (! FRAME_X_P (f))
170 abort ();
172 BLOCK_INPUT;
174 XQueryPointer (FRAME_X_DISPLAY (f),
175 DefaultRootWindow (FRAME_X_DISPLAY (f)),
177 /* The root window which contains the pointer. */
178 &root,
180 /* Window pointer is on, not used */
181 &dummy_window,
183 /* The position on that root window. */
184 x, y,
186 /* x/y in dummy_window coordinates, not used. */
187 &dummy, &dummy,
189 /* Modifier keys and pointer buttons, about which
190 we don't care. */
191 (unsigned int *) &dummy);
193 UNBLOCK_INPUT;
195 /* xmenu_show expects window coordinates, not root window
196 coordinates. Translate. */
197 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
198 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
201 #endif /* HAVE_X_WINDOWS */
203 #ifdef HAVE_MENUS
205 DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0,
206 doc: /* Pop up a dialog box and return user's selection.
207 POSITION specifies which frame to use.
208 This is normally a mouse button event or a window or frame.
209 If POSITION is t, it means to use the frame the mouse is on.
210 The dialog box appears in the middle of the specified frame.
212 CONTENTS specifies the alternatives to display in the dialog box.
213 It is a list of the form (DIALOG ITEM1 ITEM2...).
214 Each ITEM is a cons cell (STRING . VALUE).
215 The return value is VALUE from the chosen item.
217 An ITEM may also be just a string--that makes a nonselectable item.
218 An ITEM may also be nil--that means to put all preceding items
219 on the left of the dialog box and all following items on the right.
220 \(By default, approximately half appear on each side.)
222 If HEADER is non-nil, the frame title for the box is "Information",
223 otherwise it is "Question".
225 If the user gets rid of the dialog box without making a valid choice,
226 for instance using the window manager, then this produces a quit and
227 `x-popup-dialog' does not return. */)
228 (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
230 FRAME_PTR f = NULL;
231 Lisp_Object window;
233 check_x ();
235 /* Decode the first argument: find the window or frame to use. */
236 if (EQ (position, Qt)
237 || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
238 || EQ (XCAR (position), Qtool_bar))))
240 #if 0 /* Using the frame the mouse is on may not be right. */
241 /* Use the mouse's current position. */
242 FRAME_PTR new_f = SELECTED_FRAME ();
243 Lisp_Object bar_window;
244 enum scroll_bar_part part;
245 unsigned long time;
246 Lisp_Object x, y;
248 (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time);
250 if (new_f != 0)
251 XSETFRAME (window, new_f);
252 else
253 window = selected_window;
254 #endif
255 window = selected_window;
257 else if (CONSP (position))
259 Lisp_Object tem;
260 tem = Fcar (position);
261 if (CONSP (tem))
262 window = Fcar (Fcdr (position));
263 else
265 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
266 window = Fcar (tem); /* POSN_WINDOW (tem) */
269 else if (WINDOWP (position) || FRAMEP (position))
270 window = position;
271 else
272 window = Qnil;
274 /* Decode where to put the menu. */
276 if (FRAMEP (window))
277 f = XFRAME (window);
278 else if (WINDOWP (window))
280 CHECK_LIVE_WINDOW (window);
281 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
283 else
284 /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME,
285 but I don't want to make one now. */
286 CHECK_WINDOW (window);
288 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
289 error ("Can not put X dialog on this terminal");
291 /* Force a redisplay before showing the dialog. If a frame is created
292 just before showing the dialog, its contents may not have been fully
293 drawn, as this depends on timing of events from the X server. Redisplay
294 is not done when a dialog is shown. If redisplay could be done in the
295 X event loop (i.e. the X event loop does not run in a signal handler)
296 this would not be needed.
298 Do this before creating the widget value that points to Lisp
299 string contents, because Fredisplay may GC and relocate them. */
300 Fredisplay (Qt);
302 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
303 /* Display a menu with these alternatives
304 in the middle of frame F. */
306 Lisp_Object x, y, frame, newpos;
307 XSETFRAME (frame, f);
308 XSETINT (x, x_pixel_width (f) / 2);
309 XSETINT (y, x_pixel_height (f) / 2);
310 newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil));
312 return Fx_popup_menu (newpos,
313 Fcons (Fcar (contents), Fcons (contents, Qnil)));
315 #else
317 Lisp_Object title;
318 const char *error_name;
319 Lisp_Object selection;
320 int specpdl_count = SPECPDL_INDEX ();
322 /* Decode the dialog items from what was specified. */
323 title = Fcar (contents);
324 CHECK_STRING (title);
325 record_unwind_protect (unuse_menu_items, Qnil);
327 if (NILP (Fcar (Fcdr (contents))))
328 /* No buttons specified, add an "Ok" button so users can pop down
329 the dialog. Also, the lesstif/motif version crashes if there are
330 no buttons. */
331 contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil));
333 list_of_panes (Fcons (contents, Qnil));
335 /* Display them in a dialog box. */
336 BLOCK_INPUT;
337 selection = xdialog_show (f, 0, title, header, &error_name);
338 UNBLOCK_INPUT;
340 unbind_to (specpdl_count, Qnil);
341 discard_menu_items ();
343 if (error_name) error (error_name);
344 return selection;
346 #endif
350 #ifndef MSDOS
352 /* Set menu_items_inuse so no other popup menu or dialog is created. */
354 void
355 x_menu_set_in_use (int in_use)
357 menu_items_inuse = in_use ? Qt : Qnil;
358 popup_activated_flag = in_use;
359 #ifdef USE_X_TOOLKIT
360 if (popup_activated_flag)
361 x_activate_timeout_atimer ();
362 #endif
365 /* Wait for an X event to arrive or for a timer to expire. */
367 void
368 x_menu_wait_for_event (void *data)
370 /* Another way to do this is to register a timer callback, that can be
371 done in GTK and Xt. But we have to do it like this when using only X
372 anyway, and with callbacks we would have three variants for timer handling
373 instead of the small ifdefs below. */
375 while (
376 #ifdef USE_X_TOOLKIT
377 ! XtAppPending (Xt_app_con)
378 #elif defined USE_GTK
379 ! gtk_events_pending ()
380 #else
381 ! XPending ((Display*) data)
382 #endif
385 EMACS_TIME next_time = timer_check (1), *ntp;
386 long secs = EMACS_SECS (next_time);
387 long usecs = EMACS_USECS (next_time);
388 SELECT_TYPE read_fds;
389 struct x_display_info *dpyinfo;
390 int n = 0;
392 FD_ZERO (&read_fds);
393 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
395 int fd = ConnectionNumber (dpyinfo->display);
396 FD_SET (fd, &read_fds);
397 if (fd > n) n = fd;
398 XFlush (dpyinfo->display);
401 if (secs < 0 && usecs < 0)
402 ntp = 0;
403 else
404 ntp = &next_time;
406 select (n + 1, &read_fds, (SELECT_TYPE *)0, (SELECT_TYPE *)0, ntp);
409 #endif /* ! MSDOS */
412 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
414 #ifdef USE_X_TOOLKIT
416 /* Loop in Xt until the menu pulldown or dialog popup has been
417 popped down (deactivated). This is used for x-popup-menu
418 and x-popup-dialog; it is not used for the menu bar.
420 NOTE: All calls to popup_get_selection should be protected
421 with BLOCK_INPUT, UNBLOCK_INPUT wrappers. */
423 static void
424 popup_get_selection (XEvent *initial_event, struct x_display_info *dpyinfo, LWLIB_ID id, int do_timers)
426 XEvent event;
428 while (popup_activated_flag)
430 if (initial_event)
432 event = *initial_event;
433 initial_event = 0;
435 else
437 if (do_timers) x_menu_wait_for_event (0);
438 XtAppNextEvent (Xt_app_con, &event);
441 /* Make sure we don't consider buttons grabbed after menu goes.
442 And make sure to deactivate for any ButtonRelease,
443 even if XtDispatchEvent doesn't do that. */
444 if (event.type == ButtonRelease
445 && dpyinfo->display == event.xbutton.display)
447 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
448 #ifdef USE_MOTIF /* Pretending that the event came from a
449 Btn1Down seems the only way to convince Motif to
450 activate its callbacks; setting the XmNmenuPost
451 isn't working. --marcus@sysc.pdx.edu. */
452 event.xbutton.button = 1;
453 /* Motif only pops down menus when no Ctrl, Alt or Mod
454 key is pressed and the button is released. So reset key state
455 so Motif thinks this is the case. */
456 event.xbutton.state = 0;
457 #endif
459 /* Pop down on C-g and Escape. */
460 else if (event.type == KeyPress
461 && dpyinfo->display == event.xbutton.display)
463 KeySym keysym = XLookupKeysym (&event.xkey, 0);
465 if ((keysym == XK_g && (event.xkey.state & ControlMask) != 0)
466 || keysym == XK_Escape) /* Any escape, ignore modifiers. */
467 popup_activated_flag = 0;
470 x_dispatch_event (&event, event.xany.display);
474 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
475 doc: /* Start key navigation of the menu bar in FRAME.
476 This initially opens the first menu bar item and you can then navigate with the
477 arrow keys, select a menu entry with the return key or cancel with the
478 escape key. If FRAME has no menu bar this function does nothing.
480 If FRAME is nil or not given, use the selected frame. */)
481 (Lisp_Object frame)
483 XEvent ev;
484 FRAME_PTR f = check_x_frame (frame);
485 Widget menubar;
486 BLOCK_INPUT;
488 if (FRAME_EXTERNAL_MENU_BAR (f))
489 set_frame_menubar (f, 0, 1);
491 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
492 if (menubar)
494 Window child;
495 int error_p = 0;
497 x_catch_errors (FRAME_X_DISPLAY (f));
498 memset (&ev, 0, sizeof ev);
499 ev.xbutton.display = FRAME_X_DISPLAY (f);
500 ev.xbutton.window = XtWindow (menubar);
501 ev.xbutton.root = FRAME_X_DISPLAY_INFO (f)->root_window;
502 ev.xbutton.time = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
503 ev.xbutton.button = Button1;
504 ev.xbutton.x = ev.xbutton.y = FRAME_MENUBAR_HEIGHT (f) / 2;
505 ev.xbutton.same_screen = True;
507 #ifdef USE_MOTIF
509 Arg al[2];
510 WidgetList list;
511 Cardinal nr;
512 XtSetArg (al[0], XtNchildren, &list);
513 XtSetArg (al[1], XtNnumChildren, &nr);
514 XtGetValues (menubar, al, 2);
515 ev.xbutton.window = XtWindow (list[0]);
517 #endif
519 XTranslateCoordinates (FRAME_X_DISPLAY (f),
520 /* From-window, to-window. */
521 ev.xbutton.window, ev.xbutton.root,
523 /* From-position, to-position. */
524 ev.xbutton.x, ev.xbutton.y,
525 &ev.xbutton.x_root, &ev.xbutton.y_root,
527 /* Child of win. */
528 &child);
529 error_p = x_had_errors_p (FRAME_X_DISPLAY (f));
530 x_uncatch_errors ();
532 if (! error_p)
534 ev.type = ButtonPress;
535 ev.xbutton.state = 0;
537 XtDispatchEvent (&ev);
538 ev.xbutton.type = ButtonRelease;
539 ev.xbutton.state = Button1Mask;
540 XtDispatchEvent (&ev);
544 UNBLOCK_INPUT;
546 return Qnil;
548 #endif /* USE_X_TOOLKIT */
551 #ifdef USE_GTK
552 DEFUN ("x-menu-bar-open-internal", Fx_menu_bar_open_internal, Sx_menu_bar_open_internal, 0, 1, "i",
553 doc: /* Start key navigation of the menu bar in FRAME.
554 This initially opens the first menu bar item and you can then navigate with the
555 arrow keys, select a menu entry with the return key or cancel with the
556 escape key. If FRAME has no menu bar this function does nothing.
558 If FRAME is nil or not given, use the selected frame. */)
559 (Lisp_Object frame)
561 GtkWidget *menubar;
562 FRAME_PTR f;
564 /* gcc 2.95 doesn't accept the FRAME_PTR declaration after
565 BLOCK_INPUT. */
567 BLOCK_INPUT;
568 f = check_x_frame (frame);
570 if (FRAME_EXTERNAL_MENU_BAR (f))
571 set_frame_menubar (f, 0, 1);
573 menubar = FRAME_X_OUTPUT (f)->menubar_widget;
574 if (menubar)
576 /* Activate the first menu. */
577 GList *children = gtk_container_get_children (GTK_CONTAINER (menubar));
579 if (children)
581 g_signal_emit_by_name (children->data, "activate_item");
582 popup_activated_flag = 1;
583 g_list_free (children);
586 UNBLOCK_INPUT;
588 return Qnil;
591 /* Loop util popup_activated_flag is set to zero in a callback.
592 Used for popup menus and dialogs. */
594 static void
595 popup_widget_loop (int do_timers, GtkWidget *widget)
597 ++popup_activated_flag;
599 /* Process events in the Gtk event loop until done. */
600 while (popup_activated_flag)
602 if (do_timers) x_menu_wait_for_event (0);
603 gtk_main_iteration ();
606 #endif
608 /* Activate the menu bar of frame F.
609 This is called from keyboard.c when it gets the
610 MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue.
612 To activate the menu bar, we use the X button-press event
613 that was saved in saved_menu_event.
614 That makes the toolkit do its thing.
616 But first we recompute the menu bar contents (the whole tree).
618 The reason for saving the button event until here, instead of
619 passing it to the toolkit right away, is that we can safely
620 execute Lisp code. */
622 void
623 x_activate_menubar (FRAME_PTR f)
625 if (! FRAME_X_P (f))
626 abort ();
628 if (!f->output_data.x->saved_menu_event->type)
629 return;
631 #ifdef USE_GTK
632 if (! xg_win_to_widget (FRAME_X_DISPLAY (f),
633 f->output_data.x->saved_menu_event->xany.window))
634 return;
635 #endif
637 set_frame_menubar (f, 0, 1);
638 BLOCK_INPUT;
639 popup_activated_flag = 1;
640 #ifdef USE_GTK
641 XPutBackEvent (f->output_data.x->display_info->display,
642 f->output_data.x->saved_menu_event);
643 #else
644 XtDispatchEvent (f->output_data.x->saved_menu_event);
645 #endif
646 UNBLOCK_INPUT;
648 /* Ignore this if we get it a second time. */
649 f->output_data.x->saved_menu_event->type = 0;
652 /* This callback is invoked when the user selects a menubar cascade
653 pushbutton, but before the pulldown menu is posted. */
655 #ifndef USE_GTK
656 static void
657 popup_activate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
659 popup_activated_flag = 1;
660 #ifdef USE_X_TOOLKIT
661 x_activate_timeout_atimer ();
662 #endif
664 #endif
666 /* This callback is invoked when a dialog or menu is finished being
667 used and has been unposted. */
669 #ifdef USE_GTK
670 static void
671 popup_deactivate_callback (GtkWidget *widget, gpointer client_data)
673 popup_activated_flag = 0;
675 #else
676 static void
677 popup_deactivate_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
679 popup_activated_flag = 0;
681 #endif
684 /* Function that finds the frame for WIDGET and shows the HELP text
685 for that widget.
686 F is the frame if known, or NULL if not known. */
687 static void
688 show_help_event (FRAME_PTR f, xt_or_gtk_widget widget, Lisp_Object help)
690 Lisp_Object frame;
692 if (f)
694 XSETFRAME (frame, f);
695 kbd_buffer_store_help_event (frame, help);
697 else
699 #if 0 /* This code doesn't do anything useful. ++kfs */
700 /* WIDGET is the popup menu. It's parent is the frame's
701 widget. See which frame that is. */
702 xt_or_gtk_widget frame_widget = XtParent (widget);
703 Lisp_Object tail;
705 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
707 frame = XCAR (tail);
708 if (FRAMEP (frame)
709 && (f = XFRAME (frame),
710 FRAME_X_P (f) && f->output_data.x->widget == frame_widget))
711 break;
713 #endif
714 show_help_echo (help, Qnil, Qnil, Qnil, 1);
718 /* Callback called when menu items are highlighted/unhighlighted
719 while moving the mouse over them. WIDGET is the menu bar or menu
720 popup widget. ID is its LWLIB_ID. CALL_DATA contains a pointer to
721 the data structure for the menu item, or null in case of
722 unhighlighting. */
724 #ifdef USE_GTK
725 void
726 menu_highlight_callback (GtkWidget *widget, gpointer call_data)
728 xg_menu_item_cb_data *cb_data;
729 Lisp_Object help;
731 cb_data = (xg_menu_item_cb_data*) g_object_get_data (G_OBJECT (widget),
732 XG_ITEM_DATA);
733 if (! cb_data) return;
735 help = call_data ? cb_data->help : Qnil;
737 /* If popup_activated_flag is greater than 1 we are in a popup menu.
738 Don't show help for them, they won't appear before the
739 popup is popped down. */
740 if (popup_activated_flag <= 1)
741 show_help_event (cb_data->cl_data->f, widget, help);
743 #else
744 void
745 menu_highlight_callback (Widget widget, LWLIB_ID id, void *call_data)
747 struct frame *f;
748 Lisp_Object help;
750 widget_value *wv = (widget_value *) call_data;
752 help = wv ? wv->help : Qnil;
754 /* Determine the frame for the help event. */
755 f = menubar_id_to_frame (id);
757 show_help_event (f, widget, help);
759 #endif
761 #ifdef USE_GTK
762 /* Gtk calls callbacks just because we tell it what item should be
763 selected in a radio group. If this variable is set to a non-zero
764 value, we are creating menus and don't want callbacks right now.
766 static int xg_crazy_callback_abort;
768 /* This callback is called from the menu bar pulldown menu
769 when the user makes a selection.
770 Figure out what the user chose
771 and put the appropriate events into the keyboard buffer. */
772 static void
773 menubar_selection_callback (GtkWidget *widget, gpointer client_data)
775 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
777 if (xg_crazy_callback_abort)
778 return;
780 if (! cb_data || ! cb_data->cl_data || ! cb_data->cl_data->f)
781 return;
783 /* For a group of radio buttons, GTK calls the selection callback first
784 for the item that was active before the selection and then for the one that
785 is active after the selection. For C-h k this means we get the help on
786 the deselected item and then the selected item is executed. Prevent that
787 by ignoring the non-active item. */
788 if (GTK_IS_RADIO_MENU_ITEM (widget)
789 && ! gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget)))
790 return;
792 /* When a menu is popped down, X generates a focus event (i.e. focus
793 goes back to the frame below the menu). Since GTK buffers events,
794 we force it out here before the menu selection event. Otherwise
795 sit-for will exit at once if the focus event follows the menu selection
796 event. */
798 BLOCK_INPUT;
799 while (gtk_events_pending ())
800 gtk_main_iteration ();
801 UNBLOCK_INPUT;
803 find_and_call_menu_selection (cb_data->cl_data->f,
804 cb_data->cl_data->menu_bar_items_used,
805 cb_data->cl_data->menu_bar_vector,
806 cb_data->call_data);
809 #else /* not USE_GTK */
811 /* This callback is called from the menu bar pulldown menu
812 when the user makes a selection.
813 Figure out what the user chose
814 and put the appropriate events into the keyboard buffer. */
815 static void
816 menubar_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
818 FRAME_PTR f;
820 f = menubar_id_to_frame (id);
821 if (!f)
822 return;
823 find_and_call_menu_selection (f, f->menu_bar_items_used,
824 f->menu_bar_vector, client_data);
826 #endif /* not USE_GTK */
828 /* Recompute all the widgets of frame F, when the menu bar has been
829 changed. Value is non-zero if widgets were updated. */
831 static int
832 update_frame_menubar (FRAME_PTR f)
834 #ifdef USE_GTK
835 return xg_update_frame_menubar (f);
836 #else
837 struct x_output *x;
838 int columns, rows;
840 if (! FRAME_X_P (f))
841 abort ();
843 x = f->output_data.x;
845 if (!x->menubar_widget || XtIsManaged (x->menubar_widget))
846 return 0;
848 BLOCK_INPUT;
849 /* Save the size of the frame because the pane widget doesn't accept
850 to resize itself. So force it. */
851 columns = FRAME_COLS (f);
852 rows = FRAME_LINES (f);
854 /* Do the voodoo which means "I'm changing lots of things, don't try
855 to refigure sizes until I'm done." */
856 lw_refigure_widget (x->column_widget, False);
858 /* The order in which children are managed is the top to bottom
859 order in which they are displayed in the paned window. First,
860 remove the text-area widget. */
861 XtUnmanageChild (x->edit_widget);
863 /* Remove the menubar that is there now, and put up the menubar that
864 should be there. */
865 XtManageChild (x->menubar_widget);
866 XtMapWidget (x->menubar_widget);
867 XtVaSetValues (x->menubar_widget, XtNmappedWhenManaged, 1, NULL);
869 /* Re-manage the text-area widget, and then thrash the sizes. */
870 XtManageChild (x->edit_widget);
871 lw_refigure_widget (x->column_widget, True);
873 /* Force the pane widget to resize itself with the right values. */
874 EmacsFrameSetCharSize (x->edit_widget, columns, rows);
875 UNBLOCK_INPUT;
876 #endif
877 return 1;
880 #ifdef USE_LUCID
881 static void
882 apply_systemfont_to_dialog (Widget w)
884 const char *fn = xsettings_get_system_normal_font ();
885 if (fn)
887 XrmDatabase db = XtDatabase (XtDisplay (w));
888 if (db)
889 XrmPutStringResource (&db, "*dialog.faceName", fn);
893 static void
894 apply_systemfont_to_menu (Widget w)
896 const char *fn = xsettings_get_system_normal_font ();
897 int defflt;
899 if (!fn) return;
901 if (XtIsShell (w)) /* popup menu */
903 Widget *childs = NULL;
905 XtVaGetValues (w, XtNchildren, &childs, NULL);
906 if (*childs) w = *childs;
909 /* Only use system font if the default is used for the menu. */
910 XtVaGetValues (w, XtNdefaultFace, &defflt, NULL);
911 if (defflt)
912 XtVaSetValues (w, XtNfaceName, fn, NULL);
914 #endif
916 /* Set the contents of the menubar widgets of frame F.
917 The argument FIRST_TIME is currently ignored;
918 it is set the first time this is called, from initialize_frame_menubar. */
920 void
921 set_frame_menubar (FRAME_PTR f, int first_time, int deep_p)
923 xt_or_gtk_widget menubar_widget;
924 #ifdef USE_X_TOOLKIT
925 LWLIB_ID id;
926 #endif
927 Lisp_Object items;
928 widget_value *wv, *first_wv, *prev_wv = 0;
929 int i, last_i = 0;
930 int *submenu_start, *submenu_end;
931 int *submenu_top_level_items, *submenu_n_panes;
933 if (! FRAME_X_P (f))
934 abort ();
936 menubar_widget = f->output_data.x->menubar_widget;
938 XSETFRAME (Vmenu_updating_frame, f);
940 #ifdef USE_X_TOOLKIT
941 if (f->output_data.x->id == 0)
942 f->output_data.x->id = next_menubar_widget_id++;
943 id = f->output_data.x->id;
944 #endif
946 if (! menubar_widget)
947 deep_p = 1;
948 /* Make the first call for any given frame always go deep. */
949 else if (!f->output_data.x->saved_menu_event && !deep_p)
951 deep_p = 1;
952 f->output_data.x->saved_menu_event = (XEvent*)xmalloc (sizeof (XEvent));
953 f->output_data.x->saved_menu_event->type = 0;
956 #ifdef USE_GTK
957 /* If we have detached menus, we must update deep so detached menus
958 also gets updated. */
959 deep_p = deep_p || xg_have_tear_offs ();
960 #endif
962 if (deep_p)
964 /* Make a widget-value tree representing the entire menu trees. */
966 struct buffer *prev = current_buffer;
967 Lisp_Object buffer;
968 int specpdl_count = SPECPDL_INDEX ();
969 int previous_menu_items_used = f->menu_bar_items_used;
970 Lisp_Object *previous_items
971 = (Lisp_Object *) alloca (previous_menu_items_used
972 * sizeof (Lisp_Object));
974 /* If we are making a new widget, its contents are empty,
975 do always reinitialize them. */
976 if (! menubar_widget)
977 previous_menu_items_used = 0;
979 buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer;
980 specbind (Qinhibit_quit, Qt);
981 /* Don't let the debugger step into this code
982 because it is not reentrant. */
983 specbind (Qdebug_on_next_call, Qnil);
985 record_unwind_save_match_data ();
986 if (NILP (Voverriding_local_map_menu_flag))
988 specbind (Qoverriding_terminal_local_map, Qnil);
989 specbind (Qoverriding_local_map, Qnil);
992 set_buffer_internal_1 (XBUFFER (buffer));
994 /* Run the Lucid hook. */
995 safe_run_hooks (Qactivate_menubar_hook);
997 /* If it has changed current-menubar from previous value,
998 really recompute the menubar from the value. */
999 if (! NILP (Vlucid_menu_bar_dirty_flag))
1000 call0 (Qrecompute_lucid_menubar);
1001 safe_run_hooks (Qmenu_bar_update_hook);
1002 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1004 items = FRAME_MENU_BAR_ITEMS (f);
1006 /* Save the frame's previous menu bar contents data. */
1007 if (previous_menu_items_used)
1008 memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents,
1009 previous_menu_items_used * sizeof (Lisp_Object));
1011 /* Fill in menu_items with the current menu bar contents.
1012 This can evaluate Lisp code. */
1013 save_menu_items ();
1015 menu_items = f->menu_bar_vector;
1016 menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0;
1017 submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1018 submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1019 submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int));
1020 submenu_top_level_items
1021 = (int *) alloca (XVECTOR (items)->size * sizeof (int *));
1022 init_menu_items ();
1023 for (i = 0; i < XVECTOR (items)->size; i += 4)
1025 Lisp_Object key, string, maps;
1027 last_i = i;
1029 key = XVECTOR (items)->contents[i];
1030 string = XVECTOR (items)->contents[i + 1];
1031 maps = XVECTOR (items)->contents[i + 2];
1032 if (NILP (string))
1033 break;
1035 submenu_start[i] = menu_items_used;
1037 menu_items_n_panes = 0;
1038 submenu_top_level_items[i]
1039 = parse_single_submenu (key, string, maps);
1040 submenu_n_panes[i] = menu_items_n_panes;
1042 submenu_end[i] = menu_items_used;
1045 finish_menu_items ();
1047 /* Convert menu_items into widget_value trees
1048 to display the menu. This cannot evaluate Lisp code. */
1050 wv = xmalloc_widget_value ();
1051 wv->name = "menubar";
1052 wv->value = 0;
1053 wv->enabled = 1;
1054 wv->button_type = BUTTON_TYPE_NONE;
1055 wv->help = Qnil;
1056 first_wv = wv;
1058 for (i = 0; i < last_i; i += 4)
1060 menu_items_n_panes = submenu_n_panes[i];
1061 wv = digest_single_submenu (submenu_start[i], submenu_end[i],
1062 submenu_top_level_items[i]);
1063 if (prev_wv)
1064 prev_wv->next = wv;
1065 else
1066 first_wv->contents = wv;
1067 /* Don't set wv->name here; GC during the loop might relocate it. */
1068 wv->enabled = 1;
1069 wv->button_type = BUTTON_TYPE_NONE;
1070 prev_wv = wv;
1073 set_buffer_internal_1 (prev);
1075 /* If there has been no change in the Lisp-level contents
1076 of the menu bar, skip redisplaying it. Just exit. */
1078 /* Compare the new menu items with the ones computed last time. */
1079 for (i = 0; i < previous_menu_items_used; i++)
1080 if (menu_items_used == i
1081 || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i])))
1082 break;
1083 if (i == menu_items_used && i == previous_menu_items_used && i != 0)
1085 /* The menu items have not changed. Don't bother updating
1086 the menus in any form, since it would be a no-op. */
1087 free_menubar_widget_value_tree (first_wv);
1088 discard_menu_items ();
1089 unbind_to (specpdl_count, Qnil);
1090 return;
1093 /* The menu items are different, so store them in the frame. */
1094 f->menu_bar_vector = menu_items;
1095 f->menu_bar_items_used = menu_items_used;
1097 /* This undoes save_menu_items. */
1098 unbind_to (specpdl_count, Qnil);
1100 /* Now GC cannot happen during the lifetime of the widget_value,
1101 so it's safe to store data from a Lisp_String. */
1102 wv = first_wv->contents;
1103 for (i = 0; i < XVECTOR (items)->size; i += 4)
1105 Lisp_Object string;
1106 string = XVECTOR (items)->contents[i + 1];
1107 if (NILP (string))
1108 break;
1109 wv->name = (char *) SDATA (string);
1110 update_submenu_strings (wv->contents);
1111 wv = wv->next;
1115 else
1117 /* Make a widget-value tree containing
1118 just the top level menu bar strings. */
1120 wv = xmalloc_widget_value ();
1121 wv->name = "menubar";
1122 wv->value = 0;
1123 wv->enabled = 1;
1124 wv->button_type = BUTTON_TYPE_NONE;
1125 wv->help = Qnil;
1126 first_wv = wv;
1128 items = FRAME_MENU_BAR_ITEMS (f);
1129 for (i = 0; i < XVECTOR (items)->size; i += 4)
1131 Lisp_Object string;
1133 string = XVECTOR (items)->contents[i + 1];
1134 if (NILP (string))
1135 break;
1137 wv = xmalloc_widget_value ();
1138 wv->name = (char *) SDATA (string);
1139 wv->value = 0;
1140 wv->enabled = 1;
1141 wv->button_type = BUTTON_TYPE_NONE;
1142 wv->help = Qnil;
1143 /* This prevents lwlib from assuming this
1144 menu item is really supposed to be empty. */
1145 /* The EMACS_INT cast avoids a warning.
1146 This value just has to be different from small integers. */
1147 wv->call_data = (void *) (EMACS_INT) (-1);
1149 if (prev_wv)
1150 prev_wv->next = wv;
1151 else
1152 first_wv->contents = wv;
1153 prev_wv = wv;
1156 /* Forget what we thought we knew about what is in the
1157 detailed contents of the menu bar menus.
1158 Changing the top level always destroys the contents. */
1159 f->menu_bar_items_used = 0;
1162 /* Create or update the menu bar widget. */
1164 BLOCK_INPUT;
1166 #ifdef USE_GTK
1167 xg_crazy_callback_abort = 1;
1168 if (menubar_widget)
1170 /* The fourth arg is DEEP_P, which says to consider the entire
1171 menu trees we supply, rather than just the menu bar item names. */
1172 xg_modify_menubar_widgets (menubar_widget,
1174 first_wv,
1175 deep_p,
1176 G_CALLBACK (menubar_selection_callback),
1177 G_CALLBACK (popup_deactivate_callback),
1178 G_CALLBACK (menu_highlight_callback));
1180 else
1182 GtkWidget *wvbox = f->output_data.x->vbox_widget;
1184 menubar_widget
1185 = xg_create_widget ("menubar", "menubar", f, first_wv,
1186 G_CALLBACK (menubar_selection_callback),
1187 G_CALLBACK (popup_deactivate_callback),
1188 G_CALLBACK (menu_highlight_callback));
1190 f->output_data.x->menubar_widget = menubar_widget;
1194 #else /* not USE_GTK */
1195 if (menubar_widget)
1197 /* Disable resizing (done for Motif!) */
1198 lw_allow_resizing (f->output_data.x->widget, False);
1200 /* The third arg is DEEP_P, which says to consider the entire
1201 menu trees we supply, rather than just the menu bar item names. */
1202 lw_modify_all_widgets (id, first_wv, deep_p);
1204 /* Re-enable the edit widget to resize. */
1205 lw_allow_resizing (f->output_data.x->widget, True);
1207 else
1209 char menuOverride[] = "Ctrl<KeyPress>g: MenuGadgetEscape()";
1210 XtTranslations override = XtParseTranslationTable (menuOverride);
1212 menubar_widget = lw_create_widget ("menubar", "menubar", id, first_wv,
1213 f->output_data.x->column_widget,
1215 popup_activate_callback,
1216 menubar_selection_callback,
1217 popup_deactivate_callback,
1218 menu_highlight_callback);
1219 f->output_data.x->menubar_widget = menubar_widget;
1221 /* Make menu pop down on C-g. */
1222 XtOverrideTranslations (menubar_widget, override);
1223 #ifdef USE_LUCID
1224 apply_systemfont_to_menu (menubar_widget);
1225 #endif
1229 int menubar_size;
1230 if (f->output_data.x->menubar_widget)
1231 XtRealizeWidget (f->output_data.x->menubar_widget);
1233 menubar_size
1234 = (f->output_data.x->menubar_widget
1235 ? (f->output_data.x->menubar_widget->core.height
1236 + f->output_data.x->menubar_widget->core.border_width)
1237 : 0);
1239 #if 1 /* Experimentally, we now get the right results
1240 for -geometry -0-0 without this. 24 Aug 96, rms.
1241 Maybe so, but the menu bar size is missing the pixels so the
1242 WM size hints are off by these pixels. Jan D, oct 2009. */
1243 #ifdef USE_LUCID
1244 if (FRAME_EXTERNAL_MENU_BAR (f))
1246 Dimension ibw = 0;
1247 XtVaGetValues (f->output_data.x->column_widget,
1248 XtNinternalBorderWidth, &ibw, NULL);
1249 menubar_size += ibw;
1251 #endif /* USE_LUCID */
1252 #endif /* 1 */
1254 f->output_data.x->menubar_height = menubar_size;
1256 #endif /* not USE_GTK */
1258 free_menubar_widget_value_tree (first_wv);
1259 update_frame_menubar (f);
1261 #ifdef USE_GTK
1262 xg_crazy_callback_abort = 0;
1263 #endif
1265 UNBLOCK_INPUT;
1268 /* Called from Fx_create_frame to create the initial menubar of a frame
1269 before it is mapped, so that the window is mapped with the menubar already
1270 there instead of us tacking it on later and thrashing the window after it
1271 is visible. */
1273 void
1274 initialize_frame_menubar (FRAME_PTR f)
1276 /* This function is called before the first chance to redisplay
1277 the frame. It has to be, so the frame will have the right size. */
1278 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
1279 set_frame_menubar (f, 1, 1);
1283 /* Get rid of the menu bar of frame F, and free its storage.
1284 This is used when deleting a frame, and when turning off the menu bar.
1285 For GTK this function is in gtkutil.c. */
1287 #ifndef USE_GTK
1288 void
1289 free_frame_menubar (FRAME_PTR f)
1291 Widget menubar_widget;
1293 if (! FRAME_X_P (f))
1294 abort ();
1296 menubar_widget = f->output_data.x->menubar_widget;
1298 f->output_data.x->menubar_height = 0;
1300 if (menubar_widget)
1302 #ifdef USE_MOTIF
1303 /* Removing the menu bar magically changes the shell widget's x
1304 and y position of (0, 0) which, when the menu bar is turned
1305 on again, leads to pull-down menuss appearing in strange
1306 positions near the upper-left corner of the display. This
1307 happens only with some window managers like twm and ctwm,
1308 but not with other like Motif's mwm or kwm, because the
1309 latter generate ConfigureNotify events when the menu bar
1310 is switched off, which fixes the shell position. */
1311 Position x0, y0, x1, y1;
1312 #endif
1314 BLOCK_INPUT;
1316 #ifdef USE_MOTIF
1317 if (f->output_data.x->widget)
1318 XtVaGetValues (f->output_data.x->widget, XtNx, &x0, XtNy, &y0, NULL);
1319 #endif
1321 lw_destroy_all_widgets ((LWLIB_ID) f->output_data.x->id);
1322 f->output_data.x->menubar_widget = NULL;
1324 if (f->output_data.x->widget)
1326 #ifdef USE_MOTIF
1327 XtVaGetValues (f->output_data.x->widget, XtNx, &x1, XtNy, &y1, NULL);
1328 if (x1 == 0 && y1 == 0)
1329 XtVaSetValues (f->output_data.x->widget, XtNx, x0, XtNy, y0, NULL);
1330 #endif
1331 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
1333 UNBLOCK_INPUT;
1336 #endif /* not USE_GTK */
1338 #endif /* USE_X_TOOLKIT || USE_GTK */
1340 /* xmenu_show actually displays a menu using the panes and items in menu_items
1341 and returns the value selected from it.
1342 There are two versions of xmenu_show, one for Xt and one for Xlib.
1343 Both assume input is blocked by the caller. */
1345 /* F is the frame the menu is for.
1346 X and Y are the frame-relative specified position,
1347 relative to the inside upper left corner of the frame F.
1348 FOR_CLICK is nonzero if this menu was invoked for a mouse click.
1349 KEYMAPS is 1 if this menu was specified with keymaps;
1350 in that case, we return a list containing the chosen item's value
1351 and perhaps also the pane's prefix.
1352 TITLE is the specified menu title.
1353 ERROR is a place to store an error message string in case of failure.
1354 (We return nil on failure, but the value doesn't actually matter.) */
1356 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1358 /* The item selected in the popup menu. */
1359 static Lisp_Object *volatile menu_item_selection;
1361 #ifdef USE_GTK
1363 /* Used when position a popup menu. See menu_position_func and
1364 create_and_show_popup_menu below. */
1365 struct next_popup_x_y
1367 FRAME_PTR f;
1368 int x;
1369 int y;
1372 /* The menu position function to use if we are not putting a popup
1373 menu where the pointer is.
1374 MENU is the menu to pop up.
1375 X and Y shall on exit contain x/y where the menu shall pop up.
1376 PUSH_IN is not documented in the GTK manual.
1377 USER_DATA is any data passed in when calling gtk_menu_popup.
1378 Here it points to a struct next_popup_x_y where the coordinates
1379 to store in *X and *Y are as well as the frame for the popup.
1381 Here only X and Y are used. */
1382 static void
1383 menu_position_func (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer user_data)
1385 struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
1386 GtkRequisition req;
1387 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (data->f);
1388 int disp_width = x_display_pixel_width (dpyinfo);
1389 int disp_height = x_display_pixel_height (dpyinfo);
1391 *x = data->x;
1392 *y = data->y;
1394 /* Check if there is room for the menu. If not, adjust x/y so that
1395 the menu is fully visible. */
1396 gtk_widget_size_request (GTK_WIDGET (menu), &req);
1397 if (data->x + req.width > disp_width)
1398 *x -= data->x + req.width - disp_width;
1399 if (data->y + req.height > disp_height)
1400 *y -= data->y + req.height - disp_height;
1403 static void
1404 popup_selection_callback (GtkWidget *widget, gpointer client_data)
1406 xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
1408 if (xg_crazy_callback_abort) return;
1409 if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
1412 static Lisp_Object
1413 pop_down_menu (Lisp_Object arg)
1415 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1417 popup_activated_flag = 0;
1418 BLOCK_INPUT;
1419 gtk_widget_destroy (GTK_WIDGET (p->pointer));
1420 UNBLOCK_INPUT;
1421 return Qnil;
1424 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1425 menu pops down.
1426 menu_item_selection will be set to the selection. */
1427 static void
1428 create_and_show_popup_menu (FRAME_PTR f, widget_value *first_wv, int x, int y, int for_click, EMACS_UINT timestamp)
1430 int i;
1431 GtkWidget *menu;
1432 GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
1433 struct next_popup_x_y popup_x_y;
1434 int specpdl_count = SPECPDL_INDEX ();
1436 if (! FRAME_X_P (f))
1437 abort ();
1439 xg_crazy_callback_abort = 1;
1440 menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
1441 G_CALLBACK (popup_selection_callback),
1442 G_CALLBACK (popup_deactivate_callback),
1443 G_CALLBACK (menu_highlight_callback));
1444 xg_crazy_callback_abort = 0;
1446 if (! for_click)
1448 /* Not invoked by a click. pop up at x/y. */
1449 pos_func = menu_position_func;
1451 /* Adjust coordinates to be root-window-relative. */
1452 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1453 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1455 popup_x_y.x = x;
1456 popup_x_y.y = y;
1457 popup_x_y.f = f;
1459 i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
1461 else
1463 for (i = 0; i < 5; i++)
1464 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
1465 break;
1468 /* Display the menu. */
1469 gtk_widget_show_all (menu);
1471 gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i,
1472 timestamp > 0 ? timestamp : gtk_get_current_event_time());
1474 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
1476 if (gtk_widget_get_mapped (menu))
1478 /* Set this to one. popup_widget_loop increases it by one, so it becomes
1479 two. show_help_echo uses this to detect popup menus. */
1480 popup_activated_flag = 1;
1481 /* Process events that apply to the menu. */
1482 popup_widget_loop (1, menu);
1485 unbind_to (specpdl_count, Qnil);
1487 /* Must reset this manually because the button release event is not passed
1488 to Emacs event loop. */
1489 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
1492 #else /* not USE_GTK */
1494 /* We need a unique id for each widget handled by the Lucid Widget
1495 library.
1497 For the main windows, and popup menus, we use this counter,
1498 which we increment each time after use. This starts from 1<<16.
1500 For menu bars, we use numbers starting at 0, counted in
1501 next_menubar_widget_id. */
1502 LWLIB_ID widget_id_tick;
1504 static void
1505 popup_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1507 menu_item_selection = (Lisp_Object *) client_data;
1510 /* ARG is the LWLIB ID of the dialog box, represented
1511 as a Lisp object as (HIGHPART . LOWPART). */
1513 static Lisp_Object
1514 pop_down_menu (Lisp_Object arg)
1516 LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
1517 | XINT (XCDR (arg)));
1519 BLOCK_INPUT;
1520 lw_destroy_all_widgets (id);
1521 UNBLOCK_INPUT;
1522 popup_activated_flag = 0;
1524 return Qnil;
1527 /* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
1528 menu pops down.
1529 menu_item_selection will be set to the selection. */
1530 static void
1531 create_and_show_popup_menu (FRAME_PTR f, widget_value *first_wv,
1532 int x, int y, int for_click, EMACS_UINT timestamp)
1534 int i;
1535 Arg av[2];
1536 int ac = 0;
1537 XButtonPressedEvent dummy;
1538 LWLIB_ID menu_id;
1539 Widget menu;
1541 if (! FRAME_X_P (f))
1542 abort ();
1544 menu_id = widget_id_tick++;
1545 menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
1546 f->output_data.x->widget, 1, 0,
1547 popup_selection_callback,
1548 popup_deactivate_callback,
1549 menu_highlight_callback);
1551 #ifdef USE_LUCID
1552 apply_systemfont_to_menu (menu);
1553 #endif
1555 dummy.type = ButtonPress;
1556 dummy.serial = 0;
1557 dummy.send_event = 0;
1558 dummy.display = FRAME_X_DISPLAY (f);
1559 dummy.time = CurrentTime;
1560 dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
1561 dummy.window = dummy.root;
1562 dummy.subwindow = dummy.root;
1563 dummy.x = x;
1564 dummy.y = y;
1566 /* Adjust coordinates to be root-window-relative. */
1567 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
1568 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
1570 dummy.x_root = x;
1571 dummy.y_root = y;
1573 dummy.state = 0;
1574 dummy.button = 0;
1575 for (i = 0; i < 5; i++)
1576 if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
1577 dummy.button = i;
1579 /* Don't allow any geometry request from the user. */
1580 XtSetArg (av[ac], XtNgeometry, 0); ac++;
1581 XtSetValues (menu, av, ac);
1583 /* Display the menu. */
1584 lw_popup_menu (menu, (XEvent *) &dummy);
1585 popup_activated_flag = 1;
1586 x_activate_timeout_atimer ();
1589 int fact = 4 * sizeof (LWLIB_ID);
1590 int specpdl_count = SPECPDL_INDEX ();
1591 record_unwind_protect (pop_down_menu,
1592 Fcons (make_number (menu_id >> (fact)),
1593 make_number (menu_id & ~(-1 << (fact)))));
1595 /* Process events that apply to the menu. */
1596 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), menu_id, 1);
1598 unbind_to (specpdl_count, Qnil);
1602 #endif /* not USE_GTK */
1604 Lisp_Object
1605 xmenu_show (FRAME_PTR f, int x, int y, int for_click, int keymaps,
1606 Lisp_Object title, const char **error, EMACS_UINT timestamp)
1608 int i;
1609 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
1610 widget_value **submenu_stack
1611 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
1612 Lisp_Object *subprefix_stack
1613 = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object));
1614 int submenu_depth = 0;
1616 int first_pane;
1618 if (! FRAME_X_P (f))
1619 abort ();
1621 *error = NULL;
1623 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
1625 *error = "Empty menu";
1626 return Qnil;
1629 /* Create a tree of widget_value objects
1630 representing the panes and their items. */
1631 wv = xmalloc_widget_value ();
1632 wv->name = "menu";
1633 wv->value = 0;
1634 wv->enabled = 1;
1635 wv->button_type = BUTTON_TYPE_NONE;
1636 wv->help =Qnil;
1637 first_wv = wv;
1638 first_pane = 1;
1640 /* Loop over all panes and items, filling in the tree. */
1641 i = 0;
1642 while (i < menu_items_used)
1644 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1646 submenu_stack[submenu_depth++] = save_wv;
1647 save_wv = prev_wv;
1648 prev_wv = 0;
1649 first_pane = 1;
1650 i++;
1652 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1654 prev_wv = save_wv;
1655 save_wv = submenu_stack[--submenu_depth];
1656 first_pane = 0;
1657 i++;
1659 else if (EQ (XVECTOR (menu_items)->contents[i], Qt)
1660 && submenu_depth != 0)
1661 i += MENU_ITEMS_PANE_LENGTH;
1662 /* Ignore a nil in the item list.
1663 It's meaningful only for dialog boxes. */
1664 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1665 i += 1;
1666 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1668 /* Create a new pane. */
1669 Lisp_Object pane_name, prefix;
1670 const char *pane_string;
1672 pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME);
1673 prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX);
1675 #ifndef HAVE_MULTILINGUAL_MENU
1676 if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name))
1678 pane_name = ENCODE_MENU_STRING (pane_name);
1679 ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name);
1681 #endif
1682 pane_string = (NILP (pane_name)
1683 ? "" : (char *) SDATA (pane_name));
1684 /* If there is just one top-level pane, put all its items directly
1685 under the top-level menu. */
1686 if (menu_items_n_panes == 1)
1687 pane_string = "";
1689 /* If the pane has a meaningful name,
1690 make the pane a top-level menu item
1691 with its items as a submenu beneath it. */
1692 if (!keymaps && strcmp (pane_string, ""))
1694 wv = xmalloc_widget_value ();
1695 if (save_wv)
1696 save_wv->next = wv;
1697 else
1698 first_wv->contents = wv;
1699 wv->name = pane_string;
1700 if (keymaps && !NILP (prefix))
1701 wv->name++;
1702 wv->value = 0;
1703 wv->enabled = 1;
1704 wv->button_type = BUTTON_TYPE_NONE;
1705 wv->help = Qnil;
1706 save_wv = wv;
1707 prev_wv = 0;
1709 else if (first_pane)
1711 save_wv = wv;
1712 prev_wv = 0;
1714 first_pane = 0;
1715 i += MENU_ITEMS_PANE_LENGTH;
1717 else
1719 /* Create a new item within current pane. */
1720 Lisp_Object item_name, enable, descrip, def, type, selected, help;
1721 item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME);
1722 enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE);
1723 descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY);
1724 def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION);
1725 type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE);
1726 selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED);
1727 help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP);
1729 #ifndef HAVE_MULTILINGUAL_MENU
1730 if (STRINGP (item_name) && STRING_MULTIBYTE (item_name))
1732 item_name = ENCODE_MENU_STRING (item_name);
1733 ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name);
1736 if (STRINGP (descrip) && STRING_MULTIBYTE (descrip))
1738 descrip = ENCODE_MENU_STRING (descrip);
1739 ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip);
1741 #endif /* not HAVE_MULTILINGUAL_MENU */
1743 wv = xmalloc_widget_value ();
1744 if (prev_wv)
1745 prev_wv->next = wv;
1746 else
1747 save_wv->contents = wv;
1748 wv->name = (char *) SDATA (item_name);
1749 if (!NILP (descrip))
1750 wv->key = (char *) SDATA (descrip);
1751 wv->value = 0;
1752 /* If this item has a null value,
1753 make the call_data null so that it won't display a box
1754 when the mouse is on it. */
1755 wv->call_data
1756 = (!NILP (def) ? (void *) &XVECTOR (menu_items)->contents[i] : 0);
1757 wv->enabled = !NILP (enable);
1759 if (NILP (type))
1760 wv->button_type = BUTTON_TYPE_NONE;
1761 else if (EQ (type, QCtoggle))
1762 wv->button_type = BUTTON_TYPE_TOGGLE;
1763 else if (EQ (type, QCradio))
1764 wv->button_type = BUTTON_TYPE_RADIO;
1765 else
1766 abort ();
1768 wv->selected = !NILP (selected);
1770 if (! STRINGP (help))
1771 help = Qnil;
1773 wv->help = help;
1775 prev_wv = wv;
1777 i += MENU_ITEMS_ITEM_LENGTH;
1781 /* Deal with the title, if it is non-nil. */
1782 if (!NILP (title))
1784 widget_value *wv_title = xmalloc_widget_value ();
1785 widget_value *wv_sep1 = xmalloc_widget_value ();
1786 widget_value *wv_sep2 = xmalloc_widget_value ();
1788 wv_sep2->name = "--";
1789 wv_sep2->next = first_wv->contents;
1790 wv_sep2->help = Qnil;
1792 wv_sep1->name = "--";
1793 wv_sep1->next = wv_sep2;
1794 wv_sep1->help = Qnil;
1796 #ifndef HAVE_MULTILINGUAL_MENU
1797 if (STRING_MULTIBYTE (title))
1798 title = ENCODE_MENU_STRING (title);
1799 #endif
1801 wv_title->name = (char *) SDATA (title);
1802 wv_title->enabled = TRUE;
1803 wv_title->button_type = BUTTON_TYPE_NONE;
1804 wv_title->help = Qnil;
1805 wv_title->next = wv_sep1;
1806 first_wv->contents = wv_title;
1809 /* No selection has been chosen yet. */
1810 menu_item_selection = 0;
1812 /* Actually create and show the menu until popped down. */
1813 create_and_show_popup_menu (f, first_wv, x, y, for_click, timestamp);
1815 /* Free the widget_value objects we used to specify the contents. */
1816 free_menubar_widget_value_tree (first_wv);
1818 /* Find the selected item, and its pane, to return
1819 the proper value. */
1820 if (menu_item_selection != 0)
1822 Lisp_Object prefix, entry;
1824 prefix = entry = Qnil;
1825 i = 0;
1826 while (i < menu_items_used)
1828 if (EQ (XVECTOR (menu_items)->contents[i], Qnil))
1830 subprefix_stack[submenu_depth++] = prefix;
1831 prefix = entry;
1832 i++;
1834 else if (EQ (XVECTOR (menu_items)->contents[i], Qlambda))
1836 prefix = subprefix_stack[--submenu_depth];
1837 i++;
1839 else if (EQ (XVECTOR (menu_items)->contents[i], Qt))
1841 prefix
1842 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
1843 i += MENU_ITEMS_PANE_LENGTH;
1845 /* Ignore a nil in the item list.
1846 It's meaningful only for dialog boxes. */
1847 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
1848 i += 1;
1849 else
1851 entry
1852 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
1853 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
1855 if (keymaps != 0)
1857 int j;
1859 entry = Fcons (entry, Qnil);
1860 if (!NILP (prefix))
1861 entry = Fcons (prefix, entry);
1862 for (j = submenu_depth - 1; j >= 0; j--)
1863 if (!NILP (subprefix_stack[j]))
1864 entry = Fcons (subprefix_stack[j], entry);
1866 return entry;
1868 i += MENU_ITEMS_ITEM_LENGTH;
1872 else if (!for_click)
1873 /* Make "Cancel" equivalent to C-g. */
1874 Fsignal (Qquit, Qnil);
1876 return Qnil;
1879 #ifdef USE_GTK
1880 static void
1881 dialog_selection_callback (GtkWidget *widget, gpointer client_data)
1883 /* The EMACS_INT cast avoids a warning. There's no problem
1884 as long as pointers have enough bits to hold small integers. */
1885 if ((int) (EMACS_INT) client_data != -1)
1886 menu_item_selection = (Lisp_Object *) client_data;
1888 popup_activated_flag = 0;
1891 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1892 dialog pops down.
1893 menu_item_selection will be set to the selection. */
1894 static void
1895 create_and_show_dialog (FRAME_PTR f, widget_value *first_wv)
1897 GtkWidget *menu;
1899 if (! FRAME_X_P (f))
1900 abort ();
1902 menu = xg_create_widget ("dialog", first_wv->name, f, first_wv,
1903 G_CALLBACK (dialog_selection_callback),
1904 G_CALLBACK (popup_deactivate_callback),
1907 if (menu)
1909 int specpdl_count = SPECPDL_INDEX ();
1910 record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
1912 /* Display the menu. */
1913 gtk_widget_show_all (menu);
1915 /* Process events that apply to the menu. */
1916 popup_widget_loop (1, menu);
1918 unbind_to (specpdl_count, Qnil);
1922 #else /* not USE_GTK */
1923 static void
1924 dialog_selection_callback (Widget widget, LWLIB_ID id, XtPointer client_data)
1926 /* The EMACS_INT cast avoids a warning. There's no problem
1927 as long as pointers have enough bits to hold small integers. */
1928 if ((int) (EMACS_INT) client_data != -1)
1929 menu_item_selection = (Lisp_Object *) client_data;
1931 BLOCK_INPUT;
1932 lw_destroy_all_widgets (id);
1933 UNBLOCK_INPUT;
1934 popup_activated_flag = 0;
1938 /* Pop up the dialog for frame F defined by FIRST_WV and loop until the
1939 dialog pops down.
1940 menu_item_selection will be set to the selection. */
1941 static void
1942 create_and_show_dialog (FRAME_PTR f, widget_value *first_wv)
1944 LWLIB_ID dialog_id;
1946 if (!FRAME_X_P (f))
1947 abort();
1949 dialog_id = widget_id_tick++;
1950 #ifdef USE_LUCID
1951 apply_systemfont_to_dialog (f->output_data.x->widget);
1952 #endif
1953 lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv,
1954 f->output_data.x->widget, 1, 0,
1955 dialog_selection_callback, 0, 0);
1956 lw_modify_all_widgets (dialog_id, first_wv->contents, True);
1957 /* Display the dialog box. */
1958 lw_pop_up_all_widgets (dialog_id);
1959 popup_activated_flag = 1;
1960 x_activate_timeout_atimer ();
1962 /* Process events that apply to the dialog box.
1963 Also handle timers. */
1965 int count = SPECPDL_INDEX ();
1966 int fact = 4 * sizeof (LWLIB_ID);
1968 /* xdialog_show_unwind is responsible for popping the dialog box down. */
1969 record_unwind_protect (pop_down_menu,
1970 Fcons (make_number (dialog_id >> (fact)),
1971 make_number (dialog_id & ~(-1 << (fact)))));
1973 popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f),
1974 dialog_id, 1);
1976 unbind_to (count, Qnil);
1980 #endif /* not USE_GTK */
1982 static const char * button_names [] = {
1983 "button1", "button2", "button3", "button4", "button5",
1984 "button6", "button7", "button8", "button9", "button10" };
1986 static Lisp_Object
1987 xdialog_show (FRAME_PTR f,
1988 int keymaps,
1989 Lisp_Object title,
1990 Lisp_Object header,
1991 const char **error_name)
1993 int i, nb_buttons=0;
1994 char dialog_name[6];
1996 widget_value *wv, *first_wv = 0, *prev_wv = 0;
1998 /* Number of elements seen so far, before boundary. */
1999 int left_count = 0;
2000 /* 1 means we've seen the boundary between left-hand elts and right-hand. */
2001 int boundary_seen = 0;
2003 if (! FRAME_X_P (f))
2004 abort ();
2006 *error_name = NULL;
2008 if (menu_items_n_panes > 1)
2010 *error_name = "Multiple panes in dialog box";
2011 return Qnil;
2014 /* Create a tree of widget_value objects
2015 representing the text label and buttons. */
2017 Lisp_Object pane_name, prefix;
2018 const char *pane_string;
2019 pane_name = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_NAME];
2020 prefix = XVECTOR (menu_items)->contents[MENU_ITEMS_PANE_PREFIX];
2021 pane_string = (NILP (pane_name)
2022 ? "" : (char *) SDATA (pane_name));
2023 prev_wv = xmalloc_widget_value ();
2024 prev_wv->value = pane_string;
2025 if (keymaps && !NILP (prefix))
2026 prev_wv->name++;
2027 prev_wv->enabled = 1;
2028 prev_wv->name = "message";
2029 prev_wv->help = Qnil;
2030 first_wv = prev_wv;
2032 /* Loop over all panes and items, filling in the tree. */
2033 i = MENU_ITEMS_PANE_LENGTH;
2034 while (i < menu_items_used)
2037 /* Create a new item within current pane. */
2038 Lisp_Object item_name, enable, descrip;
2039 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2040 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2041 descrip
2042 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2044 if (NILP (item_name))
2046 free_menubar_widget_value_tree (first_wv);
2047 *error_name = "Submenu in dialog items";
2048 return Qnil;
2050 if (EQ (item_name, Qquote))
2052 /* This is the boundary between left-side elts
2053 and right-side elts. Stop incrementing right_count. */
2054 boundary_seen = 1;
2055 i++;
2056 continue;
2058 if (nb_buttons >= 9)
2060 free_menubar_widget_value_tree (first_wv);
2061 *error_name = "Too many dialog items";
2062 return Qnil;
2065 wv = xmalloc_widget_value ();
2066 prev_wv->next = wv;
2067 wv->name = (char *) button_names[nb_buttons];
2068 if (!NILP (descrip))
2069 wv->key = (char *) SDATA (descrip);
2070 wv->value = (char *) SDATA (item_name);
2071 wv->call_data = (void *) &XVECTOR (menu_items)->contents[i];
2072 wv->enabled = !NILP (enable);
2073 wv->help = Qnil;
2074 prev_wv = wv;
2076 if (! boundary_seen)
2077 left_count++;
2079 nb_buttons++;
2080 i += MENU_ITEMS_ITEM_LENGTH;
2083 /* If the boundary was not specified,
2084 by default put half on the left and half on the right. */
2085 if (! boundary_seen)
2086 left_count = nb_buttons - nb_buttons / 2;
2088 wv = xmalloc_widget_value ();
2089 wv->name = dialog_name;
2090 wv->help = Qnil;
2092 /* Frame title: 'Q' = Question, 'I' = Information.
2093 Can also have 'E' = Error if, one day, we want
2094 a popup for errors. */
2095 if (NILP(header))
2096 dialog_name[0] = 'Q';
2097 else
2098 dialog_name[0] = 'I';
2100 /* Dialog boxes use a really stupid name encoding
2101 which specifies how many buttons to use
2102 and how many buttons are on the right. */
2103 dialog_name[1] = '0' + nb_buttons;
2104 dialog_name[2] = 'B';
2105 dialog_name[3] = 'R';
2106 /* Number of buttons to put on the right. */
2107 dialog_name[4] = '0' + nb_buttons - left_count;
2108 dialog_name[5] = 0;
2109 wv->contents = first_wv;
2110 first_wv = wv;
2113 /* No selection has been chosen yet. */
2114 menu_item_selection = 0;
2116 /* Actually create and show the dialog. */
2117 create_and_show_dialog (f, first_wv);
2119 /* Free the widget_value objects we used to specify the contents. */
2120 free_menubar_widget_value_tree (first_wv);
2122 /* Find the selected item, and its pane, to return
2123 the proper value. */
2124 if (menu_item_selection != 0)
2126 Lisp_Object prefix;
2128 prefix = Qnil;
2129 i = 0;
2130 while (i < menu_items_used)
2132 Lisp_Object entry;
2134 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2136 prefix
2137 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2138 i += MENU_ITEMS_PANE_LENGTH;
2140 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2142 /* This is the boundary between left-side elts and
2143 right-side elts. */
2144 ++i;
2146 else
2148 entry
2149 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2150 if (menu_item_selection == &XVECTOR (menu_items)->contents[i])
2152 if (keymaps != 0)
2154 entry = Fcons (entry, Qnil);
2155 if (!NILP (prefix))
2156 entry = Fcons (prefix, entry);
2158 return entry;
2160 i += MENU_ITEMS_ITEM_LENGTH;
2164 else
2165 /* Make "Cancel" equivalent to C-g. */
2166 Fsignal (Qquit, Qnil);
2168 return Qnil;
2171 #else /* not USE_X_TOOLKIT && not USE_GTK */
2173 /* The frame of the last activated non-toolkit menu bar.
2174 Used to generate menu help events. */
2176 static struct frame *menu_help_frame;
2179 /* Show help HELP_STRING, or clear help if HELP_STRING is null.
2181 PANE is the pane number, and ITEM is the menu item number in
2182 the menu (currently not used).
2184 This cannot be done with generating a HELP_EVENT because
2185 XMenuActivate contains a loop that doesn't let Emacs process
2186 keyboard events. */
2188 static void
2189 menu_help_callback (char *help_string, int pane, int item)
2191 Lisp_Object *first_item;
2192 Lisp_Object pane_name;
2193 Lisp_Object menu_object;
2195 first_item = XVECTOR (menu_items)->contents;
2196 if (EQ (first_item[0], Qt))
2197 pane_name = first_item[MENU_ITEMS_PANE_NAME];
2198 else if (EQ (first_item[0], Qquote))
2199 /* This shouldn't happen, see xmenu_show. */
2200 pane_name = empty_unibyte_string;
2201 else
2202 pane_name = first_item[MENU_ITEMS_ITEM_NAME];
2204 /* (menu-item MENU-NAME PANE-NUMBER) */
2205 menu_object = Fcons (Qmenu_item,
2206 Fcons (pane_name,
2207 Fcons (make_number (pane), Qnil)));
2208 show_help_echo (help_string ? build_string (help_string) : Qnil,
2209 Qnil, menu_object, make_number (item), 1);
2212 static Lisp_Object
2213 pop_down_menu (Lisp_Object arg)
2215 struct Lisp_Save_Value *p1 = XSAVE_VALUE (Fcar (arg));
2216 struct Lisp_Save_Value *p2 = XSAVE_VALUE (Fcdr (arg));
2218 FRAME_PTR f = p1->pointer;
2219 XMenu *menu = p2->pointer;
2221 BLOCK_INPUT;
2222 #ifndef MSDOS
2223 XUngrabPointer (FRAME_X_DISPLAY (f), CurrentTime);
2224 XUngrabKeyboard (FRAME_X_DISPLAY (f), CurrentTime);
2225 #endif
2226 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2228 #ifdef HAVE_X_WINDOWS
2229 /* Assume the mouse has moved out of the X window.
2230 If it has actually moved in, we will get an EnterNotify. */
2231 x_mouse_leave (FRAME_X_DISPLAY_INFO (f));
2233 /* State that no mouse buttons are now held.
2234 (The oldXMenu code doesn't track this info for us.)
2235 That is not necessarily true, but the fiction leads to reasonable
2236 results, and it is a pain to ask which are actually held now. */
2237 FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
2239 #endif /* HAVE_X_WINDOWS */
2241 UNBLOCK_INPUT;
2243 return Qnil;
2247 Lisp_Object
2248 xmenu_show (FRAME_PTR f, int x, int y, int for_click, int keymaps,
2249 Lisp_Object title, const char **error, EMACS_UINT timestamp)
2251 Window root;
2252 XMenu *menu;
2253 int pane, selidx, lpane, status;
2254 Lisp_Object entry, pane_prefix;
2255 char *datap;
2256 int ulx, uly, width, height;
2257 int dispwidth, dispheight;
2258 int i, j, lines, maxlines;
2259 int maxwidth;
2260 int dummy_int;
2261 unsigned int dummy_uint;
2262 int specpdl_count = SPECPDL_INDEX ();
2264 if (! FRAME_X_P (f) && ! FRAME_MSDOS_P (f))
2265 abort ();
2267 *error = 0;
2268 if (menu_items_n_panes == 0)
2269 return Qnil;
2271 if (menu_items_used <= MENU_ITEMS_PANE_LENGTH)
2273 *error = "Empty menu";
2274 return Qnil;
2277 /* Figure out which root window F is on. */
2278 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &root,
2279 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
2280 &dummy_uint, &dummy_uint);
2282 /* Make the menu on that window. */
2283 menu = XMenuCreate (FRAME_X_DISPLAY (f), root, "emacs");
2284 if (menu == NULL)
2286 *error = "Can't create menu";
2287 return Qnil;
2290 /* Don't GC while we prepare and show the menu,
2291 because we give the oldxmenu library pointers to the
2292 contents of strings. */
2293 inhibit_garbage_collection ();
2295 #ifdef HAVE_X_WINDOWS
2296 /* Adjust coordinates to relative to the outer (window manager) window. */
2297 x += FRAME_OUTER_TO_INNER_DIFF_X (f);
2298 y += FRAME_OUTER_TO_INNER_DIFF_Y (f);
2299 #endif /* HAVE_X_WINDOWS */
2301 /* Adjust coordinates to be root-window-relative. */
2302 x += f->left_pos;
2303 y += f->top_pos;
2305 /* Create all the necessary panes and their items. */
2306 maxlines = lines = i = 0;
2307 while (i < menu_items_used)
2309 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2311 /* Create a new pane. */
2312 Lisp_Object pane_name, prefix;
2313 char *pane_string;
2315 maxlines = max (maxlines, lines);
2316 lines = 0;
2317 pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME];
2318 prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2319 pane_string = (NILP (pane_name)
2320 ? "" : (char *) SDATA (pane_name));
2321 if (keymaps && !NILP (prefix))
2322 pane_string++;
2324 lpane = XMenuAddPane (FRAME_X_DISPLAY (f), menu, pane_string, TRUE);
2325 if (lpane == XM_FAILURE)
2327 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2328 *error = "Can't create pane";
2329 return Qnil;
2331 i += MENU_ITEMS_PANE_LENGTH;
2333 /* Find the width of the widest item in this pane. */
2334 maxwidth = 0;
2335 j = i;
2336 while (j < menu_items_used)
2338 Lisp_Object item;
2339 item = XVECTOR (menu_items)->contents[j];
2340 if (EQ (item, Qt))
2341 break;
2342 if (NILP (item))
2344 j++;
2345 continue;
2347 width = SBYTES (item);
2348 if (width > maxwidth)
2349 maxwidth = width;
2351 j += MENU_ITEMS_ITEM_LENGTH;
2354 /* Ignore a nil in the item list.
2355 It's meaningful only for dialog boxes. */
2356 else if (EQ (XVECTOR (menu_items)->contents[i], Qquote))
2357 i += 1;
2358 else
2360 /* Create a new item within current pane. */
2361 Lisp_Object item_name, enable, descrip, help;
2362 unsigned char *item_data;
2363 char *help_string;
2365 item_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_NAME];
2366 enable = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_ENABLE];
2367 descrip
2368 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_EQUIV_KEY];
2369 help = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_HELP];
2370 help_string = STRINGP (help) ? SDATA (help) : NULL;
2372 if (!NILP (descrip))
2374 int gap = maxwidth - SBYTES (item_name);
2375 /* if alloca is fast, use that to make the space,
2376 to reduce gc needs. */
2377 item_data
2378 = (unsigned char *) alloca (maxwidth
2379 + SBYTES (descrip) + 1);
2380 memcpy (item_data, SDATA (item_name), SBYTES (item_name));
2381 for (j = SCHARS (item_name); j < maxwidth; j++)
2382 item_data[j] = ' ';
2383 memcpy (item_data + j, SDATA (descrip), SBYTES (descrip));
2384 item_data[j + SBYTES (descrip)] = 0;
2386 else
2387 item_data = SDATA (item_name);
2389 if (XMenuAddSelection (FRAME_X_DISPLAY (f),
2390 menu, lpane, 0, item_data,
2391 !NILP (enable), help_string)
2392 == XM_FAILURE)
2394 XMenuDestroy (FRAME_X_DISPLAY (f), menu);
2395 *error = "Can't add selection to menu";
2396 return Qnil;
2398 i += MENU_ITEMS_ITEM_LENGTH;
2399 lines++;
2403 maxlines = max (maxlines, lines);
2405 /* All set and ready to fly. */
2406 XMenuRecompute (FRAME_X_DISPLAY (f), menu);
2407 dispwidth = DisplayWidth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2408 dispheight = DisplayHeight (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f));
2409 x = min (x, dispwidth);
2410 y = min (y, dispheight);
2411 x = max (x, 1);
2412 y = max (y, 1);
2413 XMenuLocate (FRAME_X_DISPLAY (f), menu, 0, 0, x, y,
2414 &ulx, &uly, &width, &height);
2415 if (ulx+width > dispwidth)
2417 x -= (ulx + width) - dispwidth;
2418 ulx = dispwidth - width;
2420 if (uly+height > dispheight)
2422 y -= (uly + height) - dispheight;
2423 uly = dispheight - height;
2425 #ifndef HAVE_X_WINDOWS
2426 if (FRAME_HAS_MINIBUF_P (f) && uly+height > dispheight - 1)
2428 /* Move the menu away of the echo area, to avoid overwriting the
2429 menu with help echo messages or vice versa. */
2430 if (BUFFERP (echo_area_buffer[0]) && WINDOWP (echo_area_window))
2432 y -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2433 uly -= WINDOW_TOTAL_LINES (XWINDOW (echo_area_window));
2435 else
2437 y--;
2438 uly--;
2441 #endif
2442 if (ulx < 0) x -= ulx;
2443 if (uly < 0) y -= uly;
2445 if (! for_click)
2447 /* If position was not given by a mouse click, adjust so upper left
2448 corner of the menu as a whole ends up at given coordinates. This
2449 is what x-popup-menu says in its documentation. */
2450 x += width/2;
2451 y += 1.5*height/(maxlines+2);
2454 XMenuSetAEQ (menu, TRUE);
2455 XMenuSetFreeze (menu, TRUE);
2456 pane = selidx = 0;
2458 #ifndef MSDOS
2459 XMenuActivateSetWaitFunction (x_menu_wait_for_event, FRAME_X_DISPLAY (f));
2460 #endif
2462 record_unwind_protect (pop_down_menu,
2463 Fcons (make_save_value (f, 0),
2464 make_save_value (menu, 0)));
2466 /* Help display under X won't work because XMenuActivate contains
2467 a loop that doesn't give Emacs a chance to process it. */
2468 menu_help_frame = f;
2469 status = XMenuActivate (FRAME_X_DISPLAY (f), menu, &pane, &selidx,
2470 x, y, ButtonReleaseMask, &datap,
2471 menu_help_callback);
2473 switch (status)
2475 case XM_SUCCESS:
2476 #ifdef XDEBUG
2477 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
2478 #endif
2480 /* Find the item number SELIDX in pane number PANE. */
2481 i = 0;
2482 while (i < menu_items_used)
2484 if (EQ (XVECTOR (menu_items)->contents[i], Qt))
2486 if (pane == 0)
2487 pane_prefix
2488 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX];
2489 pane--;
2490 i += MENU_ITEMS_PANE_LENGTH;
2492 else
2494 if (pane == -1)
2496 if (selidx == 0)
2498 entry
2499 = XVECTOR (menu_items)->contents[i + MENU_ITEMS_ITEM_VALUE];
2500 if (keymaps != 0)
2502 entry = Fcons (entry, Qnil);
2503 if (!NILP (pane_prefix))
2504 entry = Fcons (pane_prefix, entry);
2506 break;
2508 selidx--;
2510 i += MENU_ITEMS_ITEM_LENGTH;
2513 break;
2515 case XM_FAILURE:
2516 *error = "Can't activate menu";
2517 case XM_IA_SELECT:
2518 entry = Qnil;
2519 break;
2520 case XM_NO_SELECT:
2521 /* Make "Cancel" equivalent to C-g unless FOR_CLICK (which means
2522 the menu was invoked with a mouse event as POSITION). */
2523 if (! for_click)
2524 Fsignal (Qquit, Qnil);
2525 entry = Qnil;
2526 break;
2529 unbind_to (specpdl_count, Qnil);
2531 return entry;
2534 #endif /* not USE_X_TOOLKIT */
2536 #endif /* HAVE_MENUS */
2538 #ifndef MSDOS
2539 /* Detect if a dialog or menu has been posted. MSDOS has its own
2540 implementation on msdos.c. */
2543 popup_activated (void)
2545 return popup_activated_flag;
2547 #endif /* not MSDOS */
2549 /* The following is used by delayed window autoselection. */
2551 DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
2552 doc: /* Return t if a menu or popup dialog is active. */)
2553 (void)
2555 #ifdef HAVE_MENUS
2556 return (popup_activated ()) ? Qt : Qnil;
2557 #else
2558 return Qnil;
2559 #endif /* HAVE_MENUS */
2562 void
2563 syms_of_xmenu (void)
2565 Qdebug_on_next_call = intern_c_string ("debug-on-next-call");
2566 staticpro (&Qdebug_on_next_call);
2568 #ifdef USE_X_TOOLKIT
2569 widget_id_tick = (1<<16);
2570 next_menubar_widget_id = 1;
2571 #endif
2573 defsubr (&Smenu_or_popup_active_p);
2575 #if defined (USE_GTK) || defined (USE_X_TOOLKIT)
2576 defsubr (&Sx_menu_bar_open_internal);
2577 Ffset (intern_c_string ("accelerate-menu"),
2578 intern_c_string (Sx_menu_bar_open_internal.symbol_name));
2579 #endif
2581 #ifdef HAVE_MENUS
2582 defsubr (&Sx_popup_dialog);
2583 #endif