]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/WindowAccessor.cxx
db2f938f301fe27f3ee538c709911a2fd1aae9ea
[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 WindowAccessor::~WindowAccessor() {
21 }
22
23 #if PLAT_WIN
24 bool WindowAccessor::InternalIsLeadByte(char ch) {
25 if (SC_CP_UTF8 == codePage)
26 // For lexing, all characters >= 0x80 are treated the
27 // same so none is considered a lead byte.
28 return false;
29 else
30 return IsDBCSLeadByteEx(codePage, ch);
31 }
32 #else
33 // PLAT_GTK or PLAT_WX
34 // TODO: support DBCS under GTK+ and WX
35 bool WindowAccessor::InternalIsLeadByte(char) {
36 return false;
37 }
38 #endif
39
40 void WindowAccessor::Fill(int position) {
41 if (lenDoc == -1)
42 lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0);
43 startPos = position - slopSize;
44 if (startPos + bufferSize > lenDoc)
45 startPos = lenDoc - bufferSize;
46 if (startPos < 0)
47 startPos = 0;
48 endPos = startPos + bufferSize;
49 if (endPos > lenDoc)
50 endPos = lenDoc;
51
52 TextRange tr = {{startPos, endPos}, buf};
53 Platform::SendScintilla(id, SCI_GETTEXTRANGE, 0, reinterpret_cast<long>(&tr));
54 }
55
56 char WindowAccessor::StyleAt(int position) {
57 return static_cast<char>(Platform::SendScintilla(
58 id, SCI_GETSTYLEAT, position, 0));
59 }
60
61 int WindowAccessor::GetLine(int position) {
62 return Platform::SendScintilla(id, SCI_LINEFROMPOSITION, position, 0);
63 }
64
65 int WindowAccessor::LineStart(int line) {
66 return Platform::SendScintilla(id, SCI_POSITIONFROMLINE, line, 0);
67 }
68
69 int WindowAccessor::LevelAt(int line) {
70 return Platform::SendScintilla(id, SCI_GETFOLDLEVEL, line, 0);
71 }
72
73 int WindowAccessor::Length() {
74 if (lenDoc == -1)
75 lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0);
76 return lenDoc;
77 }
78
79 int WindowAccessor::GetLineState(int line) {
80 return Platform::SendScintilla(id, SCI_GETLINESTATE, line);
81 }
82
83 int WindowAccessor::SetLineState(int line, int state) {
84 return Platform::SendScintilla(id, SCI_SETLINESTATE, line, state);
85 }
86
87 void WindowAccessor::StartAt(unsigned int start, char chMask) {
88 Platform::SendScintilla(id, SCI_STARTSTYLING, start, chMask);
89 }
90
91 void WindowAccessor::StartSegment(unsigned int pos) {
92 startSeg = pos;
93 }
94
95 void WindowAccessor::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 Platform::SendScintilla(id, SCI_SETSTYLING, pos - startSeg + 1, 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 WindowAccessor::SetLevel(int line, int level) {
120 Platform::SendScintilla(id, SCI_SETFOLDLEVEL, line, level);
121 }
122
123 void WindowAccessor::Flush() {
124 startPos = extremePosition;
125 lenDoc = -1;
126 if (validLen > 0) {
127 Platform::SendScintilla(id, SCI_SETSTYLINGEX, validLen,
128 reinterpret_cast<long>(styleBuf));
129 validLen = 0;
130 }
131 }
132
133 int WindowAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
134 int end = Length();
135 int spaceFlags = 0;
136
137 // Determines the indentation level of the current line and also checks for consistent
138 // indentation compared to the previous line.
139 // Indentation is judged consistent when the indentation whitespace of each line lines
140 // the same or the indentation of one line is a prefix of the other.
141
142 int pos = LineStart(line);
143 char ch = (*this)[pos];
144 int indent = 0;
145 bool inPrevPrefix = line > 0;
146 int posPrev = inPrevPrefix ? LineStart(line-1) : 0;
147 while ((ch == ' ' || ch == '\t') && (pos < end)) {
148 if (inPrevPrefix) {
149 char chPrev = (*this)[posPrev++];
150 if (chPrev == ' ' || chPrev == '\t') {
151 if (chPrev != ch)
152 spaceFlags |= wsInconsistent;
153 } else {
154 inPrevPrefix = false;
155 }
156 }
157 if (ch == ' ') {
158 spaceFlags |= wsSpace;
159 indent++;
160 } else { // Tab
161 spaceFlags |= wsTab;
162 if (spaceFlags & wsSpace)
163 spaceFlags |= wsSpaceTab;
164 indent = (indent / 8 + 1) * 8;
165 }
166 ch = (*this)[++pos];
167 }
168
169 *flags = spaceFlags;
170 indent += SC_FOLDLEVELBASE;
171 // if completely empty line or the start of a comment...
172 if (isspace(ch) || (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) )
173 return indent | SC_FOLDLEVELWHITEFLAG;
174 else
175 return indent;
176 }
177