[haiku-commits] haiku: hrev46701 - src/apps/haiku-depot/textview

  • From: superstippi@xxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sat, 18 Jan 2014 10:34:52 +0100 (CET)

hrev46701 adds 4 changesets to branch 'master'
old head: 8ed16a6959783a6aeb982671647f5a8fa30fe4d5
new head: 48ee828ae393a1e7ecf431b099d9750787a49fb0
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=48ee828+%5E8ed16a6

----------------------------------------------------------------------------

e97d909: ParagraphLayout: Added more utility methods
  
  * FirtOffsetOnLine(), LastOffsetOnLine(), and GetLineBounds()

00d04bc: TextDocumentLayout: Added more utility methods
  
  * CountLines(), FirstOffsetOnLine(), LastOffsetOnLine(), GetLineBounds().
  * Common code for finding a line in _ParagraphLayoutIndexForLineIndex()

1bce247: Fleshed out more TextEditor functionality.
  
  TextDocumentView now always has a TextEditor, but editing can be disabled.
  The selection code lives only in TextEditor. Implemented more cursor
  navigation code.

48ee828: HaikuDepot: Disable editing for read-only text views.

                                      [ Stephan Aßmus <superstippi@xxxxxx> ]

----------------------------------------------------------------------------

9 files changed, 387 insertions(+), 166 deletions(-)
src/apps/haiku-depot/PackageInfoView.cpp         |   1 +
.../haiku-depot/textview/ParagraphLayout.cpp     |  67 ++++++
src/apps/haiku-depot/textview/ParagraphLayout.h  |   6 +
.../haiku-depot/textview/TextDocumentLayout.cpp  |  83 +++++++
.../haiku-depot/textview/TextDocumentLayout.h    |   9 +
.../haiku-depot/textview/TextDocumentView.cpp    | 102 +++-----
src/apps/haiku-depot/textview/TextDocumentView.h |  13 +-
src/apps/haiku-depot/textview/TextEditor.cpp     | 234 +++++++++++++------
src/apps/haiku-depot/textview/TextEditor.h       |  38 +--

############################################################################

Commit:      e97d9097da30e412eaea5a55e6bece21cefc7c68
URL:         http://cgit.haiku-os.org/haiku/commit/?id=e97d909
Author:      Stephan Aßmus <superstippi@xxxxxx>
Date:        Sat Jan 18 09:28:48 2014 UTC

ParagraphLayout: Added more utility methods

* FirtOffsetOnLine(), LastOffsetOnLine(), and GetLineBounds()

----------------------------------------------------------------------------

diff --git a/src/apps/haiku-depot/textview/ParagraphLayout.cpp 
b/src/apps/haiku-depot/textview/ParagraphLayout.cpp
index 33eccf3..fe9ccc6 100644
--- a/src/apps/haiku-depot/textview/ParagraphLayout.cpp
+++ b/src/apps/haiku-depot/textview/ParagraphLayout.cpp
@@ -307,6 +307,73 @@ ParagraphLayout::LineIndexForOffset(int32 textOffset)
 }
 
 
+int32
+ParagraphLayout::FirstOffsetOnLine(int32 lineIndex)
+{
+       _ValidateLayout();
+
+       if (lineIndex < 0)
+               lineIndex = 0;
+       if (lineIndex > fLineInfos.CountItems())
+               lineIndex = fLineInfos.CountItems() - 1;
+
+       return fLineInfos.ItemAt(lineIndex).textOffset;
+}
+
+
+int32
+ParagraphLayout::LastOffsetOnLine(int32 lineIndex)
+{
+       _ValidateLayout();
+
+       if (lineIndex < 0)
+               lineIndex = 0;
+       if (lineIndex >= fLineInfos.CountItems() - 1)
+               return CountGlyphs();
+
+       return fLineInfos.ItemAt(lineIndex + 1).textOffset - 1;
+}
+
+
+void
+ParagraphLayout::GetLineBounds(int32 lineIndex, float& x1, float& y1,
+       float& x2, float& y2)
+{
+       _ValidateLayout();
+
+       if (fGlyphInfos.CountItems() == 0) {
+               x1 = 0.0f;
+               y1 = 0.0f;
+               x2 = 0.0f;
+               y2 = 0.0f;
+
+               return;
+       }
+
+       if (lineIndex < 0)
+               lineIndex = 0;
+       if (lineIndex > fLineInfos.CountItems())
+               lineIndex = fLineInfos.CountItems() - 1;
+
+       const LineInfo& lineInfo = fLineInfos.ItemAt(lineIndex);
+       int32 firstGlyphIndex = lineInfo.textOffset;
+
+       int32 lastGlyphIndex;
+       if (lineIndex < fLineInfos.CountItems() - 1)
+               lastGlyphIndex = fLineInfos.ItemAt(lineIndex + 1).textOffset - 
1;
+       else
+               lastGlyphIndex = fGlyphInfos.CountItems() - 1;
+
+       const GlyphInfo& firstInfo = fGlyphInfos.ItemAtFast(firstGlyphIndex);
+       const GlyphInfo& lastInfo = fGlyphInfos.ItemAtFast(lastGlyphIndex);
+
+       x1 = firstInfo.x;
+       y1 = lineInfo.y;
+       x2 = lastInfo.x + lastInfo.width;
+       y1 = lineInfo.y + lineInfo.height;
+}
+
+
 void
 ParagraphLayout::GetTextBounds(int32 textOffset, float& x1, float& y1,
        float& x2, float& y2)
