From edc68753698d88ade584888b4285149121ee9d19 Mon Sep 17 00:00:00 2001
From: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Date: Sat, 28 Nov 2026 13:33:53 -0800
Subject: [PATCH 2/2] gtk clients: Keep diplomacy gold limit in step with
 players' gold

The gold spin buttons of a diplomacy meeting got their range from
each player's gold when the dialog was built, and never again. Once
the player's gold changed, say at the next turn, the spin button
still held the offer to the old amount, whether that was now too low
or too high.

Set the range of the meeting's spin buttons for that player from
update_diplomacy_gold(). The value-changed handler is blocked
meanwhile, so that clamping to a lower range does not offer the
clamped amount by itself.

See RM #2517

Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
---
 client/gui-gtk-3.22/diplodlg.c | 27 ++++++++++++++++++++++++++-
 client/gui-gtk-4.0/diplodlg.c  | 27 ++++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/client/gui-gtk-3.22/diplodlg.c b/client/gui-gtk-3.22/diplodlg.c
index 6c697fdb72..7990fd431d 100644
--- a/client/gui-gtk-3.22/diplodlg.c
+++ b/client/gui-gtk-3.22/diplodlg.c
@@ -60,6 +60,9 @@ struct Diplomacy_dialog {
   GtkWidget *image0;
   GtkWidget *image1;
 
+  GtkWidget *gold0;
+  GtkWidget *gold1;
+
   GtkListStore *store;
 };
 
