1 // Scintilla source code edit control
2 // Document.h - text document that handles notifications, DBCS, styling, words and end of line
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.
9 // A Position is a position within a document between two characters or at the beginning or end.
10 // Sometimes used as a character index where it identifies the character after the position.
12 const Position invalidPosition
= -1;
14 // The range class represents a range of text in a document.
15 // The two values are not sorted as one end may be more significant than the other
16 // as is the case for the selection where the end position is the position of the caret.
17 // If either position is invalidPosition then the range is invalid and most operations will fail.
23 Range(Position pos
=0) :
24 start(pos
), end(pos
) {
26 Range(Position start_
, Position end_
) :
27 start(start_
), end(end_
) {
31 return (start
!= invalidPosition
) && (end
!= invalidPosition
);
34 bool Contains(Position pos
) const {
36 return (pos
>= start
&& pos
<= end
);
38 return (pos
<= start
&& pos
>= end
);
42 bool Contains(Range other
) const {
43 return Contains(other
.start
) && Contains(other
.end
);
46 bool Overlaps(Range other
) const {
48 Contains(other
.start
) ||
49 Contains(other
.end
) ||
50 other
.Contains(start
) ||
56 class DocModification
;
61 // Used to pair watcher pointer with user data
62 class WatcherWithUserData
{
66 WatcherWithUserData() {
81 WatcherWithUserData
*watchers
;
98 int LineFromPosition(int pos
);
99 int ClampPositionIntoDocument(int pos
);
100 bool IsCrLf(int pos
);
101 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true);
103 // Gateways to modifying document
104 void DeleteChars(int pos
, int len
);
105 void InsertStyledString(int position
, char *s
, int insertLength
);
108 bool CanUndo() { return cb
.CanUndo(); }
109 bool CanRedo() { return cb
.CanRedo(); }
110 void DeleteUndoHistory() { cb
.DeleteUndoHistory(); }
111 undoCollectionType
SetUndoCollection(undoCollectionType collectUndo
) {
112 return cb
.SetUndoCollection(collectUndo
);
114 void BeginUndoAction() { cb
.BeginUndoAction(); }
115 void EndUndoAction() { cb
.EndUndoAction(); }
117 bool IsSavePoint() { return cb
.IsSavePoint(); }
118 void Indent(bool forwards
, int lineBottom
, int lineTop
);
119 void ConvertLineEnds(int eolModeSet
);
120 void SetReadOnly(bool set
) { cb
.SetReadOnly(set
); }
122 void InsertChar(int pos
, char ch
);
123 void InsertString(int position
, const char *s
);
124 void InsertString(int position
, const char *s
, int insertLength
);
125 void ChangeChar(int pos
, char ch
);
126 void DelChar(int pos
);
127 int DelCharBack(int pos
);
129 char CharAt(int position
) { return cb
.CharAt(position
); }
130 void GetCharRange(char *buffer
, int position
, int lengthRetrieve
) {
131 cb
.GetCharRange(buffer
, position
, lengthRetrieve
);
133 char StyleAt(int position
) { return cb
.StyleAt(position
); }
134 int GetMark(int line
) { return cb
.GetMark(line
); }
135 int AddMark(int line
, int markerNum
);
136 void DeleteMark(int line
, int markerNum
);
137 void DeleteMarkFromHandle(int markerHandle
);
138 void DeleteAllMarks(int markerNum
);
139 int LineFromHandle(int markerHandle
) { return cb
.LineFromHandle(markerHandle
); }
140 int LineStart(int line
);
141 int LineEnd(int line
);
142 int LineEndPosition(int position
);
143 int VCHomePosition(int position
);
145 int SetLevel(int line
, int level
);
146 int GetLevel(int line
) { return cb
.GetLevel(line
); }
147 int GetLastChild(int lineParent
, int level
=-1);
148 int GetFoldParent(int line
);
150 void Indent(bool forwards
);
151 int ExtendWordSelect(int pos
, int delta
);
152 int NextWordStart(int pos
, int delta
);
153 int Length() { return cb
.Length(); }
154 long FindText(int minPos
, int maxPos
, const char *s
, bool caseSensitive
, bool word
);
155 long FindText(WORD iMessage
,WPARAM wParam
,LPARAM lParam
);
158 void ChangeCase(Range r
, bool makeUpperCase
);
160 void SetWordChars(unsigned char *chars
);
161 void SetStylingBits(int bits
);
162 void StartStyling(int position
, char mask
);
163 void SetStyleFor(int length
, char style
);
164 void SetStyles(int length
, char *styles
);
165 int GetEndStyled() { return endStyled
; }
166 bool EnsureStyledTo(int pos
);
168 int SetLineState(int line
, int state
) { return cb
.SetLineState(line
, state
); }
169 int GetLineState(int line
) { return cb
.GetLineState(line
); }
170 int GetMaxLineState() { return cb
.GetMaxLineState(); }
172 bool AddWatcher(DocWatcher
*watcher
, void *userData
);
173 bool RemoveWatcher(DocWatcher
*watcher
, void *userData
);
174 const WatcherWithUserData
*GetWatchers() const { return watchers
; }
175 int GetLenWatchers() const { return lenWatchers
; }
178 bool IsDBCS(int pos
);
179 bool IsWordChar(unsigned char ch
);
180 bool IsWordAt(int start
, int end
);
181 void ModifiedAt(int pos
);
183 void NotifyModifyAttempt();
184 void NotifySavePoint(bool atSavePoint
);
185 void NotifyModified(DocModification mh
);
188 // To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
189 // scope of the change.
190 // If the DocWatcher is a document view then this can be used to optimise screen updating.
191 class DocModification
{
193 int modificationType
;
196 int linesAdded
; // Negative if lines deleted
197 const char *text
; // Only valid for changes to text, not for changes to style
202 DocModification(int modificationType_
, int position_
=0, int length_
=0,
203 int linesAdded_
=0, const char *text_
=0) :
204 modificationType(modificationType_
),
207 linesAdded(linesAdded_
),
214 // A class that wants to receive notifications from a Document must be derived from DocWatcher
215 // and implement the notification methods. It can then be added to the watcher list with AddWatcher.
218 virtual ~DocWatcher() {}
220 virtual void NotifyModifyAttempt(Document
*doc
, void *userData
) = 0;
221 virtual void NotifySavePoint(Document
*doc
, void *userData
, bool atSavePoint
) = 0;
222 virtual void NotifyModified(Document
*doc
, DocModification mh
, void *userData
) = 0;
223 virtual void NotifyDeleted(Document
*doc
, void *userData
) = 0;
224 virtual void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
) = 0;