From a512ed9a8f16443b1b87f795e9a62bc85fbf2377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Fri, 17 Jan 2014 23:26:21 +0100 Subject: [PATCH] HaikuDepot: Implement support for a blinking cursor. However, since the selection is actually maintained in TextEditor, the cursor can't be navigated. The code needs to shuffle around a bit. --- .../haiku-depot/textview/TextDocumentView.cpp | 82 ++++++++++++++----- .../haiku-depot/textview/TextDocumentView.h | 3 + 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/apps/haiku-depot/textview/TextDocumentView.cpp b/src/apps/haiku-depot/textview/TextDocumentView.cpp index ab7df8d272..a9aea3ea13 100644 --- a/src/apps/haiku-depot/textview/TextDocumentView.cpp +++ b/src/apps/haiku-depot/textview/TextDocumentView.cpp @@ -67,34 +67,29 @@ TextDocumentView::Draw(BRect updateRect) fTextDocumentLayout.SetWidth(_TextLayoutWidth(Bounds().Width())); fTextDocumentLayout.Draw(this, BPoint(fInsetLeft, fInsetTop), updateRect); - if (fSelectionAnchorOffset == fCaretOffset) - return; + bool isCaret = fSelectionAnchorOffset == fCaretOffset; - int32 start; - int32 end; - GetSelection(start, end); - - BShape shape; - _GetSelectionShape(shape, start, end); - - SetDrawingMode(B_OP_SUBTRACT); - SetLineMode(B_ROUND_CAP, B_ROUND_JOIN); - MovePenTo(fInsetLeft - 0.5f, fInsetTop - 0.5f); - - if (IsFocus() && Window() != NULL && Window()->IsActive()) { - SetHighColor(30, 30, 30); - FillShape(&shape); + if (isCaret) { + if (fShowCaret && fTextEditor.Get() != NULL) + _DrawCaret(fCaretOffset); + } else { + _DrawSelection(); } - - SetHighColor(40, 40, 40); - StrokeShape(&shape); } void TextDocumentView::Pulse() { - // TODO: Blink cursor + if (fTextEditor.Get() == NULL) + return; + + // Blink cursor + fShowCaret = !fShowCaret; + if (fCaretBounds.IsValid()) + Invalidate(fCaretBounds); + else + Invalidate(); } @@ -465,7 +460,50 @@ TextDocumentView::_SetCaretOffset(int32 offset, bool updateAnchor, } -// _GetSelectionShape +void +TextDocumentView::_DrawCaret(int32 textOffset) +{ + float x1; + float y1; + float x2; + float y2; + + fTextDocumentLayout.GetTextBounds(textOffset, x1, y1, x2, y2); + x2 = x1 + 1; + + fCaretBounds = BRect(x1, y1, x2, y2); + fCaretBounds.OffsetBy(fInsetLeft, fInsetTop); + + SetDrawingMode(B_OP_INVERT); + FillRect(fCaretBounds); +} + + +void +TextDocumentView::_DrawSelection() +{ + int32 start; + int32 end; + GetSelection(start, end); + + BShape shape; + _GetSelectionShape(shape, start, end); + + SetDrawingMode(B_OP_SUBTRACT); + + SetLineMode(B_ROUND_CAP, B_ROUND_JOIN); + MovePenTo(fInsetLeft - 0.5f, fInsetTop - 0.5f); + + if (IsFocus() && Window() != NULL && Window()->IsActive()) { + SetHighColor(30, 30, 30); + FillShape(&shape); + } + + SetHighColor(40, 40, 40); + StrokeShape(&shape); +} + + void TextDocumentView::_GetSelectionShape(BShape& shape, int32 start, int32 end) { @@ -551,3 +589,5 @@ TextDocumentView::_GetSelectionShape(BShape& shape, int32 start, int32 end) shape.Close(); } } + + diff --git a/src/apps/haiku-depot/textview/TextDocumentView.h b/src/apps/haiku-depot/textview/TextDocumentView.h index 1d9498f088..85094d94f8 100644 --- a/src/apps/haiku-depot/textview/TextDocumentView.h +++ b/src/apps/haiku-depot/textview/TextDocumentView.h @@ -76,6 +76,8 @@ private: void _SetCaretOffset(int32 offset, bool updateAnchor, bool lockSelectionAnchor); + void _DrawCaret(int32 textOffset); + void _DrawSelection(); void _GetSelectionShape(BShape& shape, int32 start, int32 end); @@ -93,6 +95,7 @@ private: int32 fCaretOffset; float fCaretAnchorX; bool fShowCaret; + BRect fCaretBounds; bool fMouseDown; };