1 // Scintilla source code edit control
\r
2 /** @file WindowAccessor.cxx
\r
3 ** Rapid easy access to contents of a Scintilla.
\r
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
\r
6 // The License.txt file describes the conditions under which this software may be distributed.
\r
13 #include "Platform.h"
\r
15 #include "PropSet.h"
\r
16 #include "Accessor.h"
\r
17 #include "WindowAccessor.h"
\r
18 #include "Scintilla.h"
\r
20 #ifdef SCI_NAMESPACE
\r
21 using namespace Scintilla;
\r
24 WindowAccessor::~WindowAccessor() {
\r
27 bool WindowAccessor::InternalIsLeadByte(char ch) {
\r
28 if (SC_CP_UTF8 == codePage)
\r
29 // For lexing, all characters >= 0x80 are treated the
\r
30 // same so none is considered a lead byte.
\r
33 return Platform::IsDBCSLeadByte(codePage, ch);
\r
36 void WindowAccessor::Fill(int position) {
\r
38 lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0);
\r
39 startPos = position - slopSize;
\r
40 if (startPos + bufferSize > lenDoc)
\r
41 startPos = lenDoc - bufferSize;
\r
44 endPos = startPos + bufferSize;
\r
45 if (endPos > lenDoc)
\r
48 TextRange tr = {{startPos, endPos}, buf};
\r
49 Platform::SendScintillaPointer(id, SCI_GETTEXTRANGE, 0, &tr);
\r
52 bool WindowAccessor::Match(int pos, const char *s) {
\r
53 for (int i=0; *s; i++) {
\r
54 if (*s != SafeGetCharAt(pos+i))
\r
61 char WindowAccessor::StyleAt(int position) {
\r
62 return static_cast<char>(Platform::SendScintilla(
\r
63 id, SCI_GETSTYLEAT, position, 0));
\r
66 int WindowAccessor::GetLine(int position) {
\r
67 return Platform::SendScintilla(id, SCI_LINEFROMPOSITION, position, 0);
\r
70 int WindowAccessor::LineStart(int line) {
\r
71 return Platform::SendScintilla(id, SCI_POSITIONFROMLINE, line, 0);
\r
74 int WindowAccessor::LevelAt(int line) {
\r
75 return Platform::SendScintilla(id, SCI_GETFOLDLEVEL, line, 0);
\r
78 int WindowAccessor::Length() {
\r
80 lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0);
\r
84 int WindowAccessor::GetLineState(int line) {
\r
85 return Platform::SendScintilla(id, SCI_GETLINESTATE, line);
\r
88 int WindowAccessor::SetLineState(int line, int state) {
\r
89 return Platform::SendScintilla(id, SCI_SETLINESTATE, line, state);
\r
92 void WindowAccessor::StartAt(unsigned int start, char chMask) {
\r
93 Platform::SendScintilla(id, SCI_STARTSTYLING, start, chMask);
\r
96 void WindowAccessor::StartSegment(unsigned int pos) {
\r
100 void WindowAccessor::ColourTo(unsigned int pos, int chAttr) {
\r
101 // Only perform styling if non empty range
\r
102 if (pos != startSeg - 1) {
\r
103 if (pos < startSeg) {
\r
104 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg, pos);
\r
107 if (validLen + (pos - startSeg + 1) >= bufferSize)
\r
109 if (validLen + (pos - startSeg + 1) >= bufferSize) {
\r
110 // Too big for buffer so send directly
\r
111 Platform::SendScintilla(id, SCI_SETSTYLING, pos - startSeg + 1, chAttr);
\r
113 if (chAttr != chWhile)
\r
116 for (unsigned int i = startSeg; i <= pos; i++) {
\r
117 styleBuf[validLen++] = static_cast<char>(chAttr);
\r
124 void WindowAccessor::SetLevel(int line, int level) {
\r
125 Platform::SendScintilla(id, SCI_SETFOLDLEVEL, line, level);
\r
128 void WindowAccessor::Flush() {
\r
129 startPos = extremePosition;
\r
131 if (validLen > 0) {
\r
132 Platform::SendScintillaPointer(id, SCI_SETSTYLINGEX, validLen,
\r
138 int WindowAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
\r
139 int end = Length();
\r
140 int spaceFlags = 0;
\r
142 // Determines the indentation level of the current line and also checks for consistent
\r
143 // indentation compared to the previous line.
\r
144 // Indentation is judged consistent when the indentation whitespace of each line lines
\r
145 // the same or the indentation of one line is a prefix of the other.
\r
147 int pos = LineStart(line);
\r
148 char ch = (*this)[pos];
\r
150 bool inPrevPrefix = line > 0;
\r
151 int posPrev = inPrevPrefix ? LineStart(line-1) : 0;
\r
152 while ((ch == ' ' || ch == '\t') && (pos < end)) {
\r
153 if (inPrevPrefix) {
\r
154 char chPrev = (*this)[posPrev++];
\r
155 if (chPrev == ' ' || chPrev == '\t') {
\r
157 spaceFlags |= wsInconsistent;
\r
159 inPrevPrefix = false;
\r
163 spaceFlags |= wsSpace;
\r
166 spaceFlags |= wsTab;
\r
167 if (spaceFlags & wsSpace)
\r
168 spaceFlags |= wsSpaceTab;
\r
169 indent = (indent / 8 + 1) * 8;
\r
171 ch = (*this)[++pos];
\r
174 *flags = spaceFlags;
\r
175 indent += SC_FOLDLEVELBASE;
\r
176 // if completely empty line or the start of a comment...
\r
177 if (isspace(ch) || (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) )
\r
178 return indent | SC_FOLDLEVELWHITEFLAG;
\r
183 void WindowAccessor::IndicatorFill(int start, int end, int indicator, int value) {
\r
184 Platform::SendScintilla(id, SCI_SETINDICATORCURRENT, indicator);
\r
186 Platform::SendScintilla(id, SCI_SETINDICATORVALUE, value);
\r
187 Platform::SendScintilla(id, SCI_INDICATORFILLRANGE, start, end - start);
\r
189 Platform::SendScintilla(id, SCI_INDICATORCLEARRANGE, start, end - start);
\r