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" 
  20 WindowAccessor::~WindowAccessor() { 
  24 bool WindowAccessor::InternalIsLeadByte(char ch
) { 
  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
); 
  33 // PLAT_GTK or PLAT_WX 
  34 // TODO: support DBCS under GTK+ and WX 
  35 bool WindowAccessor::InternalIsLeadByte(char) { 
  40 void WindowAccessor::Fill(int position
) { 
  42                 lenDoc 
= Platform::SendScintilla(id
, SCI_GETTEXTLENGTH
, 0, 0); 
  43         startPos 
= position 
- slopSize
; 
  44         if (startPos 
+ bufferSize 
> lenDoc
) 
  45                 startPos 
= lenDoc 
- bufferSize
; 
  48         endPos 
= startPos 
+ bufferSize
; 
  52         TextRange tr 
= {{startPos
, endPos
}, buf
}; 
  53         Platform::SendScintilla(id
, SCI_GETTEXTRANGE
, 0, reinterpret_cast<long>(&tr
)); 
  56 char WindowAccessor::StyleAt(int position
) { 
  57         return static_cast<char>(Platform::SendScintilla( 
  58                 id
, SCI_GETSTYLEAT
, position
, 0)); 
  61 int WindowAccessor::GetLine(int position
) { 
  62         return Platform::SendScintilla(id
, SCI_LINEFROMPOSITION
, position
, 0); 
  65 int WindowAccessor::LineStart(int line
) { 
  66         return Platform::SendScintilla(id
, SCI_POSITIONFROMLINE
, line
, 0); 
  69 int WindowAccessor::LevelAt(int line
) { 
  70         return Platform::SendScintilla(id
, SCI_GETFOLDLEVEL
, line
, 0); 
  73 int WindowAccessor::Length() {  
  75                 lenDoc 
= Platform::SendScintilla(id
, SCI_GETTEXTLENGTH
, 0, 0); 
  79 int WindowAccessor::GetLineState(int line
) { 
  80         return Platform::SendScintilla(id
, SCI_GETLINESTATE
, line
); 
  83 int WindowAccessor::SetLineState(int line
, int state
) { 
  84         return Platform::SendScintilla(id
, SCI_SETLINESTATE
, line
, state
); 
  87 void WindowAccessor::StartAt(unsigned int start
, char chMask
) { 
  88         Platform::SendScintilla(id
, SCI_STARTSTYLING
, start
, chMask
); 
  91 void WindowAccessor::StartSegment(unsigned int pos
) { 
  95 void WindowAccessor::ColourTo(unsigned int pos
, int chAttr
) { 
  96         // Only perform styling if non empty range 
  97         if (pos 
!= startSeg 
- 1) { 
  99                         Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
); 
 102                 if (validLen 
+ (pos 
- startSeg 
+ 1) >= bufferSize
) 
 104                 if (validLen 
+ (pos 
- startSeg 
+ 1) >= bufferSize
) { 
 105                         // Too big for buffer so send directly 
 106                         Platform::SendScintilla(id
, SCI_SETSTYLING
, pos 
- startSeg 
+ 1, chAttr
); 
 108                         if (chAttr 
!= chWhile
) 
 111                         for (unsigned int i 
= startSeg
; i 
<= pos
; i
++) { 
 112                                 styleBuf
[validLen
++] = static_cast<char>(chAttr
); 
 119 void WindowAccessor::SetLevel(int line
, int level
) { 
 120         Platform::SendScintilla(id
, SCI_SETFOLDLEVEL
, line
, level
); 
 123 void WindowAccessor::Flush() { 
 124         startPos 
= extremePosition
; 
 127                 Platform::SendScintilla(id
, SCI_SETSTYLINGEX
, validLen
,  
 128                         reinterpret_cast<long>(styleBuf
)); 
 133 int WindowAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) { 
 137         // Determines the indentation level of the current line and also checks for consistent  
 138         // indentation compared to the previous line. 
 139         // Indentation is judged consistent when the indentation whitespace of each line lines  
 140         // the same or the indentation of one line is a prefix of the other. 
 142         int pos 
= LineStart(line
); 
 143         char ch 
= (*this)[pos
]; 
 145         bool inPrevPrefix 
= line 
> 0; 
 146         int posPrev 
= inPrevPrefix 
? LineStart(line
-1) : 0; 
 147         while ((ch 
== ' ' || ch 
== '\t') && (pos 
< end
)) { 
 149                         char chPrev 
= (*this)[posPrev
++]; 
 150                         if (chPrev 
== ' ' || chPrev 
== '\t') { 
 152                                         spaceFlags 
|= wsInconsistent
; 
 154                                 inPrevPrefix 
= false; 
 158                         spaceFlags 
|= wsSpace
; 
 162                         if (spaceFlags 
& wsSpace
) 
 163                                 spaceFlags 
|= wsSpaceTab
; 
 164                         indent 
= (indent 
/ 8 + 1) * 8; 
 170         indent 
+= SC_FOLDLEVELBASE
; 
 171         // if completely empty line or the start of a comment... 
 172         if (isspace(ch
) || (pfnIsCommentLeader 
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) ) 
 173                 return indent 
| SC_FOLDLEVELWHITEFLAG
;