+#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
+
+/* The item selected in the popup menu. */
+static Lisp_Object *volatile menu_item_selection;
+
+#ifdef USE_GTK
+
+/* Used when position a popup menu. See menu_position_func and
+ create_and_show_popup_menu below. */
+struct next_popup_x_y
+{
+ FRAME_PTR f;
+ int x;
+ int y;
+};
+
+/* The menu position function to use if we are not putting a popup
+ menu where the pointer is.
+ MENU is the menu to pop up.
+ X and Y shall on exit contain x/y where the menu shall pop up.
+ PUSH_IN is not documented in the GTK manual.
+ USER_DATA is any data passed in when calling gtk_menu_popup.
+ Here it points to a struct next_popup_x_y where the coordinates
+ to store in *X and *Y are as well as the frame for the popup.
+
+ Here only X and Y are used. */
+static void
+menu_position_func (menu, x, y, push_in, user_data)
+ GtkMenu *menu;
+ gint *x;
+ gint *y;
+ gboolean *push_in;
+ gpointer user_data;
+{
+ struct next_popup_x_y* data = (struct next_popup_x_y*)user_data;
+ GtkRequisition req;
+ int disp_width = FRAME_X_DISPLAY_INFO (data->f)->width;
+ int disp_height = FRAME_X_DISPLAY_INFO (data->f)->height;
+
+ *x = data->x;
+ *y = data->y;
+
+ /* Check if there is room for the menu. If not, adjust x/y so that
+ the menu is fully visible. */
+ gtk_widget_size_request (GTK_WIDGET (menu), &req);
+ if (data->x + req.width > disp_width)
+ *x -= data->x + req.width - disp_width;
+ if (data->y + req.height > disp_height)
+ *y -= data->y + req.height - disp_height;
+}
+
+static void
+popup_selection_callback (widget, client_data)
+ GtkWidget *widget;
+ gpointer client_data;
+{
+ xg_menu_item_cb_data *cb_data = (xg_menu_item_cb_data*) client_data;
+
+ if (xg_crazy_callback_abort) return;
+ if (cb_data) menu_item_selection = (Lisp_Object *) cb_data->call_data;
+}
+
+static Lisp_Object
+pop_down_menu (arg)
+ Lisp_Object arg;
+{
+ struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
+
+ popup_activated_flag = 0;
+ BLOCK_INPUT;
+ gtk_widget_destroy (GTK_WIDGET (p->pointer));
+ UNBLOCK_INPUT;
+ return Qnil;
+}
+
+/* Pop up the menu for frame F defined by FIRST_WV at X/Y and loop until the
+ menu pops down.
+ menu_item_selection will be set to the selection. */
+static void
+create_and_show_popup_menu (f, first_wv, x, y, for_click)
+ FRAME_PTR f;
+ widget_value *first_wv;
+ int x;
+ int y;
+ int for_click;
+{
+ int i;
+ GtkWidget *menu;
+ GtkMenuPositionFunc pos_func = 0; /* Pop up at pointer. */
+ struct next_popup_x_y popup_x_y;
+ int specpdl_count = SPECPDL_INDEX ();
+
+ xg_crazy_callback_abort = 1;
+ menu = xg_create_widget ("popup", first_wv->name, f, first_wv,
+ G_CALLBACK (popup_selection_callback),
+ G_CALLBACK (popup_deactivate_callback),
+ G_CALLBACK (menu_highlight_callback));
+ xg_crazy_callback_abort = 0;
+
+ if (! for_click)
+ {
+ /* Not invoked by a click. pop up at x/y. */
+ pos_func = menu_position_func;
+
+ /* Adjust coordinates to be root-window-relative. */
+ x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
+ y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
+
+ popup_x_y.x = x;
+ popup_x_y.y = y;
+ popup_x_y.f = f;
+
+ i = 0; /* gtk_menu_popup needs this to be 0 for a non-button popup. */
+ }
+ else
+ {
+ for (i = 0; i < 5; i++)
+ if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
+ break;
+ }
+
+ /* Display the menu. */
+ gtk_widget_show_all (menu);
+ gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
+
+ record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
+
+ if (GTK_WIDGET_MAPPED (menu))
+ {
+ /* Set this to one. popup_widget_loop increases it by one, so it becomes
+ two. show_help_echo uses this to detect popup menus. */
+ popup_activated_flag = 1;
+ /* Process events that apply to the menu. */
+ popup_widget_loop (1, menu);
+ }
+
+ unbind_to (specpdl_count, Qnil);
+
+ /* Must reset this manually because the button release event is not passed
+ to Emacs event loop. */
+ FRAME_X_DISPLAY_INFO (f)->grabbed = 0;
+}
+
+#else /* not USE_GTK */