1 // Scintilla source code edit control
2 /** @file PositionCache.h
3 ** Classes for caching layout information.
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
15 static inline bool IsEOLChar(char ch
) {
16 return (ch
== '\r') || (ch
== '\n');
23 friend class LineLayoutCache
;
26 /// Drawing is only performed for @a maxLineLength characters on each line.
30 enum { wrapWidthInfinite
= 0x7ffffff };
33 int numCharsBeforeEOL
;
34 enum validLevel
{ llInvalid
, llCheckTextAndStyle
, llPositions
, llLines
} validity
;
41 unsigned char *styles
;
44 XYPOSITION
*positions
;
45 char bracePreviousStyles
[2];
51 // Wrapped line support
54 XYPOSITION wrapIndent
; // In pixels
56 explicit LineLayout(int maxLineLength_
);
57 virtual ~LineLayout();
58 void Resize(int maxLineLength_
);
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;
77 class LineLayoutCache
{
79 std::vector
<LineLayout
*>cache
;
83 void Allocate(size_t length_
);
84 void AllocateForLevel(int linesOnScreen
, int linesInDoc
);
87 virtual ~LineLayoutCache();
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;
106 unsigned int clock
:16;
107 XYPOSITION
*positions
;
109 PositionCacheEntry();
110 ~PositionCacheEntry();
111 void Set(unsigned int styleNumber_
, const char *s_
, unsigned int len_
, XYPOSITION
*positions_
, unsigned int clock
);
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;
119 class Representation
{
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];
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;
143 Representation
*representation
;
144 TextSegment(int start_
=0, int length_
=0, Representation
*representation_
=0) :
145 start(start_
), length(length_
), representation(representation_
) {
148 return start
+ length
;
152 // Class to break a line of text into shorter runs at sensible places.
159 std::vector
<int> selAndEdge
;
160 unsigned int saeCurrentPos
;
164 EncodingFamily encodingFamily
;
165 SpecialRepresentations
*preprs
;
166 void Insert(int val
);
167 // Private so BreakFinder objects can not be copied
168 BreakFinder(const BreakFinder
&);
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_
);
182 class PositionCache
{
183 std::vector
<PositionCacheEntry
> pces
;
186 // Private so PositionCache objects can not be copied
187 PositionCache(const PositionCache
&);
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';