From d58eee1e7945098f32e2d4419506ce1a81f3e4d6 Mon Sep 17 00:00:00 2001 From: frosch Date: Mon, 4 Jun 2012 15:30:29 +0000 Subject: [PATCH] (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions. --- src/ai/ai_gui.cpp | 4 +- src/console_gui.cpp | 14 +-- src/fios_gui.cpp | 6 +- src/genworld_gui.cpp | 4 +- src/misc_gui.cpp | 14 +-- src/network/network_chat_gui.cpp | 6 +- src/network/network_content_gui.cpp | 2 +- src/network/network_gui.cpp | 6 +- src/newgrf_gui.cpp | 2 +- src/osk_gui.cpp | 14 +-- src/signs_gui.cpp | 6 +- src/textbuf.cpp | 155 +++++++++++++--------------- src/textbuf_type.h | 26 +++-- src/town_gui.cpp | 4 +- 14 files changed, 129 insertions(+), 134 deletions(-) diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index 245057ae84..96b5fdfe87 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -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) { diff --git a/src/console_gui.cpp b/src/console_gui.cpp index 8d6e974929..ac7df20206 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -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(); } /** diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 5ae42897fb..5266d6fc35 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -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(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; diff --git a/src/genworld_gui.cpp b/src/genworld_gui.cpp index 3ce473e97f..3dfff97db6 100644 --- a/src/genworld_gui.cpp +++ b/src/genworld_gui.cpp @@ -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; diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 9ff0c3dea5..bd7832c39b 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -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); diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp index 5709386e32..9709bf2634 100644 --- a/src/network/network_chat_gui.cpp +++ b/src/network/network_chat_gui.cpp @@ -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(); } diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index 5b1d97329f..f3ebfc4f4e 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -371,7 +371,7 @@ public: this->GetWidget(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); diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 151776d749..b5e04d8283 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -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); } diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 6df352324f..a62003c8df 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -637,7 +637,7 @@ struct NewGRFWindow : public QueryStringBaseWindow, NewGRFScanCallback { this->GetWidget(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); diff --git a/src/osk_gui.cpp b/src/osk_gui.cpp index 792f1161f7..9724a6c461 100644 --- a/src/osk_gui.cpp +++ b/src/osk_gui.cpp @@ -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; } diff --git a/src/signs_gui.cpp b/src/signs_gui.cpp index 91004f7ae9..55a5271368 100644 --- a/src/signs_gui.cpp +++ b/src/signs_gui.cpp @@ -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); diff --git a/src/textbuf.cpp b/src/textbuf.cpp index 49bc78e241..a7e1d83d8f 100644 --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -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; diff --git a/src/textbuf_type.h b/src/textbuf_type.h index 7176302305..0c4208f530 100644 --- a/src/textbuf_type.h +++ b/src/textbuf_type.h @@ -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 */ diff --git a/src/town_gui.cpp b/src/town_gui.cpp index bd8d440875..6a6a32b3f1 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -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);