1 // Scintilla source code edit control 
   3  ** Text document that handles notifications, DBCS, styling, words and end of line. 
   5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org> 
   6 // The License.txt file describes the conditions under which this software may be distributed. 
  12  * A Position is a position within a document between two characters or at the beginning or end. 
  13  * Sometimes used as a character index where it identifies the character after the position. 
  16 const Position invalidPosition 
= -1; 
  19  * The range class represents a range of text in a document. 
  20  * The two values are not sorted as one end may be more significant than the other 
  21  * as is the case for the selection where the end position is the position of the caret. 
  22  * If either position is invalidPosition then the range is invalid and most operations will fail. 
  29         Range(Position pos
=0) : 
  30                 start(pos
), end(pos
) { 
  32         Range(Position start_
, Position end_
) : 
  33                 start(start_
), end(end_
) { 
  37                 return (start 
!= invalidPosition
) && (end 
!= invalidPosition
); 
  40         // Is the position within the range? 
  41         bool Contains(Position pos
) const { 
  43                         return (pos 
>= start 
&& pos 
<= end
); 
  45                         return (pos 
<= start 
&& pos 
>= end
); 
  49         // Is the character after pos within the range? 
  50         bool ContainsCharacter(Position pos
) const { 
  52                         return (pos 
>= start 
&& pos 
< end
); 
  54                         return (pos 
< start 
&& pos 
>= end
); 
  58         bool Contains(Range other
) const { 
  59                 return Contains(other
.start
) && Contains(other
.end
); 
  62         bool Overlaps(Range other
) const { 
  64                 Contains(other
.start
) || 
  65                 Contains(other
.end
) || 
  66                 other
.Contains(start
) || 
  72 class DocModification
; 
  80         /** Used to pair watcher pointer with user data. */ 
  81         class WatcherWithUserData 
{ 
  85                 WatcherWithUserData() { 
  91         enum charClassification 
{ ccSpace
, ccNewLine
, ccWord
, ccPunctuation 
}; 
  96         charClassification charClass
[256]; 
 101         int enteredReadOnlyCount
; 
 103         WatcherWithUserData 
*watchers
; 
 115         /// Can also be SC_CP_UTF8 to enable UTF-8 mode 
 121         bool backspaceUnindents
; 
 129         int LineFromPosition(int pos
); 
 130         int ClampPositionIntoDocument(int pos
); 
 131         bool IsCrLf(int pos
); 
 132         int LenChar(int pos
); 
 133         int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true); 
 135         // Gateways to modifying document 
 136         bool DeleteChars(int pos
, int len
); 
 137         bool InsertStyledString(int position
, char *s
, int insertLength
); 
 140         bool CanUndo() { return cb
.CanUndo(); } 
 141         bool CanRedo() { return cb
.CanRedo(); } 
 142         void DeleteUndoHistory() { cb
.DeleteUndoHistory(); } 
 143         bool SetUndoCollection(bool collectUndo
) { 
 144                 return cb
.SetUndoCollection(collectUndo
); 
 146         bool IsCollectingUndo() { return cb
.IsCollectingUndo(); } 
 147         void BeginUndoAction() { cb
.BeginUndoAction(); } 
 148         void EndUndoAction() { cb
.EndUndoAction(); } 
 150         bool IsSavePoint() { return cb
.IsSavePoint(); } 
 152         int GetLineIndentation(int line
); 
 153         void SetLineIndentation(int line
, int indent
); 
 154         int GetLineIndentPosition(int line
); 
 155         int GetColumn(int position
); 
 156         int FindColumn(int line
, int column
); 
 157         void Indent(bool forwards
, int lineBottom
, int lineTop
); 
 158         void ConvertLineEnds(int eolModeSet
); 
 159         void SetReadOnly(bool set
) { cb
.SetReadOnly(set
); } 
 160         bool IsReadOnly() { return cb
.IsReadOnly(); } 
 162         bool InsertChar(int pos
, char ch
); 
 163         bool InsertString(int position
, const char *s
); 
 164         bool InsertString(int position
, const char *s
, size_t insertLength
); 
 165         void ChangeChar(int pos
, char ch
); 
 166         void DelChar(int pos
); 
 167         void DelCharBack(int pos
); 
 169         char CharAt(int position
) { return cb
.CharAt(position
); } 
 170         void GetCharRange(char *buffer
, int position
, int lengthRetrieve
) { 
 171                 cb
.GetCharRange(buffer
, position
, lengthRetrieve
); 
 173         char StyleAt(int position
) { return cb
.StyleAt(position
); } 
 174         int GetMark(int line
) { return cb
.GetMark(line
); } 
 175         int AddMark(int line
, int markerNum
); 
 176         void DeleteMark(int line
, int markerNum
); 
 177         void DeleteMarkFromHandle(int markerHandle
); 
 178         void DeleteAllMarks(int markerNum
); 
 179         int LineFromHandle(int markerHandle
) { return cb
.LineFromHandle(markerHandle
); } 
 180         int LineStart(int line
); 
 181         int LineEnd(int line
); 
 182         int LineEndPosition(int position
); 
 183         int VCHomePosition(int position
); 
 185         int SetLevel(int line
, int level
); 
 186         int GetLevel(int line
) { return cb
.GetLevel(line
); } 
 187         void ClearLevels() { cb
.ClearLevels(); } 
 188         int GetLastChild(int lineParent
, int level
=-1); 
 189         int GetFoldParent(int line
); 
 191         void Indent(bool forwards
); 
 192         int ExtendWordSelect(int pos
, int delta
, bool onlyWordCharacters
=false); 
 193         int NextWordStart(int pos
, int delta
); 
 194         int NextWordEnd(int pos
, int delta
); 
 195         int Length() { return cb
.Length(); } 
 196         long FindText(int minPos
, int maxPos
, const char *s
, 
 197                 bool caseSensitive
, bool word
, bool wordStart
, bool regExp
, bool posix
, int *length
); 
 198         long FindText(int iMessage
, unsigned long wParam
, long lParam
); 
 199         const char *SubstituteByPosition(const char *text
, int *length
); 
 202         void ChangeCase(Range r
, bool makeUpperCase
); 
 204         void SetDefaultCharClasses(); 
 205         void SetCharClasses(unsigned char *chars
, charClassification newCharClass
); 
 206         void SetStylingBits(int bits
); 
 207         void StartStyling(int position
, char mask
); 
 208         bool SetStyleFor(int length
, char style
); 
 209         bool SetStyles(int length
, char *styles
); 
 210         int GetEndStyled() { return endStyled
; } 
 211         bool EnsureStyledTo(int pos
); 
 212         int GetStyleClock() { return styleClock
; } 
 214         int SetLineState(int line
, int state
) { return cb
.SetLineState(line
, state
); } 
 215         int GetLineState(int line
) { return cb
.GetLineState(line
); } 
 216         int GetMaxLineState() { return cb
.GetMaxLineState(); } 
 218         bool AddWatcher(DocWatcher 
*watcher
, void *userData
); 
 219         bool RemoveWatcher(DocWatcher 
*watcher
, void *userData
); 
 220         const WatcherWithUserData 
*GetWatchers() const { return watchers
; } 
 221         int GetLenWatchers() const { return lenWatchers
; } 
 223         bool IsWordPartSeparator(char ch
); 
 224         int WordPartLeft(int pos
); 
 225         int WordPartRight(int pos
); 
 226         int ExtendStyleRange(int pos
, int delta
, bool singleLine 
= false); 
 228         int ParaDown(int pos
); 
 231         charClassification 
WordCharClass(unsigned char ch
); 
 232         bool IsWordStartAt(int pos
); 
 233         bool IsWordEndAt(int pos
); 
 234         bool IsWordAt(int start
, int end
); 
 235         void ModifiedAt(int pos
); 
 237         void NotifyModifyAttempt(); 
 238         void NotifySavePoint(bool atSavePoint
); 
 239         void NotifyModified(DocModification mh
); 
 241         int IndentSize() { return indentInChars 
? indentInChars 
: tabInChars
; } 
 245  * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the 
 246  * scope of the change. 
 247  * If the DocWatcher is a document view then this can be used to optimise screen updating. 
 249 class DocModification 
{ 
 251         int modificationType
; 
 254         int linesAdded
; /**< Negative if lines deleted. */ 
 255         const char *text
;       /**< Only valid for changes to text, not for changes to style. */ 
 260         DocModification(int modificationType_
, int position_
=0, int length_
=0, 
 261                 int linesAdded_
=0, const char *text_
=0) : 
 262                 modificationType(modificationType_
), 
 265                 linesAdded(linesAdded_
), 
 271     DocModification(int modificationType_
, const Action 
&act
, int linesAdded_
=0) : 
 272                 modificationType(modificationType_
), 
 273                 position(act
.position 
/ 2), 
 275                 linesAdded(linesAdded_
), 
 283  * A class that wants to receive notifications from a Document must be derived from DocWatcher 
 284  * and implement the notification methods. It can then be added to the watcher list with AddWatcher. 
 288         virtual ~DocWatcher() {} 
 290         virtual void NotifyModifyAttempt(Document 
*doc
, void *userData
) = 0; 
 291         virtual void NotifySavePoint(Document 
*doc
, void *userData
, bool atSavePoint
) = 0; 
 292         virtual void NotifyModified(Document 
*doc
, DocModification mh
, void *userData
) = 0; 
 293         virtual void NotifyDeleted(Document 
*doc
, void *userData
) = 0; 
 294         virtual void NotifyStyleNeeded(Document 
*doc
, void *userData
, int endPos
) = 0;