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.
14 #include "WindowAccessor.h"
15 #include "Scintilla.h"
17 WindowAccessor::~WindowAccessor() {
20 bool WindowAccessor::InternalIsLeadByte(char ch
) {
22 // TODO: support DBCS under GTK+
25 if (SC_CP_UTF8
== codePage
)
26 // For lexing, all characters >= 0x80 are treated the
27 // same so none is considered a lead byte.
30 return IsDBCSLeadByteEx(codePage
, ch
);
36 void WindowAccessor::Fill(int position
) {
38 lenDoc
= Platform::SendScintilla(id
, WM_GETTEXTLENGTH
, 0, 0);
39 startPos
= position
- slopSize
;
40 if (startPos
+ bufferSize
> lenDoc
)
41 startPos
= lenDoc
- bufferSize
;
44 endPos
= startPos
+ bufferSize
;
48 TEXTRANGE tr
= {{startPos
, endPos
}, buf
};
49 Platform::SendScintilla(id
, EM_GETTEXTRANGE
, 0, reinterpret_cast<LPARAM
>(&tr
));
52 char WindowAccessor::StyleAt(int position
) {
53 return static_cast<char>(Platform::SendScintilla(
54 id
, SCI_GETSTYLEAT
, position
, 0));
57 int WindowAccessor::GetLine(int position
) {
58 return Platform::SendScintilla(id
, EM_LINEFROMCHAR
, position
, 0);
61 int WindowAccessor::LineStart(int line
) {
62 return Platform::SendScintilla(id
, EM_LINEINDEX
, line
, 0);
65 int WindowAccessor::LevelAt(int line
) {
66 return Platform::SendScintilla(id
, SCI_GETFOLDLEVEL
, line
, 0);
69 int WindowAccessor::Length() {
71 lenDoc
= Platform::SendScintilla(id
, WM_GETTEXTLENGTH
, 0, 0);
75 int WindowAccessor::GetLineState(int line
) {
76 return Platform::SendScintilla(id
, SCI_GETLINESTATE
, line
);
79 int WindowAccessor::SetLineState(int line
, int state
) {
80 return Platform::SendScintilla(id
, SCI_SETLINESTATE
, line
, state
);
83 void WindowAccessor::StartAt(unsigned int start
, char chMask
) {
84 Platform::SendScintilla(id
, SCI_STARTSTYLING
, start
, chMask
);
87 void WindowAccessor::StartSegment(unsigned int pos
) {
91 void WindowAccessor::ColourTo(unsigned int pos
, int chAttr
) {
92 // Only perform styling if non empty range
93 if (pos
!= startSeg
- 1) {
95 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
98 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
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
);
104 if (chAttr
!= chWhile
)
107 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
108 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
115 void WindowAccessor::SetLevel(int line
, int level
) {
116 Platform::SendScintilla(id
, SCI_SETFOLDLEVEL
, line
, level
);
119 void WindowAccessor::Flush() {
120 startPos
= extremePosition
;
123 Platform::SendScintilla(id
, SCI_SETSTYLINGEX
, validLen
,
124 reinterpret_cast<LPARAM
>(styleBuf
));
129 int WindowAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
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.
138 int pos
= LineStart(line
);
139 char ch
= (*this)[pos
];
141 bool inPrevPrefix
= line
> 0;
142 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
143 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
145 char chPrev
= (*this)[posPrev
++];
146 if (chPrev
== ' ' || chPrev
== '\t') {
148 spaceFlags
|= wsInconsistent
;
150 inPrevPrefix
= false;
154 spaceFlags
|= wsSpace
;
158 if (spaceFlags
& wsSpace
)
159 spaceFlags
|= wsSpaceTab
;
160 indent
= (indent
/ 8 + 1) * 8;
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
;