(svn r14046) -Codechange: make the size of querystring "widgets" more configurable.
authorrubidium
Mon, 11 Aug 2008 22:08:56 +0000
changeset 9897 4d9a6ff6703e
parent 9896 19aa91881726
child 9898 75347c78b276
(svn r14046) -Codechange: make the size of querystring "widgets" more configurable.
src/genworld_gui.cpp
src/misc_gui.cpp
src/network/network_gui.cpp
src/osk_gui.cpp
src/querystring_gui.h
src/signs_gui.cpp
--- a/src/genworld_gui.cpp	Mon Aug 11 22:07:26 2008 +0000
+++ b/src/genworld_gui.cpp	Mon Aug 11 22:08:56 2008 +0000
@@ -251,12 +251,12 @@
 	char name[64];
 	glwp_modes mode;
 
-	GenerateLandscapeWindow(const WindowDesc *desc, WindowNumber number = 0) : QueryStringBaseWindow(desc, number)
+	GenerateLandscapeWindow(const WindowDesc *desc, WindowNumber number = 0) : QueryStringBaseWindow(11, desc, number)
 	{
 		this->LowerWidget(_settings_newgame.game_creation.landscape + GLAND_TEMPERATE);
 
-		snprintf(this->edit_str_buf, sizeof(this->edit_str_buf), "%u", _settings_newgame.game_creation.generation_seed);
-		InitializeTextBuffer(&this->text, this->edit_str_buf, lengthof(this->edit_str_buf), 120);
+		snprintf(this->edit_str_buf, this->edit_str_size, "%u", _settings_newgame.game_creation.generation_seed);
+		InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 120);
 		this->caption = STR_NULL;
 		this->afilter = CS_NUMERAL;
 
@@ -367,7 +367,7 @@
 
 			case GLAND_RANDOM_BUTTON: // Random seed
 				_settings_newgame.game_creation.generation_seed = InteractiveRandom();
-				snprintf(this->edit_str_buf, lengthof(this->edit_str_buf), "%u", _settings_newgame.game_creation.generation_seed);
+				snprintf(this->edit_str_buf, this->edit_str_size, "%u", _settings_newgame.game_creation.generation_seed);
 				UpdateTextBufferSize(&this->text);
 				this->SetDirty();
 				break;
