]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/Document.h
82931207cfcbe8cb7662f9ea07cb1e72a079936e
[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-2002 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 private:
92 int refCount;
93 CellBuffer cb;
94 enum charClassification { ccSpace, ccNewLine, ccWord, ccPunctuation };
95 charClassification charClass[256];
96 char stylingMask;
97 int endStyled;
98 int styleClock;
99 int enteredCount;
100 int enteredReadOnlyCount;
101
102 WatcherWithUserData *watchers;
103 int lenWatchers;
104
105 bool matchesValid;
106 RESearch *pre;
107 char *substituted;
108
109 public:
110 int stylingBits;
111 int stylingBitsMask;
112
113 int eolMode;
114 /// Can also be SC_CP_UTF8 to enable UTF-8 mode
115 int dbcsCodePage;
116 int tabInChars;
117 int indentInChars;
118 bool useTabs;
119 bool tabIndents;
120 bool backspaceUnindents;
121
122 Document();
123 virtual ~Document();
124
125 int AddRef();
126 int Release();
127
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);
133
134 // Gateways to modifying document
135 bool DeleteChars(int pos, int len);
136 bool InsertStyledString(int position, char *s, int insertLength);
137 int Undo();
138 int Redo();
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);
144 }
145 bool IsCollectingUndo() { return cb.IsCollectingUndo(); }
146 void BeginUndoAction() { cb.BeginUndoAction(); }
147 void EndUndoAction() { cb.EndUndoAction(); }
148 void SetSavePoint();
149 bool IsSavePoint() { return cb.IsSavePoint(); }
150
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(); }
160
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);
167
168 char CharAt(int position) { return cb.CharAt(position); }
169 void GetCharRange(char *buffer, int position, int lengthRetrieve) {
170 cb.GetCharRange(buffer, position, lengthRetrieve);
171 }
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);
183
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);
189
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);
198 int LinesTotal();
199
200 void ChangeCase(Range r, bool makeUpperCase);
201
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; }
210
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(); }
214
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; }
219
220 bool IsWordPartSeparator(char ch);
221 int WordPartLeft(int pos);
222 int WordPartRight(int pos);
223
224 private:
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);
231
232 void NotifyModifyAttempt();
233 void NotifySavePoint(bool atSavePoint);
234 void NotifyModified(DocModification mh);
235
236 int IndentSize() { return indentInChars ? indentInChars : tabInChars; }
237 };
238
239 /**
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.
243 */
244 class DocModification {
245 public:
246 int modificationType;
247 int position;
248 int length;
249 int linesAdded; /**< Negative if lines deleted. */
250 const char *text; /**< Only valid for changes to text, not for changes to style. */
251 int line;
252 int foldLevelNow;
253 int foldLevelPrev;
254
255 DocModification(int modificationType_, int position_=0, int length_=0,
256 int linesAdded_=0, const char *text_=0) :
257 modificationType(modificationType_),
258 position(position_),
259 length(length_),
260 linesAdded(linesAdded_),
261 text(text_),
262 line(0),
263 foldLevelNow(0),
264 foldLevelPrev(0) {}
265
266 DocModification(int modificationType_, const Action &act, int linesAdded_=0) :
267 modificationType(modificationType_),
268 position(act.position / 2),
269 length(act.lenData),
270 linesAdded(linesAdded_),
271 text(act.data),
272 line(0),
273 foldLevelNow(0),
274 foldLevelPrev(0) {}
275 };
276
277 /**
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.
280 */
281 class DocWatcher {
282 public:
283 virtual ~DocWatcher() {}
284
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;
290 };
291
292 #endif