Author: zooey Date: 2009-11-04 19:49:15 +0100 (Wed, 04 Nov 2009) New Revision: 33885 Changeset: http://dev.haiku-os.org/changeset/33885/haiku Modified: haiku/trunk/headers/os/interface/TextView.h haiku/trunk/src/kits/interface/TextView.cpp Log: * introduced two new private methods, _PreviousWordStart() and _NextWordStart(), which are now being used to implement the word-wise keyboard navigation, fixing #4785 Modified: haiku/trunk/headers/os/interface/TextView.h =================================================================== --- haiku/trunk/headers/os/interface/TextView.h 2009-11-04 16:50:01 UTC (rev 33884) +++ haiku/trunk/headers/os/interface/TextView.h 2009-11-04 18:49:15 UTC (rev 33885) @@ -384,6 +384,9 @@ 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, Modified: haiku/trunk/src/kits/interface/TextView.cpp =================================================================== --- haiku/trunk/src/kits/interface/TextView.cpp 2009-11-04 16:50:01 UTC (rev 33884) +++ haiku/trunk/src/kits/interface/TextView.cpp 2009-11-04 18:49:15 UTC (rev 33885) @@ -3289,7 +3289,7 @@ else { fCaretOffset = ctrlDown - ? _PreviousWordBoundary(fCaretOffset - 1) + ? _PreviousWordStart(fCaretOffset - 1) : _PreviousInitialByte(fCaretOffset); if (shiftDown && fCaretOffset != lastClickOffset) { if (fCaretOffset < fSelStart) { @@ -3315,7 +3315,7 @@ else { fCaretOffset = ctrlDown - ? _NextWordBoundary(fCaretOffset) + ? _NextWordStart(fCaretOffset) : _NextInitialByte(fCaretOffset); if (shiftDown && fCaretOffset != lastClickOffset) { if (fCaretOffset > fSelEnd) { @@ -4061,6 +4061,59 @@ } +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. */