From 1b763e30f00b6f03d506bf1e1fa0c6cb5264205e Mon Sep 17 00:00:00 2001 From: Neil Date: Sun, 9 Apr 2017 16:51:46 +1000 Subject: Modernise ViewStyle with vector, unique_ptr, any_of, and method deletion. --- src/EditView.cxx | 2 +- src/Editor.cxx | 12 ++--- src/Style.h | 4 +- src/ViewStyle.cxx | 154 ++++++++++++++++++++++++------------------------------ src/ViewStyle.h | 29 +++++----- 5 files changed, 93 insertions(+), 108 deletions(-) (limited to 'src') diff --git a/src/EditView.cxx b/src/EditView.cxx index e2fe664a7..d3ebeeda5 100644 --- a/src/EditView.cxx +++ b/src/EditView.cxx @@ -1667,7 +1667,7 @@ void EditView::DrawForeground(Surface *surface, const EditModel &model, const Vi if (vsDraw.hotspotColours.fore.isSet) textFore = vsDraw.hotspotColours.fore; } - if (vsDraw.indicatorsSetFore > 0) { + if (vsDraw.indicatorsSetFore) { // At least one indicator sets the text colour so see if it applies to this segment for (Decoration *deco = model.pdoc->decorations.Root(); deco; deco = deco->Next()) { const int indicatorValue = deco->rs.ValueAt(ts.start + posLineStart); diff --git a/src/Editor.cxx b/src/Editor.cxx index 15f1c9c29..78b1845db 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -4337,10 +4337,10 @@ bool Editor::PointInSelMargin(Point pt) const { Window::Cursor Editor::GetMarginCursor(Point pt) const { int x = 0; - for (size_t margin = 0; margin < vs.ms.size(); margin++) { - if ((pt.x >= x) && (pt.x < x + vs.ms[margin].width)) - return static_cast(vs.ms[margin].cursor); - x += vs.ms[margin].width; + for (const MarginStyle &m : vs.ms) { + if ((pt.x >= x) && (pt.x < x + m.width)) + return static_cast(m.cursor); + x += m.width; } return Window::cursorReverseArrow; } @@ -4649,7 +4649,7 @@ bool Editor::PointIsHotspot(Point pt) { void Editor::SetHoverIndicatorPosition(Sci::Position position) { const Sci::Position hoverIndicatorPosPrev = hoverIndicatorPos; hoverIndicatorPos = INVALID_POSITION; - if (vs.indicatorsDynamic == 0) + if (!vs.indicatorsDynamic) return; if (position != INVALID_POSITION) { for (Decoration *deco = pdoc->decorations.Root(); deco; deco = deco->Next()) { @@ -4666,7 +4666,7 @@ void Editor::SetHoverIndicatorPosition(Sci::Position position) { } void Editor::SetHoverIndicatorPoint(Point pt) { - if (vs.indicatorsDynamic == 0) { + if (!vs.indicatorsDynamic) { SetHoverIndicatorPosition(INVALID_POSITION); } else { SetHoverIndicatorPosition(PositionFromLocation(pt, true, true)); diff --git a/src/Style.h b/src/Style.h index cc9148af6..b3b3852da 100644 --- a/src/Style.h +++ b/src/Style.h @@ -33,8 +33,8 @@ struct FontSpecification { // Just like Font but only has a copy of the FontID so should not delete it class FontAlias : public Font { - // Private so FontAlias objects can not be assigned except for intiialization - FontAlias &operator=(const FontAlias &); + // FontAlias objects can not be assigned except for initialization + FontAlias &operator=(const FontAlias &) = delete; public: FontAlias(); FontAlias(const FontAlias &); diff --git a/src/ViewStyle.cxx b/src/ViewStyle.cxx index 701c51689..354124cd1 100644 --- a/src/ViewStyle.cxx +++ b/src/ViewStyle.cxx @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include "Platform.h" @@ -30,8 +32,8 @@ using namespace Scintilla; #endif -MarginStyle::MarginStyle() : - style(SC_MARGIN_SYMBOL), width(0), mask(0), sensitive(false), cursor(SC_CURSORREVERSEARROW) { +MarginStyle::MarginStyle(int style_, int width_, int mask_) : + style(style_), width(width_), mask(mask_), sensitive(false), cursor(SC_CURSORREVERSEARROW) { } // A list of the fontnames - avoids wasting space in each style @@ -43,8 +45,8 @@ FontNames::~FontNames() { } void FontNames::Clear() { - for (std::vector::const_iterator it=names.begin(); it != names.end(); ++it) { - delete []*it; + for (const char *name : names) { + delete []name; } names.clear(); } @@ -53,13 +55,13 @@ const char *FontNames::Save(const char *name) { if (!name) return 0; - for (std::vector::const_iterator it=names.begin(); it != names.end(); ++it) { - if (strcmp(*it, name) == 0) { - return *it; + for (const char *nm : names) { + if (strcmp(nm, name) == 0) { + return nm; } } const size_t lenName = strlen(name) + 1; - char *nameSave = new char[lenName]; + char *nameSave(new char[lenName]); memcpy(nameSave, name, lenName); names.push_back(nameSave); return nameSave; @@ -88,31 +90,27 @@ void FontRealised::Realise(Surface &surface, int zoomLevel, int technology, cons spaceWidth = surface.WidthChar(font, ' '); } -ViewStyle::ViewStyle() { +ViewStyle::ViewStyle() : markers(MARKER_MAX + 1), indicators(INDIC_MAX + 1) { Init(); } -ViewStyle::ViewStyle(const ViewStyle &source) { +// Copy constructor only called when printing copies the screen ViewStyle so it can be +// modified for printing styles. +ViewStyle::ViewStyle(const ViewStyle &source) : markers(MARKER_MAX + 1), indicators(INDIC_MAX + 1) { Init(source.styles.size()); - for (unsigned int sty=0; stysecond; - } fonts.clear(); } @@ -197,11 +192,11 @@ void ViewStyle::CalculateMarginWidthAndMask() { fixedColumnWidth = marginInside ? leftMarginWidth : 0; maskInLine = 0xffffffff; int maskDefinedMarkers = 0; - for (size_t margin = 0; margin < ms.size(); margin++) { - fixedColumnWidth += ms[margin].width; - if (ms[margin].width > 0) - maskInLine &= ~ms[margin].mask; - maskDefinedMarkers |= ms[margin].mask; + for (const MarginStyle &m : ms) { + fixedColumnWidth += m.width; + if (m.width > 0) + maskInLine &= ~m.mask; + maskDefinedMarkers |= m.mask; } maskDrawInText = 0; for (int markBit = 0; markBit < 32; markBit++) { @@ -233,8 +228,8 @@ void ViewStyle::Init(size_t stylesSize_) { indicators[2] = Indicator(INDIC_PLAIN, ColourDesired(0xff, 0, 0)); technology = SC_TECHNOLOGY_DEFAULT; - indicatorsDynamic = 0; - indicatorsSetFore = 0; + indicatorsDynamic = false; + indicatorsSetFore = false; lineHeight = 1; lineOverlap = 0; maxAscent = 1; @@ -283,15 +278,9 @@ void ViewStyle::Init(size_t stylesSize_) { leftMarginWidth = 1; rightMarginWidth = 1; ms.resize(SC_MAX_MARGIN + 1); - ms[0].style = SC_MARGIN_NUMBER; - ms[0].width = 0; - ms[0].mask = 0; - ms[1].style = SC_MARGIN_SYMBOL; - ms[1].width = 16; - ms[1].mask = ~SC_MASK_FOLDERS; - ms[2].style = SC_MARGIN_SYMBOL; - ms[2].width = 0; - ms[2].mask = 0; + ms[0] = MarginStyle(SC_MARGIN_NUMBER); + ms[1] = MarginStyle(SC_MARGIN_SYMBOL, 16, ~SC_MASK_FOLDERS); + ms[2] = MarginStyle(SC_MARGIN_SYMBOL); marginInside = true; CalculateMarginWidthAndMask(); textStart = marginInside ? fixedColumnWidth : leftMarginWidth; @@ -327,39 +316,39 @@ void ViewStyle::Init(size_t stylesSize_) { } void ViewStyle::Refresh(Surface &surface, int tabInChars) { - for (FontMap::iterator it = fonts.begin(); it != fonts.end(); ++it) { - delete it->second; - } fonts.clear(); selbar = Platform::Chrome(); selbarlight = Platform::ChromeHighlight(); - for (unsigned int i=0; isecond->Realise(surface, zoomLevel, technology, it->first); + // Ask platform to allocate each unique font. + for (std::pair> &font : fonts) { + font.second->Realise(surface, zoomLevel, technology, font.first); } - for (unsigned int k=0; kfont, *fr); - } - indicatorsDynamic = 0; - indicatorsSetFore = 0; - for (int ind = 0; ind <= INDIC_MAX; ind++) { - if (indicators[ind].IsDynamic()) - indicatorsDynamic++; - if (indicators[ind].OverridesTextFore()) - indicatorsSetFore++; + // Set the platform font handle and measurements for each style. + for (Style &style : styles) { + FontRealised *fr = Find(style); + style.Copy(fr->font, *fr); } + + indicatorsDynamic = std::any_of(indicators.cbegin(), indicators.cend(), + [](const Indicator &indicator) { return indicator.IsDynamic(); }); + + indicatorsSetFore = std::any_of(indicators.cbegin(), indicators.cend(), + [](const Indicator &indicator) { return indicator.OverridesTextFore(); }); + maxAscent = 1; maxDescent = 1; FindMaxAscentDescent(); @@ -372,16 +361,11 @@ void ViewStyle::Refresh(Surface &surface, int tabInChars) { if (lineOverlap > lineHeight) lineOverlap = lineHeight; - someStylesProtected = false; - someStylesForceCase = false; - for (unsigned int l=0; lGetHeight() > largestMarkerHeight) - largestMarkerHeight = markers[m].pxpm->GetHeight(); + if (marker.pxpm && marker.pxpm->GetHeight() > largestMarkerHeight) + largestMarkerHeight = marker.pxpm->GetHeight(); break; case SC_MARK_RGBAIMAGE: - if (markers[m].image && markers[m].image->GetHeight() > largestMarkerHeight) - largestMarkerHeight = markers[m].image->GetHeight(); + if (marker.image && marker.image->GetHeight() > largestMarkerHeight) + largestMarkerHeight = marker.image->GetHeight(); break; } } @@ -610,24 +594,24 @@ void ViewStyle::CreateAndAddFont(const FontSpecification &fs) { if (fs.fontName) { FontMap::iterator it = fonts.find(fs); if (it == fonts.end()) { - fonts[fs] = new FontRealised(); + fonts[fs] = std::unique_ptr(new FontRealised()); } } } FontRealised *ViewStyle::Find(const FontSpecification &fs) { if (!fs.fontName) // Invalid specification so return arbitrary object - return fonts.begin()->second; + return fonts.begin()->second.get(); FontMap::iterator it = fonts.find(fs); if (it != fonts.end()) { // Should always reach here since map was just set for all styles - return it->second; + return it->second.get(); } return 0; } void ViewStyle::FindMaxAscentDescent() { - for (FontMap::const_iterator it = fonts.begin(); it != fonts.end(); ++it) { + for (FontMap::const_iterator it = fonts.cbegin(); it != fonts.cend(); ++it) { if (maxAscent < it->second->ascent) maxAscent = it->second->ascent; if (maxDescent < it->second->descent) diff --git a/src/ViewStyle.h b/src/ViewStyle.h index db415344b..f08fb777a 100644 --- a/src/ViewStyle.h +++ b/src/ViewStyle.h @@ -22,17 +22,18 @@ public: int mask; bool sensitive; int cursor; - MarginStyle(); + MarginStyle(int style_= SC_MARGIN_SYMBOL, int width_=0, int mask_=0); }; /** */ class FontNames { private: - std::vector names; + std::vector names; - // Private so FontNames objects can not be copied - FontNames(const FontNames &); + // FontNames objects can not be copied + FontNames(const FontNames &) = delete; + FontNames &operator=(const FontNames &) = delete; public: FontNames(); ~FontNames(); @@ -41,9 +42,9 @@ public: }; class FontRealised : public FontMeasurements { - // Private so FontRealised objects can not be copied - FontRealised(const FontRealised &); - FontRealised &operator=(const FontRealised &); + // FontRealised objects can not be copied + FontRealised(const FontRealised &) = delete; + FontRealised &operator=(const FontRealised &) = delete; public: Font font; FontRealised(); @@ -57,7 +58,7 @@ enum WhiteSpaceVisibility {wsInvisible=0, wsVisibleAlways=1, wsVisibleAfterInden enum TabDrawMode {tdLongArrow=0, tdStrikeOut=1}; -typedef std::map FontMap; +typedef std::map> FontMap; enum WrapMode { eWrapNone, eWrapWord, eWrapChar, eWrapWhitespace }; @@ -94,11 +95,11 @@ class ViewStyle { public: std::vector