]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/CellBuffer.h
6e052077e931c9ff3167fa277a3e42f9bb6e407d
[wxWidgets.git] / contrib / 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
83 Action();
84 ~Action();
85 void Create(actionType at_, int position_=0, char *data_=0, int lenData_=0);
86 void Destroy();
87 void Grab(Action *source);
88 };
89
90 enum undoCollectionType { undoCollectNone, undoCollectAutoStart, undoCollectManualStart };
91
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 &UndoStep();
123 bool CanRedo() const;
124 int StartRedo();
125 const Action &RedoStep();
126 };
127
128 // Holder for an expandable array of characters that supports undo and line markers
129 // Based on article "Data Structures in a Bit-Mapped Text Editor"
130 // by Wilfred J. Hansen, Byte January 1987, page 183
131 class CellBuffer {
132 private:
133 char *body;
134 int size;
135 int length;
136 int part1len;
137 int gaplen;
138 char *part2body;
139 bool readOnly;
140
141 undoCollectionType collectingUndo;
142 UndoHistory uh;
143
144 LineVector lv;
145
146 SVector<int, 4000> lineStates;
147
148 void GapTo(int position);
149 void RoomFor(int insertionLength);
150
151 inline char ByteAt(int position);
152 void SetByteAt(int position, char ch);
153
154 public:
155
156 CellBuffer(int initialLength = 4000);
157 ~CellBuffer();
158
159 // Retrieving positions outside the range of the buffer works and returns 0
160 char CharAt(int position);
161 void GetCharRange(char *buffer, int position, int lengthRetrieve);
162 char StyleAt(int position);
163
164 int ByteLength();
165 int Length();
166 int Lines();
167 int LineStart(int line);
168 int LineFromPosition(int pos) { return lv.LineFromPosition(pos); }
169 const char *InsertString(int position, char *s, int insertLength);
170 void InsertCharStyle(int position, char ch, char style);
171
172 // Setting styles for positions outside the range of the buffer is safe and has no effect.
173 // True is returned if the style of a character changed.
174 bool SetStyleAt(int position, char style, char mask=(char)0xff);
175 bool SetStyleFor(int position, int length, char style, char mask);
176
177 const char *DeleteChars(int position, int deleteLength);
178
179 bool IsReadOnly();
180 void SetReadOnly(bool set);
181
182 // The save point is a marker in the undo stack where the container has stated that
183 // the buffer was saved. Undo and redo can move over the save point.
184 void SetSavePoint();
185 bool IsSavePoint();
186
187 // Line marker functions
188 int AddMark(int line, int markerNum);
189 void DeleteMark(int line, int markerNum);
190 void DeleteMarkFromHandle(int markerHandle);
191 int GetMark(int line);
192 void DeleteAllMarks(int markerNum);
193 int LineFromHandle(int markerHandle);
194
195 // Without undo
196 void BasicInsertString(int position, char *s, int insertLength);
197 void BasicDeleteChars(int position, int deleteLength);
198
199 undoCollectionType SetUndoCollection(undoCollectionType collectUndo);
200 bool IsCollectingUndo();
201 void BeginUndoAction();
202 void EndUndoAction();
203 void DeleteUndoHistory();
204
205 // To perform an undo, StartUndo is called to retrieve the number of steps, then UndoStep is
206 // called that many times. Similarly for redo.
207 bool CanUndo();
208 int StartUndo();
209 const Action &UndoStep();
210 bool CanRedo();
211 int StartRedo();
212 const Action &RedoStep();
213
214 int SetLineState(int line, int state);
215 int GetLineState(int line);
216 int GetMaxLineState();
217
218 int SetLevel(int line, int level);
219 int GetLevel(int line);
220 };
221
222 #define CELL_SIZE 2
223
224 #endif