From 6f4173141fbf8a927e76ca751de39571c015e7e9 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 21 Sep 2024 13:20:15 +0300
Subject: [PATCH 22/22] gtk: Fix editor assert failures when player government
 not known

See RM #860

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-gtk-3.0/editprop.c  | 42 ++++++++++++++++++++++++++--------
 client/gui-gtk-3.22/editprop.c | 42 ++++++++++++++++++++++++++--------
 client/gui-gtk-4.0/editprop.c  | 42 ++++++++++++++++++++++++++--------
 3 files changed, 96 insertions(+), 30 deletions(-)

diff --git a/client/gui-gtk-3.0/editprop.c b/client/gui-gtk-3.0/editprop.c
index 7b385e26ed..9fd4f2edd4 100644
--- a/client/gui-gtk-3.0/editprop.c
+++ b/client/gui-gtk-3.0/editprop.c
@@ -876,7 +876,11 @@ static gchar *propval_as_string(struct propval *pv)
     return g_strdup_printf("%s", nation_adjective_translation(pv->data.v_nation));
 
   case VALTYPE_GOV:
-    return g_strdup_printf("%s", government_name_translation(pv->data.v_gov));
+    if (pv->data.v_gov != NULL) {
+      return g_strdup_printf("%s", government_name_translation(pv->data.v_gov));
+    } else {
+      return g_strdup_printf("%s", government_name_translation(game.government_during_revolution));
+    }
 
   case VALTYPE_BUILT_ARRAY:
     {
@@ -3988,9 +3992,16 @@ static void extviewer_refresh_widgets(struct extviewer *ev,
           g_object_unref(pixbuf);
         }
       } governments_iterate_end;
