+/* F is the frame the menu is for.
+ X and Y are the frame-relative specified position,
+ relative to the inside upper left corner of the frame F.
+ FOR_CLICK is nonzero if this menu was invoked for a mouse click.
+ KEYMAPS is 1 if this menu was specified with keymaps;
+ in that case, we return a list containing the chosen item's value
+ and perhaps also the pane's prefix.
+ TITLE is the specified menu title.
+ ERROR is a place to store an error message string in case of failure.
+ (We return nil on failure, but the value doesn't actually matter.) */
+
+#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;
+
+ for (i = 0; i < 5; i++)
+ if (FRAME_X_DISPLAY_INFO (f)->grabbed & (1 << i))
+ break;
+
+ 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;
+ }
+
+ /* Display the menu. */
+ gtk_widget_show_all (menu);
+ gtk_menu_popup (GTK_MENU (menu), 0, 0, pos_func, &popup_x_y, i, 0);
+
+ fprintf (stderr, "Unwind: %p\n", menu);
+ record_unwind_protect (pop_down_menu, make_save_value (menu, 0));
+
+ /* 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 */
+
+/* We need a unique id for each widget handled by the Lucid Widget
+ library.
+
+ For the main windows, and popup menus, we use this counter,
+ which we increment each time after use. This starts from 1<<16.
+
+ For menu bars, we use numbers starting at 0, counted in
+ next_menubar_widget_id. */
+LWLIB_ID widget_id_tick;
+
+static void
+popup_selection_callback (widget, id, client_data)
+ Widget widget;
+ LWLIB_ID id;
+ XtPointer client_data;
+{
+ menu_item_selection = (Lisp_Object *) client_data;
+}
+
+/* ARG is the LWLIB ID of the dialog box, represented
+ as a Lisp object as (HIGHPART . LOWPART). */
+
+static Lisp_Object
+pop_down_menu (arg)
+ Lisp_Object arg;
+{
+ LWLIB_ID id = (XINT (XCAR (arg)) << 4 * sizeof (LWLIB_ID)
+ | XINT (XCDR (arg)));
+
+ BLOCK_INPUT;
+ lw_destroy_all_widgets (id);
+ UNBLOCK_INPUT;
+ popup_activated_flag = 0;
+
+ 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;
+ Arg av[2];
+ int ac = 0;
+ XButtonPressedEvent dummy;
+ LWLIB_ID menu_id;
+ Widget menu;
+
+ menu_id = widget_id_tick++;
+ menu = lw_create_widget ("popup", first_wv->name, menu_id, first_wv,
+ f->output_data.x->widget, 1, 0,
+ popup_selection_callback,
+ popup_deactivate_callback,
+ menu_highlight_callback);
+
+ dummy.type = ButtonPress;
+ dummy.serial = 0;
+ dummy.send_event = 0;
+ dummy.display = FRAME_X_DISPLAY (f);
+ dummy.time = CurrentTime;
+ dummy.root = FRAME_X_DISPLAY_INFO (f)->root_window;
+ dummy.window = dummy.root;
+ dummy.subwindow = dummy.root;
+ dummy.x = x;
+ dummy.y = y;
+
+ /* 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);