--- a/src/misc_gui.cpp	Mon Aug 11 22:07:26 2008 +0000
+++ b/src/misc_gui.cpp	Mon Aug 11 22:08:56 2008 +0000
@@ -1056,7 +1056,7 @@
 
 struct QueryStringWindow : public QueryStringBaseWindow
 {
-	QueryStringWindow(const WindowDesc *desc, Window *parent) : QueryStringBaseWindow(desc)
+	QueryStringWindow(size_t size, const WindowDesc *desc, Window *parent) : QueryStringBaseWindow(size, desc)
 	{
 		this->parent = parent;
 		SetBit(_no_scroll, SCROLL_EDIT);
@@ -1167,19 +1167,12 @@
 	DeleteWindowById(WC_QUERY_STRING, 0);
 	DeleteWindowById(WC_SAVELOAD, 0);
 
-	QueryStringWindow *w = new QueryStringWindow(&_query_string_desc, parent);
-
-	assert(realmaxlen < lengthof(w->edit_str_buf));
+	QueryStringWindow *w = new QueryStringWindow(realmaxlen + 1, &_query_string_desc, parent);
 
-	GetString(w->edit_str_buf, str, lastof(w->edit_str_buf));
-	w->edit_str_buf[realmaxlen - 1] = '\0';
+	GetString(w->edit_str_buf, str, &w->edit_str_buf[realmaxlen]);
+	w->edit_str_buf[realmaxlen] = '\0';
 
-	if (maxlen & 0x1000) {
-		w->orig = NULL;
-	} else {
-		strecpy(w->orig_str_buf, w->edit_str_buf, lastof(w->orig_str_buf));
-		w->orig = w->orig_str_buf;
-	}
+	if (!(maxlen & 0x1000)) w->orig = strdup(w->edit_str_buf);
 
 	w->LowerWidget(QUERY_STR_WIDGET_TEXT);
 	w->caption = caption;
@@ -1414,11 +1407,11 @@
 
 		SetDParam(0, p->index);
 		SetDParam(1, _date);
-		GetString(this->edit_str_buf, STR_4004, lastof(this->edit_str_buf));
+		GetString(this->edit_str_buf, STR_4004, &this->edit_str_buf[this->edit_str_size - 1]);
 		SanitizeFilename(this->edit_str_buf);
 	}
 
-	SaveLoadWindow(const WindowDesc *desc, SaveLoadDialogMode mode) : QueryStringBaseWindow(desc)
+	SaveLoadWindow(const WindowDesc *desc, SaveLoadDialogMode mode) : QueryStringBaseWindow(64, desc)
 	{
 		static const StringID saveload_captions[] = {
 			STR_4001_LOAD_GAME,
@@ -1445,7 +1438,7 @@
 		this->LowerWidget(7);
 
 		this->afilter = CS_ALPHANUMERAL;
-		InitializeTextBuffer(&this->text, this->edit_str_buf, lengthof(this->edit_str_buf), 240);
+		InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 240);
 
 		/* pause is only used in single-player, non-editor mode, non-menu mode. It
 		 * will be unpaused in the WE_DESTROY event handler. */
--- a/src/network/network_gui.cpp	Mon Aug 11 22:07:26 2008 +0000
+++ b/src/network/network_gui.cpp	Mon Aug 11 22:08:56 2008 +0000
@@ -288,11 +288,11 @@
 	}
 
 public:
-	NetworkGameWindow(const WindowDesc *desc) : QueryStringBaseWindow(desc)
+	NetworkGameWindow(const WindowDesc *desc) : QueryStringBaseWindow(NETWORK_NAME_LENGTH, desc)
 	{
-		ttd_strlcpy(this->edit_str_buf, _settings_client.network.player_name, lengthof(this->edit_str_buf));
+		ttd_strlcpy(this->edit_str_buf, _settings_client.network.player_name, this->edit_str_size);
 		this->afilter = CS_ALPHANUMERAL;
-		InitializeTextBuffer(&this->text, this->edit_str_buf, lengthof(this->edit_str_buf), 120);
+		InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 120);
 
 		UpdateNetworkGameWindow(true);
 
@@ -758,9 +758,9 @@
 	FiosItem *map;               ///< Selected map
 	byte widget_id;              ///< The widget that has the pop-up input menu
 
-	NetworkStartServerWindow(const WindowDesc *desc) : QueryStringBaseWindow(desc)
+	NetworkStartServerWindow(const WindowDesc *desc) : QueryStringBaseWindow(NETWORK_NAME_LENGTH, desc)
 	{
-		ttd_strlcpy(this->edit_str_buf, _settings_client.network.server_name, lengthof(this->edit_str_buf));
+		ttd_strlcpy(this->edit_str_buf, _settings_client.network.server_name, this->edit_str_size);
 
 		_saveload_mode = SLD_NEW_GAME;
 		BuildFileList();
@@ -768,7 +768,7 @@
 		this->vscroll.count = _fios_items.Length() + 1;
 
 		this->afilter = CS_ALPHANUMERAL;
-		InitializeTextBuffer(&this->text, this->edit_str_buf, lengthof(this->edit_str_buf), 160);
+		InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 160);
 
 		this->field = NSSW_GAMENAME;
 
@@ -1747,13 +1747,13 @@
 	DestType dtype;
 	int dest;
 
-	NetworkChatWindow (const WindowDesc *desc, DestType type, int dest) : QueryStringBaseWindow(desc)
+	NetworkChatWindow (const WindowDesc *desc, DestType type, int dest) : QueryStringBaseWindow(NETWORK_CHAT_LENGTH, desc)
 	{
 		this->LowerWidget(2);
 		this->dtype   = type;
 		this->dest    = dest;
 		this->afilter = CS_ALPHANUMERAL;
-		InitializeTextBuffer(&this->text, this->edit_str_buf, lengthof(this->edit_str_buf), 0);
+		InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 0);
 
 		InvalidateWindowData(WC_NEWS_WINDOW, 0, this->height);
 		SetBit(_no_scroll, SCROLL_CHAT); // do not scroll the game with the arrow-keys
@@ -1822,7 +1822,9 @@
 	 */
 	void ChatTabCompletion()
 	{
-		static char _chat_tab_completion_buf[lengthof(this->edit_str_buf)];
+		static char _chat_tab_completion_buf[NETWORK_CHAT_LENGTH];
+		assert(this->edit_str_size == lengthof(_chat_tab_completion_buf));
+
 		Textbuf *tb = &this->text;
 		size_t len, tb_len;
 		uint item;
@@ -1875,9 +1877,9 @@
 
 				/* Change to the found name. Add ': ' if we are at the start of the line (pretty) */
 				if (pre_buf == tb_buf) {
-					snprintf(tb->buf, lengthof(this->edit_str_buf), "%s: ", cur_name);
+					snprintf(tb->buf, this->edit_str_size, "%s: ", cur_name);
 				} else {
-					snprintf(tb->buf, lengthof(this->edit_str_buf), "%s %s", pre_buf, cur_name);
+					snprintf(tb->buf, this->edit_str_size, "%s %s", pre_buf, cur_name);
 				}
 
 				/* Update the textbuffer */
@@ -1988,11 +1990,11 @@
 };
 
 struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow {
-	NetworkCompanyPasswordWindow(const WindowDesc *desc, Window *parent) : QueryStringBaseWindow(desc)
+	NetworkCompanyPasswordWindow(const WindowDesc *desc, Window *parent) : QueryStringBaseWindow(lengthof(_settings_client.network.default_company_pass), desc)
 	{
 		this->parent = parent;
 		this->afilter = CS_ALPHANUMERAL;
-		InitializeTextBuffer(&this->text, this->edit_str_buf, min(lengthof(_settings_client.network.default_company_pass), lengthof(this->edit_str_buf)), 0);
+		InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 0);
 
 		this->FindWindowPlacementAndResize(desc);
 	}
@@ -2004,7 +2006,7 @@
 		}
 
 		/* empty password is a '*' because of console argument */
