]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/CellBuffer.h
5fbe2ea8a01cfdd24353786b59d7b055ded4dc59
[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 // Holder for an expandable array of characters that supports undo and line markers
93 // Based on article "Data Structures in a Bit-Mapped Text Editor"
94 // by Wilfred J. Hansen, Byte January 1987, page 183
95 class CellBuffer {
96 private:
97 char *body;
98 int size;
99 int length;
100 int part1len;
101 int gaplen;
102 char *part2body;
103 bool readOnly;
104
105 Action *actions;
106 int lenActions;
107 int maxAction;
108 int currentAction;
109 undoCollectionType collectingUndo;
110 int undoSequenceDepth;
111 int savePoint;
112
113 LineVector lv;
114
115 SVector<int, 4000> lineStates;
116
117 void GapTo(int position);
118 void RoomFor(int insertionLength);
119
120 void EnsureUndoRoom();
121 void AppendAction(actionType at, int position, char *data, int length);
122
123 inline char ByteAt(int position);
124 void SetByteAt(int position, char ch);
125
126 public:
127
128 CellBuffer(int initialLength = 4000);
129 ~CellBuffer();
130
131 // Retrieving positions outside the range of the buffer works and returns 0
132 char CharAt(int position);
133 void GetCharRange(char *buffer, int position, int lengthRetrieve);
134 char StyleAt(int position);
135
136 int ByteLength();
137 int Length();
138 int Lines();
139 int LineStart(int line);
140 int LineFromPosition(int pos) { return lv.LineFromPosition(pos); }
141 const char *InsertString(int position, char *s, int insertLength);
142 void InsertCharStyle(int position, char ch, char style);
143
144 // Setting styles for positions outside the range of the buffer is safe and has no effect.
145 // True is returned if the style of a character changed.
146 bool SetStyleAt(int position, char style, char mask=(char)0xff);
147 bool SetStyleFor(int position, int length, char style, char mask);
148
149 const char *DeleteChars(int position, int deleteLength);
150
151 bool IsReadOnly();
152 void SetReadOnly(bool set);
153
154 // The save point is a marker in the undo stack where the container has stated that
155 // the buffer was saved. Undo and redo can move over the save point.
156 void SetSavePoint();
157 bool IsSavePoint();
158
159 // Line marker functions
160 int AddMark(int line, int markerNum);
161 void DeleteMark(int line, int markerNum);
162 void DeleteMarkFromHandle(int markerHandle);
163 int GetMark(int line);
164 void DeleteAllMarks(int markerNum);
165 int LineFromHandle(int markerHandle);
166
167 // Without undo
168 void BasicInsertString(int position, char *s, int insertLength);
169 void BasicDeleteChars(int position, int deleteLength);
170
171 undoCollectionType SetUndoCollection(undoCollectionType collectUndo);
172 bool IsCollectingUndo();
173 void AppendUndoStartAction();
174 void BeginUndoAction();
175 void EndUndoAction();
176 void DeleteUndoHistory();
177
178 // To perform an undo, StartUndo is called to retreive the number of steps, then UndoStep is
179 // called that many times. Similarly for redo.
180 bool CanUndo();
181 int StartUndo();
182 const Action &UndoStep();
183 bool CanRedo();
184 int StartRedo();
185 const Action &RedoStep();
186
187 int SetLineState(int line, int state);
188 int GetLineState(int line);
189 int GetMaxLineState();
190
191 int SetLevel(int line, int level);
192 int GetLevel(int line);
193 };
194
195 #define CELL_SIZE 2
196
197 #endif