From 006b224af96d10452655ecced49ba3859c1d62ab Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 5 Apr 2019 08:32:58 +1100 Subject: Feature [feature-requests:#1272]. Add API to set default fold display text. --- doc/ScintillaDoc.html | 7 +++++++ doc/ScintillaHistory.html | 6 +++++- include/Scintilla.h | 2 ++ include/Scintilla.iface | 6 ++++++ src/ContractionState.cxx | 8 +------- src/ContractionState.h | 1 - src/EditModel.cxx | 17 +++++++++++++++++ src/EditModel.h | 4 ++++ src/EditView.cxx | 7 ++++--- src/Editor.cxx | 8 ++++++++ src/UniqueString.h | 4 ++++ test/simpleTests.py | 5 +++++ 12 files changed, 63 insertions(+), 12 deletions(-) diff --git a/doc/ScintillaDoc.html b/doc/ScintillaDoc.html index b9f15e267..ddebc65a9 100644 --- a/doc/ScintillaDoc.html +++ b/doc/ScintillaDoc.html @@ -6013,6 +6013,8 @@ sptr_t CallScintilla(unsigned int iMessage, uptr_t wParam, sptr_t lParam){ SCI_TOGGLEFOLDSHOWTEXT(int line, const char *text)
SCI_FOLDDISPLAYTEXTSETSTYLE(int style)
SCI_FOLDDISPLAYTEXTGETSTYLE → int
+ SCI_SETDEFAULTFOLDDISPLAYTEXT(const char *text)
+ SCI_GETDEFAULTFOLDDISPLAYTEXT(<unused>, char *text)
SCI_FOLDLINE(int line, int action)
SCI_FOLDCHILDREN(int line, int action)
SCI_FOLDALL(int action)
@@ -6172,6 +6174,7 @@ sptr_t CallScintilla(unsigned int iMessage, uptr_t wParam, sptr_t lParam){

An optional text tag may be shown to the right of the folded text with the text argument to SCI_TOGGLEFOLDSHOWTEXT. + The default text for all header lines can be set with SCI_SETDEFAULTFOLDDISPLAYTEXT. The text is drawn with the STYLE_FOLDDISPLAYTEXT style.

@@ -6209,6 +6212,10 @@ sptr_t CallScintilla(unsigned int iMessage, uptr_t wParam, sptr_t lParam){ +

SCI_SETDEFAULTFOLDDISPLAYTEXT(const char *text)
+ SCI_GETDEFAULTFOLDDISPLAYTEXT(<unused>, char *text) → int
+ These messages set and get the default text displayed at the right of the folded text.

+

SCI_SETFOLDEXPANDED(int line, bool expanded)
SCI_GETFOLDEXPANDED(int line) → bool
These messages set and get the expanded state of a single line. The set message has no effect diff --git a/doc/ScintillaHistory.html b/doc/ScintillaHistory.html index 8e8d734dd..b92fbcfb9 100644 --- a/doc/ScintillaHistory.html +++ b/doc/ScintillaHistory.html @@ -583,7 +583,11 @@

  • Avoid potential long hangs with idle styling for huge documents on Cocoa and GTK.
  • -

    Release 4.1.4

    diff --git a/include/Scintilla.h b/include/Scintilla.h index 6187bc612..386d372ee 100644 --- a/include/Scintilla.h +++ b/include/Scintilla.h @@ -493,6 +493,8 @@ typedef sptr_t (*SciFnDirect)(sptr_t ptr, unsigned int iMessage, uptr_t wParam, #define SC_FOLDDISPLAYTEXT_BOXED 2 #define SCI_FOLDDISPLAYTEXTSETSTYLE 2701 #define SCI_FOLDDISPLAYTEXTGETSTYLE 2707 +#define SCI_SETDEFAULTFOLDDISPLAYTEXT 2722 +#define SCI_GETDEFAULTFOLDDISPLAYTEXT 2723 #define SC_FOLDACTION_CONTRACT 0 #define SC_FOLDACTION_EXPAND 1 #define SC_FOLDACTION_TOGGLE 2 diff --git a/include/Scintilla.iface b/include/Scintilla.iface index 2b69adef3..7960bdbc6 100644 --- a/include/Scintilla.iface +++ b/include/Scintilla.iface @@ -1239,6 +1239,12 @@ set void FoldDisplayTextSetStyle=2701(int style,) # Get the style of fold display text. get int FoldDisplayTextGetStyle=2707(,) +# Set the default fold display text. +fun void SetDefaultFoldDisplayText=2722(, string text) + +# Get the default fold display text. +fun int GetDefaultFoldDisplayText=2723(, stringresult text) + enu FoldAction=SC_FOLDACTION_ val SC_FOLDACTION_CONTRACT=0 val SC_FOLDACTION_EXPAND=1 diff --git a/src/ContractionState.cxx b/src/ContractionState.cxx index 1dd25cc4d..565f9e141 100644 --- a/src/ContractionState.cxx +++ b/src/ContractionState.cxx @@ -75,7 +75,6 @@ public: bool HiddenLines() const override; const char *GetFoldDisplayText(Sci::Line lineDoc) const override; - bool GetFoldDisplayTextShown(Sci::Line lineDoc) const override; bool SetFoldDisplayText(Sci::Line lineDoc, const char *text) override; bool GetExpanded(Sci::Line lineDoc) const override; @@ -277,17 +276,12 @@ const char *ContractionState::GetFoldDisplayText(Sci::Line lineDoc) const return foldDisplayTexts->ValueAt(lineDoc).get(); } -template -bool ContractionState::GetFoldDisplayTextShown(Sci::Line lineDoc) const { - return !GetExpanded(lineDoc) && GetFoldDisplayText(lineDoc); -} - template bool ContractionState::SetFoldDisplayText(Sci::Line lineDoc, const char *text) { EnsureData(); const char *foldText = foldDisplayTexts->ValueAt(lineDoc).get(); if (!foldText || !text || 0 != strcmp(text, foldText)) { - UniqueString uns = UniqueStringCopy(text); + UniqueString uns = IsNullOrEmpty(text) ? UniqueString() : UniqueStringCopy(text); foldDisplayTexts->SetValueAt(lineDoc, std::move(uns)); Check(); return true; diff --git a/src/ContractionState.h b/src/ContractionState.h index 90f5c0784..f9ec7b645 100644 --- a/src/ContractionState.h +++ b/src/ContractionState.h @@ -32,7 +32,6 @@ public: virtual bool HiddenLines() const=0; virtual const char *GetFoldDisplayText(Sci::Line lineDoc) const=0; - virtual bool GetFoldDisplayTextShown(Sci::Line lineDoc) const=0; virtual bool SetFoldDisplayText(Sci::Line lineDoc, const char *text)=0; virtual bool GetExpanded(Sci::Line lineDoc) const=0; diff --git a/src/EditModel.cxx b/src/EditModel.cxx index 83cade5ef..db8682efb 100644 --- a/src/EditModel.cxx +++ b/src/EditModel.cxx @@ -88,3 +88,20 @@ bool EditModel::BidirectionalEnabled() const { bool EditModel::BidirectionalR2L() const { return bidirectional == Bidirectional::bidiR2L; } + +void EditModel::SetDefaultFoldDisplayText(const char *text) { + defaultFoldDisplayText = IsNullOrEmpty(text) ? UniqueString() : UniqueStringCopy(text); +} + +const char *EditModel::GetDefaultFoldDisplayText() const noexcept { + return defaultFoldDisplayText.get(); +} + +const char *EditModel::GetFoldDisplayText(Sci::Line lineDoc) const { + if (foldDisplayTextStyle == SC_FOLDDISPLAYTEXT_HIDDEN || pcs->GetExpanded(lineDoc)) { + return nullptr; + } + + const char *text = pcs->GetFoldDisplayText(lineDoc); + return text ? text : defaultFoldDisplayText.get(); +} diff --git a/src/EditModel.h b/src/EditModel.h index 5fb6a0f02..b63f2129f 100644 --- a/src/EditModel.h +++ b/src/EditModel.h @@ -42,6 +42,7 @@ public: int foldFlags; int foldDisplayTextStyle; + UniqueString defaultFoldDisplayText; std::unique_ptr pcs; // Hotspot support Range hotspot; @@ -65,6 +66,9 @@ public: virtual Range GetHotSpotRange() const noexcept = 0; bool BidirectionalEnabled() const; bool BidirectionalR2L() const; + void SetDefaultFoldDisplayText(const char *text); + const char *GetDefaultFoldDisplayText() const noexcept; + const char *GetFoldDisplayText(Sci::Line lineDoc) const; }; } diff --git a/src/EditView.cxx b/src/EditView.cxx index 91d5da13d..a26412954 100644 --- a/src/EditView.cxx +++ b/src/EditView.cxx @@ -1048,7 +1048,7 @@ void EditView::DrawEOL(Surface *surface, const EditModel &model, const ViewStyle rcSegment.left = rcLine.left; rcSegment.right = rcLine.right; - const bool fillRemainder = !lastSubLine || model.foldDisplayTextStyle == SC_FOLDDISPLAYTEXT_HIDDEN || !model.pcs->GetFoldDisplayTextShown(line); + const bool fillRemainder = !lastSubLine || !model.GetFoldDisplayText(line); if (fillRemainder) { // Fill the remainder of the line FillLineRemainder(surface, model, vsDraw, ll, line, rcSegment, subLine); @@ -1196,11 +1196,12 @@ void EditView::DrawFoldDisplayText(Surface *surface, const EditModel &model, con if (!lastSubLine) return; - if ((model.foldDisplayTextStyle == SC_FOLDDISPLAYTEXT_HIDDEN) || !model.pcs->GetFoldDisplayTextShown(line)) + const char *text = model.GetFoldDisplayText(line); + if (!text) return; PRectangle rcSegment = rcLine; - const std::string_view foldDisplayText = model.pcs->GetFoldDisplayText(line); + const std::string_view foldDisplayText(text); FontAlias fontText = vsDraw.styles[STYLE_FOLDDISPLAYTEXT].font; const int widthFoldDisplayText = static_cast(surface->WidthText(fontText, foldDisplayText)); diff --git a/src/Editor.cxx b/src/Editor.cxx index f269326a2..aa0b9723c 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -7204,6 +7204,14 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) { case SCI_FOLDDISPLAYTEXTGETSTYLE: return foldDisplayTextStyle; + case SCI_SETDEFAULTFOLDDISPLAYTEXT: + SetDefaultFoldDisplayText(CharPtrFromSPtr(lParam)); + Redraw(); + break; + + case SCI_GETDEFAULTFOLDDISPLAYTEXT: + return StringResult(lParam, GetDefaultFoldDisplayText()); + case SCI_TOGGLEFOLD: FoldLine(static_cast(wParam), SC_FOLDACTION_TOGGLE); break; diff --git a/src/UniqueString.h b/src/UniqueString.h index 18c31283e..8d95cb1ab 100644 --- a/src/UniqueString.h +++ b/src/UniqueString.h @@ -11,6 +11,10 @@ namespace Scintilla { +constexpr bool IsNullOrEmpty(const char *text) noexcept { + return text == nullptr || *text == '\0'; +} + using UniqueString = std::unique_ptr; /// Equivalent to strdup but produces a std::unique_ptr allocation to go diff --git a/test/simpleTests.py b/test/simpleTests.py index b5260c8a8..9a170a2f9 100644 --- a/test/simpleTests.py +++ b/test/simpleTests.py @@ -1639,6 +1639,11 @@ class TestStyleAttributes(unittest.TestCase): self.ed.FoldDisplayTextSetStyle(self.ed.SC_FOLDDISPLAYTEXT_BOXED) self.assertEquals(self.ed.FoldDisplayTextGetStyle(), self.ed.SC_FOLDDISPLAYTEXT_BOXED) + def testDefaultFoldDisplayText(self): + self.assertEquals(self.ed.GetDefaultFoldDisplayText(), b"") + self.ed.SetDefaultFoldDisplayText(0, b"...") + self.assertEquals(self.ed.GetDefaultFoldDisplayText(), b"...") + class TestIndices(unittest.TestCase): def setUp(self): self.xite = Xite.xiteFrame -- cgit v1.2.3