From 04e138a7ff1be84a072dcf74c20db589ec8316de Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 21 Sep 2024 13:20:15 +0300
Subject: [PATCH 56/56] 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.22/editprop.c | 42 ++++++++++++++++++++++++++--------
 client/gui-gtk-4.0/editprop.c  | 42 ++++++++++++++++++++++++++--------
 client/gui-gtk-5.0/editprop.c  | 42 ++++++++++++++++++++++++++--------
 3 files changed, 96 insertions(+), 30 deletions(-)

diff --git a/client/gui-gtk-3.22/editprop.c b/client/gui-gtk-3.22/editprop.c
index b7cb7c6b64..074dfad35b 100644
--- a/client/gui-gtk-3.22/editprop.c
+++ b/client/gui-gtk-3.22/editprop.c
@@ -878,7 +878,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:
     {
@@ -4003,9 +4007,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);
@@ -4307,11 +4318,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));
@@ -5324,7 +5341,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 78d6c960f9..3a4fcf2887 100644
--- a/client/gui-gtk-4.0/editprop.c
+++ b/client/gui-gtk-4.0/editprop.c
@@ -878,7 +878,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:
     {
@@ -4012,9 +4016,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);
@@ -4316,11 +4327,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));
@@ -5346,7 +5363,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-5.0/editprop.c b/client/gui-gtk-5.0/editprop.c
index 9afecbfb66..901491e4f0 100644
--- a/client/gui-gtk-5.0/editprop.c
+++ b/client/gui-gtk-5.0/editprop.c
@@ -878,7 +878,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:
     {
@@ -4012,9 +4016,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);
@@ -4316,11 +4327,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));
@@ -5346,7 +5363,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

