]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/CellBuffer.h
409a8749dedcd33ecb00d512cc4f134e53d5dcc7
[wxWidgets.git] / src / stc / scintilla / src / CellBuffer.h
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);
61 void InsertValue(int pos, int value);
62 void SetValue(int pos, int value);
63 void Remove(int pos);
64 int LineFromPosition(int pos);
65
66 int AddMark(int line, int marker);
67 void MergeMarkers(int pos);
68 void DeleteMark(int line, int markerNum);
69 void DeleteMarkFromHandle(int markerHandle);
70 int LineFromHandle(int markerHandle);
71 };
72
73 // Actions are used to store all the information required to perform one undo/redo step.
74 enum actionType { insertAction, removeAction, startAction };
75
76 class Action {
77 public:
78 actionType at;
79 int position;
80 char *data;
81 int lenData;
82 bool mayCoalesce;
83
84 Action();
85 ~Action();
86 void Create(actionType at_, int position_=0, char *data_=0, int lenData_=0, bool mayCoalesce_=true);
87 void Destroy();
88 void Grab(Action *source);
89 };
90
91 enum undoCollectionType { undoCollectNone, undoCollectAutoStart, undoCollectManualStart };
92
93 class UndoHistory {
94 Action *actions;
95 int lenActions;
96 int maxAction;
97 int currentAction;
98 int undoSequenceDepth;
99 int savePoint;
100
101 void EnsureUndoRoom();
102
103 public:
104 UndoHistory();
105 ~UndoHistory();
106
107 void AppendAction(actionType at, int position, char *data, int length);
108
109 void BeginUndoAction();
110 void EndUndoAction();
111 void DropUndoSequence();
112 void DeleteUndoHistory();
113
114 // The save point is a marker in the undo stack where the container has stated that
115 // the buffer was saved. Undo and redo can move over the save point.
116 void SetSavePoint();
117 bool IsSavePoint() const;
118
119 // To perform an undo, StartUndo is called to retrieve the number of steps, then UndoStep is
120 // called that many times. Similarly for redo.
121 bool CanUndo() const;
122 int StartUndo();
123 const Action &GetUndoStep() const;
124 void CompletedUndoStep();
125 bool CanRedo() const;
126 int StartRedo();
127 const Action &GetRedoStep() const;
128 void CompletedRedoStep();
129 };
130
131 // Holder for an expandable array of characters that supports undo and line markers
132 // Based on article "Data Structures in a Bit-Mapped Text Editor"
133 // by Wilfred J. Hansen, Byte January 1987, page 183
134 class CellBuffer {
135 private:
136 char *body;
137 int size;
138 int length;
139 int part1len;
140 int gaplen;
141 char *part2body;
142 bool readOnly;
143
144 undoCollectionType collectingUndo;
145 UndoHistory uh;
146
147 LineVector lv;
148
149 SVector<int, 4000> lineStates;
150
151 void GapTo(int position);
152 void RoomFor(int insertionLength);
153
154 inline char ByteAt(int position);
155 void SetByteAt(int position, char ch);
156
157 public:
158
159 CellBuffer(int initialLength = 4000);
160 ~CellBuffer();
161
162 // Retrieving positions outside the range of the buffer works and returns 0
163 char CharAt(int position);
164 void GetCharRange(char *buffer, int position, int lengthRetrieve);
165 char StyleAt(int position);
166
167 int ByteLength();
168 int Length();
169 int Lines();
170 int LineStart(int line);
171 int LineFromPosition(int pos) { return lv.LineFromPosition(pos); }
172 const char *InsertString(int position, char *s, int insertLength);
173 void InsertCharStyle(int position, char ch, char style);
174
175 // Setting styles for positions outside the range of the buffer is safe and has no effect.
176 // True is returned if the style of a character changed.
177 bool SetStyleAt(int position, char style, char mask='\377');
178 bool SetStyleFor(int position, int length, char style, char mask);
179
180 const char *DeleteChars(int position, int deleteLength);
181
182 bool IsReadOnly();
183 void SetReadOnly(bool set);
184
185 // The save point is a marker in the undo stack where the container has stated that
186 // the buffer was saved. Undo and redo can move over the save point.
187 void SetSavePoint();
188 bool IsSavePoint();
189
190 // Line marker functions
191 int AddMark(int line, int markerNum);
192 void DeleteMark(int line, int markerNum);
193 void DeleteMarkFromHandle(int markerHandle);
194 int GetMark(int line);
195 void DeleteAllMarks(int markerNum);
196 int LineFromHandle(int markerHandle);
197
198 // Without undo
199 void BasicInsertString(int position, char *s, int insertLength);
200 void BasicDeleteChars(int position, int deleteLength);
201
202 undoCollectionType SetUndoCollection(undoCollectionType collectUndo);
203 bool IsCollectingUndo();
204 void BeginUndoAction();
205 void EndUndoAction();
206 void DeleteUndoHistory();
207
208 // To perform an undo, StartUndo is called to retrieve the number of steps, then UndoStep is
209 // called that many times. Similarly for redo.
210 bool CanUndo();
211 int StartUndo();
212 const Action &GetUndoStep() const;
213 void PerformUndoStep();
214 bool CanRedo();
215 int StartRedo();
216 const Action &GetRedoStep() const;
217 void PerformRedoStep();
218
219 int SetLineState(int line, int state);
220 int GetLineState(int line);
221 int GetMaxLineState();
222
223 int SetLevel(int line, int level);
224 int GetLevel(int line);
225 };
226
227 #define CELL_SIZE 2
228
229 #endif