diff options
-rw-r--r-- | src/genworld_gui.cpp | 8 | ||||
-rw-r--r-- | src/misc_gui.cpp | 23 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 30 | ||||
-rw-r--r-- | src/osk_gui.cpp | 9 | ||||
-rw-r--r-- | src/querystring_gui.h | 31 | ||||
-rw-r--r-- | src/signs_gui.cpp | 12 |
6 files changed, 70 insertions, 43 deletions
diff --git a/src/genworld_gui.cpp b/src/genworld_gui.cpp index d9a90c0e0..1b449ba4f 100644 --- a/src/genworld_gui.cpp +++ b/src/genworld_gui.cpp @@ -251,12 +251,12 @@ struct GenerateLandscapeWindow : public QueryStringBaseWindow { 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 @@ struct GenerateLandscapeWindow : public QueryStringBaseWindow { 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; diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 5f055f1aa..7086e2d32 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -1056,7 +1056,7 @@ enum QueryStringWidgets { 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 @@ void ShowQueryString(StringID str, StringID caption, uint maxlen, uint maxwidth, DeleteWindowById(WC_QUERY_STRING, 0); DeleteWindowById(WC_SAVELOAD, 0); - QueryStringWindow *w = new QueryStringWindow(&_query_string_desc, parent); + QueryStringWindow *w = new QueryStringWindow(realmaxlen + 1, &_query_string_desc, parent); - assert(realmaxlen < lengthof(w->edit_str_buf)); + GetString(w->edit_str_buf, str, &w->edit_str_buf[realmaxlen]); + w->edit_str_buf[realmaxlen] = '\0'; - GetString(w->edit_str_buf, str, lastof(w->edit_str_buf)); - w->edit_str_buf[realmaxlen - 1] = '\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 @@ struct SaveLoadWindow : public QueryStringBaseWindow { 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 @@ struct SaveLoadWindow : public QueryStringBaseWindow { 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. */ diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index bc98ea136..70ddccd92 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -288,11 +288,11 @@ protected: } 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 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow { 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 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow { 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 @@ struct NetworkChatWindow : public QueryStringBaseWindow { 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 @@ struct NetworkChatWindow : public QueryStringBaseWindow { */ 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 @@ struct NetworkChatWindow : public QueryStringBaseWindow { /* 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 @@ enum NetworkCompanyPasswordWindowWidgets { }; 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 @@ struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow { } /* 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); } diff --git a/src/osk_gui.cpp b/src/osk_gui.cpp index 7df8836a4..9e1ee75b1 100644 --- a/src/osk_gui.cpp +++ b/src/osk_gui.cpp @@ -48,7 +48,7 @@ struct OskWindow : public Window { 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 @@ struct OskWindow : public Window { 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 @@ struct OskWindow : public Window { 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 diff --git a/src/querystring_gui.h b/src/querystring_gui.h index 004501b31..40b0c8685 100644 --- a/src/querystring_gui.h +++ b/src/querystring_gui.h @@ -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 @@ struct QueryString { 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(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(const WindowDesc *desc, WindowNumber window_number = 0) : Window(desc, window_number) + ~QueryStringBaseWindow() { + free(this->edit_str_buf); } void DrawEditBox(int wid); diff --git a/src/signs_gui.cpp b/src/signs_gui.cpp index 8759b960b..50b436ee6 100644 --- a/src/signs_gui.cpp +++ b/src/signs_gui.cpp @@ -188,7 +188,7 @@ enum QueryEditSignWidgets { 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 @@ struct SignWindow : QueryStringBaseWindow, SignList { 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); } |