diff options
-rw-r--r-- | src/ai/ai_gui.cpp | 4 | ||||
-rw-r--r-- | src/console_gui.cpp | 7 | ||||
-rw-r--r-- | src/fios_gui.cpp | 6 | ||||
-rw-r--r-- | src/genworld_gui.cpp | 4 | ||||
-rw-r--r-- | src/misc_gui.cpp | 3 | ||||
-rw-r--r-- | src/network/network_chat_gui.cpp | 1 | ||||
-rw-r--r-- | src/network/network_content_gui.cpp | 1 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 7 | ||||
-rw-r--r-- | src/newgrf_gui.cpp | 1 | ||||
-rw-r--r-- | src/querystring_gui.h | 14 | ||||
-rw-r--r-- | src/settings_gui.cpp | 1 | ||||
-rw-r--r-- | src/signs_gui.cpp | 10 | ||||
-rw-r--r-- | src/textbuf.cpp | 25 | ||||
-rw-r--r-- | src/textbuf_type.h | 6 | ||||
-rw-r--r-- | src/town_gui.cpp | 1 |
15 files changed, 29 insertions, 62 deletions
diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index 875ccbffb..303b08f17 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -1031,11 +1031,9 @@ struct AIDebugWindow : public QueryStringBaseWindow { this->last_vscroll_pos = 0; this->autoscroll = true; this->highlight_row = -1; - this->text.Initialize(this->edit_str_buf, this->edit_str_size); /* Restore the break string value from static variable */ - strecpy(this->edit_str_buf, this->break_string, this->edit_str_buf + MAX_BREAK_STR_STRING_LENGTH); - this->text.UpdateSize(); + this->text.Assign(this->break_string); /* Restore button state from static class variables */ if (ai_debug_company == OWNER_DEITY) { diff --git a/src/console_gui.cpp b/src/console_gui.cpp index ac7df2020..2cbb5f3df 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -126,7 +126,7 @@ struct IConsoleLine { /* ** main console cmd buffer ** */ -static Textbuf _iconsole_cmdline; +static Textbuf _iconsole_cmdline(ICON_CMDLN_SIZE); static char *_iconsole_history[ICON_HISTORY_SIZE]; static int _iconsole_historypos; IConsoleModes _iconsole_mode; @@ -356,10 +356,6 @@ void IConsoleGUIInit() IConsoleLine::Reset(); memset(_iconsole_history, 0, sizeof(_iconsole_history)); - _iconsole_cmdline.buf = CallocT<char>(ICON_CMDLN_SIZE); // create buffer and zero it - _iconsole_cmdline.max_bytes = ICON_CMDLN_SIZE; - _iconsole_cmdline.max_chars = ICON_CMDLN_SIZE; - IConsolePrintF(CC_WARNING, "OpenTTD Game Console Revision 7 - %s", _openttd_revision); IConsolePrint(CC_WHITE, "------------------------------------"); IConsolePrint(CC_WHITE, "use \"help\" for more information"); @@ -374,7 +370,6 @@ void IConsoleClearBuffer() void IConsoleGUIFree() { - free(_iconsole_cmdline.buf); IConsoleClearBuffer(); } diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 8cf3c840a..6eac9315e 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -239,6 +239,7 @@ public: void GenerateFileName() { GenerateDefaultSaveName(this->edit_str_buf, &this->edit_str_buf[this->edit_str_size - 1]); + this->text.UpdateSize(); } SaveLoadWindow(const WindowDesc *desc, SaveLoadDialogMode mode) : QueryStringBaseWindow(64) @@ -258,13 +259,12 @@ public: switch (mode) { case SLD_SAVE_GAME: this->GenerateFileName(); break; case SLD_SAVE_HEIGHTMAP: - case SLD_SAVE_SCENARIO: strecpy(this->edit_str_buf, "UNNAMED", &this->edit_str_buf[edit_str_size - 1]); break; + case SLD_SAVE_SCENARIO: this->text.Assign("UNNAMED"); break; default: break; } this->ok_button = WID_SL_SAVE_GAME; this->afilter = CS_ALPHANUMERAL; - this->text.Initialize(this->edit_str_buf, this->edit_str_size); this->CreateNestedTree(desc, true); if (mode == SLD_LOAD_GAME) this->GetWidget<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL); @@ -639,8 +639,6 @@ public: /* Reset file name to current date on successful delete */ if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName(); } - - this->text.UpdateSize(); } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) { _switch_mode = SM_SAVE_GAME; diff --git a/src/genworld_gui.cpp b/src/genworld_gui.cpp index 81ffd6a86..1e6914696 100644 --- a/src/genworld_gui.cpp +++ b/src/genworld_gui.cpp @@ -316,9 +316,7 @@ struct GenerateLandscapeWindow : public QueryStringBaseWindow { this->LowerWidget(_settings_newgame.game_creation.landscape + WID_GL_TEMPERATE); - /* snprintf() always outputs trailing '\0', so whole buffer can be used */ - snprintf(this->edit_str_buf, this->edit_str_size, "%u", _settings_newgame.game_creation.generation_seed); - this->text.Initialize(this->edit_str_buf, this->edit_str_size); + this->text.Print("%u", _settings_newgame.game_creation.generation_seed); this->caption = STR_NULL; this->afilter = CS_NUMERAL; diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 1391dbada..a933fb7da 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -828,6 +828,8 @@ struct QueryStringWindow : public QueryStringBaseWindow *Utf8PrevChar(this->edit_str_buf + strlen(this->edit_str_buf)) = '\0'; } + this->text.UpdateSize(); + if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->orig = strdup(this->edit_str_buf); this->caption = caption; @@ -835,7 +837,6 @@ struct QueryStringWindow : public QueryStringBaseWindow this->ok_button = WID_QS_OK; this->afilter = afilter; this->flags = flags; - this->text.Initialize(this->edit_str_buf, max_bytes, max_chars); this->InitNested(desc, WN_QUERY_STRING); diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp index 2e0be50ba..af5782037 100644 --- a/src/network/network_chat_gui.cpp +++ b/src/network/network_chat_gui.cpp @@ -302,7 +302,6 @@ struct NetworkChatWindow : public QueryStringBaseWindow { this->cancel_button = WID_NC_CLOSE; this->ok_button = WID_NC_SENDBUTTON; this->afilter = CS_ALPHANUMERAL; - this->text.Initialize(this->edit_str_buf, this->edit_str_size); static const StringID chat_captions[] = { STR_NETWORK_CHAT_ALL_CAPTION, diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index 0df8f8f99..5aa4818f5 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -417,7 +417,6 @@ public: this->GetWidget<NWidgetStacked>(WID_NCL_SEL_ALL_UPDATE)->SetDisplayedPlane(select_all); this->afilter = CS_ALPHANUMERAL; - this->text.Initialize(this->edit_str_buf, this->edit_str_size); this->SetFocusedWidget(WID_NCL_FILTER); _network_content_client.AddCallback(this); diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index a88d28a26..b5e48c4b0 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -444,9 +444,8 @@ public: this->vscroll = this->GetScrollbar(WID_NG_SCROLLBAR); this->FinishInitNested(desc, WN_NETWORK_WINDOW_GAME); - ttd_strlcpy(this->edit_str_buf, _settings_client.network.client_name, this->edit_str_size); + this->text.Assign(_settings_client.network.client_name); this->afilter = CS_ALPHANUMERAL; - this->text.Initialize(this->edit_str_buf, this->edit_str_size); this->SetFocusedWidget(WID_NG_CLIENT); this->last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); @@ -1006,10 +1005,9 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow { { this->InitNested(desc, WN_NETWORK_WINDOW_START); - ttd_strlcpy(this->edit_str_buf, _settings_client.network.server_name, this->edit_str_size); + this->text.Assign(_settings_client.network.server_name); this->afilter = CS_ALPHANUMERAL; - this->text.Initialize(this->edit_str_buf, this->edit_str_size); this->SetFocusedWidget(WID_NSS_GAMENAME); } @@ -2112,7 +2110,6 @@ struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow { this->cancel_button = WID_NCP_CANCEL; this->ok_button = WID_NCP_OK; this->afilter = CS_ALPHANUMERAL; - this->text.Initialize(this->edit_str_buf, this->edit_str_size); this->SetFocusedWidget(WID_NCP_PASSWORD); } diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 827e694ff..e83259d4e 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -637,7 +637,6 @@ struct NewGRFWindow : public QueryStringBaseWindow, NewGRFScanCallback { this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL); this->FinishInitNested(desc, WN_GAME_OPTIONS_NEWGRF_STATE); - this->text.Initialize(this->edit_str_buf, this->edit_str_size); this->SetFocusedWidget(WID_NS_FILTER); this->avails.SetListing(this->last_sorting); diff --git a/src/querystring_gui.h b/src/querystring_gui.h index 0eebd6206..1ce9b93f7 100644 --- a/src/querystring_gui.h +++ b/src/querystring_gui.h @@ -40,9 +40,11 @@ struct QueryString { bool handled; /** - * Make sure everything gets initialized properly. + * Initialize string. + * @param size Maximum size in bytes. + * @param chars Maximum size in chars. */ - QueryString() : ok_button(-1), cancel_button(-1), orig(NULL) + QueryString(uint16 size, uint16 chars = UINT16_MAX) : ok_button(-1), cancel_button(-1), text(size, chars), orig(NULL) { } @@ -67,16 +69,10 @@ struct QueryStringBaseWindow : public Window, public QueryString { const uint16 edit_str_size; ///< Maximum length of string (in bytes), including terminating '\0'. const uint16 max_chars; ///< Maximum length of string (in characters), including terminating '\0'. - QueryStringBaseWindow(uint16 size, uint16 chars = UINT16_MAX) : Window(), edit_str_size(size), max_chars(chars == UINT16_MAX ? size : chars) + QueryStringBaseWindow(uint16 size, uint16 chars = UINT16_MAX) : Window(), QueryString(size, chars), edit_str_buf(text.buf), edit_str_size(text.max_bytes), max_chars(text.max_chars) { - assert(size != 0); - this->edit_str_buf = CallocT<char>(size); } - ~QueryStringBaseWindow() - { - free(this->edit_str_buf); - } }; void ShowOnScreenKeyboard(QueryStringBaseWindow *parent, int button); diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 720bdac1a..cf2981418 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -2002,7 +2002,6 @@ struct GameSettingsWindow : QueryStringBaseWindow { this->vscroll = this->GetScrollbar(WID_GS_SCROLLBAR); this->FinishInitNested(desc, WN_GAME_OPTIONS_GAME_SETTINGS); - this->text.Initialize(this->edit_str_buf, this->edit_str_size); this->SetFocusedWidget(WID_GS_FILTER); this->InvalidateData(); diff --git a/src/signs_gui.cpp b/src/signs_gui.cpp index a7af05dbf..09739e6f5 100644 --- a/src/signs_gui.cpp +++ b/src/signs_gui.cpp @@ -159,8 +159,6 @@ struct SignListWindow : QueryStringBaseWindow, SignList { this->ok_button = WID_SIL_FILTER_ENTER_BTN; this->cancel_button = WID_SIL_FILTER_CLEAR_BTN; this->afilter = CS_ALPHANUMERAL; - this->text.Initialize(this->edit_str_buf, MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS); - ClearFilterTextWidget(); /* Initialize the filtering variables */ this->SetFilterString(""); @@ -450,19 +448,15 @@ struct SignWindow : QueryStringBaseWindow, SignList { void UpdateSignEditWindow(const Sign *si) { - char *last_of = &this->edit_str_buf[this->edit_str_size - 1]; // points to terminating '\0' - /* 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, last_of); + this->text.Assign(STR_SIGN_NAME); } else { - GetString(this->edit_str_buf, STR_EMPTY, last_of); + this->text.DeleteAll(); } - *last_of = '\0'; this->cur_sign = si->index; - this->text.Initialize(this->edit_str_buf, this->edit_str_size, this->max_chars); this->SetWidgetDirty(WID_QES_TEXT); this->SetFocusedWidget(WID_QES_TEXT); diff --git a/src/textbuf.cpp b/src/textbuf.cpp index 0816c5dcf..fe32712dc 100644 --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -18,6 +18,7 @@ #include "gfx_type.h" #include "gfx_func.h" #include "window_func.h" +#include "core/alloc_func.hpp" /** * Try to retrive the current clipboard contents. @@ -355,29 +356,23 @@ bool Textbuf::MovePos(int navmode) * and the maximum length of this buffer * @param buf the buffer that will be holding the data for input * @param max_bytes maximum size in bytes, including terminating '\0' - */ -void Textbuf::Initialize(char *buf, uint16 max_bytes) -{ - this->Initialize(buf, max_bytes, max_bytes); -} - -/** - * Initialize the textbuffer by supplying it the buffer to write into - * and the maximum length of this buffer - * @param buf the buffer that will be holding the data for input - * @param max_bytes maximum size in bytes, including terminating '\0' * @param max_chars maximum size in chars, including terminating '\0' */ -void Textbuf::Initialize(char *buf, uint16 max_bytes, uint16 max_chars) +Textbuf::Textbuf(uint16 max_bytes, uint16 max_chars) + : buf(MallocT<char>(max_bytes)) { assert(max_bytes != 0); assert(max_chars != 0); - this->buf = buf; this->max_bytes = max_bytes; - this->max_chars = max_chars; + this->max_chars = max_chars == UINT16_MAX ? max_bytes : max_chars; this->caret = true; - this->UpdateSize(); + this->DeleteAll(); +} + +Textbuf::~Textbuf() +{ + free(this->buf); } /** diff --git a/src/textbuf_type.h b/src/textbuf_type.h index 77a957f84..8d38e8b8a 100644 --- a/src/textbuf_type.h +++ b/src/textbuf_type.h @@ -17,7 +17,7 @@ /** Helper/buffer for input fields. */ struct Textbuf { - char *buf; ///< buffer in which text is saved + char * const buf; ///< buffer in which text is saved uint16 max_bytes; ///< the maximum size of the buffer in bytes (including terminating '\0') uint16 max_chars; ///< the maximum size of the buffer in characters (including terminating '\0') uint16 bytes; ///< the current size of the string in bytes (including terminating '\0') @@ -27,8 +27,8 @@ struct Textbuf { uint16 caretpos; ///< the current position of the caret in the buffer, in bytes uint16 caretxoffs; ///< the current position of the caret in pixels - void Initialize(char *buf, uint16 max_bytes); - void Initialize(char *buf, uint16 max_bytes, uint16 max_chars); + explicit Textbuf(uint16 max_bytes, uint16 max_chars = UINT16_MAX); + ~Textbuf(); void Assign(StringID string); void Assign(const char *text); diff --git a/src/town_gui.cpp b/src/town_gui.cpp index dba12cac7..9634383f9 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -995,7 +995,6 @@ public: params(_settings_game.game_creation.town_name) { this->InitNested(desc, window_number); - this->text.Initialize(this->edit_str_buf, this->edit_str_size, this->max_chars); this->RandomTownName(); this->UpdateButtons(true); } |