]>
Commit | Line | Data |
---|---|---|
9ce192d4 | 1 | // Scintilla source code edit control |
65ec6247 RD |
2 | /** @file CellBuffer.h |
3 | ** Manages the text of the document. | |
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 CELLBUFFER_H | |
9 | #define CELLBUFFER_H | |
10 | ||
65ec6247 RD |
11 | /** |
12 | * This holds the marker identifier and the marker type to display. | |
13 | * MarkerHandleNumbers are members of lists. | |
14 | */ | |
9ce192d4 RD |
15 | struct MarkerHandleNumber { |
16 | int handle; | |
17 | int number; | |
18 | MarkerHandleNumber *next; | |
19 | }; | |
20 | ||
65ec6247 RD |
21 | /** |
22 | * A marker handle set contains any number of MarkerHandleNumbers. | |
23 | */ | |
9ce192d4 RD |
24 | class MarkerHandleSet { |
25 | MarkerHandleNumber *root; | |
65ec6247 | 26 | |
9ce192d4 RD |
27 | public: |
28 | MarkerHandleSet(); | |
29 | ~MarkerHandleSet(); | |
30 | int Length(); | |
31 | int NumberFromHandle(int handle); | |
65ec6247 | 32 | int MarkValue(); ///< Bit set of marker numbers. |
9ce192d4 RD |
33 | bool Contains(int handle); |
34 | bool InsertHandle(int handle, int markerNum); | |
35 | void RemoveHandle(int handle); | |
36 | void RemoveNumber(int markerNum); | |
37 | void CombineWith(MarkerHandleSet *other); | |
38 | }; | |
39 | ||
65ec6247 RD |
40 | /** |
41 | * Each line stores the starting position of the first character of the line in the cell buffer | |
42 | * and potentially a marker handle set. Often a line will not have any attached markers. | |
43 | */ | |
9ce192d4 RD |
44 | struct LineData { |
45 | int startPosition; | |
46 | MarkerHandleSet *handleSet; | |
47 | LineData() : startPosition(0), handleSet(0) { | |
48 | } | |
49 | }; | |
50 | ||
65ec6247 RD |
51 | /** |
52 | * The line vector contains information about each of the lines in a cell buffer. | |
53 | */ | |
9ce192d4 RD |
54 | class LineVector { |
55 | public: | |
56 | enum { growSize = 4000 }; | |
57 | int lines; | |
58 | LineData *linesData; | |
59 | int size; | |
60 | int *levels; | |
61 | int sizeLevels; | |
65ec6247 RD |
62 | |
63 | /// Handles are allocated sequentially and should never have to be reused as 32 bit ints are very big. | |
9ce192d4 | 64 | int handleCurrent; |
65ec6247 | 65 | |
9ce192d4 RD |
66 | LineVector(); |
67 | ~LineVector(); | |
68 | void Init(); | |
69 | ||
70 | void Expand(int sizeNew); | |
71 | void ExpandLevels(int sizeNew=-1); | |
65ec6247 | 72 | void ClearLevels(); |
9ce192d4 RD |
73 | void InsertValue(int pos, int value); |
74 | void SetValue(int pos, int value); | |
75 | void Remove(int pos); | |
76 | int LineFromPosition(int pos); | |
65ec6247 | 77 | |
9ce192d4 RD |
78 | int AddMark(int line, int marker); |
79 | void MergeMarkers(int pos); | |
80 | void DeleteMark(int line, int markerNum); | |
81 | void DeleteMarkFromHandle(int markerHandle); | |
82 | int LineFromHandle(int markerHandle); | |
83 | }; | |
84 | ||
9ce192d4 RD |
85 | enum actionType { insertAction, removeAction, startAction }; |
86 | ||
65ec6247 RD |
87 | /** |
88 | * Actions are used to store all the information required to perform one undo/redo step. | |
89 | */ | |
9ce192d4 RD |
90 | class Action { |
91 | public: | |
92 | actionType at; | |
93 | int position; | |
94 | char *data; | |
95 | int lenData; | |
f6bcfd97 | 96 | bool mayCoalesce; |
9ce192d4 RD |
97 | |
98 | Action(); | |
99 | ~Action(); | |
f6bcfd97 | 100 | void Create(actionType at_, int position_=0, char *data_=0, int lenData_=0, bool mayCoalesce_=true); |
9ce192d4 RD |
101 | void Destroy(); |
102 | void Grab(Action *source); | |
103 | }; | |
104 | ||
65ec6247 RD |
105 | /** |
106 | * | |
107 | */ | |
f6bcfd97 BP |
108 | class UndoHistory { |
109 | Action *actions; | |
110 | int lenActions; | |
111 | int maxAction; | |
112 | int currentAction; | |
113 | int undoSequenceDepth; | |
114 | int savePoint; | |
115 | ||
116 | void EnsureUndoRoom(); | |
65ec6247 | 117 | |
f6bcfd97 BP |
118 | public: |
119 | UndoHistory(); | |
120 | ~UndoHistory(); | |
65ec6247 | 121 | |
f6bcfd97 BP |
122 | void AppendAction(actionType at, int position, char *data, int length); |
123 | ||
124 | void BeginUndoAction(); | |
125 | void EndUndoAction(); | |
126 | void DropUndoSequence(); | |
127 | void DeleteUndoHistory(); | |
65ec6247 RD |
128 | |
129 | /// The save point is a marker in the undo stack where the container has stated that | |
130 | /// the buffer was saved. Undo and redo can move over the save point. | |
f6bcfd97 BP |
131 | void SetSavePoint(); |
132 | bool IsSavePoint() const; | |
133 | ||
65ec6247 RD |
134 | /// To perform an undo, StartUndo is called to retrieve the number of steps, then UndoStep is |
135 | /// called that many times. Similarly for redo. | |
f6bcfd97 BP |
136 | bool CanUndo() const; |
137 | int StartUndo(); | |
138 | const Action &GetUndoStep() const; | |
139 | void CompletedUndoStep(); | |
140 | bool CanRedo() const; | |
141 | int StartRedo(); | |
142 | const Action &GetRedoStep() const; | |
143 | void CompletedRedoStep(); | |
144 | }; | |
145 | ||
65ec6247 RD |
146 | /** |
147 | * Holder for an expandable array of characters that supports undo and line markers. | |
148 | * Based on article "Data Structures in a Bit-Mapped Text Editor" | |
149 | * by Wilfred J. Hansen, Byte January 1987, page 183. | |
150 | */ | |
9ce192d4 RD |
151 | class CellBuffer { |
152 | private: | |
153 | char *body; | |
154 | int size; | |
155 | int length; | |
156 | int part1len; | |
157 | int gaplen; | |
158 | char *part2body; | |
159 | bool readOnly; | |
65ec6247 | 160 | int growSize; |
9ce192d4 | 161 | |
d134f170 | 162 | bool collectingUndo; |
f6bcfd97 | 163 | UndoHistory uh; |
9ce192d4 RD |
164 | |
165 | LineVector lv; | |
166 | ||
d134f170 | 167 | SVector lineStates; |
9ce192d4 RD |
168 | |
169 | void GapTo(int position); | |
170 | void RoomFor(int insertionLength); | |
171 | ||
9ce192d4 RD |
172 | inline char ByteAt(int position); |
173 | void SetByteAt(int position, char ch); | |
174 | ||
175 | public: | |
176 | ||
177 | CellBuffer(int initialLength = 4000); | |
178 | ~CellBuffer(); | |
65ec6247 RD |
179 | |
180 | /// Retrieving positions outside the range of the buffer works and returns 0 | |
9ce192d4 RD |
181 | char CharAt(int position); |
182 | void GetCharRange(char *buffer, int position, int lengthRetrieve); | |
183 | char StyleAt(int position); | |
65ec6247 | 184 | |
9ce192d4 RD |
185 | int ByteLength(); |
186 | int Length(); | |
187 | int Lines(); | |
188 | int LineStart(int line); | |
189 | int LineFromPosition(int pos) { return lv.LineFromPosition(pos); } | |
190 | const char *InsertString(int position, char *s, int insertLength); | |
191 | void InsertCharStyle(int position, char ch, char style); | |
65ec6247 RD |
192 | |
193 | /// Setting styles for positions outside the range of the buffer is safe and has no effect. | |
194 | /// @return true if the style of a character is changed. | |
f6bcfd97 | 195 | bool SetStyleAt(int position, char style, char mask='\377'); |
9ce192d4 | 196 | bool SetStyleFor(int position, int length, char style, char mask); |
65ec6247 | 197 | |
9ce192d4 RD |
198 | const char *DeleteChars(int position, int deleteLength); |
199 | ||
200 | bool IsReadOnly(); | |
201 | void SetReadOnly(bool set); | |
202 | ||
65ec6247 RD |
203 | /// The save point is a marker in the undo stack where the container has stated that |
204 | /// the buffer was saved. Undo and redo can move over the save point. | |
9ce192d4 RD |
205 | void SetSavePoint(); |
206 | bool IsSavePoint(); | |
207 | ||
65ec6247 | 208 | /// Line marker functions |
9ce192d4 RD |
209 | int AddMark(int line, int markerNum); |
210 | void DeleteMark(int line, int markerNum); | |
211 | void DeleteMarkFromHandle(int markerHandle); | |
212 | int GetMark(int line); | |
213 | void DeleteAllMarks(int markerNum); | |
214 | int LineFromHandle(int markerHandle); | |
215 | ||
65ec6247 | 216 | /// Actions without undo |
9ce192d4 RD |
217 | void BasicInsertString(int position, char *s, int insertLength); |
218 | void BasicDeleteChars(int position, int deleteLength); | |
219 | ||
d134f170 | 220 | bool SetUndoCollection(bool collectUndo); |
9ce192d4 | 221 | bool IsCollectingUndo(); |
9ce192d4 RD |
222 | void BeginUndoAction(); |
223 | void EndUndoAction(); | |
224 | void DeleteUndoHistory(); | |
65ec6247 RD |
225 | |
226 | /// To perform an undo, StartUndo is called to retrieve the number of steps, then UndoStep is | |
227 | /// called that many times. Similarly for redo. | |
9ce192d4 RD |
228 | bool CanUndo(); |
229 | int StartUndo(); | |
f6bcfd97 BP |
230 | const Action &GetUndoStep() const; |
231 | void PerformUndoStep(); | |
9ce192d4 RD |
232 | bool CanRedo(); |
233 | int StartRedo(); | |
f6bcfd97 BP |
234 | const Action &GetRedoStep() const; |
235 | void PerformRedoStep(); | |
65ec6247 | 236 | |
9ce192d4 RD |
237 | int SetLineState(int line, int state); |
238 | int GetLineState(int line); | |
239 | int GetMaxLineState(); | |
65ec6247 | 240 | |
9ce192d4 RD |
241 | int SetLevel(int line, int level); |
242 | int GetLevel(int line); | |
65ec6247 | 243 | void ClearLevels(); |
9ce192d4 RD |
244 | }; |
245 | ||
246 | #define CELL_SIZE 2 | |
247 | ||
248 | #endif |