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