From 9239807ee4912a0ea8ef2b36503f3b38bbcbaf3d Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 18 Mar 2023 01:55:53 +0200 Subject: [PATCH 6/6] gtk4: Add Set Worklist submenu to cityreport See osdn #47590 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/cityrep.c | 75 +++++++++++++++--------------------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/client/gui-gtk-4.0/cityrep.c b/client/gui-gtk-4.0/cityrep.c index cd8cd8d0e0..a88ab84815 100644 --- a/client/gui-gtk-4.0/cityrep.c +++ b/client/gui-gtk-4.0/cityrep.c @@ -95,10 +95,6 @@ static GMenu *create_change_menu(GActionGroup *group, const char *mname, const char *human_mname, enum city_operation_type oper); -#ifdef MENUS_GTK3 -static void create_next_to_last_menu(GtkWidget *item); -#endif /* MENUS_GTK3 */ - static struct gui_dialog *city_dialog_shell = NULL; enum { @@ -131,6 +127,7 @@ static GMenu *add_first_menu; static GMenu *add_last_menu; static GMenu *add_next_menu; static GMenu *add_2ndlast_menu; +static GMenu *wl_set_menu; #ifdef MENUS_GTK3 static GtkWidget *select_island_item; @@ -945,6 +942,7 @@ static void append_worklist_callback(GtkMenuItem *menuitem, gpointer data) append_worklist_foreach, (gpointer) global_worklist_get(pgwl)); } +#endif /* MENUS_GTK3 */ /************************************************************************//** Helper function to set a worklist for one city in the city report. This @@ -967,7 +965,9 @@ static void set_worklist_foreach(GtkTreeModel *model, GtkTreePath *path, Menu item callback to set a city's worklist to the global worklist associated with this menu item. The worklist pointer is passed in 'data'. ****************************************************************************/ -static void set_worklist_callback(GtkMenuItem *menuitem, gpointer data) +static void set_worklist_callback(GSimpleAction *action, + GVariant *parameter, + gpointer data) { struct global_worklist *pgwl = global_worklist_by_id(GPOINTER_TO_INT(data)); @@ -987,56 +987,44 @@ static void set_worklist_callback(GtkMenuItem *menuitem, gpointer data) } /************************************************************************//** - Empty and refill the submenu of the menu item passed as 'data'. The menu - will be filled with menu items corresponding to the global worklists. + Create submenu based on global worklist. ****************************************************************************/ -static void production_menu_shown(GtkWidget *widget, gpointer data) +static GMenu *create_wl_menu(GActionGroup *group, GCallback cb) { - GtkWidget *menu, *item; - GtkMenuItem *parent_item; - GCallback callback; + GMenu *menu; + GSimpleAction *act; int count = 0; - parent_item = data; - fc_assert_ret(parent_item != NULL); - fc_assert_ret(GTK_IS_MENU_ITEM(parent_item)); - - callback = g_object_get_data(G_OBJECT(parent_item), "item_callback"); - fc_assert_ret(callback != NULL); - - menu = gtk_menu_item_get_submenu(parent_item); - if (menu != NULL && gtk_widget_get_visible(menu)) { - gtk_menu_shell_deactivate(GTK_MENU_SHELL(menu)); - } - - if (menu == NULL) { - menu = gtk_menu_button_new(); - gtk_menu_item_set_submenu(parent_item, menu); - } + menu = g_menu_new(); if (!can_client_issue_orders()) { - return; + return NULL; } - gtk_container_forall(GTK_CONTAINER(menu), - (GtkCallback) gtk_widget_destroy, NULL); - global_worklists_iterate(pgwl) { - item = gtk_menu_item_new_with_label(global_worklist_name(pgwl)); - gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); - g_signal_connect(item, "activate", callback, + char buf[128]; + + fc_snprintf(buf, sizeof(buf), "wlset%d", count); + act = g_simple_action_new(buf, NULL); + g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); + g_signal_connect(act, "activate", cb, GINT_TO_POINTER(global_worklist_id(pgwl))); + fc_snprintf(buf, sizeof(buf), "win.wlset%d", count); + menu_item_append_unref(menu, + g_menu_item_new(global_worklist_name(pgwl), + buf)); count++; } global_worklists_iterate_end; if (count == 0) { - item = gtk_menu_item_new_with_label(_("(no worklists defined)")); - gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); + act = g_simple_action_new("wlsetnone", NULL); + menu_item_append_unref(menu, + g_menu_item_new(_("(no worklists defined)"), + "win.wlsetnone")); } - gtk_widget_show(menu); + return menu; } -#endif /* MENUS_GTK3 */ /************************************************************************//** Update city report views @@ -1142,12 +1130,6 @@ static GtkWidget *create_city_report_menu(void) cityrep_menu = g_menu_new(); #ifdef MENUS_GTK3 - item = gtk_menu_item_new_with_label(_("Set Worklist")); - gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); - g_object_set_data(G_OBJECT(item), "item_callback", - set_worklist_callback); - g_signal_connect(menu, "show", G_CALLBACK(production_menu_shown), item); - item = gtk_menu_item_new_with_label(_("Append Worklist")); gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); g_object_set_data(G_OBJECT(item), "item_callback", @@ -1684,6 +1666,10 @@ static GMenu *create_production_menu(GActionGroup *group) submenu_append_unref(prod_menu, _("Add _2nd Last"), G_MENU_MODEL(add_2ndlast_menu)); + wl_set_menu = create_wl_menu(group, G_CALLBACK(set_worklist_callback)); + submenu_append_unref(prod_menu, _("Set Worklist"), + G_MENU_MODEL(wl_set_menu)); + act = g_simple_action_new("clear_worklist", NULL); g_action_map_add_action(G_ACTION_MAP(group), G_ACTION(act)); g_signal_connect(act, "activate", G_CALLBACK(city_clear_worklist_callback), @@ -1705,6 +1691,7 @@ static void recreate_production_menu(GActionGroup *group) g_menu_remove_all(add_last_menu); g_menu_remove_all(add_next_menu); g_menu_remove_all(add_2ndlast_menu); + g_menu_remove_all(wl_set_menu); g_menu_remove_all(prod_menu); } g_menu_remove(cityrep_menu, 0); -- 2.39.2