-		if (StrEmpty(this->edit_str_buf)) snprintf(this->edit_str_buf, lengthof(this->edit_str_buf), "*");
+		if (StrEmpty(this->edit_str_buf)) snprintf(this->edit_str_buf, this->edit_str_size, "*");
 		char *password = this->edit_str_buf;
 		NetworkChangeCompanyPassword(1, &password);
 	}
--- a/src/osk_gui.cpp	Mon Aug 11 22:07:26 2008 +0000
+++ b/src/osk_gui.cpp	Mon Aug 11 22:08:56 2008 +0000
@@ -48,7 +48,7 @@
 	int ok_btn;            ///< widget number of parent's ok button (=0 when ok shouldn't be passed on)
 	int cancel_btn;        ///< widget number of parent's cancel button (=0 when cancel shouldn't be passed on; text will be reverted to original)
 	Textbuf *text;         ///< pointer to parent's textbuffer (to update caret position)
-	char orig_str_buf[64]; ///< Original string.
+	char *orig_str_buf;    ///< Original string.
 
 	OskWindow(const WindowDesc *desc, QueryStringBaseWindow *parent, int button, int cancel, int ok) : Window(desc)
 	{
@@ -64,7 +64,7 @@
 		this->text       = &parent->text;
 
 		/* make a copy in case we need to reset later */
-		strcpy(this->orig_str_buf, this->qs->text.buf);
+		this->orig_str_buf = strdup(this->qs->text.buf);
 
 		SetBit(_no_scroll, SCROLL_EDIT);
 		/* Not needed by default. */
@@ -73,6 +73,11 @@
 		this->FindWindowPlacementAndResize(desc);
 	}
 
