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