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