(svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.

This commit is contained in:
frosch 2012-06-04 15:30:29 +00:00
parent 31eb896143
commit d58eee1e79
14 changed files with 129 additions and 134 deletions

View File

@ -1009,11 +1009,11 @@ struct AIDebugWindow : public QueryStringBaseWindow {
this->last_vscroll_pos = 0;
this->autoscroll = true;
this->highlight_row = -1;
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_BREAK_STR_STRING_LENGTH);
this->text.Initialize(this->edit_str_buf, this->edit_str_size, MAX_BREAK_STR_STRING_LENGTH);
/* 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);
UpdateTextBufferSize(&this->text);
this->text.UpdateSize();
/* Restore button state from static class variables */
if (ai_debug_company == OWNER_DEITY) {

View File

@ -233,7 +233,7 @@ struct IConsoleWindow : Window
virtual void OnMouseLoop()
{
if (HandleCaret(&_iconsole_cmdline)) this->SetDirty();
if (_iconsole_cmdline.HandleCaret()) this->SetDirty();
}
virtual EventState OnKeyPress(uint16 key, uint16 keycode)
@ -294,7 +294,7 @@ struct IConsoleWindow : Window
case (WKC_META | 'V'):
#endif
case (WKC_CTRL | 'V'):
if (InsertTextBufferClipboard(&_iconsole_cmdline)) {
if (_iconsole_cmdline.InsertClipboard()) {
IConsoleResetHistoryPos();
this->SetDirty();
}
@ -308,19 +308,19 @@ struct IConsoleWindow : Window
case (WKC_META | 'U'):
#endif
case (WKC_CTRL | 'U'):
DeleteTextBufferAll(&_iconsole_cmdline);
_iconsole_cmdline.DeleteAll();
this->SetDirty();
break;
case WKC_BACKSPACE: case WKC_DELETE:
if (DeleteTextBufferChar(&_iconsole_cmdline, keycode)) {
if (_iconsole_cmdline.DeleteChar(keycode)) {
IConsoleResetHistoryPos();
this->SetDirty();
}
break;
case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
if (MoveTextBufferPos(&_iconsole_cmdline, keycode)) {
if (_iconsole_cmdline.MovePos(keycode)) {
IConsoleResetHistoryPos();
this->SetDirty();
}
@ -329,7 +329,7 @@ struct IConsoleWindow : Window
default:
if (IsValidChar(key, CS_ALPHANUMERAL)) {
IConsoleWindow::scroll = 0;
InsertTextBufferChar(&_iconsole_cmdline, key);
_iconsole_cmdline.InsertChar(key);
IConsoleResetHistoryPos();
this->SetDirty();
} else {
@ -460,7 +460,7 @@ static void IConsoleHistoryNavigate(int direction)
} else {
ttd_strlcpy(_iconsole_cmdline.buf, _iconsole_history[_iconsole_historypos], _iconsole_cmdline.max_bytes);
}
UpdateTextBufferSize(&_iconsole_cmdline);
_iconsole_cmdline.UpdateSize();
}
/**

View File

@ -263,7 +263,7 @@ public:
}
this->afilter = CS_ALPHANUMERAL;
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size);
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);
@ -571,7 +571,7 @@ public:
if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
/* Copy clicked name to editbox */
ttd_strlcpy(this->text.buf, file->title, this->text.max_bytes);
UpdateTextBufferSize(&this->text);
this->text.UpdateSize();
this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE);
}
} else if (!_load_check_data.HasErrors()) {
@ -651,7 +651,7 @@ public:
if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
}
UpdateTextBufferSize(&this->text);
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;

View File

@ -318,7 +318,7 @@ struct GenerateLandscapeWindow : public QueryStringBaseWindow {
/* 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);
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size);
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
this->caption = STR_NULL;
this->afilter = CS_NUMERAL;
@ -556,7 +556,7 @@ struct GenerateLandscapeWindow : public QueryStringBaseWindow {
case WID_GL_RANDOM_BUTTON: // Random seed
_settings_newgame.game_creation.generation_seed = InteractiveRandom();
snprintf(this->edit_str_buf, this->edit_str_size, "%u", _settings_newgame.game_creation.generation_seed);
UpdateTextBufferSize(&this->text);
this->text.UpdateSize();
this->SetDirty();
break;

View File

@ -727,28 +727,28 @@ HandleEditBoxResult QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key
case (WKC_META | 'V'):
#endif
case (WKC_CTRL | 'V'):
if (InsertTextBufferClipboard(&this->text)) w->SetWidgetDirty(wid);
if (this->text.InsertClipboard()) w->SetWidgetDirty(wid);
break;
#ifdef WITH_COCOA
case (WKC_META | 'U'):
#endif
case (WKC_CTRL | 'U'):
DeleteTextBufferAll(&this->text);
this->text.DeleteAll();
w->SetWidgetDirty(wid);
break;
case WKC_BACKSPACE: case WKC_DELETE:
if (DeleteTextBufferChar(&this->text, keycode)) w->SetWidgetDirty(wid);
if (this->text.DeleteChar(keycode)) w->SetWidgetDirty(wid);
break;
case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
if (MoveTextBufferPos(&this->text, keycode)) w->SetWidgetDirty(wid);
if (this->text.MovePos(keycode)) w->SetWidgetDirty(wid);
break;
default:
if (IsValidChar(key, this->afilter)) {
if (InsertTextBufferChar(&this->text, key)) w->SetWidgetDirty(wid);
if (this->text.InsertChar(key)) w->SetWidgetDirty(wid);
} else {
state = ES_NOT_HANDLED;
}
@ -759,7 +759,7 @@ HandleEditBoxResult QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key
void QueryString::HandleEditBox(Window *w, int wid)
{
if (HasEditBoxFocus(w, wid) && HandleCaret(&this->text)) {
if (HasEditBoxFocus(w, wid) && this->text.HandleCaret()) {
w->SetWidgetDirty(wid);
/* When we're not the OSK, notify 'our' OSK to redraw the widget,
* so the caret changes appropriately. */
@ -847,7 +847,7 @@ struct QueryStringWindow : public QueryStringBaseWindow
this->caption = caption;
this->afilter = afilter;
this->flags = flags;
InitializeTextBuffer(&this->text, this->edit_str_buf, max_bytes, max_chars);
this->text.Initialize(this->edit_str_buf, max_bytes, max_chars);
this->InitNested(desc, WN_QUERY_STRING);

View File

@ -300,7 +300,7 @@ struct NetworkChatWindow : public QueryStringBaseWindow {
this->dtype = type;
this->dest = dest;
this->afilter = CS_ALPHANUMERAL;
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size);
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
static const StringID chat_captions[] = {
STR_NETWORK_CHAT_ALL_CAPTION,
@ -442,7 +442,7 @@ struct NetworkChatWindow : public QueryStringBaseWindow {
}
/* Update the textbuffer */
UpdateTextBufferSize(&this->text);
this->text.UpdateSize();
this->SetDirty();
free(pre_buf);
@ -456,7 +456,7 @@ struct NetworkChatWindow : public QueryStringBaseWindow {
_chat_tab_completion_active = false;
/* Update the textbuffer */
UpdateTextBufferSize(&this->text);
this->text.UpdateSize();
this->SetDirty();
}

View File

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

View File

@ -447,7 +447,7 @@ public:
ttd_strlcpy(this->edit_str_buf, _settings_client.network.client_name, this->edit_str_size);
this->afilter = CS_ALPHANUMERAL;
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 120);
this->text.Initialize(this->edit_str_buf, this->edit_str_size, 120);
this->SetFocusedWidget(WID_NG_CLIENT);
this->field = WID_NG_CLIENT;
@ -1019,7 +1019,7 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow {
ttd_strlcpy(this->edit_str_buf, _settings_client.network.server_name, this->edit_str_size);
this->afilter = CS_ALPHANUMERAL;
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 160);
this->text.Initialize(this->edit_str_buf, this->edit_str_size, 160);
this->SetFocusedWidget(WID_NSS_GAMENAME);
this->field = WID_NSS_GAMENAME;
@ -2142,7 +2142,7 @@ struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow {
this->parent = parent;
this->afilter = CS_ALPHANUMERAL;
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size);
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
this->SetFocusedWidget(WID_NCP_PASSWORD);
}

View File

@ -637,7 +637,7 @@ 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);
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size);
this->text.Initialize(this->edit_str_buf, this->edit_str_size);
this->SetFocusedWidget(WID_NS_FILTER);
this->avails.SetListing(this->last_sorting);

