]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/Document.h
New devs.
[wxWidgets.git] / src / stc / scintilla / src / Document.h
1 // Scintilla source code edit control
2 /** @file Document.h
3 ** Text document that handles notifications, DBCS, styling, words and end of line.
4 **/
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.
7
8 #ifndef DOCUMENT_H
9 #define DOCUMENT_H
10
11 #ifdef SCI_NAMESPACE
12 namespace Scintilla {
13 #endif
14
15 /**
16 * A Position is a position within a document between two characters or at the beginning or end.
17 * Sometimes used as a character index where it identifies the character after the position.
18 */
19 typedef int Position;
20 const Position invalidPosition = -1;
21
22 /**
23 * The range class represents a range of text in a document.
24 * The two values are not sorted as one end may be more significant than the other
25 * as is the case for the selection where the end position is the position of the caret.
26 * If either position is invalidPosition then the range is invalid and most operations will fail.
27 */
28 class Range {
29 public:
30 Position start;
31 Position end;
32
33 Range(Position pos=0) :
34 start(pos), end(pos) {
35 };
36 Range(Position start_, Position end_) :
37 start(start_), end(end_) {
38 };
39
40 bool Valid() const {
41 return (start != invalidPosition) && (end != invalidPosition);
42 }
43
44 // Is the position within the range?
45 bool Contains(Position pos) const {
46 if (start < end) {
47 return (pos >= start && pos <= end);
48 } else {
49 return (pos <= start && pos >= end);
50 }
51 }
52
53 // Is the character after pos within the range?
54 bool ContainsCharacter(Position pos) const {
55 if (start < end) {
56 return (pos >= start && pos < end);
57 } else {
58 return (pos < start && pos >= end);
59 }
60 }
61
62 bool Contains(Range other) const {
63 return Contains(other.start) && Contains(other.end);
64 }
65
66 bool Overlaps(Range other) const {
67 return
68 Contains(other.start) ||
69 Contains(other.end) ||
70 other.Contains(start) ||
71 other.Contains(end);
72 }
73 };
74
75 class DocWatcher;
76 class DocModification;
77 class RESearch;
78
79 /**
80 */
81 class Document {
82
83 public:
84 /** Used to pair watcher pointer with user data. */
85 class WatcherWithUserData {
86 public:
87 DocWatcher *watcher;
88 void *userData;
89 WatcherWithUserData() {
90 watcher = 0;
91 userData = 0;
92 }
93 };
94
95 enum charClassification { ccSpace, ccNewLine, ccWord, ccPunctuation };
96
97 private:
98 int refCount;
99 CellBuffer cb;
100 CharClassify charClass;
101 char stylingMask;
102 int endStyled;
103 int styleClock;
104 int enteredModification;
105 int enteredStyling;
106 int enteredReadOnlyCount;
107
108 WatcherWithUserData *watchers;
109 int lenWatchers;
110
111 bool matchesValid;
112 RESearch *pre;
113 char *substituted;
114
115 public:
116 int stylingBits;
117 int stylingBitsMask;
118
119 int eolMode;
120 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
121 int dbcsCodePage;
122 int tabInChars;
123 int indentInChars;
124 int actualIndentInChars;
125 bool useTabs;
126 bool tabIndents;
127 bool backspaceUnindents;
128
129 DecorationList decorations;
130
131 Document();
132 virtual ~Document();
133
134 int AddRef();
135 int Release();
136
137 int LineFromPosition(int pos);
138 int ClampPositionIntoDocument(int pos);
139 bool IsCrLf(int pos);
140 int LenChar(int pos);
141 bool InGoodUTF8(int pos, int &start, int &end);
142 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true);
143
144 // Gateways to modifying document
145 void ModifiedAt(int pos);
146 void CheckReadOnly();
147 bool DeleteChars(int pos, int len);
148 bool InsertString(int position, const char *s, int insertLength);
149 int Undo();
150 int Redo();
151 bool CanUndo() { return cb.CanUndo(); }
152 bool CanRedo() { return cb.CanRedo(); }
153 void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
154 bool SetUndoCollection(bool collectUndo) {
155 return cb.SetUndoCollection(collectUndo);
156 }
157 bool IsCollectingUndo() { return cb.IsCollectingUndo(); }
158 void BeginUndoAction() { cb.BeginUndoAction(); }
159 void EndUndoAction() { cb.EndUndoAction(); }
160 void SetSavePoint();
161 bool IsSavePoint() { return cb.IsSavePoint(); }
162
163 int GetLineIndentation(int line);
164 void SetLineIndentation(int line, int indent);
165 int GetLineIndentPosition(int line) const;
166 int GetColumn(int position);
167 int FindColumn(int line, int column);
168 void Indent(bool forwards, int lineBottom, int lineTop);
169 static char *TransformLineEnds(int *pLenOut, const char *s, size_t len, int eolMode);
170 void ConvertLineEnds(int eolModeSet);
171 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
172 bool IsReadOnly() { return cb.IsReadOnly(); }
173
174 bool InsertChar(int pos, char ch);
175 bool InsertCString(int position, const char *s);
176 void ChangeChar(int pos, char ch);
177 void DelChar(int pos);
178 void DelCharBack(int pos);
179
180 char CharAt(int position) { return cb.CharAt(position); }
181 void GetCharRange(char *buffer, int position, int lengthRetrieve) {
182 cb.GetCharRange(buffer, position, lengthRetrieve);
183 }
184 char StyleAt(int position) { return cb.StyleAt(position); }
185 int GetMark(int line) { return cb.GetMark(line); }
186 int AddMark(int line, int markerNum);
187 void AddMarkSet(int line, int valueSet);
188 void DeleteMark(int line, int markerNum);
189 void DeleteMarkFromHandle(int markerHandle);
190 void DeleteAllMarks(int markerNum);
191 int LineFromHandle(int markerHandle) { return cb.LineFromHandle(markerHandle); }
192 int LineStart(int line) const;
193 int LineEnd(int line) const;
194 int LineEndPosition(int position);
195 int VCHomePosition(int position);
196
197 int SetLevel(int line, int level);
198 int GetLevel(int line) { return cb.GetLevel(line); }
199 void ClearLevels() { cb.ClearLevels(); }
200 int GetLastChild(int lineParent, int level=-1);
201 int GetFoldParent(int line);
202
203 void Indent(bool forwards);
204 int ExtendWordSelect(int pos, int delta, bool onlyWordCharacters=false);
205 int NextWordStart(int pos, int delta);
206 int NextWordEnd(int pos, int delta);
207 int Length() const { return cb.Length(); }
208 void Allocate(int newSize) { cb.Allocate(newSize); }
209 long FindText(int minPos, int maxPos, const char *s,
210 bool caseSensitive, bool word, bool wordStart, bool regExp, bool posix, int *length);
211 long FindText(int iMessage, unsigned long wParam, long lParam);
212 const char *SubstituteByPosition(const char *text, int *length);
213 int LinesTotal() const;
214
215 void ChangeCase(Range r, bool makeUpperCase);
216
217 void SetDefaultCharClasses(bool includeWordClass);
218 void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass);
219 void SetStylingBits(int bits);
220 void StartStyling(int position, char mask);
221 bool SetStyleFor(int length, char style);
222 bool SetStyles(int length, char *styles);
223 int GetEndStyled() { return endStyled; }
224 void EnsureStyledTo(int pos);
225 int GetStyleClock() { return styleClock; }
226 void IncrementStyleClock();
227 void DecorationFillRange(int position, int value, int fillLength);
228
229 int SetLineState(int line, int state);
230 int GetLineState(int line) { return cb.GetLineState(line); }
231 int GetMaxLineState() { return cb.GetMaxLineState(); }
232
233 bool AddWatcher(DocWatcher *watcher, void *userData);
234 bool RemoveWatcher(DocWatcher *watcher, void *userData);
235 const WatcherWithUserData *GetWatchers() const { return watchers; }
236 int GetLenWatchers() const { return lenWatchers; }
237
238 bool IsWordPartSeparator(char ch);
239 int WordPartLeft(int pos);
240 int WordPartRight(int pos);
241 int ExtendStyleRange(int pos, int delta, bool singleLine = false);
242 bool IsWhiteLine(int line) const;
243 int ParaUp(int pos);
244 int ParaDown(int pos);
245 int IndentSize() { return actualIndentInChars; }
246 int BraceMatch(int position, int maxReStyle);
247
248 private:
249 CharClassify::cc WordCharClass(unsigned char ch);
250 bool IsWordStartAt(int pos);
251 bool IsWordEndAt(int pos);
252 bool IsWordAt(int start, int end);
253
254 void NotifyModifyAttempt();
255 void NotifySavePoint(bool atSavePoint);
256 void NotifyModified(DocModification mh);
257 };
258
259 /**
260 * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
261 * scope of the change.
262 * If the DocWatcher is a document view then this can be used to optimise screen updating.
263 */
264 class DocModification {
265 public:
266 int modificationType;
267 int position;
268 int length;
269 int linesAdded; /**< Negative if lines deleted. */
270 const char *text; /**< Only valid for changes to text, not for changes to style. */
271 int line;
272 int foldLevelNow;
273 int foldLevelPrev;
274
275 DocModification(int modificationType_, int position_=0, int length_=0,
276 int linesAdded_=0, const char *text_=0, int line_=0) :
277 modificationType(modificationType_),
278 position(position_),
279 length(length_),
280 linesAdded(linesAdded_),
281 text(text_),
282 line(line_),
283 foldLevelNow(0),
284 foldLevelPrev(0) {}
285
286 DocModification(int modificationType_, const Action &act, int linesAdded_=0) :
287 modificationType(modificationType_),
288 position(act.position),
289 length(act.lenData),
290 linesAdded(linesAdded_),
291 text(act.data),
292 line(0),
293 foldLevelNow(0),
294 foldLevelPrev(0) {}
295 };
296
297 /**
298 * A class that wants to receive notifications from a Document must be derived from DocWatcher
299 * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
300 */
301 class DocWatcher {
302 public:
303 virtual ~DocWatcher() {}
304
305 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
306 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
307 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
308 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
309 virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0;
310 };
311
312 #ifdef SCI_NAMESPACE
313 }
314 #endif
315
316 #endif