From b6562a4b35b9a1a2172b0e823619fa61a8689d10 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 24 Jul 2022 14:14:01 +0300 Subject: [PATCH 39/39] gtk4: Make governments menu to update See osdn #45140 Signed-off-by: Marko Lindqvist --- client/gui-gtk-4.0/gui_main.c | 38 +++--------- client/gui-gtk-4.0/gui_main.h | 4 +- client/gui-gtk-4.0/menu.c | 109 +++++++++++++++++++++++++--------- client/gui-gtk-4.0/menu.h | 3 +- client/gui-gtk-4.0/pages.c | 1 + 5 files changed, 93 insertions(+), 62 deletions(-) diff --git a/client/gui-gtk-4.0/gui_main.c b/client/gui-gtk-4.0/gui_main.c index a5b98df083..8ff5785e44 100644 --- a/client/gui-gtk-4.0/gui_main.c +++ b/client/gui-gtk-4.0/gui_main.c @@ -155,7 +155,6 @@ GtkWidget *econ_widget; const char *const gui_character_encoding = "UTF-8"; const bool gui_use_transliteration = FALSE; -static GMenu *main_menubar = NULL; static GdkPaintable *empty_unit_paintable = NULL; static GtkWidget *unit_pic_table; static GtkWidget *unit_pic; @@ -1042,35 +1041,6 @@ void reset_unit_table(void) update_unit_pix_label(get_units_in_focus()); } -/**********************************************************************//** - Enable/Disable the game page menu bar. -**************************************************************************/ -void enable_menus(bool enable) -{ - if (enable) { - if (main_menubar == NULL) { - setup_app_actions(G_APPLICATION(fc_app)); - main_menubar = setup_menus(fc_app); - /* Ensure the menus are really created before performing any operations - * on them. */ - while (g_main_context_pending(NULL)) { - g_main_context_iteration(NULL, FALSE); - } - } - - gtk_application_window_set_show_menubar(GTK_APPLICATION_WINDOW(toplevel), TRUE); - gtk_application_set_menubar(fc_app, G_MENU_MODEL(main_menubar)); - - } else { - gtk_application_window_set_show_menubar(GTK_APPLICATION_WINDOW(toplevel), FALSE); - gtk_application_set_menubar(fc_app, NULL); - } - - /* Workaround for gtk bug that (re)setting the menubar after the window has - * been already created is not noticed. */ - g_object_notify(G_OBJECT(gtk_settings_get_default()), "gtk-shell-shows-menubar"); -} - /**********************************************************************//** Workaround for a crash that occurs when a button release event is emitted for a notebook with no pages. See PR#40743. @@ -2698,3 +2668,11 @@ static void adjust_default_options(void) } } } + +/**********************************************************************//** + Return the client GtkApplication +**************************************************************************/ +GtkApplication *gui_app(void) +{ + return fc_app; +} diff --git a/client/gui-gtk-4.0/gui_main.h b/client/gui-gtk-4.0/gui_main.h index 722a61c041..85cbce647e 100644 --- a/client/gui-gtk-4.0/gui_main.h +++ b/client/gui-gtk-4.0/gui_main.h @@ -64,9 +64,6 @@ extern GtkTextBuffer * message_buffer; extern int overview_canvas_store_width; extern int overview_canvas_store_height; - -void enable_menus(bool enable); - gboolean map_canvas_focus(void); void reset_unit_table(void); @@ -78,6 +75,7 @@ int screen_width(void); int screen_height(void); struct video_mode *resolution_request_get(void); +GtkApplication *gui_app(void); bool is_gui_up(void); bool terminate_signal_processing(void); diff --git a/client/gui-gtk-4.0/menu.c b/client/gui-gtk-4.0/menu.c index 3b1b02a83f..5eb6ccd9bd 100644 --- a/client/gui-gtk-4.0/menu.c +++ b/client/gui-gtk-4.0/menu.c @@ -74,8 +74,11 @@ #define GTK_STOCK_EDIT NULL #endif +static GMenu *main_menubar = NULL; static bool menus_built = FALSE; +static GMenu *setup_menus(GtkApplication *app); + #ifndef FREECIV_DEBUG static void menu_entry_set_visible(const char *key, gboolean is_visible, @@ -98,6 +101,8 @@ static void menu_entry_group_set_sensitive(enum menu_entry_grouping group, gboolean is_sensitive); #endif /* MENUS_GTK3 */ +static GMenu *gov_menu = NULL; + struct menu_entry_info { const char *key; const char *name; @@ -2050,13 +2055,13 @@ void setup_app_actions(GApplication *fc_app) /************************************************************************//** Creates the menu bar. ****************************************************************************/ -GMenu *setup_menus(GtkApplication *app) +static GMenu *setup_menus(GtkApplication *app) { GMenu *menubar; GMenu *topmenu; GMenu *submenu; int i; - GApplication *fc_app = G_APPLICATION(app); + // GApplication *fc_app = G_APPLICATION(app); fc_assert(!menus_built); @@ -2090,36 +2095,15 @@ GMenu *setup_menus(GtkApplication *app) g_menu_append_submenu(menubar, _("_Work"), G_MENU_MODEL(topmenu)); - topmenu = g_menu_new(); + gov_menu = g_menu_new(); + /* Placeholder submenu (so that menu update has something to replace) */ submenu = g_menu_new(); - menu_entry_init(submenu, "START_REVOLUTION"); - - i = 0; - governments_iterate(g) { - if (g != game.government_during_revolution) { - GMenuItem *item; - char name[256]; - char actname[256]; - GSimpleAction *act; - - fc_snprintf(actname, sizeof(actname), "government_%d", i); - act = g_simple_action_new(actname, NULL); - g_simple_action_set_enabled(act, can_change_to_government(client_player(), g)); - g_action_map_add_action(G_ACTION_MAP(fc_app), G_ACTION(act)); - g_signal_connect(act, "activate", G_CALLBACK(government_callback), g); + g_menu_append_submenu(gov_menu, _("_Government"), G_MENU_MODEL(submenu)); - /* TRANS: %s is a government name */ - fc_snprintf(name, sizeof(name), _("%s..."), - government_name_translation(g)); - fc_snprintf(actname, sizeof(actname), "app.government_%d", i++); - item = g_menu_item_new(name, actname); - g_menu_append_item(submenu, item); - } - } governments_iterate_end; - g_menu_append_submenu(topmenu, _("_Government"), G_MENU_MODEL(submenu)); + g_menu_append_submenu(menubar, _("C_ivilization"), G_MENU_MODEL(gov_menu)); - g_menu_append_submenu(menubar, _("C_ivilization"), G_MENU_MODEL(topmenu)); + real_menus_update(); #ifndef FREECIV_DEBUG menu_entry_set_visible("RELOAD_TILESET", FALSE, FALSE); @@ -2291,6 +2275,44 @@ static const char *get_tile_change_menu_text(struct tile *ptile, ****************************************************************************/ void real_menus_update(void) { + GtkApplication *fc_app = gui_app(); + GMenu *submenu; + int i; + + if (!menus_built) { + return; + } + + submenu = g_menu_new(); + menu_entry_init(submenu, "START_REVOLUTION"); + + i = 0; + governments_iterate(g) { + if (g != game.government_during_revolution) { + GMenuItem *item; + char name[256]; + char actname[256]; + GSimpleAction *act; + + fc_snprintf(actname, sizeof(actname), "government_%d", i); + act = g_simple_action_new(actname, NULL); + g_simple_action_set_enabled(act, can_change_to_government(client_player(), g)); + g_action_map_add_action(G_ACTION_MAP(fc_app), G_ACTION(act)); + g_signal_connect(act, "activate", G_CALLBACK(government_callback), g); + + /* TRANS: %s is a government name */ + fc_snprintf(name, sizeof(name), _("%s..."), + government_name_translation(g)); + fc_snprintf(actname, sizeof(actname), "app.government_%d", i++); + item = g_menu_item_new(name, actname); + g_menu_append_item(submenu, item); + } + } governments_iterate_end; + g_menu_remove(gov_menu, 0); + g_menu_append_submenu(gov_menu, _("_Government"), G_MENU_MODEL(submenu)); + + return; + struct unit_list *punits = NULL; #ifdef MENUS_GTK3 bool units_all_same_tile = TRUE, units_all_same_type = TRUE; @@ -3106,3 +3128,34 @@ void real_menus_init(void) #endif /* MENUS_GTK3 */ } + +/**********************************************************************//** + Enable/Disable the game page menu bar. +**************************************************************************/ +void enable_menus(bool enable) +{ + GtkApplication *fc_app = gui_app(); + + if (enable) { + if (main_menubar == NULL) { + setup_app_actions(G_APPLICATION(fc_app)); + main_menubar = setup_menus(fc_app); + /* Ensure the menus are really created before performing any operations + * on them. */ + while (g_main_context_pending(NULL)) { + g_main_context_iteration(NULL, FALSE); + } + } + + gtk_application_window_set_show_menubar(GTK_APPLICATION_WINDOW(toplevel), TRUE); + gtk_application_set_menubar(fc_app, G_MENU_MODEL(main_menubar)); + + } else { + gtk_application_window_set_show_menubar(GTK_APPLICATION_WINDOW(toplevel), FALSE); + gtk_application_set_menubar(fc_app, NULL); + } + + /* Workaround for gtk bug that (re)setting the menubar after the window has + * been already created is not noticed. */ + g_object_notify(G_OBJECT(gtk_settings_get_default()), "gtk-shell-shows-menubar"); +} diff --git a/client/gui-gtk-4.0/menu.h b/client/gui-gtk-4.0/menu.h index e0fbdae801..02e0742ed1 100644 --- a/client/gui-gtk-4.0/menu.h +++ b/client/gui-gtk-4.0/menu.h @@ -18,7 +18,8 @@ /* client */ #include "menu_g.h" +void enable_menus(bool enable); + void setup_app_actions(GApplication *fc_app); -GMenu *setup_menus(GtkApplication *app); #endif /* FC__MENU_H */ diff --git a/client/gui-gtk-4.0/pages.c b/client/gui-gtk-4.0/pages.c index 87e3987044..7469cf2853 100644 --- a/client/gui-gtk-4.0/pages.c +++ b/client/gui-gtk-4.0/pages.c @@ -52,6 +52,7 @@ #include "gui_main.h" #include "gui_stuff.h" #include "mapview.h" +#include "menu.h" #include "optiondlg.h" #include "plrdlg.h" /* get_flag() */ #include "repodlgs.h" -- 2.35.1