]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/Document.h
fba611c7f7962d1a86d55de42cd577ec075945aa
[wxWidgets.git] / contrib / 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 AppendUndoStartAction() { cb.AppendUndoStartAction(); }
115 void BeginUndoAction() { cb.BeginUndoAction(); }
116 void EndUndoAction() { cb.EndUndoAction(); }
117 void SetSavePoint();
118 bool IsSavePoint() { return cb.IsSavePoint(); }
119 void Indent(bool forwards, int lineBottom, int lineTop);
120 void ConvertLineEnds(int eolModeSet);
121 void SetReadOnly(bool set) { cb.SetReadOnly(set); }
122
123 void InsertChar(int pos, char ch);
124 void InsertString(int position, const char *s);
125 void InsertString(int position, const char *s, int insertLength);
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) { return cb.AddMark(line, markerNum); }
136 void DeleteMark(int line, int markerNum) { cb.DeleteMark(line, markerNum); }
137 void DeleteMarkFromHandle(int markerHandle) { cb.DeleteMarkFromHandle(markerHandle); }
138 void DeleteAllMarks(int markerNum) { cb.DeleteAllMarks(markerNum); }
139 int LineFromHandle(int markerHandle) { return cb.LineFromHandle(markerHandle); }
140 int LineStart(int line);
141 int LineEndPosition(int position);
142 int VCHomePosition(int position);
143
144 int SetLevel(int line, int level);
145 int GetLevel(int line) { return cb.GetLevel(line); }
146 int GetLastChild(int lineParent, int level=-1);
147 int GetFoldParent(int line);
148
149 void Indent(bool forwards);
150 int ExtendWordSelect(int pos, int delta);
151 int NextWordStart(int pos, int delta);
152 int Length() { return cb.Length(); }
153 long FindText(int minPos, int maxPos, const char *s, bool caseSensitive, bool word);
154 long FindText(WORD iMessage,WPARAM wParam,LPARAM lParam);
155 int LinesTotal();
156
157 void SetWordChars(unsigned char *chars);
158 void SetStylingBits(int bits);
159 void StartStyling(int position, char mask);
160 void SetStyleFor(int length, char style);
161 void SetStyles(int length, char *styles);
162 int GetEndStyled() { return endStyled; }
163
164 int SetLineState(int line, int state) { return cb.SetLineState(line, state); }
165 int GetLineState(int line) { return cb.GetLineState(line); }
166 int GetMaxLineState() { return cb.GetMaxLineState(); }
167
168 bool AddWatcher(DocWatcher *watcher, void *userData);
169 bool RemoveWatcher(DocWatcher *watcher, void *userData);
170 const WatcherWithUserData *GetWatchers() const { return watchers; }
171 int GetLenWatchers() const { return lenWatchers; }
172
173 private:
174 bool IsDBCS(int pos);
175 bool IsWordChar(unsigned char ch);
176 bool IsWordAt(int start, int end);
177 void ModifiedAt(int pos);
178
179 void NotifyModifyAttempt();
180 void NotifySavePoint(bool atSavePoint);
181 void NotifyModified(DocModification mh);
182 };
183
184 // To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
185 // scope of the change.
186 // If the DocWatcher is a document view then this can be used to optimise screen updating.
187 class DocModification {
188 public:
189 int modificationType;
190 int position;
191 int length;
192 int linesAdded; // Negative if lines deleted
193 const char *text; // Only valid for changes to text, not for changes to style
194 int line;
195 int foldLevelNow;
196 int foldLevelPrev;
197
198 DocModification(int modificationType_, int position_=0, int length_=0,
199 int linesAdded_=0, const char *text_=0) :
200 modificationType(modificationType_),
201 position(position_),
202 length(length_),
203 linesAdded(linesAdded_),
204 text(text_),
205 line(0),
206 foldLevelNow(0),
207 foldLevelPrev(0) {}
208 };
209
210 // A class that wants to receive notifications from a Document must be derived from DocWatcher
211 // and implement the notification methods. It can then be added to the watcher list with AddWatcher.
212 class DocWatcher {
213 public:
214 virtual ~DocWatcher() {}
215
216 virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
217 virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
218 virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
219 virtual void NotifyDeleted(Document *doc, void *userData) = 0;
220 };
221
222 #endif