-      gtk_label_set_text(GTK_LABEL(ev->panel_label),
-                         government_name_translation(pv->data.v_gov));
-      pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+      if (pv->data.v_gov != NULL) {
+        gtk_label_set_text(GTK_LABEL(ev->panel_label),
+                           government_name_translation(pv->data.v_gov));
+        pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+      } else {
+        gtk_label_set_text(GTK_LABEL(ev->panel_label), "?");
+        pixbuf = sprite_get_pixbuf(get_government_sprite(tileset,
+                                                         game.government_during_revolution));
+      }
+
       gtk_image_set_from_pixbuf(GTK_IMAGE(ev->panel_image), pixbuf);
       if (pixbuf) {
         g_object_unref(pixbuf);
@@ -4292,11 +4303,17 @@ static void extviewer_view_cell_toggled(GtkCellRendererToggle *cell,
     if (!(0 <= id && id < government_count()) || !present) {
       return;
     }
-    old_id = government_index(pv->data.v_gov);
-    pv->data.v_gov = government_by_number(id);
-    gtk_list_store_set(ev->store, &iter, 0, TRUE, -1);
-    gtk_tree_model_iter_nth_child(model, &iter, NULL, old_id);
-    gtk_list_store_set(ev->store, &iter, 0, FALSE, -1);
+    if (pv->data.v_gov != NULL) {
+      old_id = government_index(pv->data.v_gov);
+      pv->data.v_gov = government_by_number(id);
+      gtk_list_store_set(ev->store, &iter, 0, TRUE, -1);
+      gtk_tree_model_iter_nth_child(model, &iter, NULL, old_id);
+      gtk_list_store_set(ev->store, &iter, 0, FALSE, -1);
+    } else {
+      pv->data.v_gov = government_by_number(id);
+      gtk_list_store_set(ev->store, &iter, 0, TRUE, -1);
+    }
+
     gtk_label_set_text(GTK_LABEL(ev->panel_label),
                        government_name_translation(pv->data.v_gov));
     pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
@@ -5304,7 +5321,12 @@ static bool property_page_set_store_value(struct property_page *pp,
     }
     break;
   case VALTYPE_GOV:
-    pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+    if (pv->data.v_gov != NULL) {
+      pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+    } else {
+      pixbuf = sprite_get_pixbuf(get_government_sprite(tileset,
+                                                       game.government_during_revolution));
+    }
     gtk_list_store_set(store, iter, col_id, pixbuf, -1);
     if (pixbuf) {
       g_object_unref(pixbuf);
diff --git a/client/gui-gtk-3.22/editprop.c b/client/gui-gtk-3.22/editprop.c
index 628c044ef1..c0ec98d4af 100644
--- a/client/gui-gtk-3.22/editprop.c
+++ b/client/gui-gtk-3.22/editprop.c
@@ -876,7 +876,11 @@ static gchar *propval_as_string(struct propval *pv)
     return g_strdup_printf("%s", nation_adjective_translation(pv->data.v_nation));
 
   case VALTYPE_GOV:
-    return g_strdup_printf("%s", government_name_translation(pv->data.v_gov));
+    if (pv->data.v_gov != NULL) {
+      return g_strdup_printf("%s", government_name_translation(pv->data.v_gov));
+    } else {
+      return g_strdup_printf("%s", government_name_translation(game.government_during_revolution));
+    }
 
   case VALTYPE_BUILT_ARRAY:
     {
@@ -3987,9 +3991,16 @@ static void extviewer_refresh_widgets(struct extviewer *ev,
           g_object_unref(pixbuf);
         }
       } governments_iterate_end;
-      gtk_label_set_text(GTK_LABEL(ev->panel_label),
-                         government_name_translation(pv->data.v_gov));
-      pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+      if (pv->data.v_gov != NULL) {
+        gtk_label_set_text(GTK_LABEL(ev->panel_label),
+                           government_name_translation(pv->data.v_gov));
+        pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+      } else {
+        gtk_label_set_text(GTK_LABEL(ev->panel_label), "?");
+        pixbuf = sprite_get_pixbuf(get_government_sprite(tileset,
+                                                         game.government_during_revolution));
+      }
+
       gtk_image_set_from_pixbuf(GTK_IMAGE(ev->panel_image), pixbuf);
       if (pixbuf) {
         g_object_unref(pixbuf);
@@ -4291,11 +4302,17 @@ static void extviewer_view_cell_toggled(GtkCellRendererToggle *cell,
     if (!(0 <= id && id < government_count()) || !present) {
       return;
     }
-    old_id = government_index(pv->data.v_gov);
-    pv->data.v_gov = government_by_number(id);
-    gtk_list_store_set(ev->store, &iter, 0, TRUE, -1);
-    gtk_tree_model_iter_nth_child(model, &iter, NULL, old_id);
-    gtk_list_store_set(ev->store, &iter, 0, FALSE, -1);
+    if (pv->data.v_gov != NULL) {
+      old_id = government_index(pv->data.v_gov);
+      pv->data.v_gov = government_by_number(id);
+      gtk_list_store_set(ev->store, &iter, 0, TRUE, -1);
+      gtk_tree_model_iter_nth_child(model, &iter, NULL, old_id);
+      gtk_list_store_set(ev->store, &iter, 0, FALSE, -1);
+    } else {
+      pv->data.v_gov = government_by_number(id);
+      gtk_list_store_set(ev->store, &iter, 0, TRUE, -1);
+    }
+
     gtk_label_set_text(GTK_LABEL(ev->panel_label),
                        government_name_translation(pv->data.v_gov));
     pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
@@ -5304,7 +5321,12 @@ static bool property_page_set_store_value(struct property_page *pp,
     }
     break;
   case VALTYPE_GOV:
-    pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+    if (pv->data.v_gov != NULL) {
+      pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+    } else {
+      pixbuf = sprite_get_pixbuf(get_government_sprite(tileset,
+                                                       game.government_during_revolution));
+    }
     gtk_list_store_set(store, iter, col_id, pixbuf, -1);
     if (pixbuf) {
       g_object_unref(pixbuf);
diff --git a/client/gui-gtk-4.0/editprop.c b/client/gui-gtk-4.0/editprop.c
index 370e77faa3..e59fe5fc99 100644
--- a/client/gui-gtk-4.0/editprop.c
+++ b/client/gui-gtk-4.0/editprop.c
@@ -876,7 +876,11 @@ static gchar *propval_as_string(struct propval *pv)
     return g_strdup_printf("%s", nation_adjective_translation(pv->data.v_nation));
 
   case VALTYPE_GOV:
-    return g_strdup_printf("%s", government_name_translation(pv->data.v_gov));
+    if (pv->data.v_gov != NULL) {
+      return g_strdup_printf("%s", government_name_translation(pv->data.v_gov));
+    } else {
+      return g_strdup_printf("%s", government_name_translation(game.government_during_revolution));
+    }
 
   case VALTYPE_BUILT_ARRAY:
     {
@@ -3995,9 +3999,16 @@ static void extviewer_refresh_widgets(struct extviewer *ev,
           g_object_unref(pixbuf);
         }
       } governments_iterate_end;
-      gtk_label_set_text(GTK_LABEL(ev->panel_label),
-                         government_name_translation(pv->data.v_gov));
-      pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+      if (pv->data.v_gov != NULL) {
+        gtk_label_set_text(GTK_LABEL(ev->panel_label),
+                           government_name_translation(pv->data.v_gov));
+        pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+      } else {
+        gtk_label_set_text(GTK_LABEL(ev->panel_label), "?");
+        pixbuf = sprite_get_pixbuf(get_government_sprite(tileset,
+                                                         game.government_during_revolution));
+      }
+
       gtk_image_set_from_pixbuf(GTK_IMAGE(ev->panel_image), pixbuf);
       if (pixbuf) {
         g_object_unref(pixbuf);
@@ -4299,11 +4310,17 @@ static void extviewer_view_cell_toggled(GtkCellRendererToggle *cell,
     if (!(0 <= id && id < government_count()) || !present) {
       return;
     }
-    old_id = government_index(pv->data.v_gov);
-    pv->data.v_gov = government_by_number(id);
-    gtk_list_store_set(ev->store, &iter, 0, TRUE, -1);
-    gtk_tree_model_iter_nth_child(model, &iter, NULL, old_id);
-    gtk_list_store_set(ev->store, &iter, 0, FALSE, -1);
+    if (pv->data.v_gov != NULL) {
+      old_id = government_index(pv->data.v_gov);
+      pv->data.v_gov = government_by_number(id);
+      gtk_list_store_set(ev->store, &iter, 0, TRUE, -1);
+      gtk_tree_model_iter_nth_child(model, &iter, NULL, old_id);
+      gtk_list_store_set(ev->store, &iter, 0, FALSE, -1);
+    } else {
+      pv->data.v_gov = government_by_number(id);
+      gtk_list_store_set(ev->store, &iter, 0, TRUE, -1);
+    }
+
     gtk_label_set_text(GTK_LABEL(ev->panel_label),
                        government_name_translation(pv->data.v_gov));
     pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
@@ -5329,7 +5346,12 @@ static bool property_page_set_store_value(struct property_page *pp,
     }
     break;
   case VALTYPE_GOV:
-    pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+    if (pv->data.v_gov != NULL) {
+      pixbuf = sprite_get_pixbuf(get_government_sprite(tileset, pv->data.v_gov));
+    } else {
+      pixbuf = sprite_get_pixbuf(get_government_sprite(tileset,
+                                                       game.government_during_revolution));
+    }
     gtk_list_store_set(store, iter, col_id, pixbuf, -1);
     if (pixbuf) {
       g_object_unref(pixbuf);
-- 
2.45.2

