X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/b79c8219e88f33e55a58789e4ea42f3c2b23c728..de9855d5a3ca490f67933aa1ae5a88f571199626:/src/xmenu.c diff --git a/src/xmenu.c b/src/xmenu.c index 468462a6ce..d049fb99c6 100644 --- a/src/xmenu.c +++ b/src/xmenu.c @@ -1,6 +1,6 @@ /* X Communication module for terminals which understand the X protocol. Copyright (C) 1986, 1988, 1993, 1994, 1996, 1999, 2000, 2001, 2002, 2003, - 2004, 2005 Free Software Foundation, Inc. + 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Emacs. @@ -97,10 +97,10 @@ Boston, MA 02110-1301, USA. */ #define FALSE 0 #endif /* no TRUE */ -Lisp_Object Vmenu_updating_frame; - Lisp_Object Qdebug_on_next_call; +extern Lisp_Object Vmenu_updating_frame; + extern Lisp_Object Qmenu_bar; extern Lisp_Object QCtoggle, QCradio; @@ -266,14 +266,15 @@ menubar_id_to_frame (id) static void init_menu_items () { + if (!NILP (menu_items_inuse)) + error ("Trying to use a menu from within a menu-entry"); + if (NILP (menu_items)) { menu_items_allocated = 60; menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil); } - if (!NILP (menu_items_inuse)) - error ("Trying to use a menu from within a menu-entry"); menu_items_inuse = Qt; menu_items_used = 0; menu_items_n_panes = 0; @@ -310,6 +311,40 @@ discard_menu_items () xassert (NILP (menu_items_inuse)); } +/* This undoes save_menu_items, and it is called by the specpdl unwind + mechanism. */ + +static Lisp_Object +restore_menu_items (saved) + Lisp_Object saved; +{ + menu_items = XCAR (saved); + menu_items_inuse = (! NILP (menu_items) ? Qt : Qnil); + menu_items_allocated = (VECTORP (menu_items) ? ASIZE (menu_items) : 0); + saved = XCDR (saved); + menu_items_used = XINT (XCAR (saved)); + saved = XCDR (saved); + menu_items_n_panes = XINT (XCAR (saved)); + saved = XCDR (saved); + menu_items_submenu_depth = XINT (XCAR (saved)); + return Qnil; +} + +/* Push the whole state of menu_items processing onto the specpdl. + It will be restored when the specpdl is unwound. */ + +static void +save_menu_items () +{ + Lisp_Object saved = list4 (!NILP (menu_items_inuse) ? menu_items : Qnil, + make_number (menu_items_used), + make_number (menu_items_n_panes), + make_number (menu_items_submenu_depth)); + record_unwind_protect (restore_menu_items, saved); + menu_items_inuse = Qnil; + menu_items = Qnil; +} + /* Make the menu_items vector twice as large. */ static void @@ -320,6 +355,7 @@ grow_menu_items () old = menu_items; menu_items_allocated *= 2; + menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil); bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents, old_size * sizeof (Lisp_Object)); @@ -899,8 +935,11 @@ no quit occurs and `x-popup-menu' returns nil. */) xpos += XINT (x); ypos += XINT (y); + + XSETFRAME (Vmenu_updating_frame, f); } - Vmenu_updating_frame = Qnil; + else + Vmenu_updating_frame = Qnil; #endif /* HAVE_MENUS */ record_unwind_protect (unuse_menu_items, Qnil); @@ -1143,6 +1182,10 @@ x_menu_set_in_use (in_use) { menu_items_inuse = in_use ? Qt : Qnil; popup_activated_flag = in_use; +#ifdef USE_X_TOOLKIT + if (popup_activated_flag) + x_activate_timeout_atimer (); +#endif } /* Wait for an X event to arrive or for a timer to expire. */ @@ -1262,9 +1305,119 @@ popup_get_selection (initial_event, dpyinfo, id, do_timers) } } +DEFUN ("menu-bar-open", Fmenu_bar_open, Smenu_bar_open, 0, 1, "i", + doc: /* Start key navigation of the menu bar in FRAME. +This initially opens the first menu bar item and you can then navigate with the +arrow keys, select a menu entry with the return key or cancel with the +escape key. If FRAME has no menu bar this function does nothing. + +If FRAME is nil or not given, use the selected frame. */) + (frame) + Lisp_Object frame; +{ + XEvent ev; + FRAME_PTR f = check_x_frame (frame); + Widget menubar; + BLOCK_INPUT; + + if (FRAME_EXTERNAL_MENU_BAR (f)) + set_frame_menubar (f, 0, 1); + + menubar = FRAME_X_OUTPUT (f)->menubar_widget; + if (menubar) + { + Window child; + int error_p = 0; + + x_catch_errors (FRAME_X_DISPLAY (f)); + memset (&ev, 0, sizeof ev); + ev.xbutton.display = FRAME_X_DISPLAY (f); + ev.xbutton.window = XtWindow (menubar); + ev.xbutton.root = FRAME_X_DISPLAY_INFO (f)->root_window; + ev.xbutton.time = XtLastTimestampProcessed (FRAME_X_DISPLAY (f)); + ev.xbutton.button = Button1; + ev.xbutton.x = ev.xbutton.y = FRAME_MENUBAR_HEIGHT (f) / 2; + ev.xbutton.same_screen = True; + +#ifdef USE_MOTIF + { + Arg al[2]; + WidgetList list; + Cardinal nr; + XtSetArg (al[0], XtNchildren, &list); + XtSetArg (al[1], XtNnumChildren, &nr); + XtGetValues (menubar, al, 2); + ev.xbutton.window = XtWindow (list[0]); + } +#endif + + XTranslateCoordinates (FRAME_X_DISPLAY (f), + /* From-window, to-window. */ + ev.xbutton.window, ev.xbutton.root, + + /* From-position, to-position. */ + ev.xbutton.x, ev.xbutton.y, + &ev.xbutton.x_root, &ev.xbutton.y_root, + + /* Child of win. */ + &child); + error_p = x_had_errors_p (FRAME_X_DISPLAY (f)); + x_uncatch_errors (); + + if (! error_p) + { + ev.type = ButtonPress; + ev.xbutton.state = 0; + + XtDispatchEvent (&ev); + ev.xbutton.type = ButtonRelease; + ev.xbutton.state = Button1Mask; + XtDispatchEvent (&ev); + } + } + + UNBLOCK_INPUT; + + return Qnil; +} #endif /* USE_X_TOOLKIT */ + #ifdef USE_GTK +DEFUN ("menu-bar-open", Fmenu_bar_open, Smenu_bar_open, 0, 1, "i", + doc: /* Start key navigation of the menu bar in FRAME. +This initially opens the first menu bar item and you can then navigate with the +arrow keys, select a menu entry with the return key or cancel with the +escape key. If FRAME has no menu bar this function does nothing. + +If FRAME is nil or not given, use the selected frame. */) + (frame) + Lisp_Object frame; +{ + GtkWidget *menubar; + BLOCK_INPUT; + FRAME_PTR f = check_x_frame (frame); + + if (FRAME_EXTERNAL_MENU_BAR (f)) + set_frame_menubar (f, 0, 1); + + menubar = FRAME_X_OUTPUT (f)->menubar_widget; + if (menubar) + { + /* Activate the first menu. */ + GList *children = gtk_container_get_children (GTK_CONTAINER (menubar)); + + gtk_menu_shell_select_item (GTK_MENU_SHELL (menubar), + GTK_WIDGET (children->data)); + + popup_activated_flag = 1; + g_list_free (children); + } + UNBLOCK_INPUT; + + return Qnil; +} + /* Loop util popup_activated_flag is set to zero in a callback. Used for popup menus and dialogs. */ @@ -1349,6 +1502,9 @@ popup_activate_callback (widget, id, client_data) XtPointer client_data; { popup_activated_flag = 1; +#ifdef USE_X_TOOLKIT + x_activate_timeout_atimer (); +#endif } #endif @@ -1725,6 +1881,7 @@ digest_single_submenu (start, end, top_level_items) int i; int submenu_depth = 0; widget_value **submenu_stack; + int panes_seen = 0; submenu_stack = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); @@ -1771,6 +1928,8 @@ digest_single_submenu (start, end, top_level_items) Lisp_Object pane_name, prefix; char *pane_string; + panes_seen++; + pane_name = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_NAME]; prefix = XVECTOR (menu_items)->contents[i + MENU_ITEMS_PANE_PREFIX]; @@ -1804,8 +1963,11 @@ digest_single_submenu (start, end, top_level_items) wv->enabled = 1; wv->button_type = BUTTON_TYPE_NONE; wv->help = Qnil; + save_wv = wv; } - save_wv = wv; + else + save_wv = first_wv; + prev_wv = 0; i += MENU_ITEMS_PANE_LENGTH; } @@ -1815,6 +1977,10 @@ digest_single_submenu (start, end, top_level_items) Lisp_Object item_name, enable, descrip, def, type, selected; Lisp_Object help; + /* All items should be contained in panes. */ + if (panes_seen == 0) + abort (); + item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME); enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE); descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY); @@ -2040,7 +2206,6 @@ set_frame_menubar (f, first_time, deep_p) specbind (Qdebug_on_next_call, Qnil); record_unwind_save_match_data (); - record_unwind_protect (unuse_menu_items, Qnil); if (NILP (Voverriding_local_map_menu_flag)) { specbind (Qoverriding_terminal_local_map, Qnil); @@ -2068,6 +2233,8 @@ set_frame_menubar (f, first_time, deep_p) /* Fill in menu_items with the current menu bar contents. This can evaluate Lisp code. */ + save_menu_items (); + menu_items = f->menu_bar_vector; menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0; submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *)); @@ -2127,23 +2294,33 @@ set_frame_menubar (f, first_time, deep_p) } set_buffer_internal_1 (prev); - unbind_to (specpdl_count, Qnil); /* If there has been no change in the Lisp-level contents of the menu bar, skip redisplaying it. Just exit. */ + /* Compare the new menu items with the ones computed last time. */ for (i = 0; i < previous_menu_items_used; i++) if (menu_items_used == i || (!EQ (previous_items[i], XVECTOR (menu_items)->contents[i]))) break; if (i == menu_items_used && i == previous_menu_items_used && i != 0) { + /* The menu items have not changed. Don't bother updating + the menus in any form, since it would be a no-op. */ free_menubar_widget_value_tree (first_wv); discard_menu_items (); - + unbind_to (specpdl_count, Qnil); return; } + /* The menu items are different, so store them in the frame. */ + f->menu_bar_vector = menu_items; + f->menu_bar_items_used = menu_items_used; + + /* This calls restore_menu_items to restore menu_items, etc., + as they were outside. */ + unbind_to (specpdl_count, Qnil); + /* Now GC cannot happen during the lifetime of the widget_value, so it's safe to store data from a Lisp_String. */ wv = first_wv->contents; @@ -2158,9 +2335,6 @@ set_frame_menubar (f, first_time, deep_p) wv = wv->next; } - f->menu_bar_vector = menu_items; - f->menu_bar_items_used = menu_items_used; - discard_menu_items (); } else { @@ -2631,6 +2805,7 @@ create_and_show_popup_menu (f, first_wv, x, y, for_click) /* Display the menu. */ lw_popup_menu (menu, (XEvent *) &dummy); popup_activated_flag = 1; + x_activate_timeout_atimer (); { int fact = 4 * sizeof (LWLIB_ID); @@ -3008,6 +3183,7 @@ create_and_show_dialog (f, first_wv) /* Display the dialog box. */ lw_pop_up_all_widgets (dialog_id); popup_activated_flag = 1; + x_activate_timeout_atimer (); /* Process events that apply to the dialog box. Also handle timers. */ @@ -3340,6 +3516,11 @@ xmenu_show (f, x, y, for_click, keymaps, title, error) return Qnil; } + /* Don't GC while we prepare and show the menu, + because we give the oldxmenu library pointers to the + contents of strings. */ + inhibit_garbage_collection (); + #ifdef HAVE_X_WINDOWS /* Adjust coordinates to relative to the outer (window manager) window. */ x += FRAME_OUTER_TO_INNER_DIFF_X (f); @@ -3586,17 +3767,18 @@ syms_of_xmenu () Qdebug_on_next_call = intern ("debug-on-next-call"); staticpro (&Qdebug_on_next_call); - DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame, - doc: /* Frame for which we are updating a menu. -The enable predicate for a menu command should check this variable. */); - Vmenu_updating_frame = Qnil; - #ifdef USE_X_TOOLKIT widget_id_tick = (1<<16); next_menubar_widget_id = 1; #endif defsubr (&Sx_popup_menu); + +#if defined (USE_GTK) || defined (USE_X_TOOLKIT) + defsubr (&Smenu_bar_open); + Ffset (intern ("accelerate-menu"), intern (Smenu_bar_open.symbol_name)); +#endif + #ifdef HAVE_MENUS defsubr (&Sx_popup_dialog); #endif