]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/DocumentAccessor.cxx
fixed wxListBox inheritance
[wxWidgets.git] / src / stc / scintilla / src / DocumentAccessor.cxx
1 // SciTE - Scintilla based Text Editor
2 // Accessor.cxx - rapid easy access to contents of a Scintilla
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <stdio.h>
10
11 #include "Platform.h"
12
13 #include "PropSet.h"
14 #include "SVector.h"
15 #include "Accessor.h"
16 #include "DocumentAccessor.h"
17 #include "CellBuffer.h"
18 #include "Scintilla.h"
19 #include "Document.h"
20
21 DocumentAccessor::~DocumentAccessor() {
22 }
23
24 #if PLAT_WIN
25 bool DocumentAccessor::InternalIsLeadByte(char ch) {
26 if (SC_CP_UTF8 == codePage)
27 // For lexing, all characters >= 0x80 are treated the
28 // same so none is considered a lead byte.
29 return false;
30 else
31 return IsDBCSLeadByteEx(codePage, ch);
32 }
33 #else
34 // PLAT_GTK or PLAT_WX
35 // TODO: support DBCS under GTK+ and WX
36 bool DocumentAccessor::InternalIsLeadByte(char) {
37 return false;
38 }
39 #endif
40
41 void DocumentAccessor::Fill(int position) {
42 if (lenDoc == -1)
43 lenDoc = pdoc->Length();
44 startPos = position - slopSize;
45 if (startPos + bufferSize > lenDoc)
46 startPos = lenDoc - bufferSize;
47 if (startPos < 0)
48 startPos = 0;
49 endPos = startPos + bufferSize;
50 if (endPos > lenDoc)
51 endPos = lenDoc;
52
53 pdoc->GetCharRange(buf, startPos, endPos-startPos);
54 buf[endPos-startPos] = '\0';
55 }
56
57 char DocumentAccessor::StyleAt(int position) {
58 return pdoc->StyleAt(position);
59 }
60
61 int DocumentAccessor::GetLine(int position) {
62 return pdoc->LineFromPosition(position);
63 }
64
65 int DocumentAccessor::LineStart(int line) {
66 return pdoc->LineStart(line);
67 }
68
69 int DocumentAccessor::LevelAt(int line) {
70 return pdoc->GetLevel(line);
71 }
72
73 int DocumentAccessor::Length() {
74 if (lenDoc == -1)
75 lenDoc = pdoc->Length();
76 return lenDoc;
77 }
78
79 int DocumentAccessor::GetLineState(int line) {
80 return pdoc->GetLineState(line);
81 }
82
83 int DocumentAccessor::SetLineState(int line, int state) {
84 return pdoc->SetLineState(line, state);
85 }
86
87 void DocumentAccessor::StartAt(unsigned int start, char chMask) {
88 pdoc->StartStyling(start, chMask);
89 }
90
91 void DocumentAccessor::StartSegment(unsigned int pos) {
92 startSeg = pos;
93 }
94
95 void DocumentAccessor::ColourTo(unsigned int pos, int chAttr) {
96 // Only perform styling if non empty range
97 if (pos != startSeg - 1) {
98 if (pos < startSeg) {
99 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg, pos);
100 }
101
102 if (validLen + (pos - startSeg + 1) >= bufferSize)
103 Flush();
104 if (validLen + (pos - startSeg + 1) >= bufferSize) {
105 // Too big for buffer so send directly
106 pdoc->SetStyleFor(pos - startSeg + 1, static_cast<char>(chAttr));
107 } else {
108 if (chAttr != chWhile)
109 chFlags = 0;
110 chAttr |= chFlags;
111 for (unsigned int i = startSeg; i <= pos; i++) {
112 styleBuf[validLen++] = static_cast<char>(chAttr);
113 }
114 }
115 }
116 startSeg = pos+1;
117 }
118
119 void DocumentAccessor::SetLevel(int line, int level) {
120 pdoc->SetLevel(line, level);
121 }
122
123 void DocumentAccessor::Flush() {
124 startPos = extremePosition;
125 lenDoc = -1;
126 if (validLen > 0) {
127 pdoc->SetStyles(validLen, styleBuf);
128 validLen = 0;
129 }
130 }
131
132 int DocumentAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
133 int end = Length();
134 int spaceFlags = 0;
135
136 // Determines the indentation level of the current line and also checks for consistent
137 // indentation compared to the previous line.
138 // Indentation is judged consistent when the indentation whitespace of each line lines
139 // the same or the indentation of one line is a prefix of the other.
140
141 int pos = LineStart(line);
142 char ch = (*this)[pos];
143 int indent = 0;
144 bool inPrevPrefix = line > 0;
145 int posPrev = inPrevPrefix ? LineStart(line-1) : 0;
146 while ((ch == ' ' || ch == '\t') && (pos < end)) {
147 if (inPrevPrefix) {
148 char chPrev = (*this)[posPrev++];
149 if (chPrev == ' ' || chPrev == '\t') {
150 if (chPrev != ch)
151 spaceFlags |= wsInconsistent;
152 } else {
153 inPrevPrefix = false;
154 }
155 }
156 if (ch == ' ') {
157 spaceFlags |= wsSpace;
158 indent++;
159 } else { // Tab
160 spaceFlags |= wsTab;
161 if (spaceFlags & wsSpace)
162 spaceFlags |= wsSpaceTab;
163 indent = (indent / 8 + 1) * 8;
164 }
165 ch = (*this)[++pos];
166 }
167
168 *flags = spaceFlags;
169 indent += SC_FOLDLEVELBASE;
170 // if completely empty line or the start of a comment...
171 if (isspace(ch) || (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) )
172 return indent | SC_FOLDLEVELWHITEFLAG;
173 else
174 return indent;
175 }
176