Skip to content

Commit

Permalink
add highlight cursor line style option
Browse files Browse the repository at this point in the history
  • Loading branch information
scheffle committed Oct 23, 2024
1 parent ef11640 commit 00d0754
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
22 changes: 18 additions & 4 deletions vstgui/lib/ctexteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ bool TextEditorView::attached (CView* parent)
md.scrollView->registerViewEventListener (this);
if (auto sb = md.scrollView->getVerticalScrollbar ())
sb->registerViewEventListener (this);
if (md.style->showLineNumbers)
if (md.style->flags & Style::Flags::ShowLineNumbers)
{
md.lineNumberView = makeOwned<LineNumberView> (this);
md.lineNumberView->setStyle (md.style, md.lineHeight);
Expand Down Expand Up @@ -767,7 +767,7 @@ void TextEditorView::onStyleChanged () const

if (md.lineNumberView)
{
if (md.style->showLineNumbers)
if (md.style->flags & Style::Flags::ShowLineNumbers)
{
md.lineNumberView->setStyle (md.style, md.lineHeight);
updateLineNumbersView ();
Expand All @@ -778,7 +778,7 @@ void TextEditorView::onStyleChanged () const
md.lineNumberView = nullptr;
}
}
else if (md.style->showLineNumbers && md.scrollView)
else if (md.style->flags & Style::Flags::ShowLineNumbers && md.scrollView)
{
md.lineNumberView = makeOwned<LineNumberView> (&mutableThis ());
md.lineNumberView->setStyle (md.style, md.lineHeight);
Expand Down Expand Up @@ -972,6 +972,8 @@ void TextEditorView::drawRect (CDrawContext* context, const CRect& dirtyRect)
context->setFillColor (md.style->backColor);
context->setDrawMode (kAntiAliasing);
context->drawRect (dirtyRect, kDrawFilled);
bool drawCursorLineHighlight = (md.editState.select_start == md.editState.select_end) &&
(md.style->flags & Style::Flags::HighlightCursorLine);

auto styleProvider = dynamic_cast<TextEditorColorization::IStyleProvider*> (md.controller);
if (styleProvider)
Expand All @@ -986,6 +988,13 @@ void TextEditorView::drawRect (CDrawContext* context, const CRect& dirtyRect)
y += md.lineHeight;
if (y < dirtyRect.top)
continue;
if (drawCursorLineHighlight && md.selectedLines.start == index)
{
auto lineRect = calculateLineRect (index);
lineRect.right = getViewSize ().right;
context->setFillColor (md.style->highlightCursorLineColor);
context->drawRect (lineRect, kDrawFilled);
}
context->setFontColor (md.style->textColor);
context->setFont (md.style->font);
auto selRange =
Expand Down Expand Up @@ -1091,7 +1100,12 @@ void TextEditorView::invalidLine (Lines::const_iterator it, bool completeWidth)
}

//------------------------------------------------------------------------
void TextEditorView::invalidSelectedLines () const { invalidateRect (calculateSelectionRect ()); }
void TextEditorView::invalidSelectedLines () const
{
if (md.selectedLines.length == 0 && (md.style->flags & Style::Flags::HighlightCursorLine))
invalidLine (md.selectedLines.start, true);
invalidateRect (calculateSelectionRect ());
}

//------------------------------------------------------------------------
CRect TextEditorView::calculateLineRect (size_t index) const
Expand Down
10 changes: 9 additions & 1 deletion vstgui/lib/ctexteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ struct ITextEditor

struct Style
{
enum class Flags : uint32_t
{
ShowLineNumbers,
HighlightCursorLine
};
using FlagsBitset = EnumBitset<Flags>;

SharedPointer<CFontDesc> font {kNormalFont};
SharedPointer<CFontDesc> lineNumbersFont {kNormalFontSmall};
CColor textColor {kBlackCColor};
CColor backColor {kWhiteCColor};
CColor highlightCursorLineColor {0, 0, 0, 10};
CColor selectionBackColor {kGreyCColor};
CColor frameColor {kGreyCColor};
CColor lineNumberTextColor {127, 127, 127, 100};
Expand All @@ -62,7 +70,7 @@ struct ITextEditor
CCoord lineNumberRightMargin {2.};
uint32_t cursorBlinkTime {500}; // in milliseconds
uint32_t tabWidth {4};
bool showLineNumbers {true};
FlagsBitset flags {{Flags::ShowLineNumbers, Flags::HighlightCursorLine}};
};

virtual void setStyle (const Style& style) const = 0;
Expand Down
4 changes: 2 additions & 2 deletions vstgui/standalone/examples/standalone/resource/test.uidesc
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@
"auto-drag-scrolling": "false",
"auto-hide-scrollbars": "true",
"autosize": "left right top bottom ",
"background-color": "~ TransparentCColor",
"background-color": "control.back",
"background-color-draw-style": "filled and stroked",
"bordered": "true",
"class": "CScrollView",
Expand All @@ -965,7 +965,7 @@
"scrollbar-background-color": "~ TransparentCColor",
"scrollbar-frame-color": "~ TransparentCColor",
"scrollbar-min-scroller-size": "18",
"scrollbar-scroller-color": "~ WhiteCColor",
"scrollbar-scroller-color": "control.frame",
"scrollbar-width": "12",
"size": "270, 140",
"transparent": "false",
Expand Down

0 comments on commit 00d0754

Please sign in to comment.