summaryrefslogtreecommitdiff
path: root/src/textbuf.cpp
diff options
context:
space:
mode:
authormichi_cc <michi_cc@openttd.org>2013-08-05 20:35:31 +0000
committermichi_cc <michi_cc@openttd.org>2013-08-05 20:35:31 +0000
commit76367f6bf1b5b459c9a15faa0cc0ea1dab191c6f (patch)
treeda45ccb0547732c9ff21f670971d5e7979c38f8c /src/textbuf.cpp
parente7dc14b25af4b2802a956dd1cd99c187fb4acb56 (diff)
downloadopenttd-76367f6bf1b5b459c9a15faa0cc0ea1dab191c6f.tar.xz
(svn r25653) -Add: Caret movement by words for CJK languages.
Diffstat (limited to 'src/textbuf.cpp')
-rw-r--r--src/textbuf.cpp120
1 files changed, 18 insertions, 102 deletions
diff --git a/src/textbuf.cpp b/src/textbuf.cpp
index 6ea042244..9a307058f 100644
--- a/src/textbuf.cpp
+++ b/src/textbuf.cpp
@@ -219,70 +219,12 @@ bool Textbuf::InsertClipboard()
return true;
}
-/**
- * Checks if it is possible to move caret to the left
- * @return true if the caret can be moved to the left, otherwise false.
- */
-bool Textbuf::CanMoveCaretLeft()
-{
- return this->caretpos != 0;
-}
-
-/**
- * Moves the caret to the left.
- * @pre Ensure that Textbuf::CanMoveCaretLeft returns true
- * @return The character under the caret.
- */
-WChar Textbuf::MoveCaretLeft()
-{
- assert(this->CanMoveCaretLeft());
-
- size_t pos = this->char_iter->Prev();
- if (pos == StringIterator::END) pos = 0;
-
- this->caretpos = (uint16)pos;
- this->UpdateCaretPosition();
-
- WChar c;
- Utf8Decode(&c, this->buf + this->caretpos);
-
- return c;
-}
-
-/**
- * Checks if it is possible to move caret to the right
- * @return true if the caret can be moved to the right, otherwise false.
- */
-bool Textbuf::CanMoveCaretRight()
-{
- return this->caretpos < this->bytes - 1;
-}
-
-/**
- * Moves the caret to the right.
- * @pre Ensure that Textbuf::CanMoveCaretRight returns true
- * @return The character under the caret.
- */
-WChar Textbuf::MoveCaretRight()
-{
- assert(this->CanMoveCaretRight());
-
- size_t pos = this->char_iter->Next();
- if (pos == StringIterator::END) pos = this->bytes - 1;
-
- this->caretpos = (uint16)pos;
- this->UpdateCaretPosition();
-
- WChar c;
- Utf8Decode(&c, this->buf + this->caretpos);
- return c;
-}
-
/** Update the character iter after the text has changed. */
void Textbuf::UpdateStringIter()
{
this->char_iter->SetString(this->buf);
- this->caretpos = (uint16)this->char_iter->SetCurPosition(this->caretpos);
+ size_t pos = this->char_iter->SetCurPosition(this->caretpos);
+ this->caretpos = pos == StringIterator::END ? 0 : (uint16)pos;
}
/** Update pixel width of the text. */
@@ -307,64 +249,38 @@ bool Textbuf::MovePos(uint16 keycode)
{
switch (keycode) {
case WKC_LEFT:
- if (this->CanMoveCaretLeft()) {
- this->MoveCaretLeft();
- return true;
- }
- break;
-
case WKC_CTRL | WKC_LEFT: {
- if (!this->CanMoveCaretLeft()) break;
-
- /* Unconditionally move one char to the left. */
- WChar c = this->MoveCaretLeft();
- /* Consume left whitespaces. */
- while (IsWhitespace(c)) {
- if (!this->CanMoveCaretLeft()) return true;
- c = this->MoveCaretLeft();
- }
- /* Consume left word. */
- while (!IsWhitespace(c)) {
- if (!this->CanMoveCaretLeft()) return true;
- c = this->MoveCaretLeft();
- }
- /* Place caret at the beginning of the left word. */
- this->MoveCaretRight();
+ if (this->caretpos == 0) break;
+
+ size_t pos = this->char_iter->Prev(keycode & WKC_CTRL ? StringIterator::ITER_WORD : StringIterator::ITER_CHARACTER);
+ if (pos == StringIterator::END) return true;
+
+ this->caretpos = (uint16)pos;
+ this->UpdateCaretPosition();
return true;
}
case WKC_RIGHT:
- if (this->CanMoveCaretRight()) {
- this->MoveCaretRight();
- return true;
- }
- break;
-
case WKC_CTRL | WKC_RIGHT: {
- if (!this->CanMoveCaretRight()) break;
-
- /* Unconditionally move one char to the right. */
- WChar c = this->MoveCaretRight();
- /* Continue to consume current word. */
- while (!IsWhitespace(c)) {
- if (!this->CanMoveCaretRight()) return true;
- c = this->MoveCaretRight();
- }
- /* Consume right whitespaces. */
- while (IsWhitespace(c)) {
- if (!this->CanMoveCaretRight()) return true;
- c = this->MoveCaretRight();
- }
+ if (this->caretpos >= this->bytes - 1) break;
+
+ size_t pos = this->char_iter->Next(keycode & WKC_CTRL ? StringIterator::ITER_WORD : StringIterator::ITER_CHARACTER);
+ if (pos == StringIterator::END) return true;
+
+ this->caretpos = (uint16)pos;
+ this->UpdateCaretPosition();
return true;
}
case WKC_HOME:
this->caretpos = 0;
+ this->char_iter->SetCurPosition(this->caretpos);
this->UpdateCaretPosition();
return true;
case WKC_END:
this->caretpos = this->bytes - 1;
+ this->char_iter->SetCurPosition(this->caretpos);
this->UpdateCaretPosition();
return true;