]>
Commit | Line | Data |
---|---|---|
65ec6247 RD |
1 | // Scintilla source code edit control |
2 | /** @file WindowAccessor.cxx | |
3 | ** Rapid easy access to contents of a Scintilla. | |
4 | **/ | |
5 | // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org> | |
f6bcfd97 BP |
6 | // The License.txt file describes the conditions under which this software may be distributed. |
7 | ||
8 | #include <stdlib.h> | |
d134f170 | 9 | #include <string.h> |
88a8b04e | 10 | #include <ctype.h> |
f6bcfd97 BP |
11 | #include <stdio.h> |
12 | ||
13 | #include "Platform.h" | |
14 | ||
15 | #include "PropSet.h" | |
16 | #include "Accessor.h" | |
17 | #include "WindowAccessor.h" | |
18 | #include "Scintilla.h" | |
19 | ||
7e0c58e9 RD |
20 | #ifdef SCI_NAMESPACE |
21 | using namespace Scintilla; | |
22 | #endif | |
23 | ||
f6bcfd97 BP |
24 | WindowAccessor::~WindowAccessor() { |
25 | } | |
26 | ||
27 | bool WindowAccessor::InternalIsLeadByte(char ch) { | |
f6bcfd97 BP |
28 | if (SC_CP_UTF8 == codePage) |
29 | // For lexing, all characters >= 0x80 are treated the | |
30 | // same so none is considered a lead byte. | |
88a8b04e | 31 | return false; |
f6bcfd97 | 32 | else |
1a2fb4cd | 33 | return Platform::IsDBCSLeadByte(codePage, ch); |
d134f170 | 34 | } |
f6bcfd97 BP |
35 | |
36 | void WindowAccessor::Fill(int position) { | |
37 | if (lenDoc == -1) | |
d134f170 | 38 | lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0); |
f6bcfd97 BP |
39 | startPos = position - slopSize; |
40 | if (startPos + bufferSize > lenDoc) | |
41 | startPos = lenDoc - bufferSize; | |
42 | if (startPos < 0) | |
43 | startPos = 0; | |
44 | endPos = startPos + bufferSize; | |
45 | if (endPos > lenDoc) | |
46 | endPos = lenDoc; | |
47 | ||
d134f170 | 48 | TextRange tr = {{startPos, endPos}, buf}; |
a834585d RD |
49 | Platform::SendScintillaPointer(id, SCI_GETTEXTRANGE, 0, &tr); |
50 | } | |
51 | ||
52 | bool WindowAccessor::Match(int pos, const char *s) { | |
53 | for (int i=0; *s; i++) { | |
54 | if (*s != SafeGetCharAt(pos+i)) | |
55 | return false; | |
56 | s++; | |
57 | } | |
58 | return true; | |
f6bcfd97 BP |
59 | } |
60 | ||
61 | char WindowAccessor::StyleAt(int position) { | |
62 | return static_cast<char>(Platform::SendScintilla( | |
63 | id, SCI_GETSTYLEAT, position, 0)); | |
64 | } | |
65 | ||
66 | int WindowAccessor::GetLine(int position) { | |
d134f170 | 67 | return Platform::SendScintilla(id, SCI_LINEFROMPOSITION, position, 0); |
f6bcfd97 BP |
68 | } |
69 | ||
70 | int WindowAccessor::LineStart(int line) { | |
d134f170 | 71 | return Platform::SendScintilla(id, SCI_POSITIONFROMLINE, line, 0); |
f6bcfd97 BP |
72 | } |
73 | ||
74 | int WindowAccessor::LevelAt(int line) { | |
75 | return Platform::SendScintilla(id, SCI_GETFOLDLEVEL, line, 0); | |
76 | } | |
77 | ||
88a8b04e RD |
78 | int WindowAccessor::Length() { |
79 | if (lenDoc == -1) | |
d134f170 | 80 | lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0); |
88a8b04e | 81 | return lenDoc; |
f6bcfd97 BP |
82 | } |
83 | ||
84 | int WindowAccessor::GetLineState(int line) { | |
85 | return Platform::SendScintilla(id, SCI_GETLINESTATE, line); | |
86 | } | |
87 | ||
88 | int WindowAccessor::SetLineState(int line, int state) { | |
89 | return Platform::SendScintilla(id, SCI_SETLINESTATE, line, state); | |
90 | } | |
91 | ||
92 | void WindowAccessor::StartAt(unsigned int start, char chMask) { | |
93 | Platform::SendScintilla(id, SCI_STARTSTYLING, start, chMask); | |
94 | } | |
95 | ||
96 | void WindowAccessor::StartSegment(unsigned int pos) { | |
97 | startSeg = pos; | |
98 | } | |
99 | ||
100 | void WindowAccessor::ColourTo(unsigned int pos, int chAttr) { | |
101 | // Only perform styling if non empty range | |
102 | if (pos != startSeg - 1) { | |
103 | if (pos < startSeg) { | |
104 | Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg, pos); | |
105 | } | |
106 | ||
107 | if (validLen + (pos - startSeg + 1) >= bufferSize) | |
108 | Flush(); | |
109 | if (validLen + (pos - startSeg + 1) >= bufferSize) { | |
110 | // Too big for buffer so send directly | |
111 | Platform::SendScintilla(id, SCI_SETSTYLING, pos - startSeg + 1, chAttr); | |
112 | } else { | |
113 | if (chAttr != chWhile) | |
114 | chFlags = 0; | |
115 | chAttr |= chFlags; | |
116 | for (unsigned int i = startSeg; i <= pos; i++) { | |
117 | styleBuf[validLen++] = static_cast<char>(chAttr); | |
118 | } | |
119 | } | |
120 | } | |
121 | startSeg = pos+1; | |
122 | } | |
123 | ||
124 | void WindowAccessor::SetLevel(int line, int level) { | |
125 | Platform::SendScintilla(id, SCI_SETFOLDLEVEL, line, level); | |
126 | } | |
127 | ||
128 | void WindowAccessor::Flush() { | |
129 | startPos = extremePosition; | |
130 | lenDoc = -1; | |
131 | if (validLen > 0) { | |
88a8b04e | 132 | Platform::SendScintillaPointer(id, SCI_SETSTYLINGEX, validLen, |
a834585d | 133 | styleBuf); |
f6bcfd97 BP |
134 | validLen = 0; |
135 | } | |
136 | } | |
137 | ||
138 | int WindowAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) { | |
139 | int end = Length(); | |
140 | int spaceFlags = 0; | |
88a8b04e RD |
141 | |
142 | // Determines the indentation level of the current line and also checks for consistent | |
f6bcfd97 | 143 | // indentation compared to the previous line. |
88a8b04e | 144 | // Indentation is judged consistent when the indentation whitespace of each line lines |
f6bcfd97 | 145 | // the same or the indentation of one line is a prefix of the other. |
88a8b04e | 146 | |
f6bcfd97 BP |
147 | int pos = LineStart(line); |
148 | char ch = (*this)[pos]; | |
149 | int indent = 0; | |
150 | bool inPrevPrefix = line > 0; | |
151 | int posPrev = inPrevPrefix ? LineStart(line-1) : 0; | |
152 | while ((ch == ' ' || ch == '\t') && (pos < end)) { | |
153 | if (inPrevPrefix) { | |
154 | char chPrev = (*this)[posPrev++]; | |
155 | if (chPrev == ' ' || chPrev == '\t') { | |
156 | if (chPrev != ch) | |
157 | spaceFlags |= wsInconsistent; | |
158 | } else { | |
159 | inPrevPrefix = false; | |
160 | } | |
161 | } | |
162 | if (ch == ' ') { | |
163 | spaceFlags |= wsSpace; | |
164 | indent++; | |
165 | } else { // Tab | |
166 | spaceFlags |= wsTab; | |
167 | if (spaceFlags & wsSpace) | |
168 | spaceFlags |= wsSpaceTab; | |
169 | indent = (indent / 8 + 1) * 8; | |
170 | } | |
171 | ch = (*this)[++pos]; | |
172 | } | |
88a8b04e | 173 | |
f6bcfd97 BP |
174 | *flags = spaceFlags; |
175 | indent += SC_FOLDLEVELBASE; | |
176 | // if completely empty line or the start of a comment... | |
177 | if (isspace(ch) || (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) ) | |
178 | return indent | SC_FOLDLEVELWHITEFLAG; | |
179 | else | |
180 | return indent; | |
181 | } | |
182 | ||
7e0c58e9 RD |
183 | void WindowAccessor::IndicatorFill(int start, int end, int indicator, int value) { |
184 | Platform::SendScintilla(id, SCI_SETINDICATORCURRENT, indicator); | |
185 | if (value) { | |
186 | Platform::SendScintilla(id, SCI_SETINDICATORVALUE, value); | |
187 | Platform::SendScintilla(id, SCI_INDICATORFILLRANGE, start, end - start); | |
188 | } else { | |
189 | Platform::SendScintilla(id, SCI_INDICATORCLEARRANGE, start, end - start); | |
190 | } | |
191 | } |