]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/Accessor.cxx
f080742bd62c26a34586fd5904cc5d006eb38b83
[wxWidgets.git] / src / stc / scintilla / src / Accessor.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 <ctype.h>
8 #include <stdio.h>
9
10 #include "Platform.h"
11
12 #include "PropSet.h"
13 #include "Accessor.h"
14 #include "Scintilla.h"
15
16 bool Accessor::InternalIsLeadByte(char ch) {
17 #if PLAT_GTK
18 // TODO: support DBCS under GTK+
19 return false;
20 #elif PLAT_WIN
21 return IsDBCSLeadByteEx(codePage, ch);
22 #elif PLAT_WX
23 return false;
24 #endif
25 }
26
27 void Accessor::Fill(int position) {
28 if (lenDoc == -1)
29 lenDoc = Platform::SendScintilla(id, WM_GETTEXTLENGTH, 0, 0);
30 startPos = position - slopSize;
31 if (startPos + bufferSize > lenDoc)
32 startPos = lenDoc - bufferSize;
33 if (startPos < 0)
34 startPos = 0;
35 endPos = startPos + bufferSize;
36 if (endPos > lenDoc)
37 endPos = lenDoc;
38
39 TEXTRANGE tr = {{startPos, endPos}, buf};
40 Platform::SendScintilla(id, EM_GETTEXTRANGE, 0, reinterpret_cast<LPARAM>(&tr));
41 }
42
43 char Accessor::StyleAt(int position) {
44 return static_cast<char>(Platform::SendScintilla(
45 id, SCI_GETSTYLEAT, position, 0));
46 }
47
48 int Accessor::GetLine(int position) {
49 return Platform::SendScintilla(id, EM_LINEFROMCHAR, position, 0);
50 }
51
52 int Accessor::LineStart(int line) {
53 return Platform::SendScintilla(id, EM_LINEINDEX, line, 0);
54 }
55
56 int Accessor::LevelAt(int line) {
57 return Platform::SendScintilla(id, SCI_GETFOLDLEVEL, line, 0);
58 }
59
60 int Accessor::Length() {
61 if (lenDoc == -1)
62 lenDoc = Platform::SendScintilla(id, WM_GETTEXTLENGTH, 0, 0);
63 return lenDoc;
64 }
65
66 int Accessor::GetLineState(int line) {
67 return Platform::SendScintilla(id, SCI_GETLINESTATE, line);
68 }
69
70 int Accessor::SetLineState(int line, int state) {
71 return Platform::SendScintilla(id, SCI_SETLINESTATE, line, state);
72 }
73
74 void StylingContext::StartAt(unsigned int start, char chMask) {
75 Platform::SendScintilla(id, SCI_STARTSTYLING, start, chMask);
76 }
77
78 void StylingContext::StartSegment(unsigned int pos) {
79 startSeg = pos;
80 }
81
82 void StylingContext::ColourTo(unsigned int pos, int chAttr) {
83 // Only perform styling if non empty range
84 if (pos != startSeg - 1) {
85 if (pos < startSeg) {
86 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg, pos);
87 }
88
89 if (validLen + (pos - startSeg + 1) >= bufferSize)
90 Flush();
91 if (validLen + (pos - startSeg + 1) >= bufferSize) {
92 // Too big for buffer so send directly
93 Platform::SendScintilla(id, SCI_SETSTYLING, pos - startSeg + 1, chAttr);
94 } else {
95 if (chAttr != chWhile)
96 chFlags = 0;
97 chAttr |= chFlags;
98 for (unsigned int i = startSeg; i <= pos; i++) {
99 styleBuf[validLen++] = chAttr;
100 }
101 }
102 }
103 startSeg = pos+1;
104 }
105
106 int StylingContext::GetLine(int position) {
107 return Platform::SendScintilla(id, EM_LINEFROMCHAR, position, 0);
108 }
109
110 void StylingContext::SetLevel(int line, int level) {
111 Platform::SendScintilla(id, SCI_SETFOLDLEVEL, line, level);
112 }
113
114 void StylingContext::Flush() {
115 if (validLen > 0) {
116 Platform::SendScintilla(id, SCI_SETSTYLINGEX, validLen,
117 reinterpret_cast<LPARAM>(styleBuf));
118 validLen = 0;
119 }
120 }
121
122 int StylingContext::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
123 int end = Length();
124 int spaceFlags = 0;
125
126 // Determines the indentation level of the current line and also checks for consistent
127 // indentation compared to the previous line.
128 // Indentation is judged consistent when the indentation whitespace of each line lines
129 // the same or the indentation of one line is a prefix of the other.
130
131 int pos = LineStart(line);
132 char ch = (*this)[pos];
133 int indent = 0;
134 bool inPrevPrefix = line > 0;
135 int posPrev = inPrevPrefix ? LineStart(line-1) : 0;
136 while ((ch == ' ' || ch == '\t') && (pos < end)) {
137 if (inPrevPrefix) {
138 char chPrev = (*this)[posPrev++];
139 if (chPrev == ' ' || chPrev == '\t') {
140 if (chPrev != ch)
141 spaceFlags |= wsInconsistent;
142 } else {
143 inPrevPrefix = false;
144 }
145 }
146 if (ch == ' ') {
147 spaceFlags |= wsSpace;
148 indent++;
149 } else { // Tab
150 spaceFlags |= wsTab;
151 if (spaceFlags & wsSpace)
152 spaceFlags |= wsSpaceTab;
153 indent = (indent / 8 + 1) * 8;
154 }
155 ch = (*this)[++pos];
156 }
157
158 *flags = spaceFlags;
159 indent += SC_FOLDLEVELBASE;
160 // if completely empty line or the start of a comment...
161 if (isspace(ch) || (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) )
162 return indent | SC_FOLDLEVELWHITEFLAG;
163 else
164 return indent;
165 }
166