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