]>
Commit | Line | Data |
---|---|---|
9ce192d4 | 1 | // Scintilla source code edit control |
65ec6247 RD |
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> | |
9ce192d4 RD |
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 | ||
65ec6247 RD |
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 | */ | |
9ce192d4 RD |
15 | typedef int Position; |
16 | const Position invalidPosition = -1; | |
17 | ||
65ec6247 RD |
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 | */ | |
9ce192d4 RD |
24 | class Range { |
25 | public: | |
26 | Position start; | |
27 | Position end; | |
65ec6247 | 28 | |
9ce192d4 RD |
29 | Range(Position pos=0) : |
30 | start(pos), end(pos) { | |
31 | }; | |
32 | Range(Position start_, Position end_) : | |
33 | start(start_), end(end_) { | |
34 | }; | |
65ec6247 | 35 | |
9ce192d4 RD |
36 | bool Valid() const { |
37 | return (start != invalidPosition) && (end != invalidPosition); | |
38 | } | |
65ec6247 | 39 | |
9ce192d4 RD |
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 | } | |
65ec6247 | 47 | |
9ce192d4 RD |
48 | bool Contains(Range other) const { |
49 | return Contains(other.start) && Contains(other.end); | |
50 | } | |
65ec6247 | 51 | |
9ce192d4 RD |
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; | |
65ec6247 | 63 | class RESearch; |
9ce192d4 | 64 | |
65ec6247 RD |
65 | /** |
66 | */ | |
9ce192d4 RD |
67 | class Document { |
68 | ||
69 | public: | |
65ec6247 | 70 | /** Used to pair watcher pointer with user data. */ |
9ce192d4 RD |
71 | class WatcherWithUserData { |
72 | public: | |
73 | DocWatcher *watcher; | |
74 | void *userData; | |
75 | WatcherWithUserData() { | |
76 | watcher = 0; | |
77 | userData = 0; | |
78 | } | |
79 | }; | |
65ec6247 | 80 | |
9ce192d4 RD |
81 | private: |
82 | int refCount; | |
83 | CellBuffer cb; | |
84 | bool wordchars[256]; | |
f6bcfd97 | 85 | char stylingMask; |
9ce192d4 RD |
86 | int endStyled; |
87 | int enteredCount; | |
f6bcfd97 | 88 | int enteredReadOnlyCount; |
65ec6247 | 89 | |
9ce192d4 RD |
90 | WatcherWithUserData *watchers; |
91 | int lenWatchers; | |
65ec6247 RD |
92 | |
93 | bool matchesValid; | |
94 | RESearch *pre; | |
95 | char *substituted; | |
96 | ||
9ce192d4 RD |
97 | public: |
98 | int stylingBits; | |
99 | int stylingBitsMask; | |
65ec6247 | 100 | |
9ce192d4 | 101 | int eolMode; |
65ec6247 | 102 | /// Can also be SC_CP_UTF8 to enable UTF-8 mode |
9ce192d4 RD |
103 | int dbcsCodePage; |
104 | int tabInChars; | |
f6bcfd97 BP |
105 | int indentInChars; |
106 | bool useTabs; | |
65ec6247 RD |
107 | bool tabIndents; |
108 | bool backspaceUnindents; | |
109 | ||
9ce192d4 RD |
110 | Document(); |
111 | virtual ~Document(); | |
65ec6247 | 112 | |
9ce192d4 RD |
113 | int AddRef(); |
114 | int Release(); | |
65ec6247 | 115 | |
9ce192d4 RD |
116 | int LineFromPosition(int pos); |
117 | int ClampPositionIntoDocument(int pos); | |
118 | bool IsCrLf(int pos); | |
f6bcfd97 | 119 | int LenChar(int pos); |
9ce192d4 RD |
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(); } | |
d134f170 | 130 | bool SetUndoCollection(bool collectUndo) { |
9ce192d4 RD |
131 | return cb.SetUndoCollection(collectUndo); |
132 | } | |
d134f170 | 133 | bool IsCollectingUndo() { return cb.IsCollectingUndo(); } |
9ce192d4 RD |
134 | void BeginUndoAction() { cb.BeginUndoAction(); } |
135 | void EndUndoAction() { cb.EndUndoAction(); } | |
136 | void SetSavePoint(); | |
137 | bool IsSavePoint() { return cb.IsSavePoint(); } | |
f6bcfd97 BP |
138 | |
139 | int GetLineIndentation(int line); | |
140 | void SetLineIndentation(int line, int indent); | |
141 | int GetLineIndentPosition(int line); | |
d134f170 | 142 | int GetColumn(int position); |
9ce192d4 RD |
143 | void Indent(bool forwards, int lineBottom, int lineTop); |
144 | void ConvertLineEnds(int eolModeSet); | |
145 | void SetReadOnly(bool set) { cb.SetReadOnly(set); } | |
d134f170 | 146 | bool IsReadOnly() { return cb.IsReadOnly(); } |
9ce192d4 RD |
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); | |
f6bcfd97 | 151 | void ChangeChar(int pos, char ch); |
9ce192d4 RD |
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); } | |
f6bcfd97 BP |
161 | int AddMark(int line, int markerNum); |
162 | void DeleteMark(int line, int markerNum); | |
163 | void DeleteMarkFromHandle(int markerHandle); | |
164 | void DeleteAllMarks(int markerNum); | |
9ce192d4 RD |
165 | int LineFromHandle(int markerHandle) { return cb.LineFromHandle(markerHandle); } |
166 | int LineStart(int line); | |
f6bcfd97 | 167 | int LineEnd(int line); |
9ce192d4 RD |
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); } | |
d134f170 | 173 | void ClearLevels() { cb.ClearLevels(); } |
9ce192d4 RD |
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(); } | |
d134f170 | 181 | long FindText(int minPos, int maxPos, const char *s, |
65ec6247 | 182 | bool caseSensitive, bool word, bool wordStart, bool regExp, int *length); |
d134f170 | 183 | long FindText(int iMessage, unsigned long wParam, long lParam); |
65ec6247 | 184 | const char *SubstituteByPosition(const char *text, int *length); |
9ce192d4 | 185 | int LinesTotal(); |
65ec6247 | 186 | |
f6bcfd97 | 187 | void ChangeCase(Range r, bool makeUpperCase); |
65ec6247 | 188 | |
9ce192d4 RD |
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; } | |
f6bcfd97 | 195 | bool EnsureStyledTo(int pos); |
9ce192d4 RD |
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(); } | |
65ec6247 | 200 | |
9ce192d4 RD |
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; } | |
65ec6247 RD |
205 | |
206 | bool IsWordPartSeparator(char ch); | |
207 | int WordPartLeft(int pos); | |
208 | int WordPartRight(int pos); | |
209 | ||
9ce192d4 RD |
210 | private: |
211 | bool IsDBCS(int pos); | |
212 | bool IsWordChar(unsigned char ch); | |
d134f170 RD |
213 | bool IsWordStartAt(int pos); |
214 | bool IsWordEndAt(int pos); | |
9ce192d4 RD |
215 | bool IsWordAt(int start, int end); |
216 | void ModifiedAt(int pos); | |
65ec6247 | 217 | |
9ce192d4 RD |
218 | void NotifyModifyAttempt(); |
219 | void NotifySavePoint(bool atSavePoint); | |
220 | void NotifyModified(DocModification mh); | |
65ec6247 | 221 | |
f6bcfd97 | 222 | int IndentSize() { return indentInChars ? indentInChars : tabInChars; } |
9ce192d4 RD |
223 | }; |
224 | ||
65ec6247 RD |
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 | */ | |
9ce192d4 RD |
230 | class DocModification { |
231 | public: | |
232 | int modificationType; | |
233 | int position; | |
234 | int length; | |
65ec6247 RD |
235 | int linesAdded; /**< Negative if lines deleted. */ |
236 | const char *text; /**< Only valid for changes to text, not for changes to style. */ | |
9ce192d4 RD |
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) {} | |
f6bcfd97 BP |
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) {} | |
9ce192d4 RD |
261 | }; |
262 | ||
65ec6247 RD |
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 | */ | |
9ce192d4 RD |
267 | class DocWatcher { |
268 | public: | |
269 | virtual ~DocWatcher() {} | |
65ec6247 | 270 | |
9ce192d4 RD |
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; | |
f6bcfd97 | 275 | virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0; |
9ce192d4 RD |
276 | }; |
277 | ||
278 | #endif |