diff --git a/src/apps/haiku-depot/textview/ParagraphLayout.h 
b/src/apps/haiku-depot/textview/ParagraphLayout.h
index 8e622ce..99bf5b7 100644
--- a/src/apps/haiku-depot/textview/ParagraphLayout.h
+++ b/src/apps/haiku-depot/textview/ParagraphLayout.h
@@ -193,6 +193,12 @@ public:
                        int32                           CountLines();
 
                        int32                           
LineIndexForOffset(int32 textOffset);
+                       int32                           FirstOffsetOnLine(int32 
lineIndex);
+                       int32                           LastOffsetOnLine(int32 
lineIndex);
+
+                       void                            GetLineBounds(int32 
lineIndex,
+                                                                       float& 
x1, float& y1,
+                                                                       float& 
x2, float& y2);
 
                        void                            GetTextBounds(int32 
textOffset,
                                                                        float& 
x1, float& y1,

############################################################################

Commit:      00d04bc7f24e130e0559470729c924c723d8a0da
URL:         http://cgit.haiku-os.org/haiku/commit/?id=00d04bc
Author:      Stephan Aßmus <superstippi@xxxxxx>
Date:        Sat Jan 18 09:30:05 2014 UTC

TextDocumentLayout: Added more utility methods

* CountLines(), FirstOffsetOnLine(), LastOffsetOnLine(), GetLineBounds().
* Common code for finding a line in _ParagraphLayoutIndexForLineIndex()

----------------------------------------------------------------------------

diff --git a/src/apps/haiku-depot/textview/TextDocumentLayout.cpp 
b/src/apps/haiku-depot/textview/TextDocumentLayout.cpp
index 2b1806b..afea2f6 100644
--- a/src/apps/haiku-depot/textview/TextDocumentLayout.cpp
+++ b/src/apps/haiku-depot/textview/TextDocumentLayout.cpp
@@ -123,6 +123,69 @@ TextDocumentLayout::LineIndexForOffset(int32 textOffset)
 }
 
 
+int32
+TextDocumentLayout::FirstOffsetOnLine(int32 lineIndex)
+{
+       int32 index = _ParagraphLayoutIndexForLineIndex(lineIndex);
+       if (index >= 0) {
+               const ParagraphLayoutInfo& info = 
fParagraphLayouts.ItemAtFast(index);
+               return info.layout->FirstOffsetOnLine(lineIndex);
+       }
+
+       return 0;
+}
+
+
+int32
+TextDocumentLayout::LastOffsetOnLine(int32 lineIndex)
+{
+       int32 index = _ParagraphLayoutIndexForLineIndex(lineIndex);
+       if (index >= 0) {
+               const ParagraphLayoutInfo& info = 
fParagraphLayouts.ItemAtFast(index);
+               return info.layout->LastOffsetOnLine(lineIndex);
+       }
+
+       return 0;
+}
+
+
+int32
+TextDocumentLayout::CountLines()
+{
+       _ValidateLayout();
+
+       int32 lineCount = 0;
+
+       int32 count = fParagraphLayouts.CountItems();
+       for (int32 i = 0; i < count; i++) {
+               const ParagraphLayoutInfo& info = 
fParagraphLayouts.ItemAtFast(i);
+               lineCount += info.layout->CountLines();
+       }
+
+       return lineCount;
+}
+
+
+void
+TextDocumentLayout::GetLineBounds(int32 lineIndex, float& x1, float& y1,
+       float& x2, float& y2)
+{
+       int32 index = _ParagraphLayoutIndexForLineIndex(lineIndex);
+       if (index >= 0) {
+               const ParagraphLayoutInfo& info = 
fParagraphLayouts.ItemAtFast(index);
+               info.layout->GetLineBounds(lineIndex, x1, y1, x2, y2);
+               y1 += info.y;
+               y2 += info.y;
+               return;
+       }
+
+       x1 = 0.0f;
+       y1 = 0.0f;
+       x2 = 0.0f;
+       y2 = 0.0f;
+}
+
+
 void
 TextDocumentLayout::GetTextBounds(int32 textOffset, float& x1, float& y1,
        float& x2, float& y2)
