]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/CellBuffer.h
Updated wxSTC's copy of Scintilla to version 1.61
[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-2004 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; ///< The cell buffer itself.
154 int size; ///< Allocated size of the buffer.
155 int length; ///< Total length of the data.
156 int part1len; ///< Length of the first part.
157 int gaplen; ///< Length of the gap between the two parts.
158 char *part2body; ///< The second part of the cell buffer.
159 ///< Doesn't point after the gap but set so that
160 ///< part2body[position] is consistent with body[position].
161 bool readOnly;
162 int growSize;
163
164 bool collectingUndo;
165 UndoHistory uh;
166
167 LineVector lv;
168
169 SVector lineStates;
170
171 void GapTo(int position);
172 void RoomFor(int insertionLength);
173
174 inline char ByteAt(int position);
175 void SetByteAt(int position, char ch);
176
177 public:
178
179 CellBuffer(int initialLength = 4000);
180 ~CellBuffer();
181
182 /// Retrieving positions outside the range of the buffer works and returns 0
183 char CharAt(int position);
184 void GetCharRange(char *buffer, int position, int lengthRetrieve);
185 char StyleAt(int position);
186
187 int ByteLength();
188 int Length();
189 void Allocate(int newSize);
190 int Lines();
191 int LineStart(int line);
192 int LineFromPosition(int pos) { return lv.LineFromPosition(pos); }
193 const char *InsertString(int position, char *s, int insertLength);
194 void InsertCharStyle(int position, char ch, char style);
195
196 /// Setting styles for positions outside the range of the buffer is safe and has no effect.
197 /// @return true if the style of a character is changed.
198 bool SetStyleAt(int position, char style, char mask='\377');
199 bool SetStyleFor(int position, int length, char style, char mask);
200
201 const char *DeleteChars(int position, int deleteLength);
202
203 bool IsReadOnly();
204 void SetReadOnly(bool set);
205
206 /// The save point is a marker in the undo stack where the container has stated that
207 /// the buffer was saved. Undo and redo can move over the save point.
208 void SetSavePoint();
209 bool IsSavePoint();
210
211 /// Line marker functions
212 int AddMark(int line, int markerNum);
213 void DeleteMark(int line, int markerNum);
214 void DeleteMarkFromHandle(int markerHandle);
215 int GetMark(int line);
216 void DeleteAllMarks(int markerNum);
217 int LineFromHandle(int markerHandle);
218
219 /// Actions without undo
220 void BasicInsertString(int position, char *s, int insertLength);
221 void BasicDeleteChars(int position, int deleteLength);
222
223 bool SetUndoCollection(bool collectUndo);
224 bool IsCollectingUndo();
225 void BeginUndoAction();
226 void EndUndoAction();
227 void DeleteUndoHistory();
228
229 /// To perform an undo, StartUndo is called to retrieve the number of steps, then UndoStep is
230 /// called that many times. Similarly for redo.
231 bool CanUndo();
232 int StartUndo();
233 const Action &GetUndoStep() const;
234 void PerformUndoStep();
235 bool CanRedo();
236 int StartRedo();
237 const Action &GetRedoStep() const;
238 void PerformRedoStep();
239
240 int SetLineState(int line, int state);
241 int GetLineState(int line);
242 int GetMaxLineState();
243
244 int SetLevel(int line, int level);
245 int GetLevel(int line);
246 void ClearLevels();
247 };
248
249 #define CELL_SIZE 2
250
251 #endif