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