From a85a827cd0df535159856c3f31d187d5f2ae952e Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Wed, 4 Nov 2009 18:49:15 +0000 Subject: [PATCH] * introduced two new private methods, _PreviousWordStart() and _NextWordStart(), which are now being used to implement the word-wise keyboard navigation, fixing #4785 git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33885 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/interface/TextView.h | 3 ++ src/kits/interface/TextView.cpp | 57 +++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/headers/os/interface/TextView.h b/headers/os/interface/TextView.h index 803b18d5af..039f28be71 100644 --- a/headers/os/interface/TextView.h +++ b/headers/os/interface/TextView.h @@ -384,6 +384,9 @@ private: int32 _PreviousWordBoundary(int32 offset); int32 _NextWordBoundary(int32 offset); + int32 _PreviousWordStart(int32 offset); + int32 _NextWordStart(int32 offset); + bool _GetProperty(BMessage* specifier, int32 form, const char* property, BMessage* reply); bool _SetProperty(BMessage* specifier, int32 form, diff --git a/src/kits/interface/TextView.cpp b/src/kits/interface/TextView.cpp index d1768e0389..34f8b319dc 100644 --- a/src/kits/interface/TextView.cpp +++ b/src/kits/interface/TextView.cpp @@ -3289,7 +3289,7 @@ BTextView::_HandleArrowKey(uint32 inArrowKey) else { fCaretOffset = ctrlDown - ? _PreviousWordBoundary(fCaretOffset - 1) + ? _PreviousWordStart(fCaretOffset - 1) : _PreviousInitialByte(fCaretOffset); if (shiftDown && fCaretOffset != lastClickOffset) { if (fCaretOffset < fSelStart) { @@ -3315,7 +3315,7 @@ BTextView::_HandleArrowKey(uint32 inArrowKey) else { fCaretOffset = ctrlDown - ? _NextWordBoundary(fCaretOffset) + ? _NextWordStart(fCaretOffset) : _NextInitialByte(fCaretOffset); if (shiftDown && fCaretOffset != lastClickOffset) { if (fCaretOffset > fSelEnd) { @@ -4061,6 +4061,59 @@ BTextView::_NextWordBoundary(int32 offset) } +int32 +BTextView::_PreviousWordStart(int32 offset) +{ + if (offset <= 1) + return 0; + + --offset; // need to look at previous char + if (_CharClassification(offset) == CHAR_CLASS_WHITESPACE) { + // skip whitespace + while (offset > 0) { + offset = _PreviousInitialByte(offset); + if (_CharClassification(offset) != CHAR_CLASS_WHITESPACE) + break; + } + } + while (offset > 0) { + // find preceeding whitespace char. + int32 previous = _PreviousInitialByte(offset); + if (_CharClassification(previous) == CHAR_CLASS_WHITESPACE) + break; + offset = previous; + } + + return offset; +} + + +int32 +BTextView::_NextWordStart(int32 offset) +{ + int32 textLen = TextLength(); + if (offset >= textLen) + return textLen; + + if (_CharClassification(offset) != CHAR_CLASS_WHITESPACE) { + // skip until the next whitespace + while (offset < textLen) { + offset = _NextInitialByte(offset); + if (_CharClassification(offset) == CHAR_CLASS_WHITESPACE) + break; + } + } + while (offset < textLen) { + // find next non-white char + offset = _NextInitialByte(offset); + if (_CharClassification(offset) != CHAR_CLASS_WHITESPACE) + break; + } + + return offset; +} + + /*! \brief Returns the width used by the characters starting at the given offset with the given length, expanding all tab characters as needed. */