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