1
0
Fork 0

(svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.

release/1.3
frosch 2012-11-14 22:50:17 +00:00
parent 0ea2152355
commit f6d4200f86
15 changed files with 29 additions and 62 deletions

View File

@ -1031,11 +1031,9 @@ struct AIDebugWindow : public QueryStringBaseWindow {
this->last_vscroll_pos = 0; this->last_vscroll_pos = 0;
this->autoscroll = true; this->autoscroll = true;
this->highlight_row = -1; this->highlight_row = -1;
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
/* Restore the break string value from static variable */ /* 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.Assign(this->break_string);
this->text.UpdateSize();
/* Restore button state from static class variables */ /* Restore button state from static class variables */
if (ai_debug_company == OWNER_DEITY) { if (ai_debug_company == OWNER_DEITY) {

View File

@ -126,7 +126,7 @@ struct IConsoleLine {
/* ** main console cmd buffer ** */ /* ** main console cmd buffer ** */
static Textbuf _iconsole_cmdline; static Textbuf _iconsole_cmdline(ICON_CMDLN_SIZE);
static char *_iconsole_history[ICON_HISTORY_SIZE]; static char *_iconsole_history[ICON_HISTORY_SIZE];
static int _iconsole_historypos; static int _iconsole_historypos;
IConsoleModes _iconsole_mode; IConsoleModes _iconsole_mode;
@ -356,10 +356,6 @@ void IConsoleGUIInit()
IConsoleLine::Reset(); IConsoleLine::Reset();
memset(_iconsole_history, 0, sizeof(_iconsole_history)); 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); IConsolePrintF(CC_WARNING, "OpenTTD Game Console Revision 7 - %s", _openttd_revision);
IConsolePrint(CC_WHITE, "------------------------------------"); IConsolePrint(CC_WHITE, "------------------------------------");
IConsolePrint(CC_WHITE, "use \"help\" for more information"); IConsolePrint(CC_WHITE, "use \"help\" for more information");
@ -374,7 +370,6 @@ void IConsoleClearBuffer()
void IConsoleGUIFree() void IConsoleGUIFree()
{ {
free(_iconsole_cmdline.buf);
IConsoleClearBuffer(); IConsoleClearBuffer();
} }

View File

@ -239,6 +239,7 @@ public:
void GenerateFileName() void GenerateFileName()
{ {
GenerateDefaultSaveName(this->edit_str_buf, &this->edit_str_buf[this->edit_str_size - 1]); 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) SaveLoadWindow(const WindowDesc *desc, SaveLoadDialogMode mode) : QueryStringBaseWindow(64)
@ -258,13 +259,12 @@ public:
switch (mode) { switch (mode) {
case SLD_SAVE_GAME: this->GenerateFileName(); break; case SLD_SAVE_GAME: this->GenerateFileName(); break;
case SLD_SAVE_HEIGHTMAP: 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; default: break;
} }
this->ok_button = WID_SL_SAVE_GAME; this->ok_button = WID_SL_SAVE_GAME;
this->afilter = CS_ALPHANUMERAL; this->afilter = CS_ALPHANUMERAL;
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
this->CreateNestedTree(desc, true); this->CreateNestedTree(desc, true);
if (mode == SLD_LOAD_GAME) this->GetWidget<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL); 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 */ /* Reset file name to current date on successful delete */
if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName(); if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
} }
this->text.UpdateSize();
} else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked
if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) { if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
_switch_mode = SM_SAVE_GAME; _switch_mode = SM_SAVE_GAME;

View File

@ -316,9 +316,7 @@ struct GenerateLandscapeWindow : public QueryStringBaseWindow {
this->LowerWidget(_settings_newgame.game_creation.landscape + WID_GL_TEMPERATE); this->LowerWidget(_settings_newgame.game_creation.landscape + WID_GL_TEMPERATE);
/* snprintf() always outputs trailing '\0', so whole buffer can be used */ this->text.Print("%u", _settings_newgame.game_creation.generation_seed);
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->caption = STR_NULL; this->caption = STR_NULL;
this->afilter = CS_NUMERAL; this->afilter = CS_NUMERAL;

View File

@ -828,6 +828,8 @@ struct QueryStringWindow : public QueryStringBaseWindow
*Utf8PrevChar(this->edit_str_buf + strlen(this->edit_str_buf)) = '\0'; *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); if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->orig = strdup(this->edit_str_buf);
this->caption = caption; this->caption = caption;
@ -835,7 +837,6 @@ struct QueryStringWindow : public QueryStringBaseWindow
this->ok_button = WID_QS_OK; this->ok_button = WID_QS_OK;
this->afilter = afilter; this->afilter = afilter;
this->flags = flags; this->flags = flags;
this->text.Initialize(this->edit_str_buf, max_bytes, max_chars);
this->InitNested(desc, WN_QUERY_STRING); this->InitNested(desc, WN_QUERY_STRING);

View File

@ -302,7 +302,6 @@ struct NetworkChatWindow : public QueryStringBaseWindow {
this->cancel_button = WID_NC_CLOSE; this->cancel_button = WID_NC_CLOSE;
this->ok_button = WID_NC_SENDBUTTON; this->ok_button = WID_NC_SENDBUTTON;
this->afilter = CS_ALPHANUMERAL; this->afilter = CS_ALPHANUMERAL;
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
static const StringID chat_captions[] = { static const StringID chat_captions[] = {
STR_NETWORK_CHAT_ALL_CAPTION, STR_NETWORK_CHAT_ALL_CAPTION,

View File

@ -417,7 +417,6 @@ public:
this->GetWidget<NWidgetStacked>(WID_NCL_SEL_ALL_UPDATE)->SetDisplayedPlane(select_all); this->GetWidget<NWidgetStacked>(WID_NCL_SEL_ALL_UPDATE)->SetDisplayedPlane(select_all);
this->afilter = CS_ALPHANUMERAL; this->afilter = CS_ALPHANUMERAL;
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
this->SetFocusedWidget(WID_NCL_FILTER); this->SetFocusedWidget(WID_NCL_FILTER);
_network_content_client.AddCallback(this); _network_content_client.AddCallback(this);

View File

@ -444,9 +444,8 @@ public:
this->vscroll = this->GetScrollbar(WID_NG_SCROLLBAR); this->vscroll = this->GetScrollbar(WID_NG_SCROLLBAR);
this->FinishInitNested(desc, WN_NETWORK_WINDOW_GAME); 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->afilter = CS_ALPHANUMERAL;
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
this->SetFocusedWidget(WID_NG_CLIENT); this->SetFocusedWidget(WID_NG_CLIENT);
this->last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); 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); 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->afilter = CS_ALPHANUMERAL;
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
this->SetFocusedWidget(WID_NSS_GAMENAME); this->SetFocusedWidget(WID_NSS_GAMENAME);
} }
@ -2112,7 +2110,6 @@ struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow {
this->cancel_button = WID_NCP_CANCEL; this->cancel_button = WID_NCP_CANCEL;
this->ok_button = WID_NCP_OK; this->ok_button = WID_NCP_OK;
this->afilter = CS_ALPHANUMERAL; this->afilter = CS_ALPHANUMERAL;
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
this->SetFocusedWidget(WID_NCP_PASSWORD); this->SetFocusedWidget(WID_NCP_PASSWORD);
} }

