diff -r d15eca5fd8b3 -r b130f80ec7f9 ui/gtk/menu.c --- a/ui/gtk/menu.c Thu Nov 21 13:17:56 2024 +0100 +++ b/ui/gtk/menu.c Thu Nov 21 18:45:47 2024 +0100 @@ -75,12 +75,6 @@ return mb; } -GtkMenu* ui_create_menu(UiMenuBuilder *builder, UiObject *obj) { - GtkWidget *menu_widget = gtk_menu_new(); - ui_add_menu_items(menu_widget, 0, builder->menus_begin, obj); - return GTK_MENU(menu_widget); -} - void ui_add_menu_items(GtkWidget *parent, int i, UiMenu *menu, UiObject *obj) { UiMenuItemI *it = menu->items_begin; int index = 0; @@ -361,12 +355,18 @@ * widget menu functions */ +UIMENU ui_contextmenu_create(UiMenuBuilder *builder, UiObject *obj, UIWIDGET widget) { + GtkWidget *menu_widget = gtk_menu_new(); + ui_add_menu_items(menu_widget, 0, builder->menus_begin, obj); + return GTK_MENU(menu_widget); +} + static gboolean ui_button_press_event(GtkWidget *widget, GdkEvent *event, GtkMenu *menu) { if(event->type == GDK_BUTTON_PRESS) { GdkEventButton *e = (GdkEventButton*)event; if(e->button == 3) { gtk_widget_show_all(GTK_WIDGET(menu)); - ui_contextmenu_popup(menu); + ui_contextmenu_popup(menu, widget, 0, 0); return TRUE; } } @@ -377,7 +377,7 @@ g_signal_connect(widget, "button-press-event", (GCallback) ui_button_press_event, menu); } -void ui_contextmenu_popup(UIMENU menu) { +void ui_contextmenu_popup(UIMENU menu, GtkWidget *widget, int x, int y) { #if GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16 gtk_menu_popup_at_pointer(menu, NULL); #else @@ -590,35 +590,40 @@ /* --------------------- context menu / menubuilder --------------------- */ -GtkPopoverMenu* ui_create_menu(UiMenuBuilder *builder, UiObject *obj) { +static void remove_popover(GtkWidget *object, GtkPopoverMenu *menu) { + gtk_widget_unparent(GTK_WIDGET(menu)); +} + +UIMENU ui_contextmenu_create(UiMenuBuilder *builder, UiObject *obj, GtkWidget *widget) { GMenu *menu = g_menu_new(); ui_gmenu_add_menu_items(menu, 0, builder->menus_begin, obj); GtkWidget *contextmenu = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu)); gtk_popover_set_has_arrow(GTK_POPOVER(contextmenu), FALSE); gtk_widget_set_halign(contextmenu, GTK_ALIGN_START); + gtk_widget_set_parent(GTK_WIDGET(contextmenu), widget); + g_signal_connect( + widget, + "destroy", + G_CALLBACK(remove_popover), + contextmenu); return GTK_POPOVER_MENU(contextmenu); } static void gesture_button_press(GtkGestureClick *gesture, gint n_press, gdouble x, gdouble y, gpointer user_data) { - gtk_popover_set_pointing_to (GTK_POPOVER (user_data), &(GdkRectangle){ x, y, 0, 0 }); + gtk_popover_set_pointing_to(GTK_POPOVER(user_data), &(GdkRectangle){ x, y, 0, 0 }); gtk_popover_popup(GTK_POPOVER(user_data)); } -static void remove_popover(GtkWidget *object, GtkPopoverMenu *menu) { - gtk_widget_unparent(GTK_WIDGET(menu)); -} - void ui_widget_set_contextmenu(GtkWidget *widget, GtkPopoverMenu *menu) { GtkGesture *gesture = gtk_gesture_click_new(); gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), 3); gtk_widget_add_controller(widget, GTK_EVENT_CONTROLLER(gesture)); g_signal_connect(gesture, "pressed", G_CALLBACK(gesture_button_press), menu); - gtk_widget_set_parent(GTK_WIDGET(menu), widget); - g_signal_connect( - widget, - "destroy", - G_CALLBACK(remove_popover), - menu); +} + +void ui_contextmenu_popup(UIMENU menu, UIWIDGET widget, int x, int y) { + gtk_popover_set_pointing_to(GTK_POPOVER(menu), &(GdkRectangle){ x, y, 0, 0 }); + gtk_popover_popup(GTK_POPOVER(menu)); } #endif