+	~OskWindow()
+	{
+		free(this->orig_str_buf);
+	}
+
 	/**
 	 * Only show valid characters; do not show characters that would
 	 * only insert a space when we have a spacebar to do that or
--- a/src/querystring_gui.h	Mon Aug 11 22:07:26 2008 +0000
+++ b/src/querystring_gui.h	Mon Aug 11 22:08:56 2008 +0000
@@ -8,6 +8,9 @@
 #include "textbuf_gui.h"
 #include "window_gui.h"
 
+/**
+ * Data stored about a string that can be modified in the GUI
+ */
 struct QueryString {
 	StringID caption;
 	Textbuf text;
@@ -15,17 +18,39 @@
 	CharSetFilter afilter;
 	bool handled;
 
+	/**
+	 * Make sure everything gets initialized properly.
+	 */
+	QueryString() : orig(NULL)
+	{
+	}
+
+	/**
+	 * Make sure everything gets freed.
+	 */
+	~QueryString()
+	{
+		free((void*)this->orig);
+	}
+
 	void DrawEditBox(Window *w, int wid);
 	void HandleEditBox(Window *w, int wid);
 	int HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state);
 };
 
 struct QueryStringBaseWindow : public Window, public QueryString {
-	char edit_str_buf[64];
-	char orig_str_buf[64];
+	const size_t edit_str_size;
+	char *edit_str_buf;
+	char *orig_str_buf;
 
-	QueryStringBaseWindow(const WindowDesc *desc, WindowNumber window_number = 0) : Window(desc, window_number)
+	QueryStringBaseWindow(size_t size, const WindowDesc *desc, WindowNumber window_number = 0) : Window(desc, window_number), edit_str_size(size)
 	{
+		this->edit_str_buf = CallocT<char>(size);
+	}
+
+	~QueryStringBaseWindow()
+	{
+		free(this->edit_str_buf);
 	}
 
 	void DrawEditBox(int wid);
--- a/src/signs_gui.cpp	Mon Aug 11 22:07:26 2008 +0000
+++ b/src/signs_gui.cpp	Mon Aug 11 22:08:56 2008 +0000
@@ -188,7 +188,7 @@
 struct SignWindow : QueryStringBaseWindow, SignList {
 	SignID cur_sign;
 
-	SignWindow(const WindowDesc *desc, const Sign *si) : QueryStringBaseWindow(desc)
+	SignWindow(const WindowDesc *desc, const Sign *si) : QueryStringBaseWindow(31, desc)
 	{
 		SetBit(_no_scroll, SCROLL_EDIT);
 		this->caption = STR_280B_EDIT_SIGN_TEXT;
@@ -206,17 +206,19 @@
 
 	void UpdateSignEditWindow(const Sign *si)
 	{
+		char *last_of = &this->edit_str_buf[this->edit_str_size - 1];
+
 		/* Display an empty string when the sign hasnt been edited yet */
 		if (si->name != NULL) {
 			SetDParam(0, si->index);
-			GetString(this->edit_str_buf, STR_SIGN_NAME, lastof(this->edit_str_buf));
+			GetString(this->edit_str_buf, STR_SIGN_NAME, last_of);
 		} else {
-			GetString(this->edit_str_buf, STR_EMPTY, lastof(this->edit_str_buf));
+			GetString(this->edit_str_buf, STR_EMPTY, last_of);
 		}
-		this->edit_str_buf[lengthof(this->edit_str_buf) - 1] = '\0';
+		*last_of = '\0';
 
 		this->cur_sign = si->index;
-		InitializeTextBuffer(&this->text, this->edit_str_buf, 31, 255); // Allow 31 characters (including \0)
+		InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 255);
 
 		this->InvalidateWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
 	}