@@ -707,6 +710,8 @@ static struct Diplomacy_dialog *create_diplomacy_dialog(struct treaty *ptreaty,
 
   dialog_list_prepend(dialog_list, pdialog);
   pdialog->treaty = ptreaty;
+  pdialog->gold0 = NULL;
+  pdialog->gold1 = NULL;
 
   /* Get main diplomacy tab. */
   dipl_dialog = diplomacy_main_create();
@@ -822,6 +827,7 @@ static struct Diplomacy_dialog *create_diplomacy_dialog(struct treaty *ptreaty,
     gtk_entry_set_width_chars(GTK_ENTRY(spin), 16);
     gtk_grid_attach(GTK_GRID(table), spin, 1, 0, 1, 1);
     g_object_set_data(G_OBJECT(spin), "plr", plr0);
+    pdialog->gold0 = spin;
     g_signal_connect_after(spin, "value-changed",
                            G_CALLBACK(diplo_dialog_returnkey), pdialog);
 
@@ -904,6 +910,7 @@ static struct Diplomacy_dialog *create_diplomacy_dialog(struct treaty *ptreaty,
     gtk_entry_set_width_chars(GTK_ENTRY(spin), 16);
     gtk_grid_attach(GTK_GRID(table), spin, 1, 0, 1, 1);
     g_object_set_data(G_OBJECT(spin), "plr", plr1);
+    pdialog->gold1 = spin;
     g_signal_connect_after(spin, "value-changed",
                            G_CALLBACK(diplo_dialog_returnkey), pdialog);
 
@@ -1254,12 +1261,30 @@ static void diplo_dialog_returnkey(GtkWidget *w, gpointer data)
   }
 }
 
+/************************************************************************//**
+  Keep the range of a gold spin button in step with its player's gold
+****************************************************************************/
+static void update_gold_range(struct Diplomacy_dialog *pdialog,
+                              GtkWidget *spin, struct player *pplayer)
+{
+  if (spin != NULL && g_object_get_data(G_OBJECT(spin), "plr") == pplayer) {
+    /* Clamping the value to a lower range must not offer that amount. */
+    g_signal_handlers_block_by_func(spin, diplo_dialog_returnkey, pdialog);
+    gtk_spin_button_set_range(GTK_SPIN_BUTTON(spin), 0.0,
+                              pplayer->economic.gold + 0.1);
+    g_signal_handlers_unblock_by_func(spin, diplo_dialog_returnkey, pdialog);
+  }
+}
+
 /************************************************************************//**
   Player's gold may have changed
 ****************************************************************************/
 void update_diplomacy_gold(struct player *pplayer)
 {
-  /* PORTME */
+  dialog_list_iterate(dialog_list, pdialog) {
+    update_gold_range(pdialog, pdialog->gold0, pplayer);
+    update_gold_range(pdialog, pdialog->gold1, pplayer);
+  } dialog_list_iterate_end;
 }
 
 /************************************************************************//**
diff --git a/client/gui-gtk-4.0/diplodlg.c b/client/gui-gtk-4.0/diplodlg.c
index a431dfdffb..f897c87252 100644
--- a/client/gui-gtk-4.0/diplodlg.c
+++ b/client/gui-gtk-4.0/diplodlg.c
@@ -57,6 +57,9 @@ struct Diplomacy_dialog {
   GtkWidget *pic0;
   GtkWidget *pic1;
 
+  GtkWidget *gold0;
+  GtkWidget *gold1;
+
   GtkListStore *store;
 };
 
@@ -782,6 +785,8 @@ static struct Diplomacy_dialog *create_diplomacy_dialog(struct treaty *ptreaty,
 
   dialog_list_prepend(dialog_list, pdialog);
   pdialog->treaty = ptreaty;
+  pdialog->gold0 = NULL;
+  pdialog->gold1 = NULL;
 
   /* Get main diplomacy tab. */
   dipl_dialog = diplomacy_main_create();
@@ -888,6 +893,7 @@ static struct Diplomacy_dialog *create_diplomacy_dialog(struct treaty *ptreaty,
     gtk_editable_set_width_chars(GTK_EDITABLE(spin), 16);
     gtk_grid_attach(GTK_GRID(table), spin, 1, 0, 1, 1);
     g_object_set_data(G_OBJECT(spin), "plr", plr0);
+    pdialog->gold0 = spin;
     g_signal_connect_after(spin, "value-changed",
                            G_CALLBACK(diplo_dialog_returnkey), pdialog);
 
@@ -967,6 +973,7 @@ static struct Diplomacy_dialog *create_diplomacy_dialog(struct treaty *ptreaty,
     gtk_editable_set_width_chars(GTK_EDITABLE(spin), 16);
     gtk_grid_attach(GTK_GRID(table), spin, 1, 0, 1, 1);
     g_object_set_data(G_OBJECT(spin), "plr", plr1);
+    pdialog->gold1 = spin;
     g_signal_connect_after(spin, "value-changed",
                            G_CALLBACK(diplo_dialog_returnkey), pdialog);
 
@@ -1338,12 +1345,30 @@ static void diplo_dialog_returnkey(GtkWidget *w, gpointer data)
   }
 }
 
+/************************************************************************//**
+  Keep the range of a gold spin button in step with its player's gold
+****************************************************************************/
+static void update_gold_range(struct Diplomacy_dialog *pdialog,
+                              GtkWidget *spin, struct player *pplayer)
+{
+  if (spin != NULL && g_object_get_data(G_OBJECT(spin), "plr") == pplayer) {
+    /* Clamping the value to a lower range must not offer that amount. */
+    g_signal_handlers_block_by_func(spin, diplo_dialog_returnkey, pdialog);
+    gtk_spin_button_set_range(GTK_SPIN_BUTTON(spin), 0.0,
+                              pplayer->economic.gold + 0.1);
+    g_signal_handlers_unblock_by_func(spin, diplo_dialog_returnkey, pdialog);
+  }
+}
+
 /************************************************************************//**
   Player's gold may have changed
 ****************************************************************************/
 void update_diplomacy_gold(struct player *pplayer)
 {
-  /* PORTME */
+  dialog_list_iterate(dialog_list, pdialog) {
+    update_gold_range(pdialog, pdialog->gold0, pplayer);
+    update_gold_range(pdialog, pdialog->gold1, pplayer);
+  } dialog_list_iterate_end;
 }
 
 /************************************************************************//**
-- 
2.55.0

