1 // Scintilla source code edit control
3 ** Text document that handles notifications, DBCS, styling, words and end of line.
5 // Copyright 1998-2002 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() {
94 enum charClassification
{ ccSpace
, ccNewLine
, ccWord
, ccPunctuation
};
95 charClassification charClass
[256];
100 int enteredReadOnlyCount
;
102 WatcherWithUserData
*watchers
;
114 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
120 bool backspaceUnindents
;
128 int LineFromPosition(int pos
);
129 int ClampPositionIntoDocument(int pos
);
130 bool IsCrLf(int pos
);
131 int LenChar(int pos
);
132 int MovePositionOutsideChar(int pos
, int moveDir
, bool checkLineEnd
=true);
134 // Gateways to modifying document
135 bool DeleteChars(int pos
, int len
);
136 bool InsertStyledString(int position
, char *s
, int insertLength
);
139 bool CanUndo() { return cb
.CanUndo(); }
140 bool CanRedo() { return cb
.CanRedo(); }
141 void DeleteUndoHistory() { cb
.DeleteUndoHistory(); }
142 bool SetUndoCollection(bool collectUndo
) {
143 return cb
.SetUndoCollection(collectUndo
);
145 bool IsCollectingUndo() { return cb
.IsCollectingUndo(); }
146 void BeginUndoAction() { cb
.BeginUndoAction(); }
147 void EndUndoAction() { cb
.EndUndoAction(); }
149 bool IsSavePoint() { return cb
.IsSavePoint(); }
151 int GetLineIndentation(int line
);
152 void SetLineIndentation(int line
, int indent
);
153 int GetLineIndentPosition(int line
);
154 int GetColumn(int position
);
155 int FindColumn(int line
, int column
);
156 void Indent(bool forwards
, int lineBottom
, int lineTop
);
157 void ConvertLineEnds(int eolModeSet
);
158 void SetReadOnly(bool set
) { cb
.SetReadOnly(set
); }
159 bool IsReadOnly() { return cb
.IsReadOnly(); }
161 bool InsertChar(int pos
, char ch
);
162 bool InsertString(int position
, const char *s
);
163 bool InsertString(int position
, const char *s
, size_t insertLength
);
164 void ChangeChar(int pos
, char ch
);
165 void DelChar(int pos
);
166 void DelCharBack(int pos
);
168 char CharAt(int position
) { return cb
.CharAt(position
); }
169 void GetCharRange(char *buffer
, int position
, int lengthRetrieve
) {
170 cb
.GetCharRange(buffer
, position
, lengthRetrieve
);
172 char StyleAt(int position
) { return cb
.StyleAt(position
); }
173 int GetMark(int line
) { return cb
.GetMark(line
); }
174 int AddMark(int line
, int markerNum
);
175 void DeleteMark(int line
, int markerNum
);
176 void DeleteMarkFromHandle(int markerHandle
);
177 void DeleteAllMarks(int markerNum
);
178 int LineFromHandle(int markerHandle
) { return cb
.LineFromHandle(markerHandle
); }
179 int LineStart(int line
);
180 int LineEnd(int line
);
181 int LineEndPosition(int position
);
182 int VCHomePosition(int position
);
184 int SetLevel(int line
, int level
);
185 int GetLevel(int line
) { return cb
.GetLevel(line
); }
186 void ClearLevels() { cb
.ClearLevels(); }
187 int GetLastChild(int lineParent
, int level
=-1);
188 int GetFoldParent(int line
);
190 void Indent(bool forwards
);
191 int ExtendWordSelect(int pos
, int delta
, bool onlyWordCharacters
=false);
192 int NextWordStart(int pos
, int delta
);
193 int Length() { return cb
.Length(); }
194 long FindText(int minPos
, int maxPos
, const char *s
,
195 bool caseSensitive
, bool word
, bool wordStart
, bool regExp
, int *length
);
196 long FindText(int iMessage
, unsigned long wParam
, long lParam
);
197 const char *SubstituteByPosition(const char *text
, int *length
);
200 void ChangeCase(Range r
, bool makeUpperCase
);
202 void SetWordChars(unsigned char *chars
);
203 void SetStylingBits(int bits
);
204 void StartStyling(int position
, char mask
);
205 bool SetStyleFor(int length
, char style
);
206 bool SetStyles(int length
, char *styles
);
207 int GetEndStyled() { return endStyled
; }
208 bool EnsureStyledTo(int pos
);
209 int GetStyleClock() { return styleClock
; }
211 int SetLineState(int line
, int state
) { return cb
.SetLineState(line
, state
); }
212 int GetLineState(int line
) { return cb
.GetLineState(line
); }
213 int GetMaxLineState() { return cb
.GetMaxLineState(); }
215 bool AddWatcher(DocWatcher
*watcher
, void *userData
);
216 bool RemoveWatcher(DocWatcher
*watcher
, void *userData
);
217 const WatcherWithUserData
*GetWatchers() const { return watchers
; }
218 int GetLenWatchers() const { return lenWatchers
; }
220 bool IsWordPartSeparator(char ch
);
221 int WordPartLeft(int pos
);
222 int WordPartRight(int pos
);
225 bool IsDBCS(int pos
);
226 charClassification
WordCharClass(unsigned char ch
);
227 bool IsWordStartAt(int pos
);
228 bool IsWordEndAt(int pos
);
229 bool IsWordAt(int start
, int end
);
230 void ModifiedAt(int pos
);
232 void NotifyModifyAttempt();
233 void NotifySavePoint(bool atSavePoint
);
234 void NotifyModified(DocModification mh
);
236 int IndentSize() { return indentInChars
? indentInChars
: tabInChars
; }
240 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
241 * scope of the change.
242 * If the DocWatcher is a document view then this can be used to optimise screen updating.
244 class DocModification
{
246 int modificationType
;
249 int linesAdded
; /**< Negative if lines deleted. */
250 const char *text
; /**< Only valid for changes to text, not for changes to style. */
255 DocModification(int modificationType_
, int position_
=0, int length_
=0,
256 int linesAdded_
=0, const char *text_
=0) :
257 modificationType(modificationType_
),
260 linesAdded(linesAdded_
),
266 DocModification(int modificationType_
, const Action
&act
, int linesAdded_
=0) :
267 modificationType(modificationType_
),
268 position(act
.position
/ 2),
270 linesAdded(linesAdded_
),
278 * A class that wants to receive notifications from a Document must be derived from DocWatcher
279 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
283 virtual ~DocWatcher() {}
285 virtual void NotifyModifyAttempt(Document
*doc
, void *userData
) = 0;
286 virtual void NotifySavePoint(Document
*doc
, void *userData
, bool atSavePoint
) = 0;
287 virtual void NotifyModified(Document
*doc
, DocModification mh
, void *userData
) = 0;
288 virtual void NotifyDeleted(Document
*doc
, void *userData
) = 0;
289 virtual void NotifyStyleNeeded(Document
*doc
, void *userData
, int endPos
) = 0;