]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/Document.h
4c212b7bd17c62af226c20c49f386d89a3597024
[wxWidgets.git] / src / stc / scintilla / src / Document.h
1 // Scintilla source code edit control
2 // Document.h - text document that handles notifications, DBCS, styling, words and end of line
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
5
6 #ifndef DOCUMENT_H
7 #define DOCUMENT_H
8
9 // A Position is a position within a document between two characters or at the beginning or end.
10 // Sometimes used as a character index where it identifies the character after the position.
11 typedef int Position;
12 const Position invalidPosition = -1;
13
14 // The range class represents a range of text in a document.
15 // The two values are not sorted as one end may be more significant than the other
16 // as is the case for the selection where the end position is the position of the caret.
17 // If either position is invalidPosition then the range is invalid and most operations will fail.
18 class Range {
19 public:
20 Position start;
21 Position end;
22
23 Range(Position pos=0) :
24 start(pos), end(pos) {
25 };
26 Range(Position start_, Position end_) :
27 start(start_), end(end_) {
28 };
29
30 bool Valid() const {
31 return (start != invalidPosition) && (end != invalidPosition);
32 }
33
34 bool Contains(Position pos) const {
35 if (start < end) {
36 return (pos >= start && pos <= end);
37 } else {
38 return (pos <= start && pos >= end);
39 }
40 }
41
42 bool Contains(Range other) const {
43 return Contains(other.start) && Contains(other.end);
44 }
45
46 bool Overlaps(Range other) const {
47 return
48 Contains(other.start) ||
49 Contains(other.end) ||
50 other.Contains(start) ||
51 other.Contains(end);
52 }
53 };
54
55 class DocWatcher;
56 class DocModification;
57
58 class Document {
59
60 public:
61 // Used to pair watcher pointer with user data
62 class WatcherWithUserData {
63 public:
64 DocWatcher *watcher;
65 void *userData;
66 WatcherWithUserData() {
67 watcher = 0;
68 userData = 0;
69 }
70 };
71
72 private:
73 int refCount;
74 CellBuffer cb;
75 bool wordchars[256];
76 int stylingPos;
77 int stylingMask;
78 int endStyled;
79 int enteredCount;
80
81 WatcherWithUserData *watchers;
82 int lenWatchers;
83
84 public:
85 int stylingBits;
86 int stylingBitsMask;
87
88 int eolMode;
89 int dbcsCodePage;
90 int tabInChars;
91
92 Document();
93 virtual ~Document();
94
95 int AddRef();
96 int Release();
97
98 int LineFromPosition(int pos);
99 int ClampPositionIntoDocument(int pos);
100 bool IsCrLf(int pos);
101 int MovePositionOutsideChar(int pos, int moveDir, bool checkLineEnd=true);
102
103 // Gateways to modifying document
104 void DeleteChars(int pos, int len);
105 void InsertStyledString(int position, char *s, int insertLength);
106 int Undo();
107 int Redo();
108 bool CanUndo() { return cb.CanUndo(); }
109 bool CanRedo() { return cb.CanRedo(); }
110 void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
111 undoCollectionType SetUndoCollection(undoCollectionType collectUndo) {
112 return cb.SetUndoCollection(collectUndo);
113 }
114 void BeginUndoAction() { cb.BeginUndoAction(); }
115 void EndUndoAction() { cb.EndUndoAction(); }
116 void SetSavePoint();
117 bool IsSavePoint() { return cb.IsSavePoint(); }
118 void Indent(bool forwards, int lineBottom, int lineTop);
119 void ConvertLineEnds(int eolModeSet);
120 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
121
122 void InsertChar(int pos, char ch);
123 void InsertString(int position, const char *s);
124 void InsertString(int position, const char *s, int insertLength);
125 void ChangeChar(int pos, char ch);
126 void DelChar(int pos);
127 int DelCharBack(int pos);
128
129 char CharAt(int position) { return cb.CharAt(position); }
130 void GetCharRange(char *buffer, int position, int lengthRetrieve) {
131 cb.GetCharRange(buffer, position, lengthRetrieve);
132 }
133 char StyleAt(int position) { return cb.StyleAt(position); }
134 int GetMark(int line) { return cb.GetMark(line); }
135 int AddMark(int line, int markerNum);
136 void DeleteMark(int line, int markerNum);
137 void DeleteMarkFromHandle(int markerHandle);
138 void DeleteAllMarks(int markerNum);
139 int LineFromHandle(int markerHandle) { return cb.LineFromHandle(markerHandle); }
140 int LineStart(int line);
141 int LineEnd(int line);
142 int LineEndPosition(int position);
143 int VCHomePosition(int position);
144
145 int SetLevel(int line, int level);
146 int GetLevel(int line) { return cb.GetLevel(line); }
147 int GetLastChild(int lineParent, int level=-1);
148 int GetFoldParent(int line);
149
150 void Indent(bool forwards);
151 int ExtendWordSelect(int pos, int delta);
152 int NextWordStart(int pos, int delta);
153 int Length() { return cb.Length(); }
154 long FindText(int minPos, int maxPos, const char *s, bool caseSensitive, bool word);
155 long FindText(WORD iMessage,WPARAM wParam,LPARAM lParam);
156 int LinesTotal();
157
158 void ChangeCase(Range r, bool makeUpperCase);
159
160 void SetWordChars(unsigned char *chars);
161 void SetStylingBits(int bits);
162 void StartStyling(int position, char mask);
163 void SetStyleFor(int length, char style);
164 void SetStyles(int length, char *styles);
165 int GetEndStyled() { return endStyled; }
166 bool EnsureStyledTo(int pos);
167
168 int SetLineState(int line, int state) { return cb.SetLineState(line, state); }
169 int GetLineState(int line) { return cb.GetLineState(line); }
170 int GetMaxLineState() { return cb.GetMaxLineState(); }
171
172 bool AddWatcher(DocWatcher *watcher, void *userData);
173 bool RemoveWatcher(DocWatcher *watcher, void *userData);
174 const WatcherWithUserData *GetWatchers() const { return watchers; }
175 int GetLenWatchers() const { return lenWatchers; }
176
177 private:
178 bool IsDBCS(int pos);
179 bool IsWordChar(unsigned char ch);
180 bool IsWordAt(int start, int end);
181 void ModifiedAt(int pos);
182
183 void NotifyModifyAttempt();
184 void NotifySavePoint(bool atSavePoint);
185 void NotifyModified(DocModification mh);
186 };
187
188 // To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
189 // scope of the change.
190 // If the DocWatcher is a document view then this can be used to optimise screen updating.
191 class DocModification {
192 public:
193 int modificationType;
194 int position;
195 int length;
196 int linesAdded; // Negative if lines deleted
197 const char *text; // Only valid for changes to text, not for changes to style
198 int line;
199 int foldLevelNow;
200 int foldLevelPrev;
201
202 DocModification(int modificationType_, int position_=0, int length_=0,
203 int linesAdded_=0, const char *text_=0) :
204 modificationType(modificationType_),
205 position(position_),
206 length(length_),
207 linesAdded(linesAdded_),
208 text(text_),
209 line(0),
210 foldLevelNow(0),
211 foldLevelPrev(0) {}
212 };
213
214 // A class that wants to receive notifications from a Document must be derived from DocWatcher
215 // and implement the notification methods. It can then be added to the watcher list with AddWatcher.
216 class DocWatcher {
217 public:
218 virtual ~DocWatcher() {}
219
220 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
221 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
222 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
223 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
224 virtual void NotifyStyleNeeded(Document *doc, void *userData, int endPos) = 0;
225 };
226
227 #endif