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