Update Scintilla to 3.5.0 pre-release
[geany-mirror.git] / scintilla / src / PositionCache.h
blob9d9821f8fae7ea943b372935093c59f81994157a
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 Range lineRange;
152 int posLineStart;
153 int nextBreak;
154 std::vector<int> selAndEdge;
155 unsigned int saeCurrentPos;
156 int saeNext;
157 int subBreak;
158 const Document *pdoc;
159 EncodingFamily encodingFamily;
160 const SpecialRepresentations *preprs;
161 void Insert(int val);
162 // Private so BreakFinder objects can not be copied
163 BreakFinder(const BreakFinder &);
164 public:
165 // If a whole run is longer than lengthStartSubdivision then subdivide
166 // into smaller runs at spaces or punctuation.
167 enum { lengthStartSubdivision = 300 };
168 // Try to make each subdivided run lengthEachSubdivision or shorter.
169 enum { lengthEachSubdivision = 100 };
170 BreakFinder(const LineLayout *ll_, const Selection *psel, Range rangeLine_, int posLineStart_,
171 int xStart, bool breakForSelection, const Document *pdoc_, const SpecialRepresentations *preprs_);
172 ~BreakFinder();
173 TextSegment Next();
174 bool More() const;
177 class PositionCache {
178 std::vector<PositionCacheEntry> pces;
179 unsigned int clock;
180 bool allClear;
181 // Private so PositionCache objects can not be copied
182 PositionCache(const PositionCache &);
183 public:
184 PositionCache();
185 ~PositionCache();
186 void Clear();
187 void SetSize(size_t size_);
188 size_t GetSize() const { return pces.size(); }
189 void MeasureWidths(Surface *surface, const ViewStyle &vstyle, unsigned int styleNumber,
190 const char *s, unsigned int len, XYPOSITION *positions, Document *pdoc);
193 inline bool IsSpaceOrTab(int ch) {
194 return ch == ' ' || ch == '\t';
197 #ifdef SCI_NAMESPACE
199 #endif
201 #endif