Update Scintilla to version 3.7.1
[geany-mirror.git] / scintilla / src / PositionCache.h
blob5a829b76b202e2ed555cd772c58adb0c05b1f669
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 // There are two points for some positions and this enumeration
20 // can choose between the end of the first line or subline
21 // and the start of the next line or subline.
22 enum PointEnd {
23 peDefault = 0x0,
24 peLineEnd = 0x1,
25 peSubLineEnd = 0x2
28 /**
30 class LineLayout {
31 private:
32 friend class LineLayoutCache;
33 int *lineStarts;
34 int lenLineStarts;
35 /// Drawing is only performed for @a maxLineLength characters on each line.
36 int lineNumber;
37 bool inCache;
38 public:
39 enum { wrapWidthInfinite = 0x7ffffff };
41 int maxLineLength;
42 int numCharsInLine;
43 int numCharsBeforeEOL;
44 enum validLevel { llInvalid, llCheckTextAndStyle, llPositions, llLines } validity;
45 int xHighlightGuide;
46 bool highlightColumn;
47 bool containsCaret;
48 int edgeColumn;
49 char *chars;
50 unsigned char *styles;
51 XYPOSITION *positions;
52 char bracePreviousStyles[2];
54 // Hotspot support
55 Range hotspot;
57 // Wrapped line support
58 int widthLine;
59 int lines;
60 XYPOSITION wrapIndent; // In pixels
62 explicit LineLayout(int maxLineLength_);
63 virtual ~LineLayout();
64 void Resize(int maxLineLength_);
65 void Free();
66 void Invalidate(validLevel validity_);
67 int LineStart(int line) const;
68 int LineLastVisible(int line) const;
69 Range SubLineRange(int line) const;
70 bool InLine(int offset, int line) const;
71 void SetLineStart(int line, int start);
72 void SetBracesHighlight(Range rangeLine, const Position braces[],
73 char bracesMatchStyle, int xHighlight, bool ignoreStyle);
74 void RestoreBracesHighlight(Range rangeLine, const Position braces[], bool ignoreStyle);
75 int FindBefore(XYPOSITION x, int lower, int upper) const;
76 int FindPositionFromX(XYPOSITION x, Range range, bool charPosition) const;
77 Point PointFromPosition(int posInLine, int lineHeight, PointEnd pe) const;
78 int EndLineStyle() const;
81 /**
83 class LineLayoutCache {
84 int level;
85 std::vector<LineLayout *>cache;
86 bool allInvalidated;
87 int styleClock;
88 int useCount;
89 void Allocate(size_t length_);
90 void AllocateForLevel(int linesOnScreen, int linesInDoc);
91 public:
92 LineLayoutCache();
93 virtual ~LineLayoutCache();
94 void Deallocate();
95 enum {
96 llcNone=SC_CACHE_NONE,
97 llcCaret=SC_CACHE_CARET,
98 llcPage=SC_CACHE_PAGE,
99 llcDocument=SC_CACHE_DOCUMENT
101 void Invalidate(LineLayout::validLevel validity_);
102 void SetLevel(int level_);
103 int GetLevel() const { return level; }
104 LineLayout *Retrieve(int lineNumber, int lineCaret, int maxChars, int styleClock_,
105 int linesOnScreen, int linesInDoc);
106 void Dispose(LineLayout *ll);
109 class PositionCacheEntry {
110 unsigned int styleNumber:8;
111 unsigned int len:8;
112 unsigned int clock:16;
113 XYPOSITION *positions;
114 public:
115 PositionCacheEntry();
116 ~PositionCacheEntry();
117 void Set(unsigned int styleNumber_, const char *s_, unsigned int len_, XYPOSITION *positions_, unsigned int clock_);
118 void Clear();
119 bool Retrieve(unsigned int styleNumber_, const char *s_, unsigned int len_, XYPOSITION *positions_) const;
120 static unsigned int Hash(unsigned int styleNumber_, const char *s, unsigned int len);
121 bool NewerThan(const PositionCacheEntry &other) const;
122 void ResetClock();
125 class Representation {
126 public:
127 std::string stringRep;
128 explicit Representation(const char *value="") : stringRep(value) {
132 typedef std::map<int, Representation> MapRepresentation;
134 class SpecialRepresentations {
135 MapRepresentation mapReprs;
136 short startByteHasReprs[0x100];
137 public:
138 SpecialRepresentations();
139 void SetRepresentation(const char *charBytes, const char *value);
140 void ClearRepresentation(const char *charBytes);
141 const Representation *RepresentationFromCharacter(const char *charBytes, size_t len) const;
142 bool Contains(const char *charBytes, size_t len) const;
143 void Clear();
146 struct TextSegment {
147 int start;
148 int length;
149 const Representation *representation;
150 TextSegment(int start_=0, int length_=0, const Representation *representation_=0) :
151 start(start_), length(length_), representation(representation_) {
153 int end() const {
154 return start + length;
158 // Class to break a line of text into shorter runs at sensible places.
159 class BreakFinder {
160 const LineLayout *ll;
161 Range lineRange;
162 int posLineStart;
163 int nextBreak;
164 std::vector<int> selAndEdge;
165 unsigned int saeCurrentPos;
166 int saeNext;
167 int subBreak;
168 const Document *pdoc;
169 EncodingFamily encodingFamily;
170 const SpecialRepresentations *preprs;
171 void Insert(int val);
172 // Private so BreakFinder objects can not be copied
173 BreakFinder(const BreakFinder &);
174 public:
175 // If a whole run is longer than lengthStartSubdivision then subdivide
176 // into smaller runs at spaces or punctuation.
177 enum { lengthStartSubdivision = 300 };
178 // Try to make each subdivided run lengthEachSubdivision or shorter.
179 enum { lengthEachSubdivision = 100 };
180 BreakFinder(const LineLayout *ll_, const Selection *psel, Range rangeLine_, int posLineStart_,
181 int xStart, bool breakForSelection, const Document *pdoc_, const SpecialRepresentations *preprs_, const ViewStyle *pvsDraw);
182 ~BreakFinder();
183 TextSegment Next();
184 bool More() const;
187 class PositionCache {
188 std::vector<PositionCacheEntry> pces;
189 unsigned int clock;
190 bool allClear;
191 // Private so PositionCache objects can not be copied
192 PositionCache(const PositionCache &);
193 public:
194 PositionCache();
195 ~PositionCache();
196 void Clear();
197 void SetSize(size_t size_);
198 size_t GetSize() const { return pces.size(); }
199 void MeasureWidths(Surface *surface, const ViewStyle &vstyle, unsigned int styleNumber,
200 const char *s, unsigned int len, XYPOSITION *positions, Document *pdoc);
203 inline bool IsSpaceOrTab(int ch) {
204 return ch == ' ' || ch == '\t';
207 #ifdef SCI_NAMESPACE
209 #endif
211 #endif