@@ -242,3 +305,23 @@ TextDocumentLayout::_ParagraphLayoutIndexForOffset(int32& 
textOffset)
        return -1;
 }
 
+int32
+TextDocumentLayout::_ParagraphLayoutIndexForLineIndex(int32& lineIndex)
+{
+       _ValidateLayout();
+
+       int32 paragraphs = fParagraphLayouts.CountItems();
+       for (int32 i = 0; i < paragraphs; i++) {
+               const ParagraphLayoutInfo& info = 
fParagraphLayouts.ItemAtFast(i);
+
+               int32 lineCount = info.layout->CountLines();
+               if (lineIndex > lineCount) {
+                       lineIndex -= lineCount;
+                       continue;
+               }
+               
+               return i;
+       }
+
+       return -1;
+}
diff --git a/src/apps/haiku-depot/textview/TextDocumentLayout.h 
b/src/apps/haiku-depot/textview/TextDocumentLayout.h
index 676b10c..8d43958 100644
--- a/src/apps/haiku-depot/textview/TextDocumentLayout.h
+++ b/src/apps/haiku-depot/textview/TextDocumentLayout.h
@@ -87,6 +87,13 @@ public:
                                                                        const 
BRect& updateRect);
 
                        int32                           
LineIndexForOffset(int32 textOffset);
+                       int32                           FirstOffsetOnLine(int32 
lineIndex);
+                       int32                           LastOffsetOnLine(int32 
lineIndex);
+                       int32                           CountLines();
+
+                       void                            GetLineBounds(int32 
lineIndex,
+                                                                       float& 
x1, float& y1,
+                                                                       float& 
x2, float& y2);
 
                        void                            GetTextBounds(int32 
textOffset,
                                                                        float& 
x1, float& y1,
@@ -105,6 +112,8 @@ private:
 
                        int32                           
_ParagraphLayoutIndexForOffset(
                                                                        int32& 
textOffset);
+                       int32                           
_ParagraphLayoutIndexForLineIndex(
+                                                                       int32& 
lineIndex);
 
 private:
                        float                           fWidth;

############################################################################

Commit:      1bce247b22f95e60cfcf844264aa820998088e5d
URL:         http://cgit.haiku-os.org/haiku/commit/?id=1bce247
Author:      Stephan Aßmus <superstippi@xxxxxx>
Date:        Sat Jan 18 09:32:14 2014 UTC

Fleshed out more TextEditor functionality.

TextDocumentView now always has a TextEditor, but editing can be disabled.
The selection code lives only in TextEditor. Implemented more cursor
navigation code.

----------------------------------------------------------------------------

diff --git a/src/apps/haiku-depot/textview/TextDocumentView.cpp 
b/src/apps/haiku-depot/textview/TextDocumentView.cpp
index a9aea3e..48a7a4e 100644
--- a/src/apps/haiku-depot/textview/TextDocumentView.cpp
+++ b/src/apps/haiku-depot/textview/TextDocumentView.cpp
@@ -24,14 +24,14 @@ TextDocumentView::TextDocumentView(const char* name)
        fInsetRight(0.0f),
        fInsetBottom(0.0f),
 
-       fSelectionAnchorOffset(0),
-       fCaretOffset(0),
-       fCaretAnchorX(0.0f),
+       fCaretBounds(),
        fShowCaret(false),
-
        fMouseDown(false)
 {
        fTextDocumentLayout.SetWidth(_TextLayoutWidth(Bounds().Width()));
+       
+       // Set default TextEditor
+       SetTextEditor(TextEditorRef(new(std::nothrow) TextEditor(), true));
 
        SetViewColor(B_TRANSPARENT_COLOR);
        SetLowColor(255, 255, 255, 255);
@@ -67,11 +67,14 @@ TextDocumentView::Draw(BRect updateRect)
        fTextDocumentLayout.SetWidth(_TextLayoutWidth(Bounds().Width()));
        fTextDocumentLayout.Draw(this, BPoint(fInsetLeft, fInsetTop), 
updateRect);
 
-       bool isCaret = fSelectionAnchorOffset == fCaretOffset;
+       if (fTextEditor.Get() == NULL)
+               return;
+
+       bool isCaret = fTextEditor->SelectionLength() == 0;
 
        if (isCaret) {
-               if (fShowCaret && fTextEditor.Get() != NULL)
-                       _DrawCaret(fCaretOffset);
+               if (fShowCaret && fTextEditor->IsEditingEnabled())
+                       _DrawCaret(fTextEditor->CaretOffset());
        } else {
                _DrawSelection();
        }
@@ -174,11 +177,13 @@ TextDocumentView::KeyDown(const char* bytes, int32 
numBytes)
        
        if (Window() != NULL && Window()->CurrentMessage() != NULL) {
                BMessage* message = Window()->CurrentMessage();
-               message->FindInt32("key", &event.key);
+               message->FindInt32("raw_char", &event.key);
                message->FindInt32("modifiers", &event.modifiers);
        }
 
        fTextEditor->KeyDown(event);
+       fShowCaret = true;
+       Invalidate();
 }
 
 
@@ -245,10 +250,6 @@ TextDocumentView::SetTextDocument(const TextDocumentRef& 
document)
        if (fTextEditor.Get() != NULL)
                fTextEditor->SetDocument(document);
 
-       fSelectionAnchorOffset = 0;
-       fCaretOffset = 0;
-       fCaretAnchorX = 0.0f;
-
        InvalidateLayout();
        Invalidate();
        _UpdateScrollBars();
@@ -256,12 +257,22 @@ TextDocumentView::SetTextDocument(const TextDocumentRef& 
document)
 
 
 void
+TextDocumentView::SetEditingEnabled(bool enabled)
+{
+       if (fTextEditor.Get() != NULL)
+               fTextEditor->SetEditingEnabled(enabled);
+}
+
+
+void
 TextDocumentView::SetTextEditor(const TextEditorRef& editor)
 {
        if (fTextEditor == editor)
                return;
 
        if (fTextEditor.Get() != NULL) {
+               fTextEditor->SetDocument(TextDocumentRef());
+               fTextEditor->SetLayout(TextDocumentLayoutRef());
                // TODO: Probably has to remove listeners
        }
 
@@ -269,6 +280,8 @@ TextDocumentView::SetTextEditor(const TextEditorRef& editor)
 
        if (fTextEditor.Get() != NULL) {
                fTextEditor->SetDocument(fTextDocument);
+               fTextEditor->SetLayout(TextDocumentLayoutRef(
+                       &fTextDocumentLayout));
                // TODO: Probably has to add listeners
        }
 }
@@ -307,38 +320,33 @@ TextDocumentView::SetInsets(float left, float top, float 
right, float bottom)
 
 
 void
-TextDocumentView::SetCaret(const BPoint& location, bool extendSelection)
+TextDocumentView::SetCaret(BPoint location, bool extendSelection)
 {
-       if (fTextDocument.Get() == NULL)
+       if (fTextEditor.Get() == NULL)
                return;
 
-       bool rightOfChar = false;
-       int32 caretOffset = fTextDocumentLayout.TextOffsetAt(
-               location.x - fInsetLeft, location.y - fInsetTop, rightOfChar);
+       location.x -= fInsetLeft;
+       location.y -= fInsetTop;
 
-       if (rightOfChar)
-               caretOffset++;
-
-       _SetCaretOffset(caretOffset, true, extendSelection);
+       fTextEditor->SetCaret(location, extendSelection);
+       fShowCaret = !extendSelection;
+       Invalidate();
 }
 
 
 bool
 TextDocumentView::HasSelection() const
 {
-       return fSelectionAnchorOffset != fCaretOffset;
+       return fTextEditor.Get() != NULL && fTextEditor->HasSelection();
 }
 
 
 void
 TextDocumentView::GetSelection(int32& start, int32& end) const
 {
-       if (fSelectionAnchorOffset <= fCaretOffset) {
-               start = fSelectionAnchorOffset;
-               end = fCaretOffset;
-       } else {
-               start = fCaretOffset;
-               end = fSelectionAnchorOffset;
+       if (fTextEditor.Get()) {
+               start = fTextEditor->SelectionStart();
+               end = fTextEditor->SelectionEnd();
        }
 }
 
@@ -346,8 +354,7 @@ TextDocumentView::GetSelection(int32& start, int32& end) 
const
 void
 TextDocumentView::Copy(BClipboard* clipboard)
 {
-       if (fSelectionAnchorOffset == fCaretOffset
-               || fTextDocument.Get() == NULL) {
+       if (!HasSelection() || fTextDocument.Get() == NULL) {
                // Nothing to copy, don't clear clipboard contents for now 
reason.
                return;
        }
@@ -426,41 +433,6 @@ TextDocumentView::_UpdateScrollBars()
 
 
 void
-TextDocumentView::_SetCaretOffset(int32 offset, bool updateAnchor,
-       bool lockSelectionAnchor)
-{
-       if (offset < 0)
-               offset = 0;
-       int32 length = fTextDocument->Length();
-       if (offset > length)
-               offset = length;
-
-       if (offset == fCaretOffset && (lockSelectionAnchor
-                       || offset == fSelectionAnchorOffset)) {
-               return;
-       }
-
-       if (!lockSelectionAnchor)
-               fSelectionAnchorOffset = offset;
-
-       fCaretOffset = offset;
-       fShowCaret = true;
-
-       if (updateAnchor) {
-               float x1;
-               float y1;
-               float x2;
-               float y2;
-
-               fTextDocumentLayout.GetTextBounds(fCaretOffset, x1, y1, x2, y2);
-               fCaretAnchorX = x1;
-       }
-
-       Invalidate();
-}
-
-
-void
 TextDocumentView::_DrawCaret(int32 textOffset)
 {
        float x1;
diff --git a/src/apps/haiku-depot/textview/TextDocumentView.h 
b/src/apps/haiku-depot/textview/TextDocumentView.h
index 85094d9..65ef4c4 100644
--- a/src/apps/haiku-depot/textview/TextDocumentView.h
+++ b/src/apps/haiku-depot/textview/TextDocumentView.h
@@ -52,6 +52,7 @@ public:
                        void                            SetTextDocument(
                                                                        const 
TextDocumentRef& document);
 
+                       void                            SetEditingEnabled(bool 
enabled);
                        void                            SetTextEditor(
                                                                        const 
TextEditorRef& editor);
 
@@ -60,8 +61,7 @@ public:
                        void                            SetInsets(float left, 
float top, float right,
                                                                        float 
bottom);
 
-                       void                            SetCaret(const BPoint& 
where,
-                                                                       bool 
extendSelection);
+                       void                            SetCaret(BPoint where, 
bool extendSelection);
 
                        bool                            HasSelection() const;
                        void                            GetSelection(int32& 
start, int32& end) const;
@@ -73,9 +73,6 @@ private:
 
                        void                            _UpdateScrollBars();
 
-                       void                            _SetCaretOffset(int32 
offset, bool updateAnchor,
-                                                                       bool 
lockSelectionAnchor);
-
                        void                            _DrawCaret(int32 
textOffset);
                        void                            _DrawSelection();
                        void                            
_GetSelectionShape(BShape& shape,
@@ -91,12 +88,8 @@ private:
                        float                           fInsetRight;
                        float                           fInsetBottom;
 
-                       int32                           fSelectionAnchorOffset;
-                       int32                           fCaretOffset;
-                       float                           fCaretAnchorX;
-                       bool                            fShowCaret;
                        BRect                           fCaretBounds;
-
+                       bool                            fShowCaret;
                        bool                            fMouseDown;
 };
 
diff --git a/src/apps/haiku-depot/textview/TextEditor.cpp 
b/src/apps/haiku-depot/textview/TextEditor.cpp
index f3a28b3..ae5948c 100644
--- a/src/apps/haiku-depot/textview/TextEditor.cpp
+++ b/src/apps/haiku-depot/textview/TextEditor.cpp
@@ -14,7 +14,8 @@ TextEditor::TextEditor()
        fLayout(),
        fSelection(),
        fCaretAnchorX(0.0f),
-       fStyleAtCaret()
+       fStyleAtCaret(),
+       fEditingEnabled(true)
 {
 }
 
@@ -25,7 +26,13 @@ TextEditor::TextEditor(const TextEditor& other)
        fLayout(other.fLayout),
        fSelection(other.fSelection),
        fCaretAnchorX(other.fCaretAnchorX),
-       fStyleAtCaret(other.fStyleAtCaret)
+       fStyleAtCaret(other.fStyleAtCaret),
+       fEditingEnabled(other.fEditingEnabled)
+{
+}
+
+
+TextEditor::~TextEditor()
 {
 }
 
@@ -41,6 +48,7 @@ TextEditor::operator=(const TextEditor& other)
        fSelection = other.fSelection;
        fCaretAnchorX = other.fCaretAnchorX;
        fStyleAtCaret = other.fStyleAtCaret;
+       fEditingEnabled = other.fEditingEnabled;
        return *this;
 }
 
@@ -55,7 +63,8 @@ TextEditor::operator==(const TextEditor& other) const
                && fLayout == other.fLayout
                && fSelection == other.fSelection
                && fCaretAnchorX == other.fCaretAnchorX
-               && fStyleAtCaret == other.fStyleAtCaret;
+               && fStyleAtCaret == other.fStyleAtCaret
+               && fEditingEnabled == other.fEditingEnabled;
 }
 
 
@@ -86,6 +95,30 @@ TextEditor::SetLayout(const TextDocumentLayoutRef& ref)
 
 
 void
+TextEditor::SetEditingEnabled(bool enabled)
+{
+       fEditingEnabled = enabled;
+}
+
+
+void
+TextEditor::SetCaret(BPoint location, bool extendSelection)
+{
+       if (fDocument.Get() == NULL || fLayout.Get() == NULL)
+               return;
+
+       bool rightOfChar = false;
+       int32 caretOffset = fLayout->TextOffsetAt(location.x, location.y,
+               rightOfChar);
+
+       if (rightOfChar)
+               caretOffset++;
+
+       _SetCaretOffset(caretOffset, true, extendSelection, true);
+}
+
+
+void
 TextEditor::SetSelection(TextSelection selection)
 {
        _SetSelection(selection.Caret(), selection.Anchor(), true, true);
@@ -116,15 +149,15 @@ TextEditor::KeyDown(KeyEvent event)
 
        switch (event.key) {
                case B_UP_ARROW:
-                       _LineUp(select);
+                       LineUp(select);
                        break;
 
                case B_DOWN_ARROW:
-                       _LineDown(select);
+                       LineDown(select);
                        break;
 
                case B_LEFT_ARROW:
-                       if (_HasSelection() && !select) {
+                       if (HasSelection() && !select) {
                                _SetCaretOffset(
                                        std::min(fSelection.Caret(), 
fSelection.Anchor()),
                                        true, false, true
@@ -134,7 +167,7 @@ TextEditor::KeyDown(KeyEvent event)
                        break;
 
                case B_RIGHT_ARROW:
-                       if (_HasSelection() && !select) {
+                       if (HasSelection() && !select) {
                                _SetCaretOffset(
                                        std::max(fSelection.Caret(), 
fSelection.Anchor()),
                                        true, false, true
@@ -144,11 +177,11 @@ TextEditor::KeyDown(KeyEvent event)
                        break;
 
                case B_HOME:
-                       _LineStart(select);
+                       LineStart(select);
                        break;
 
                case B_END:
-                       _LineEnd(select);
+                       LineEnd(select);
                        break;
 
                case B_ENTER:
@@ -164,8 +197,8 @@ TextEditor::KeyDown(KeyEvent event)
                        break;
 
                case B_BACKSPACE:
-                       if (_HasSelection()) {
-                               Remove(_SelectionStart(), _SelectionLength());
+                       if (HasSelection()) {
+                               Remove(SelectionStart(), SelectionLength());
                        } else {
                                if (fSelection.Caret() > 0)
                                        Remove(fSelection.Caret() - 1, 1);
@@ -173,8 +206,8 @@ TextEditor::KeyDown(KeyEvent event)
                        break;
 
                case B_DELETE:
-                       if (_HasSelection()) {
-                               Remove(_SelectionStart(), _SelectionLength());
+                       if (HasSelection()) {
+                               Remove(SelectionStart(), SelectionLength());
                        } else {
                                if (fSelection.Caret() < fDocument->Length())
                                        Remove(fSelection.Caret(), 1);
@@ -207,6 +240,9 @@ TextEditor::KeyDown(KeyEvent event)
 void
 TextEditor::Insert(int32 offset, const BString& string)
 {
+       if (!fEditingEnabled)
+               return;
+
        // TODO: ...
 }
 
@@ -214,132 +250,176 @@ TextEditor::Insert(int32 offset, const BString& string)
 void
 TextEditor::Remove(int32 offset, int32 length)
 {
+       if (!fEditingEnabled)
+               return;
+
        // TODO: ...
 }
 
 
-// #pragma mark - private
+// #pragma mark -
 
 
-// _SetCaretOffset
 void
-TextEditor::_SetCaretOffset(int32 offset, bool updateAnchor,
-       bool lockSelectionAnchor, bool updateSelectionStyle)
+TextEditor::LineUp(bool select)
 {
-       if (fDocument.Get() == NULL)
+       if (fLayout.Get() == NULL)
                return;
 
-       if (offset < 0)
-               offset = 0;
-       int32 textLength = fDocument->Length();
-       if (offset > textLength)
-               offset = textLength;
-
-       int32 caret = offset;
-       int32 anchor = lockSelectionAnchor ? fSelection.Anchor() : offset;
-       _SetSelection(caret, anchor, updateAnchor, updateSelectionStyle);
+       int32 lineIndex = fLayout->LineIndexForOffset(fSelection.Caret());
+       _MoveToLine(lineIndex - 1, select);
 }
 
 
-// _SetSelection
 void
-TextEditor::_SetSelection(int32 caret, int32 anchor, bool updateAnchor,
-       bool updateSelectionStyle)
+TextEditor::LineDown(bool select)
 {
        if (fLayout.Get() == NULL)
                return;
-       
-       if (caret == fSelection.Caret() && caret == fSelection.Anchor())
-               return;
 
-       fSelection.SetAnchor(anchor);
-       fSelection.SetCaret(caret);
+       int32 lineIndex = fLayout->LineIndexForOffset(fSelection.Caret());
+       _MoveToLine(lineIndex + 1, select);
+}
 
-       if (updateAnchor) {
-               float x1;
-               float y1;
-               float x2;
-               float y2;
 
-               fLayout->GetTextBounds(caret, x1, y1, x2, y2);
-               fCaretAnchorX = x1;
-       }
+void
+TextEditor::LineStart(bool select)
+{
+       if (fLayout.Get() == NULL)
+               return;
 
-       if (updateSelectionStyle)
-               _UpdateStyleAtCaret();
+       int32 lineIndex = fLayout->LineIndexForOffset(fSelection.Caret());
+       _SetCaretOffset(fLayout->FirstOffsetOnLine(lineIndex), true, select,
+               true);
 }
 
 
 void
-TextEditor::_UpdateStyleAtCaret()
+TextEditor::LineEnd(bool select)
 {
-       if (fDocument.Get() == NULL)
+       if (fLayout.Get() == NULL)
                return;
 
-       int32 offset = fSelection.Caret() - 1;
-       if (offset < 0)
-               offset = 0;
-       SetCharacterStyle(fDocument->CharacterStyleAt(offset));
+       int32 lineIndex = fLayout->LineIndexForOffset(fSelection.Caret());
+       _SetCaretOffset(fLayout->LastOffsetOnLine(lineIndex), true, select,
+               true);
 }
 
 
 // #pragma mark -
 
 
-void
-TextEditor::_LineUp(bool select)
+bool
+TextEditor::HasSelection() const
 {
-       // TODO
+       return SelectionLength() > 0;
 }
 
 
-void
-TextEditor::_LineDown(bool select)
+int32
+TextEditor::SelectionStart() const
 {
-       // TODO
+       return std::min(fSelection.Caret(), fSelection.Anchor());
 }
 
 
-void
-TextEditor::_LineStart(bool select)
+int32
+TextEditor::SelectionEnd() const
 {
-       // TODO
+       return std::max(fSelection.Caret(), fSelection.Anchor());
 }
 
 
-void
-TextEditor::_LineEnd(bool select)
+int32
+TextEditor::SelectionLength() const
 {
-       // TODO
+       return SelectionEnd() - SelectionStart();
 }
 
 
-// #pragma mark -
+// #pragma mark - private
 
 
-bool
-TextEditor::_HasSelection() const
+// _MoveToLine
+void
+TextEditor::_MoveToLine(int32 lineIndex, bool select)
 {
-       return _SelectionLength() > 0;
-}
+       if (lineIndex < 0 || lineIndex >= fLayout->CountLines())
+               return;
 
+       float x1;
+       float y1;
+       float x2;
+       float y2;
+       fLayout->GetLineBounds(lineIndex , x1, y1, x2, y2);
 
-int32
-TextEditor::_SelectionStart() const
+       bool rightOfCenter;
+       int32 textOffset = fLayout->TextOffsetAt(fCaretAnchorX, (y1 + y2) / 2,
+               rightOfCenter);
+
+       if (rightOfCenter)
+               textOffset++;
+
+       _SetCaretOffset(textOffset, false, select, true);
+}
+
+void
+TextEditor::_SetCaretOffset(int32 offset, bool updateAnchor,
+       bool lockSelectionAnchor, bool updateSelectionStyle)
 {
-       return std::min(fSelection.Caret(), fSelection.Anchor());
+       if (fDocument.Get() == NULL)
+               return;
+
+       if (offset < 0)
+               offset = 0;
+       int32 textLength = fDocument->Length();
+       if (offset > textLength)
+               offset = textLength;
+
+       int32 caret = offset;
+       int32 anchor = lockSelectionAnchor ? fSelection.Anchor() : offset;
+       _SetSelection(caret, anchor, updateAnchor, updateSelectionStyle);
 }
 
 
-int32
-TextEditor::_SelectionEnd() const
+void
+TextEditor::_SetSelection(int32 caret, int32 anchor, bool updateAnchor,
+       bool updateSelectionStyle)
 {
-       return std::max(fSelection.Caret(), fSelection.Anchor());
+       if (fLayout.Get() == NULL)
+               return;
+       
+       if (caret == fSelection.Caret() && anchor == fSelection.Anchor())
+               return;
+
+       fSelection.SetCaret(caret);
+       fSelection.SetAnchor(anchor);
+
+       if (updateAnchor) {
+               float x1;
+               float y1;
+               float x2;
+               float y2;
+
+               fLayout->GetTextBounds(caret, x1, y1, x2, y2);
+               fCaretAnchorX = x1;
+       }
+
+       if (updateSelectionStyle)
+               _UpdateStyleAtCaret();
 }
 
 
-int32
-TextEditor::_SelectionLength() const
+void
+TextEditor::_UpdateStyleAtCaret()
 {
-       return _SelectionEnd() - _SelectionStart();
+       if (fDocument.Get() == NULL)
+               return;
+
+       int32 offset = fSelection.Caret() - 1;
+       if (offset < 0)
+               offset = 0;
+       SetCharacterStyle(fDocument->CharacterStyleAt(offset));
 }
+
+
diff --git a/src/apps/haiku-depot/textview/TextEditor.h 
b/src/apps/haiku-depot/textview/TextEditor.h
index 77c851e..89bdb47 100644
--- a/src/apps/haiku-depot/textview/TextEditor.h
+++ b/src/apps/haiku-depot/textview/TextEditor.h
@@ -6,6 +6,7 @@
 #define TEXT_EDITOR_H
 
 
+#include <Point.h>
 #include <Referenceable.h>
 
 #include "CharacterStyle.h"
@@ -27,6 +28,7 @@ class TextEditor : public BReferenceable {
 public:
                                                                TextEditor();
                                                                
TextEditor(const TextEditor& other);
+       virtual                                         ~TextEditor();
 
                        TextEditor&                     operator=(const 
TextEditor& other);
                        bool                            operator==(const 
TextEditor& other) const;
@@ -41,6 +43,11 @@ public:
                        TextDocumentLayoutRef Layout() const
                                                                        { 
return fLayout; }
 
+                       void                            SetEditingEnabled(bool 
enabled);
+       inline  bool                            IsEditingEnabled() const
+                                                                       { 
return fEditingEnabled; }
+
+                       void                            SetCaret(BPoint 
location, bool extendSelection);
                        void                            
SetSelection(TextSelection selection);
        inline  TextSelection           Selection() const
                                                                        { 
return fSelection; }
@@ -49,12 +56,25 @@ public:
                        ::CharacterStyle        CharacterStyle() const
                                                                        { 
return fStyleAtCaret; }
 
-                       void                            KeyDown(KeyEvent event);
+       virtual void                            KeyDown(KeyEvent event);
+
+       virtual void                            Insert(int32 offset, const 
BString& string);
+       virtual void                            Remove(int32 offset, int32 
length);
+
+                       void                            LineUp(bool select);
+                       void                            LineDown(bool select);
+                       void                            LineStart(bool select);
+                       void                            LineEnd(bool select);
 
-                       void                            Insert(int32 offset, 
const BString& string);
-                       void                            Remove(int32 offset, 
int32 length);
+                       bool                            HasSelection() const;
+                       int32                           SelectionStart() const;
+                       int32                           SelectionEnd() const;
+                       int32                           SelectionLength() const;
+       inline  int32                           CaretOffset() const
+                                                                       { 
return fSelection.Caret(); }
 
 private:
+                       void                            _MoveToLine(int32 
lineIndex, bool select);
                        void                            _SetCaretOffset(int32 
offset,
                                                                        bool 
updateAnchor,
                                                                        bool 
lockSelectionAnchor,
@@ -64,23 +84,13 @@ private:
                                                                        bool 
updateSelectionStyle);
 
                        void                            _UpdateStyleAtCaret();
-
-                       void                            _LineUp(bool select);
-                       void                            _LineDown(bool select);
-                       void                            _LineStart(bool select);
-                       void                            _LineEnd(bool select);
-
-                       bool                            _HasSelection() const;
-                       int32                           _SelectionStart() const;
-                       int32                           _SelectionEnd() const;
-                       int32                           _SelectionLength() 
const;
-
 private:
                        TextDocumentRef         fDocument;
                        TextDocumentLayoutRef fLayout;
                        TextSelection           fSelection;
                        float                           fCaretAnchorX;
                        ::CharacterStyle        fStyleAtCaret;
+                       bool                            fEditingEnabled;
 };
 
 

############################################################################

Revision:    hrev46701
Commit:      48ee828ae393a1e7ecf431b099d9750787a49fb0
URL:         http://cgit.haiku-os.org/haiku/commit/?id=48ee828
Author:      Stephan Aßmus <superstippi@xxxxxx>
Date:        Sat Jan 18 09:33:59 2014 UTC

HaikuDepot: Disable editing for read-only text views.

----------------------------------------------------------------------------

diff --git a/src/apps/haiku-depot/PackageInfoView.cpp 
b/src/apps/haiku-depot/PackageInfoView.cpp
index 0790955..b84414c 100644
--- a/src/apps/haiku-depot/PackageInfoView.cpp
+++ b/src/apps/haiku-depot/PackageInfoView.cpp
@@ -111,6 +111,7 @@ public:
                :
                TextDocumentView(name)
        {
+               SetEditingEnabled(false);
                CharacterStyle regularStyle;
 
                float fontSize = regularStyle.Font().Size();


Other related posts:

  • » [haiku-commits] haiku: hrev46701 - src/apps/haiku-depot/textview - superstippi