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.
15 #include "WindowAccessor.h"
16 #include "Scintilla.h"
18 WindowAccessor::~WindowAccessor() {
22 bool WindowAccessor::InternalIsLeadByte(char ch
) {
23 if (SC_CP_UTF8
== codePage
)
24 // For lexing, all characters >= 0x80 are treated the
25 // same so none is considered a lead byte.
28 return IsDBCSLeadByteEx(codePage
, ch
);
31 // PLAT_GTK or PLAT_WX
32 // TODO: support DBCS under GTK+ and WX
33 bool WindowAccessor::InternalIsLeadByte(char) {
38 void WindowAccessor::Fill(int position
) {
40 lenDoc
= Platform::SendScintilla(id
, SCI_GETTEXTLENGTH
, 0, 0);
41 startPos
= position
- slopSize
;
42 if (startPos
+ bufferSize
> lenDoc
)
43 startPos
= lenDoc
- bufferSize
;
46 endPos
= startPos
+ bufferSize
;
50 TextRange tr
= {{startPos
, endPos
}, buf
};
51 Platform::SendScintilla(id
, SCI_GETTEXTRANGE
, 0, reinterpret_cast<long>(&tr
));
54 char WindowAccessor::StyleAt(int position
) {
55 return static_cast<char>(Platform::SendScintilla(
56 id
, SCI_GETSTYLEAT
, position
, 0));
59 int WindowAccessor::GetLine(int position
) {
60 return Platform::SendScintilla(id
, SCI_LINEFROMPOSITION
, position
, 0);
63 int WindowAccessor::LineStart(int line
) {
64 return Platform::SendScintilla(id
, SCI_POSITIONFROMLINE
, line
, 0);
67 int WindowAccessor::LevelAt(int line
) {
68 return Platform::SendScintilla(id
, SCI_GETFOLDLEVEL
, line
, 0);
71 int WindowAccessor::Length() {
73 lenDoc
= Platform::SendScintilla(id
, SCI_GETTEXTLENGTH
, 0, 0);
77 int WindowAccessor::GetLineState(int line
) {
78 return Platform::SendScintilla(id
, SCI_GETLINESTATE
, line
);
81 int WindowAccessor::SetLineState(int line
, int state
) {
82 return Platform::SendScintilla(id
, SCI_SETLINESTATE
, line
, state
);
85 void WindowAccessor::StartAt(unsigned int start
, char chMask
) {
86 Platform::SendScintilla(id
, SCI_STARTSTYLING
, start
, chMask
);
89 void WindowAccessor::StartSegment(unsigned int pos
) {
93 void WindowAccessor::ColourTo(unsigned int pos
, int chAttr
) {
94 // Only perform styling if non empty range
95 if (pos
!= startSeg
- 1) {
97 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
100 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
102 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
103 // Too big for buffer so send directly
104 Platform::SendScintilla(id
, SCI_SETSTYLING
, pos
- startSeg
+ 1, chAttr
);
106 if (chAttr
!= chWhile
)
109 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
110 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
117 void WindowAccessor::SetLevel(int line
, int level
) {
118 Platform::SendScintilla(id
, SCI_SETFOLDLEVEL
, line
, level
);
121 void WindowAccessor::Flush() {
122 startPos
= extremePosition
;
125 Platform::SendScintilla(id
, SCI_SETSTYLINGEX
, validLen
,
126 reinterpret_cast<long>(styleBuf
));
131 int WindowAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
135 // Determines the indentation level of the current line and also checks for consistent
136 // indentation compared to the previous line.
137 // Indentation is judged consistent when the indentation whitespace of each line lines
138 // the same or the indentation of one line is a prefix of the other.
140 int pos
= LineStart(line
);
141 char ch
= (*this)[pos
];
143 bool inPrevPrefix
= line
> 0;
144 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
145 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
147 char chPrev
= (*this)[posPrev
++];
148 if (chPrev
== ' ' || chPrev
== '\t') {
150 spaceFlags
|= wsInconsistent
;
152 inPrevPrefix
= false;
156 spaceFlags
|= wsSpace
;
160 if (spaceFlags
& wsSpace
)
161 spaceFlags
|= wsSpaceTab
;
162 indent
= (indent
/ 8 + 1) * 8;
168 indent
+= SC_FOLDLEVELBASE
;
169 // if completely empty line or the start of a comment...
170 if (isspace(ch
) || (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
171 return indent
| SC_FOLDLEVELWHITEFLAG
;