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
;
40 unsigned char *styles
;
41 XYPOSITION
*positions
;
42 char bracePreviousStyles
[2];
47 // Wrapped line support
50 XYPOSITION wrapIndent
; // In pixels
52 explicit LineLayout(int maxLineLength_
);
53 virtual ~LineLayout();
54 void Resize(int maxLineLength_
);
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;
73 class LineLayoutCache
{
75 std::vector
<LineLayout
*>cache
;
79 void Allocate(size_t length_
);
80 void AllocateForLevel(int linesOnScreen
, int linesInDoc
);
83 virtual ~LineLayoutCache();
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;
102 unsigned int clock
:16;
103 XYPOSITION
*positions
;
105 PositionCacheEntry();
106 ~PositionCacheEntry();
107 void Set(unsigned int styleNumber_
, const char *s_
, unsigned int len_
, XYPOSITION
*positions_
, unsigned int clock
);
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;
115 class Representation
{
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];
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;
139 const Representation
*representation
;
140 TextSegment(int start_
=0, int length_
=0, const Representation
*representation_
=0) :
141 start(start_
), length(length_
), representation(representation_
) {
144 return start
+ length
;
148 // Class to break a line of text into shorter runs at sensible places.
150 const LineLayout
*ll
;
155 std::vector
<int> selAndEdge
;
156 unsigned int saeCurrentPos
;
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
&);
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_
);
178 class PositionCache
{
179 std::vector
<PositionCacheEntry
> pces
;
182 // Private so PositionCache objects can not be copied
183 PositionCache(const PositionCache
&);
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';