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