View File

@ -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->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->FinishInitNested(desc, WN_GAME_OPTIONS_NEWGRF_STATE);
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
this->SetFocusedWidget(WID_NS_FILTER); this->SetFocusedWidget(WID_NS_FILTER);
this->avails.SetListing(this->last_sorting); this->avails.SetListing(this->last_sorting);

View File

@ -40,9 +40,11 @@ struct QueryString {
bool handled; 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 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'. 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); void ShowOnScreenKeyboard(QueryStringBaseWindow *parent, int button);

View File

@ -2002,7 +2002,6 @@ struct GameSettingsWindow : QueryStringBaseWindow {
this->vscroll = this->GetScrollbar(WID_GS_SCROLLBAR); this->vscroll = this->GetScrollbar(WID_GS_SCROLLBAR);
this->FinishInitNested(desc, WN_GAME_OPTIONS_GAME_SETTINGS); 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->SetFocusedWidget(WID_GS_FILTER);
this->InvalidateData(); this->InvalidateData();

View File

@ -159,8 +159,6 @@ struct SignListWindow : QueryStringBaseWindow, SignList {
this->ok_button = WID_SIL_FILTER_ENTER_BTN; this->ok_button = WID_SIL_FILTER_ENTER_BTN;
this->cancel_button = WID_SIL_FILTER_CLEAR_BTN; this->cancel_button = WID_SIL_FILTER_CLEAR_BTN;
this->afilter = CS_ALPHANUMERAL; 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 */ /* Initialize the filtering variables */
this->SetFilterString(""); this->SetFilterString("");
@ -450,19 +448,15 @@ struct SignWindow : QueryStringBaseWindow, SignList {
void UpdateSignEditWindow(const Sign *si) 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 */ /* Display an empty string when the sign hasnt been edited yet */
if (si->name != NULL) { if (si->name != NULL) {
SetDParam(0, si->index); SetDParam(0, si->index);
GetString(this->edit_str_buf, STR_SIGN_NAME, last_of); this->text.Assign(STR_SIGN_NAME);
} else { } else {
GetString(this->edit_str_buf, STR_EMPTY, last_of); this->text.DeleteAll();
} }
*last_of = '\0';
this->cur_sign = si->index; 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->SetWidgetDirty(WID_QES_TEXT);
this->SetFocusedWidget(WID_QES_TEXT); this->SetFocusedWidget(WID_QES_TEXT);

View File

@ -18,6 +18,7 @@
#include "gfx_type.h" #include "gfx_type.h"
#include "gfx_func.h" #include "gfx_func.h"
#include "window_func.h" #include "window_func.h"
#include "core/alloc_func.hpp"
/** /**
* Try to retrive the current clipboard contents. * Try to retrive the current clipboard contents.
@ -350,17 +351,6 @@ bool Textbuf::MovePos(int navmode)
return false; return false;
} }
/**
* 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'
*/
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 * Initialize the textbuffer by supplying it the buffer to write into
* and the maximum length of this buffer * and the maximum length of this buffer
@ -368,16 +358,21 @@ void Textbuf::Initialize(char *buf, uint16 max_bytes)
* @param max_bytes maximum size in bytes, including terminating '\0' * @param max_bytes maximum size in bytes, including terminating '\0'
* @param max_chars maximum size in chars, 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_bytes != 0);
assert(max_chars != 0); assert(max_chars != 0);
this->buf = buf;
this->max_bytes = max_bytes; 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->caret = true;
this->UpdateSize(); this->DeleteAll();
}
Textbuf::~Textbuf()
{
free(this->buf);
} }
/** /**

View File

@ -17,7 +17,7 @@
/** Helper/buffer for input fields. */ /** Helper/buffer for input fields. */
struct Textbuf { 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_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 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') 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 caretpos; ///< the current position of the caret in the buffer, in bytes
uint16 caretxoffs; ///< the current position of the caret in pixels uint16 caretxoffs; ///< the current position of the caret in pixels
void Initialize(char *buf, uint16 max_bytes); explicit Textbuf(uint16 max_bytes, uint16 max_chars = UINT16_MAX);
void Initialize(char *buf, uint16 max_bytes, uint16 max_chars); ~Textbuf();
void Assign(StringID string); void Assign(StringID string);
void Assign(const char *text); void Assign(const char *text);

View File

@ -995,7 +995,6 @@ public:
params(_settings_game.game_creation.town_name) params(_settings_game.game_creation.town_name)
{ {
this->InitNested(desc, window_number); this->InitNested(desc, window_number);
this->text.Initialize(this->edit_str_buf, this->edit_str_size, this->max_chars);
this->RandomTownName(); this->RandomTownName();
this->UpdateButtons(true); this->UpdateButtons(true);
} }