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