Update Scintilla to 3.7.2
[TortoiseGit.git] / ext / scintilla / src / PositionCache.h
blob7daa8c98b838b8e0f8fb046e5273a699254f1c8a
1 // Scintilla source code edit control
2 /** @file PositionCache.h
3 ** Classes for caching layout information.
4 **/
5 // Copyright 1998-2009 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #ifndef POSITIONCACHE_H
9 #define POSITIONCACHE_H
11 #ifdef SCI_NAMESPACE
12 namespace Scintilla {
13 #endif
15 static inline bool IsEOLChar(char ch) {
16 return (ch == '\r') || (ch == '\n');
19 /**
20 * A point in document space.
21 * Uses double for sufficient resolution in large (>20,000,000 line) documents.
23 class PointDocument {
24 public:
25 double x;
26 double y;
28 explicit PointDocument(double x_ = 0, double y_ = 0) : x(x_), y(y_) {
31 // Conversion from Point.
32 explicit PointDocument(Point pt) : x(pt.x), y(pt.y) {
36 // There are two points for some positions and this enumeration
37 // can choose between the end of the first line or subline
38 // and the start of the next line or subline.
39 enum PointEnd {
40 peDefault = 0x0,
41 peLineEnd = 0x1,
42 peSubLineEnd = 0x2
45 /**
47 class LineLayout {
48 private:
49 friend class LineLayoutCache;
50 int *lineStarts;
51 int lenLineStarts;
52 /// Drawing is only performed for @a maxLineLength characters on each line.
53 int lineNumber;
54 bool inCache;
55 public:
56 enum { wrapWidthInfinite = 0x7ffffff };
58 int maxLineLength;
59 int numCharsInLine;
60 int numCharsBeforeEOL;
61 enum validLevel { llInvalid, llCheckTextAndStyle, llPositions, llLines } validity;
62 int xHighlightGuide;
63 bool highlightColumn;
64 bool containsCaret;
65 int edgeColumn;
66 char *chars;
67 unsigned char *styles;
68 XYPOSITION *positions;
69 char bracePreviousStyles[2];
71 // Hotspot support
72 Range hotspot;
74 // Wrapped line support
75 int widthLine;
76 int lines;
77 XYPOSITION wrapIndent; // In pixels
79 explicit LineLayout(int maxLineLength_);
80 virtual ~LineLayout();
81 void Resize(int maxLineLength_);
82 void Free();
83 void Invalidate(validLevel validity_);
84 int LineStart(int line) const;
85 int LineLastVisible(int line) const;
86 Range SubLineRange(int line) const;
87 bool InLine(int offset, int line) const;
88 void SetLineStart(int line, int start);
89 void SetBracesHighlight(Range rangeLine, const Position braces[],
90 char bracesMatchStyle, int xHighlight, bool ignoreStyle);
91 void RestoreBracesHighlight(Range rangeLine, const Position braces[], bool ignoreStyle);
92 int FindBefore(XYPOSITION x, int lower, int upper) const;
93 int FindPositionFromX(XYPOSITION x, Range range, bool charPosition) const;
94 Point PointFromPosition(int posInLine, int lineHeight, PointEnd pe) const;
95 int EndLineStyle() const;
98 /**
100 class LineLayoutCache {
101 int level;
102 std::vector<LineLayout *>cache;
103 bool allInvalidated;
104 int styleClock;
105 int useCount;
106 void Allocate(size_t length_);
107 void AllocateForLevel(int linesOnScreen, int linesInDoc);
108 public:
109 LineLayoutCache();
110 virtual ~LineLayoutCache();
111 void Deallocate();
112 enum {
113 llcNone=SC_CACHE_NONE,
114 llcCaret=SC_CACHE_CARET,
115 llcPage=SC_CACHE_PAGE,
116 llcDocument=SC_CACHE_DOCUMENT
118 void Invalidate(LineLayout::validLevel validity_);
119 void SetLevel(int level_);
120 int GetLevel() const { return level; }
121 LineLayout *Retrieve(int lineNumber, int lineCaret, int maxChars, int styleClock_,
122 int linesOnScreen, int linesInDoc);
123 void Dispose(LineLayout *ll);
126 class PositionCacheEntry {
127 unsigned int styleNumber:8;
128 unsigned int len:8;
129 unsigned int clock:16;
130 XYPOSITION *positions;
131 public:
132 PositionCacheEntry();
133 ~PositionCacheEntry();
134 void Set(unsigned int styleNumber_, const char *s_, unsigned int len_, XYPOSITION *positions_, unsigned int clock_);
135 void Clear();
136 bool Retrieve(unsigned int styleNumber_, const char *s_, unsigned int len_, XYPOSITION *positions_) const;
137 static unsigned int Hash(unsigned int styleNumber_, const char *s, unsigned int len);
138 bool NewerThan(const PositionCacheEntry &other) const;
139 void ResetClock();
142 class Representation {
143 public:
144 std::string stringRep;
145 explicit Representation(const char *value="") : stringRep(value) {
149 typedef std::map<int, Representation> MapRepresentation;
151 class SpecialRepresentations {
152 MapRepresentation mapReprs;
153 short startByteHasReprs[0x100];
154 public:
155 SpecialRepresentations();
156 void SetRepresentation(const char *charBytes, const char *value);
157 void ClearRepresentation(const char *charBytes);
158 const Representation *RepresentationFromCharacter(const char *charBytes, size_t len) const;
159 bool Contains(const char *charBytes, size_t len) const;
160 void Clear();
163 struct TextSegment {
164 int start;
165 int length;
166 const Representation *representation;
167 TextSegment(int start_=0, int length_=0, const Representation *representation_=0) :
168 start(start_), length(length_), representation(representation_) {
170 int end() const {
171 return start + length;
175 // Class to break a line of text into shorter runs at sensible places.
176 class BreakFinder {
177 const LineLayout *ll;
178 Range lineRange;
179 int posLineStart;
180 int nextBreak;
181 std::vector<int> selAndEdge;
182 unsigned int saeCurrentPos;
183 int saeNext;
184 int subBreak;
185 const Document *pdoc;
186 EncodingFamily encodingFamily;
187 const SpecialRepresentations *preprs;
188 void Insert(int val);
189 // Private so BreakFinder objects can not be copied
190 BreakFinder(const BreakFinder &);
191 public:
192 // If a whole run is longer than lengthStartSubdivision then subdivide
193 // into smaller runs at spaces or punctuation.
194 enum { lengthStartSubdivision = 300 };
195 // Try to make each subdivided run lengthEachSubdivision or shorter.
196 enum { lengthEachSubdivision = 100 };
197 BreakFinder(const LineLayout *ll_, const Selection *psel, Range rangeLine_, int posLineStart_,
198 int xStart, bool breakForSelection, const Document *pdoc_, const SpecialRepresentations *preprs_, const ViewStyle *pvsDraw);
199 ~BreakFinder();
200 TextSegment Next();
201 bool More() const;
204 class PositionCache {
205 std::vector<PositionCacheEntry> pces;
206 unsigned int clock;
207 bool allClear;
208 // Private so PositionCache objects can not be copied
209 PositionCache(const PositionCache &);
210 public:
211 PositionCache();
212 ~PositionCache();
213 void Clear();
214 void SetSize(size_t size_);
215 size_t GetSize() const { return pces.size(); }
216 void MeasureWidths(Surface *surface, const ViewStyle &vstyle, unsigned int styleNumber,
217 const char *s, unsigned int len, XYPOSITION *positions, Document *pdoc);
220 inline bool IsSpaceOrTab(int ch) {
221 return ch == ' ' || ch == '\t';
224 #ifdef SCI_NAMESPACE
226 #endif
228 #endif