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