Update Scintilla to version 3.4.4
[geany-mirror.git] / scintilla / src / PositionCache.h
blob05005e9ac618fa0ee2eb633a487f8e5d424072dc
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 bool containsCaret;
38 int edgeColumn;
39 char *chars;
40 unsigned char *styles;
41 XYPOSITION *positions;
42 char bracePreviousStyles[2];
44 // Hotspot support
45 Range hotspot;
47 // Wrapped line support
48 int widthLine;
49 int lines;
50 XYPOSITION wrapIndent; // In pixels
52 explicit LineLayout(int maxLineLength_);
53 virtual ~LineLayout();
54 void Resize(int maxLineLength_);
55 void Free();
56 void Invalidate(validLevel validity_);
57 int LineStart(int line) const;
58 int LineLastVisible(int line) const;
59 Range SubLineRange(int line) const;
60 bool InLine(int offset, int line) const;
61 void SetLineStart(int line, int start);
62 void SetBracesHighlight(Range rangeLine, const Position braces[],
63 char bracesMatchStyle, int xHighlight, bool ignoreStyle);
64 void RestoreBracesHighlight(Range rangeLine, const Position braces[], bool ignoreStyle);
65 int FindBefore(XYPOSITION x, int lower, int upper) const;
66 int FindPositionFromX(XYPOSITION x, Range range, bool charPosition) const;
67 Point PointFromPosition(int posInLine, int lineHeight) const;
68 int EndLineStyle() const;
71 /**
73 class LineLayoutCache {
74 int level;
75 std::vector<LineLayout *>cache;
76 bool allInvalidated;
77 int styleClock;
78 int useCount;
79 void Allocate(size_t length_);
80 void AllocateForLevel(int linesOnScreen, int linesInDoc);
81 public:
82 LineLayoutCache();
83 virtual ~LineLayoutCache();
84 void Deallocate();
85 enum {
86 llcNone=SC_CACHE_NONE,
87 llcCaret=SC_CACHE_CARET,
88 llcPage=SC_CACHE_PAGE,
89 llcDocument=SC_CACHE_DOCUMENT
91 void Invalidate(LineLayout::validLevel validity_);
92 void SetLevel(int level_);
93 int GetLevel() const { return level; }
94 LineLayout *Retrieve(int lineNumber, int lineCaret, int maxChars, int styleClock_,
95 int linesOnScreen, int linesInDoc);
96 void Dispose(LineLayout *ll);
99 class PositionCacheEntry {
100 unsigned int styleNumber:8;
101 unsigned int len:8;
102 unsigned int clock:16;
103 XYPOSITION *positions;
104 public:
105 PositionCacheEntry();
106 ~PositionCacheEntry();
107 void Set(unsigned int styleNumber_, const char *s_, unsigned int len_, XYPOSITION *positions_, unsigned int clock);
108 void Clear();
109 bool Retrieve(unsigned int styleNumber_, const char *s_, unsigned int len_, XYPOSITION *positions_) const;
110 static unsigned int Hash(unsigned int styleNumber_, const char *s, unsigned int len);
111 bool NewerThan(const PositionCacheEntry &other) const;
112 void ResetClock();
115 class Representation {
116 public:
117 std::string stringRep;
118 explicit Representation(const char *value="") : stringRep(value) {
122 typedef std::map<int, Representation> MapRepresentation;
124 class SpecialRepresentations {
125 MapRepresentation mapReprs;
126 short startByteHasReprs[0x100];
127 public:
128 SpecialRepresentations();
129 void SetRepresentation(const char *charBytes, const char *value);
130 void ClearRepresentation(const char *charBytes);
131 const Representation *RepresentationFromCharacter(const char *charBytes, size_t len) const;
132 bool Contains(const char *charBytes, size_t len) const;
133 void Clear();
136 struct TextSegment {
137 int start;
138 int length;
139 const Representation *representation;
140 TextSegment(int start_=0, int length_=0, const Representation *representation_=0) :
141 start(start_), length(length_), representation(representation_) {
143 int end() const {
144 return start + length;
148 // Class to break a line of text into shorter runs at sensible places.
149 class BreakFinder {
150 const LineLayout *ll;
151 int lineStart;
152 int lineEnd;
153 int posLineStart;
154 int nextBreak;
155 std::vector<int> selAndEdge;
156 unsigned int saeCurrentPos;
157 int saeNext;
158 int subBreak;
159 const Document *pdoc;
160 EncodingFamily encodingFamily;
161 const SpecialRepresentations *preprs;
162 void Insert(int val);
163 // Private so BreakFinder objects can not be copied
164 BreakFinder(const BreakFinder &);
165 public:
166 // If a whole run is longer than lengthStartSubdivision then subdivide
167 // into smaller runs at spaces or punctuation.
168 enum { lengthStartSubdivision = 300 };
169 // Try to make each subdivided run lengthEachSubdivision or shorter.
170 enum { lengthEachSubdivision = 100 };
171 BreakFinder(const LineLayout *ll_, const Selection *psel, int lineStart_, int lineEnd_, int posLineStart_,
172 int xStart, bool breakForSelection, const Document *pdoc_, const SpecialRepresentations *preprs_);
173 ~BreakFinder();
174 TextSegment Next();
175 bool More() const;
178 class PositionCache {
179 std::vector<PositionCacheEntry> pces;
180 unsigned int clock;
181 bool allClear;
182 // Private so PositionCache objects can not be copied
183 PositionCache(const PositionCache &);
184 public:
185 PositionCache();
186 ~PositionCache();
187 void Clear();
188 void SetSize(size_t size_);
189 size_t GetSize() const { return pces.size(); }
190 void MeasureWidths(Surface *surface, const ViewStyle &vstyle, unsigned int styleNumber,
191 const char *s, unsigned int len, XYPOSITION *positions, Document *pdoc);
194 inline bool IsSpaceOrTab(int ch) {
195 return ch == ' ' || ch == '\t';
198 #ifdef SCI_NAMESPACE
200 #endif
202 #endif