1 // Scintilla source code edit control
2 /** @file WindowAccessor.cxx
3 ** Rapid easy access to contents of a Scintilla.
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.
17 #include "WindowAccessor.h"
18 #include "Scintilla.h"
21 using namespace Scintilla
;
24 WindowAccessor::~WindowAccessor() {
27 bool WindowAccessor::InternalIsLeadByte(char ch
) {
28 if (SC_CP_UTF8
== codePage
)
29 // For lexing, all characters >= 0x80 are treated the
30 // same so none is considered a lead byte.
33 return Platform::IsDBCSLeadByte(codePage
, ch
);
36 void WindowAccessor::Fill(int position
) {
38 lenDoc
= Platform::SendScintilla(id
, SCI_GETTEXTLENGTH
, 0, 0);
39 startPos
= position
- slopSize
;
40 if (startPos
+ bufferSize
> lenDoc
)
41 startPos
= lenDoc
- bufferSize
;
44 endPos
= startPos
+ bufferSize
;
48 Sci_TextRange tr
= {{startPos
, endPos
}, buf
};
49 Platform::SendScintillaPointer(id
, SCI_GETTEXTRANGE
, 0, &tr
);
52 bool WindowAccessor::Match(int pos
, const char *s
) {
53 for (int i
=0; *s
; i
++) {
54 if (*s
!= SafeGetCharAt(pos
+i
))
61 char WindowAccessor::StyleAt(int position
) {
62 return static_cast<char>(Platform::SendScintilla(
63 id
, SCI_GETSTYLEAT
, position
, 0));
66 int WindowAccessor::GetLine(int position
) {
67 return Platform::SendScintilla(id
, SCI_LINEFROMPOSITION
, position
, 0);
70 int WindowAccessor::LineStart(int line
) {
71 return Platform::SendScintilla(id
, SCI_POSITIONFROMLINE
, line
, 0);
74 int WindowAccessor::LevelAt(int line
) {
75 return Platform::SendScintilla(id
, SCI_GETFOLDLEVEL
, line
, 0);
78 int WindowAccessor::Length() {
80 lenDoc
= Platform::SendScintilla(id
, SCI_GETTEXTLENGTH
, 0, 0);
84 int WindowAccessor::GetLineState(int line
) {
85 return Platform::SendScintilla(id
, SCI_GETLINESTATE
, line
);
88 int WindowAccessor::SetLineState(int line
, int state
) {
89 return Platform::SendScintilla(id
, SCI_SETLINESTATE
, line
, state
);
92 void WindowAccessor::StartAt(unsigned int start
, char chMask
) {
93 Platform::SendScintilla(id
, SCI_STARTSTYLING
, start
, chMask
);
96 void WindowAccessor::StartSegment(unsigned int pos
) {
100 void WindowAccessor::ColourTo(unsigned int pos
, int chAttr
) {
101 // Only perform styling if non empty range
102 if (pos
!= startSeg
- 1) {
103 if (pos
< startSeg
) {
104 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
107 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
109 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
110 // Too big for buffer so send directly
111 Platform::SendScintilla(id
, SCI_SETSTYLING
, pos
- startSeg
+ 1, chAttr
);
113 if (chAttr
!= chWhile
)
116 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
117 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
124 void WindowAccessor::SetLevel(int line
, int level
) {
125 Platform::SendScintilla(id
, SCI_SETFOLDLEVEL
, line
, level
);
128 void WindowAccessor::Flush() {
129 startPos
= extremePosition
;
132 Platform::SendScintillaPointer(id
, SCI_SETSTYLINGEX
, validLen
,
138 int WindowAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
142 // Determines the indentation level of the current line and also checks for consistent
143 // indentation compared to the previous line.
144 // Indentation is judged consistent when the indentation whitespace of each line lines
145 // the same or the indentation of one line is a prefix of the other.
147 int pos
= LineStart(line
);
148 char ch
= (*this)[pos
];
150 bool inPrevPrefix
= line
> 0;
151 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
152 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
154 char chPrev
= (*this)[posPrev
++];
155 if (chPrev
== ' ' || chPrev
== '\t') {
157 spaceFlags
|= wsInconsistent
;
159 inPrevPrefix
= false;
163 spaceFlags
|= wsSpace
;
167 if (spaceFlags
& wsSpace
)
168 spaceFlags
|= wsSpaceTab
;
169 indent
= (indent
/ 8 + 1) * 8;
175 indent
+= SC_FOLDLEVELBASE
;
176 // if completely empty line or the start of a comment...
177 if (isspace(ch
) || (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
178 return indent
| SC_FOLDLEVELWHITEFLAG
;
183 void WindowAccessor::IndicatorFill(int start
, int end
, int indicator
, int value
) {
184 Platform::SendScintilla(id
, SCI_SETINDICATORCURRENT
, indicator
);
186 Platform::SendScintilla(id
, SCI_SETINDICATORVALUE
, value
);
187 Platform::SendScintilla(id
, SCI_INDICATORFILLRANGE
, start
, end
- start
);
189 Platform::SendScintilla(id
, SCI_INDICATORCLEARRANGE
, start
, end
- start
);