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.
This commit is contained in:
Stephan Aßmus 2014-01-17 23:26:21 +01:00
parent 02d77b77aa
commit a512ed9a8f
2 changed files with 64 additions and 21 deletions

View File

@ -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();
}
}

View File

@ -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;
};