View File

@ -124,7 +124,7 @@ struct OskWindow : public Window {
if (!IsValidChar(c, this->qs->afilter)) return;
if (InsertTextBufferChar(&this->qs->text, c)) this->InvalidateParent();
if (this->qs->text.InsertChar(c)) this->InvalidateParent();
if (HasBit(_keystate, KEYS_SHIFT)) {
ToggleBit(_keystate, KEYS_SHIFT);
@ -139,7 +139,7 @@ struct OskWindow : public Window {
switch (widget) {
case WID_OSK_BACKSPACE:
if (DeleteTextBufferChar(&this->qs->text, WKC_BACKSPACE)) this->InvalidateParent();
if (this->qs->text.DeleteChar(WKC_BACKSPACE)) this->InvalidateParent();
break;
case WID_OSK_SPECIAL:
@ -163,15 +163,15 @@ struct OskWindow : public Window {
break;
case WID_OSK_SPACE:
if (InsertTextBufferChar(&this->qs->text, ' ')) this->InvalidateParent();
if (this->qs->text.InsertChar(' ')) this->InvalidateParent();
break;
case WID_OSK_LEFT:
if (MoveTextBufferPos(&this->qs->text, WKC_LEFT)) this->InvalidateParent();
if (this->qs->text.MovePos(WKC_LEFT)) this->InvalidateParent();
break;
case WID_OSK_RIGHT:
if (MoveTextBufferPos(&this->qs->text, WKC_RIGHT)) this->InvalidateParent();
if (this->qs->text.MovePos(WKC_RIGHT)) this->InvalidateParent();
break;
case WID_OSK_OK:
@ -193,8 +193,8 @@ struct OskWindow : public Window {
return;
} else { // or reset to original string
strcpy(qs->text.buf, this->orig_str_buf);
UpdateTextBufferSize(&qs->text);
MoveTextBufferPos(&qs->text, WKC_END);
qs->text.UpdateSize();
qs->text.MovePos(WKC_END);
this->InvalidateParent();
delete this;
}

View File

@ -167,7 +167,7 @@ struct SignListWindow : QueryStringBaseWindow, SignList {
/* Initialize the text edit widget */
this->afilter = CS_ALPHANUMERAL;
InitializeTextBuffer(&this->text, this->edit_str_buf, MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS);
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 */
@ -186,7 +186,7 @@ struct SignListWindow : QueryStringBaseWindow, SignList {
void ClearFilterTextWidget()
{
this->edit_str_buf[0] = '\0';
UpdateTextBufferSize(&this->text);
this->text.UpdateSize();
this->SetWidgetDirty(WID_SIL_FILTER_TEXT);
}
@ -500,7 +500,7 @@ struct SignWindow : QueryStringBaseWindow, SignList {
*last_of = '\0';
this->cur_sign = si->index;
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, this->max_chars);
this->text.Initialize(this->edit_str_buf, this->edit_str_size, this->max_chars);
this->SetWidgetDirty(WID_QES_TEXT);
this->SetFocusedWidget(WID_QES_TEXT);

View File

@ -30,42 +30,41 @@ int _caret_timer;
/* Delete a character at the caret position in a text buf.
* If backspace is set, delete the character before the caret,
* else delete the character after it. */
static void DelChar(Textbuf *tb, bool backspace)
void Textbuf::DelChar(bool backspace)
{
WChar c;
char *s = tb->buf + tb->caretpos;
char *s = this->buf + this->caretpos;
if (backspace) s = Utf8PrevChar(s);
uint16 len = (uint16)Utf8Decode(&c, s);
uint width = GetCharacterWidth(FS_NORMAL, c);
tb->pixels -= width;
this->pixels -= width;
if (backspace) {
tb->caretpos -= len;
tb->caretxoffs -= width;
this->caretpos -= len;
this->caretxoffs -= width;
}
/* Move the remaining characters over the marker */
memmove(s, s + len, tb->bytes - (s - tb->buf) - len);
tb->bytes -= len;
tb->chars--;
memmove(s, s + len, this->bytes - (s - this->buf) - len);
this->bytes -= len;
this->chars--;
}
/**
* Delete a character from a textbuffer, either with 'Delete' or 'Backspace'
* The character is delete from the position the caret is at
* @param tb Textbuf type to be changed
* @param delmode Type of deletion, either WKC_BACKSPACE or WKC_DELETE
* @return Return true on successful change of Textbuf, or false otherwise
*/
bool DeleteTextBufferChar(Textbuf *tb, int delmode)
bool Textbuf::DeleteChar(int delmode)
{
if (delmode == WKC_BACKSPACE && tb->caretpos != 0) {
DelChar(tb, true);
if (delmode == WKC_BACKSPACE && this->caretpos != 0) {
this->DelChar(true);
return true;
} else if (delmode == WKC_DELETE && tb->caretpos < tb->bytes - 1) {
DelChar(tb, false);
} else if (delmode == WKC_DELETE && this->caretpos < this->bytes - 1) {
this->DelChar(false);
return true;
}
@ -74,36 +73,34 @@ bool DeleteTextBufferChar(Textbuf *tb, int delmode)
/**
* Delete every character in the textbuffer
* @param tb Textbuf buffer to be emptied
*/
void DeleteTextBufferAll(Textbuf *tb)
void Textbuf::DeleteAll()
{
memset(tb->buf, 0, tb->max_bytes);
tb->bytes = tb->chars = 1;
tb->pixels = tb->caretpos = tb->caretxoffs = 0;
memset(this->buf, 0, this->max_bytes);
this->bytes = this->chars = 1;
this->pixels = this->caretpos = this->caretxoffs = 0;
}
/**
* Insert a character to a textbuffer. If maxwidth of the Textbuf is zero,
* we don't care about the visual-length but only about the physical
* length of the string
* @param tb Textbuf type to be changed
* @param key Character to be inserted
* @return Return true on successful change of Textbuf, or false otherwise
*/
bool InsertTextBufferChar(Textbuf *tb, WChar key)
bool Textbuf::InsertChar(WChar key)
{
const byte charwidth = GetCharacterWidth(FS_NORMAL, key);
uint16 len = (uint16)Utf8CharLen(key);
if (tb->bytes + len <= tb->max_bytes && tb->chars + 1 <= tb->max_chars) {
memmove(tb->buf + tb->caretpos + len, tb->buf + tb->caretpos, tb->bytes - tb->caretpos);
Utf8Encode(tb->buf + tb->caretpos, key);
tb->chars++;
tb->bytes += len;
tb->pixels += charwidth;
if (this->bytes + len <= this->max_bytes && this->chars + 1 <= this->max_chars) {
memmove(this->buf + this->caretpos + len, this->buf + this->caretpos, this->bytes - this->caretpos);
Utf8Encode(this->buf + this->caretpos, key);
this->chars++;
this->bytes += len;
this->pixels += charwidth;
tb->caretpos += len;
tb->caretxoffs += charwidth;
this->caretpos += len;
this->caretxoffs += charwidth;
return true;
}
return false;
@ -113,10 +110,9 @@ bool InsertTextBufferChar(Textbuf *tb, WChar key)
* Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
* and append this up to the maximum length (either absolute or screenlength). If maxlength
* is zero, we don't care about the screenlength but only about the physical length of the string
* @param tb Textbuf type to be changed
* @return true on successful change of Textbuf, or false otherwise
*/
bool InsertTextBufferClipboard(Textbuf *tb)
bool Textbuf::InsertClipboard()
{
char utf8_buf[512];
@ -128,8 +124,8 @@ bool InsertTextBufferClipboard(Textbuf *tb)
if (!IsPrintable(c)) break;
byte len = Utf8CharLen(c);
if (tb->bytes + bytes + len > tb->max_bytes) break;
if (tb->chars + chars + 1 > tb->max_chars) break;
if (this->bytes + bytes + len > this->max_bytes) break;
if (this->chars + chars + 1 > this->max_chars) break;
byte char_pixels = GetCharacterWidth(FS_NORMAL, c);
@ -140,17 +136,17 @@ bool InsertTextBufferClipboard(Textbuf *tb)
if (bytes == 0) return false;
memmove(tb->buf + tb->caretpos + bytes, tb->buf + tb->caretpos, tb->bytes - tb->caretpos);
memcpy(tb->buf + tb->caretpos, utf8_buf, bytes);
tb->pixels += pixels;
tb->caretxoffs += pixels;
memmove(this->buf + this->caretpos + bytes, this->buf + this->caretpos, this->bytes - this->caretpos);
memcpy(this->buf + this->caretpos, utf8_buf, bytes);
this->pixels += pixels;
this->caretxoffs += pixels;
tb->bytes += bytes;
tb->chars += chars;
tb->caretpos += bytes;
assert(tb->bytes <= tb->max_bytes);
assert(tb->chars <= tb->max_chars);
tb->buf[tb->bytes - 1] = '\0'; // terminating zero
this->bytes += bytes;
this->chars += chars;
this->caretpos += bytes;
assert(this->bytes <= this->max_bytes);
assert(this->chars <= this->max_chars);
this->buf[this->bytes - 1] = '\0'; // terminating zero
return true;
}
@ -158,44 +154,43 @@ bool InsertTextBufferClipboard(Textbuf *tb)
/**
* Handle text navigation with arrow keys left/right.
* This defines where the caret will blink and the next characer interaction will occur
* @param tb Textbuf type where navigation occurs
* @param navmode Direction in which navigation occurs WKC_LEFT, WKC_RIGHT, WKC_END, WKC_HOME
* @return Return true on successful change of Textbuf, or false otherwise
*/
bool MoveTextBufferPos(Textbuf *tb, int navmode)
bool Textbuf::MovePos(int navmode)
{
switch (navmode) {
case WKC_LEFT:
if (tb->caretpos != 0) {
if (this->caretpos != 0) {
WChar c;
const char *s = Utf8PrevChar(tb->buf + tb->caretpos);
const char *s = Utf8PrevChar(this->buf + this->caretpos);
Utf8Decode(&c, s);
tb->caretpos = s - tb->buf; // -= (tb->buf + tb->caretpos - s)
tb->caretxoffs -= GetCharacterWidth(FS_NORMAL, c);
this->caretpos = s - this->buf; // -= (this->buf + this->caretpos - s)
this->caretxoffs -= GetCharacterWidth(FS_NORMAL, c);
return true;
}
break;
case WKC_RIGHT:
if (tb->caretpos < tb->bytes - 1) {
if (this->caretpos < this->bytes - 1) {
WChar c;
tb->caretpos += (uint16)Utf8Decode(&c, tb->buf + tb->caretpos);
tb->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
this->caretpos += (uint16)Utf8Decode(&c, this->buf + this->caretpos);
this->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
return true;
}
break;
case WKC_HOME:
tb->caretpos = 0;
tb->caretxoffs = 0;
this->caretpos = 0;
this->caretxoffs = 0;
return true;
case WKC_END:
tb->caretpos = tb->bytes - 1;
tb->caretxoffs = tb->pixels;
this->caretpos = this->bytes - 1;
this->caretxoffs = this->pixels;
return true;
default:
@ -208,74 +203,70 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode)
/**
* Initialize the textbuffer by supplying it the buffer to write into
* and the maximum length of this buffer
* @param tb Textbuf type which is getting initialized
* @param buf the buffer that will be holding the data for input
* @param max_bytes maximum size in bytes, including terminating '\0'
*/
void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 max_bytes)
void Textbuf::Initialize(char *buf, uint16 max_bytes)
{
InitializeTextBuffer(tb, buf, max_bytes, 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 tb Textbuf type which is getting initialized
* @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 InitializeTextBuffer(Textbuf *tb, char *buf, uint16 max_bytes, uint16 max_chars)
void Textbuf::Initialize(char *buf, uint16 max_bytes, uint16 max_chars)
{
assert(max_bytes != 0);
assert(max_chars != 0);
tb->buf = buf;
tb->max_bytes = max_bytes;
tb->max_chars = max_chars;
tb->caret = true;
UpdateTextBufferSize(tb);
this->buf = buf;
this->max_bytes = max_bytes;
this->max_chars = max_chars;
this->caret = true;
this->UpdateSize();
}
/**
* Update Textbuf type with its actual physical character and screenlength
* Get the count of characters in the string as well as the width in pixels.
* Useful when copying in a larger amount of text at once
* @param tb Textbuf type which length is calculated
*/
void UpdateTextBufferSize(Textbuf *tb)
void Textbuf::UpdateSize()
{
const char *buf = tb->buf;
const char *buf = this->buf;
tb->pixels = 0;
tb->chars = tb->bytes = 1; // terminating zero
this->pixels = 0;
this->chars = this->bytes = 1; // terminating zero
WChar c;
while ((c = Utf8Consume(&buf)) != '\0') {
tb->pixels += GetCharacterWidth(FS_NORMAL, c);
tb->bytes += Utf8CharLen(c);
tb->chars++;
this->pixels += GetCharacterWidth(FS_NORMAL, c);
this->bytes += Utf8CharLen(c);
this->chars++;
}
assert(tb->bytes <= tb->max_bytes);
assert(tb->chars <= tb->max_chars);
assert(this->bytes <= this->max_bytes);
assert(this->chars <= this->max_chars);
tb->caretpos = tb->bytes - 1;
tb->caretxoffs = tb->pixels;
this->caretpos = this->bytes - 1;
this->caretxoffs = this->pixels;
}
/**
* Handle the flashing of the caret.
* @param tb The text buffer to handle the caret of.
* @return True if the caret state changes.
*/
bool HandleCaret(Textbuf *tb)
bool Textbuf::HandleCaret()
{
/* caret changed? */
bool b = !!(_caret_timer & 0x20);
if (b != tb->caret) {
tb->caret = b;
if (b != this->caret) {
this->caret = b;
return true;
}
return false;

View File

@ -23,17 +23,21 @@ struct Textbuf {
bool caret; ///< is the caret ("_") visible or not
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);
void DeleteAll();
bool DeleteChar(int delmode);
bool InsertChar(uint32 key);
bool InsertClipboard();
bool MovePos(int navmode);
bool HandleCaret();
void UpdateSize();
private:
void DelChar(bool backspace);
};
bool HandleCaret(Textbuf *tb);
void DeleteTextBufferAll(Textbuf *tb);
bool DeleteTextBufferChar(Textbuf *tb, int delmode);
bool InsertTextBufferChar(Textbuf *tb, uint32 key);
bool InsertTextBufferClipboard(Textbuf *tb);
bool MoveTextBufferPos(Textbuf *tb, int navmode);
void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 max_bytes);
void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 max_bytes, uint16 max_chars);
void UpdateTextBufferSize(Textbuf *tb);
#endif /* TEXTBUF_TYPE_H */

View File

@ -995,7 +995,7 @@ public:
params(_settings_game.game_creation.town_name)
{
this->InitNested(desc, window_number);
InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, this->max_chars);
this->text.Initialize(this->edit_str_buf, this->edit_str_size, this->max_chars);
this->RandomTownName();
this->UpdateButtons(true);
}
@ -1009,7 +1009,7 @@ public:
} else {
GetTownName(this->edit_str_buf, &this->params, this->townnameparts, &this->edit_str_buf[this->edit_str_size - 1]);
}
UpdateTextBufferSize(&this->text);
this->text.UpdateSize();
UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX);