From 4ab569544b34cda1feb23528bc07aa6e5ae4776e Mon Sep 17 00:00:00 2001
From: rubidium <rubidium@openttd.org>
Date: Sat, 10 Oct 2009 12:47:04 +0000
Subject: [PATCH] (svn r17752) -Fix [FS#3231]: OSK could reset town name to an
 already used town name for the 'Generate Town' window

---
 src/osk_gui.cpp       | 18 ++++++++++++++++++
 src/querystring_gui.h |  1 +
 src/town_gui.cpp      |  1 +
 3 files changed, 20 insertions(+)

diff --git a/src/osk_gui.cpp b/src/osk_gui.cpp
index fdf89ab218..cb4386ee60 100644
--- a/src/osk_gui.cpp
+++ b/src/osk_gui.cpp
@@ -547,3 +547,21 @@ void ShowOnScreenKeyboard(QueryStringBaseWindow *parent, int button, int cancel,
 	GetKeyboardLayout();
 	new OskWindow(&_osk_desc, parent, button, cancel, ok);
 }
+
+/**
+ * Updates the original text of the OSK so when the 'parent' changes the
+ * original and you press on cancel you won't get the 'old' original text
+ * but the updated one.
+ * @param parent window that just updated it's orignal text
+ * @param button widget number of parent's textbox to update
+ */
+void UpdateOSKOriginalText(const QueryStringBaseWindow *parent, int button)
+{
+	OskWindow *osk = dynamic_cast<OskWindow *>(FindWindowById(WC_OSK, 0));
+	if (osk == NULL || osk->qs != parent || osk->text_btn != button) return;
+
+	free(osk->orig_str_buf);
+	osk->orig_str_buf = strdup(osk->qs->text.buf);
+
+	osk->SetDirty();
+}
diff --git a/src/querystring_gui.h b/src/querystring_gui.h
index eb49c9fea2..e378b4ebfe 100644
--- a/src/querystring_gui.h
+++ b/src/querystring_gui.h
@@ -86,5 +86,6 @@ struct QueryStringBaseWindow : public Window, public QueryString {
 };
 
 void ShowOnScreenKeyboard(QueryStringBaseWindow *parent, int button, int cancel, int ok);
+void UpdateOSKOriginalText(const QueryStringBaseWindow *parent, int button);
 
 #endif /* QUERYSTRING_GUI_H */
diff --git a/src/town_gui.cpp b/src/town_gui.cpp
index 8c39e42e29..be0a0d4b41 100644
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -978,6 +978,7 @@ public:
 			GetTownName(this->edit_str_buf, &this->params, this->townnameparts, &this->edit_str_buf[this->edit_str_size - 1]);
 		}
 		UpdateTextBufferSize(&this->text);
+		UpdateOSKOriginalText(this, TSEW_TOWNNAME_EDITBOX);
 
 		this->SetFocusedWidget(TSEW_TOWNNAME_EDITBOX);
 		this->SetWidgetDirty(TSEW_TOWNNAME_EDITBOX);