]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/WindowAccessor.cxx
9de106fb78e47b21b4a60be4158e57bd15ed89bb
[wxWidgets.git] / src / stc / scintilla / src / WindowAccessor.cxx
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>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
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
20 #ifdef SCI_NAMESPACE
21 using namespace Scintilla;
22 #endif
23
24 WindowAccessor::~WindowAccessor() {
25 }
26
27 bool WindowAccessor::InternalIsLeadByte(char ch) {
28 if (SC_CP_UTF8 == codePage)
29 // For lexing, all characters >= 0x80 are treated the
30 // same so none is considered a lead byte.
31 return false;
32 else
33 return Platform::IsDBCSLeadByte(codePage, ch);
34 }
35
36 void WindowAccessor::Fill(int position) {
37 if (lenDoc == -1)
38 lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0);
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
48 Sci_TextRange tr = {{startPos, endPos}, buf};
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;
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) {
67 return Platform::SendScintilla(id, SCI_LINEFROMPOSITION, position, 0);
68 }
69
70 int WindowAccessor::LineStart(int line) {
71 return Platform::SendScintilla(id, SCI_POSITIONFROMLINE, line, 0);
72 }
73
74 int WindowAccessor::LevelAt(int line) {
75 return Platform::SendScintilla(id, SCI_GETFOLDLEVEL, line, 0);
76 }
77
78 int WindowAccessor::Length() {
79 if (lenDoc == -1)
80 lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0);
81 return lenDoc;
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) {
132 Platform::SendScintillaPointer(id, SCI_SETSTYLINGEX, validLen,
133 styleBuf);
134 validLen = 0;
135 }
136 }
137
138 int WindowAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
139 int end = Length();
140 int spaceFlags = 0;
141
142 // Determines the indentation level of the current line and also checks for consistent
143 // indentation compared to the previous line.
144 // Indentation is judged consistent when the indentation whitespace of each line lines
145 // the same or the indentation of one line is a prefix of the other.
146
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 }
173
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
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 }