# HG changeset patch # Parent 0b91d3d1097449812489975d689a2ad0216b5123 # User sbr # Date 1346747440 -7200 Add Ctrl+left and Ctrl+right word by word caret moves to input fields. (r24510) diff -r 0b91d3d10974 src/misc_gui.cpp --- a/src/misc_gui.cpp Mon Sep 03 17:45:12 2012 +0000 +++ b/src/misc_gui.cpp Tue Sep 04 10:48:49 2012 +0200 @@ -744,6 +744,7 @@ break; case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME: + case WKC_CTRL | WKC_LEFT: case WKC_CTRL | WKC_RIGHT: if (this->text.MovePos(keycode)) w->SetWidgetDirty(wid); break; diff -r 0b91d3d10974 src/textbuf.cpp --- a/src/textbuf.cpp Mon Sep 03 17:45:12 2012 +0000 +++ b/src/textbuf.cpp Tue Sep 04 10:48:49 2012 +0200 @@ -152,37 +152,116 @@ } /** + * Returns true if the caret can be moved to the left, false otherwise. + */ +bool Textbuf::CanMoveCaretLeft() +{ + return this->caretpos != 0; +} + +/** + * Moves the caret to the left. + * @warning You should ensure Textbuf::CanMoveCaretLeft returns true before calling this function. + * @return The character under the caret. + */ +WChar Textbuf::MoveCaretLeft() +{ + assert(this->CanMoveCaretLeft()); + + WChar c; + const char *s = Utf8PrevChar(this->buf + this->caretpos); + Utf8Decode(&c, s); + this->caretpos = s - this->buf; // -= (this->buf + this->caretpos - s) + this->caretxoffs -= GetCharacterWidth(FS_NORMAL, c); + + return c; +} + +/** + * Returns true if the caret can be moved to the right, false otherwise. + */ +bool Textbuf::CanMoveCaretRight() +{ + return this->caretpos < this->bytes - 1; +} + +/** + * Moves the caret to the right. + * @warning You should ensure Textbuf::CanMoveCaretRight returns true before calling this function. + * @return The character under the caret. + */ +WChar Textbuf::MoveCaretRight() +{ + assert(this->CanMoveCaretRight()); + + WChar c; + this->caretpos += (uint16)Utf8Decode(&c, this->buf + this->caretpos); + this->caretxoffs += GetCharacterWidth(FS_NORMAL, c); + + return c; +} + +/** * Handle text navigation with arrow keys left/right. * This defines where the caret will blink and the next characer interaction will occur - * @param navmode Direction in which navigation occurs WKC_LEFT, WKC_RIGHT, WKC_END, WKC_HOME + * @param navmode Direction in which navigation occurs (WKC_CTRL |) WKC_LEFT, (WKC_CTRL |) WKC_RIGHT, WKC_END, WKC_HOME * @return Return true on successful change of Textbuf, or false otherwise */ bool Textbuf::MovePos(int navmode) { switch (navmode) { case WKC_LEFT: - if (this->caretpos != 0) { - WChar c; - const char *s = Utf8PrevChar(this->buf + this->caretpos); - Utf8Decode(&c, s); - this->caretpos = s - this->buf; // -= (this->buf + this->caretpos - s) - this->caretxoffs -= GetCharacterWidth(FS_NORMAL, c); - + if (this->CanMoveCaretLeft()) { + this->MoveCaretLeft(); return true; } break; + case WKC_CTRL | WKC_LEFT: { + if (!this->CanMoveCaretLeft()) break; + + WChar c; + /* Consume left whitespaces. */ + do { + c = this->MoveCaretLeft(); + if (!this->CanMoveCaretLeft()) return true; + } while (IsWhitespace(c)); + /* Consume left word. */ + do { + c = this->MoveCaretLeft(); + if (!this->CanMoveCaretLeft()) return true; + } while (!IsWhitespace(c)); + /* Replace caret at the begining of the left word. */ + this->MoveCaretRight(); + return true; + } + case WKC_RIGHT: - if (this->caretpos < this->bytes - 1) { - WChar c; - - this->caretpos += (uint16)Utf8Decode(&c, this->buf + this->caretpos); - this->caretxoffs += GetCharacterWidth(FS_NORMAL, c); - + if (this->CanMoveCaretRight()) { + this->MoveCaretRight(); return true; } break; + case WKC_CTRL | WKC_RIGHT: { + if (!this->CanMoveCaretRight()) break; + + WChar c; + /* Consume right word. */ + do { + c = this->MoveCaretRight(); + if (!this->CanMoveCaretRight()) return true; + } while (!IsWhitespace(c)); + /* Consume right whitespaces. */ + do { + c = this->MoveCaretRight(); + if (!this->CanMoveCaretRight()) return true; + } while (IsWhitespace(c)); + /* Replace caret at the begining of the right word. */ + this->MoveCaretLeft(); + return true; + } + case WKC_HOME: this->caretpos = 0; this->caretxoffs = 0; diff -r 0b91d3d10974 src/textbuf_type.h --- a/src/textbuf_type.h Mon Sep 03 17:45:12 2012 +0000 +++ b/src/textbuf_type.h Tue Sep 04 10:48:49 2012 +0200 @@ -12,6 +12,8 @@ #ifndef TEXTBUF_TYPE_H #define TEXTBUF_TYPE_H +#include "string_type.h" + /** Helper/buffer for input fields. */ struct Textbuf { char *buf; ///< buffer in which text is saved @@ -38,6 +40,10 @@ private: void DelChar(bool backspace); + bool CanMoveCaretLeft(); + WChar MoveCaretLeft(); + bool CanMoveCaretRight(); + WChar MoveCaretRight(); }; #endif /* TEXTBUF_TYPE_H */