diff options
author | zuu <zuu@openttd.org> | 2012-09-10 18:42:34 +0000 |
---|---|---|
committer | zuu <zuu@openttd.org> | 2012-09-10 18:42:34 +0000 |
commit | bacad1478a9412c5a056c13eacf1328a960fec0f (patch) | |
tree | b5f6d6f775434315c8d099db425aba39202b9848 /src | |
parent | ae28432e623d55fd495f0f456045c2509f1c8383 (diff) | |
download | openttd-bacad1478a9412c5a056c13eacf1328a960fec0f.tar.xz |
(svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
Diffstat (limited to 'src')
-rw-r--r-- | src/textbuf.cpp | 35 | ||||
-rw-r--r-- | src/textbuf_type.h | 1 |
2 files changed, 27 insertions, 9 deletions
diff --git a/src/textbuf.cpp b/src/textbuf.cpp index 90fd76266..673e77e49 100644 --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -27,11 +27,27 @@ bool GetClipboardContents(char *buffer, size_t buff_len); 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. */ +/** + * Checks if it is possible to delete a character. + * @param backspace if set, delete the character before the caret, + * otherwise, delete the character after it. + * @return true if a character can be deleted in the given direction. + */ +bool Textbuf::CanDelChar(bool backspace) +{ + return backspace ? this->caretpos != 0 : this->caretpos < this->bytes - 1; +} + +/** + * Delete a character at the caret position in a text buf. + * @param backspace if set, delete the character before the caret, + * else delete the character after it. + * @warning You should ensure Textbuf::CanDelChar returns true before calling this function. + */ void Textbuf::DelChar(bool backspace) { + assert(this->CanDelChar(backspace)); + WChar c; char *s = this->buf + this->caretpos; @@ -60,12 +76,13 @@ void Textbuf::DelChar(bool backspace) */ bool Textbuf::DeleteChar(int delmode) { - if (delmode == WKC_BACKSPACE && this->caretpos != 0) { - this->DelChar(true); - return true; - } else if (delmode == WKC_DELETE && this->caretpos < this->bytes - 1) { - this->DelChar(false); - return true; + if (delmode == WKC_BACKSPACE || delmode == WKC_DELETE) { + bool backspace = delmode == WKC_BACKSPACE; + if (CanDelChar(backspace)) { + this->DelChar(backspace); + return true; + } + return false; } return false; diff --git a/src/textbuf_type.h b/src/textbuf_type.h index 194c241ad..1a404ad58 100644 --- a/src/textbuf_type.h +++ b/src/textbuf_type.h @@ -39,6 +39,7 @@ struct Textbuf { void UpdateSize(); private: + bool CanDelChar(bool backspace); void DelChar(bool backspace); bool CanMoveCaretLeft(); WChar MoveCaretLeft(); |