]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/CellBuffer.h
2866d548cb808560373c6d7040c495a0ae277020
[wxWidgets.git] / contrib / src / stc / scintilla / src / CellBuffer.h
1 // Scintilla source code edit control
2 /** @file CellBuffer.h
3 ** Manages the text of the document.
4 **/
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
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
11 /**
12 * This holds the marker identifier and the marker type to display.
13 * MarkerHandleNumbers are members of lists.
14 */
15 struct MarkerHandleNumber {
16 int handle;
17 int number;
18 MarkerHandleNumber *next;
19 };
20
21 /**
22 * A marker handle set contains any number of MarkerHandleNumbers.
23 */
24 class MarkerHandleSet {
25 MarkerHandleNumber *root;
26
27 public:
28 MarkerHandleSet();
29 ~MarkerHandleSet();
30 int Length();
31 int NumberFromHandle(int handle);
32 int MarkValue(); ///< Bit set of marker numbers.
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
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 */
44 struct LineData {
45 int startPosition;
46 MarkerHandleSet *handleSet;
47 LineData() : startPosition(0), handleSet(0) {
48 }
49 };
50
51 /**
52 * The line vector contains information about each of the lines in a cell buffer.
53 */
54 class LineVector {
55 public:
56 int growSize;
57 int lines;
58 LineData *linesData;
59 int size;
60 int *levels;
61 int sizeLevels;
62
63 /// Handles are allocated sequentially and should never have to be reused as 32 bit ints are very big.
64 int handleCurrent;
65
66 LineVector();
67 ~LineVector();
68 void Init();
69
70 void Expand(int sizeNew);
71 void ExpandLevels(int sizeNew=-1);
72 void ClearLevels();
73 void InsertValue(int pos, int value);
74 void SetValue(int pos, int value);
75 void Remove(int pos);
76 int LineFromPosition(int pos);
77
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
85 enum actionType { insertAction, removeAction, startAction };
86
87 /**
88 * Actions are used to store all the information required to perform one undo/redo step.
89 */
90 class Action {
91 public:
92 actionType at;
93 int position;
94 char *data;
95 int lenData;
96 bool mayCoalesce;
97
98 Action();
99 ~Action();
100 void Create(actionType at_, int position_=0, char *data_=0, int lenData_=0, bool mayCoalesce_=true);
101 void Destroy();
102 void Grab(Action *source);
103 };
104
105 /**
106 *
107 */
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();
117
118 public:
119 UndoHistory();
120 ~UndoHistory();
121
122 void AppendAction(actionType at, int position, char *data, int length);
123
124 void BeginUndoAction();
125 void EndUndoAction();
126 void DropUndoSequence();
127 void DeleteUndoHistory();
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.
131 void SetSavePoint();
132 bool IsSavePoint() const;
133
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.
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
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 */
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;
160 int growSize;
161
162 bool collectingUndo;
163 UndoHistory uh;
164
165 LineVector lv;
166
167 SVector lineStates;
168
169 void GapTo(int position);
170 void RoomFor(int insertionLength);
171
172 inline char ByteAt(int position);
173 void SetByteAt(int position, char ch);
174
175 public:
176
177 CellBuffer(int initialLength = 4000);
178 ~CellBuffer();
179
180 /// Retrieving positions outside the range of the buffer works and returns 0
181 char CharAt(int position);
182 void GetCharRange(char *buffer, int position, int lengthRetrieve);
183 char StyleAt(int position);
184
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);
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.
195 bool SetStyleAt(int position, char style, char mask='\377');
196 bool SetStyleFor(int position, int length, char style, char mask);
197
198 const char *DeleteChars(int position, int deleteLength);
199
200 bool IsReadOnly();
201 void SetReadOnly(bool set);
202
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.
205 void SetSavePoint();
206 bool IsSavePoint();
207
208 /// Line marker functions
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
216 /// Actions without undo
217 void BasicInsertString(int position, char *s, int insertLength);
218 void BasicDeleteChars(int position, int deleteLength);
219
220 bool SetUndoCollection(bool collectUndo);
221 bool IsCollectingUndo();
222 void BeginUndoAction();
223 void EndUndoAction();
224 void DeleteUndoHistory();
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.
228 bool CanUndo();
229 int StartUndo();
230 const Action &GetUndoStep() const;
231 void PerformUndoStep();
232 bool CanRedo();
233 int StartRedo();
234 const Action &GetRedoStep() const;
235 void PerformRedoStep();
236
237 int SetLineState(int line, int state);
238 int GetLineState(int line);
239 int GetMaxLineState();
240
241 int SetLevel(int line, int level);
242 int GetLevel(int line);
243 void ClearLevels();
244 };
245
246 #define CELL_SIZE 2
247
248 #endif