updated Scintilla to 2.29
[TortoiseGit.git] / ext / scintilla / src / ContractionState.cxx
blob3b77beb1514b10d96cb7ad0ee13b9d0a10859107
1 // Scintilla source code edit control
2 /** @file ContractionState.cxx
3 ** Manages visibility of lines for folding and wrapping.
4 **/
5 // Copyright 1998-2007 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #include <string.h>
10 #include "Platform.h"
12 #include "SplitVector.h"
13 #include "Partitioning.h"
14 #include "RunStyles.h"
15 #include "ContractionState.h"
17 #ifdef SCI_NAMESPACE
18 using namespace Scintilla;
19 #endif
21 ContractionState::ContractionState() : visible(0), expanded(0), heights(0), displayLines(0), linesInDocument(1) {
22 //InsertLine(0);
25 ContractionState::~ContractionState() {
26 Clear();
29 void ContractionState::EnsureData() {
30 if (OneToOne()) {
31 visible = new RunStyles();
32 expanded = new RunStyles();
33 heights = new RunStyles();
34 displayLines = new Partitioning(4);
35 InsertLines(0, linesInDocument);
39 void ContractionState::Clear() {
40 delete visible;
41 visible = 0;
42 delete expanded;
43 expanded = 0;
44 delete heights;
45 heights = 0;
46 delete displayLines;
47 displayLines = 0;
48 linesInDocument = 1;
51 int ContractionState::LinesInDoc() const {
52 if (OneToOne()) {
53 return linesInDocument;
54 } else {
55 return displayLines->Partitions() - 1;
59 int ContractionState::LinesDisplayed() const {
60 if (OneToOne()) {
61 return linesInDocument;
62 } else {
63 return displayLines->PositionFromPartition(LinesInDoc());
67 int ContractionState::DisplayFromDoc(int lineDoc) const {
68 if (OneToOne()) {
69 return lineDoc;
70 } else {
71 if (lineDoc > displayLines->Partitions())
72 lineDoc = displayLines->Partitions();
73 return displayLines->PositionFromPartition(lineDoc);
77 int ContractionState::DocFromDisplay(int lineDisplay) const {
78 if (OneToOne()) {
79 return lineDisplay;
80 } else {
81 if (lineDisplay <= 0) {
82 return 0;
84 if (lineDisplay > LinesDisplayed()) {
85 return displayLines->PartitionFromPosition(LinesDisplayed());
87 int lineDoc = displayLines->PartitionFromPosition(lineDisplay);
88 PLATFORM_ASSERT(GetVisible(lineDoc));
89 return lineDoc;
93 void ContractionState::InsertLine(int lineDoc) {
94 if (OneToOne()) {
95 linesInDocument++;
96 } else {
97 visible->InsertSpace(lineDoc, 1);
98 visible->SetValueAt(lineDoc, 1);
99 expanded->InsertSpace(lineDoc, 1);
100 expanded->SetValueAt(lineDoc, 1);
101 heights->InsertSpace(lineDoc, 1);
102 heights->SetValueAt(lineDoc, 1);
103 int lineDisplay = DisplayFromDoc(lineDoc);
104 displayLines->InsertPartition(lineDoc, lineDisplay);
105 displayLines->InsertText(lineDoc, 1);
109 void ContractionState::InsertLines(int lineDoc, int lineCount) {
110 for (int l = 0; l < lineCount; l++) {
111 InsertLine(lineDoc + l);
113 Check();
116 void ContractionState::DeleteLine(int lineDoc) {
117 if (OneToOne()) {
118 linesInDocument--;
119 } else {
120 if (GetVisible(lineDoc)) {
121 displayLines->InsertText(lineDoc, -heights->ValueAt(lineDoc));
123 displayLines->RemovePartition(lineDoc);
124 visible->DeleteRange(lineDoc, 1);
125 expanded->DeleteRange(lineDoc, 1);
126 heights->DeleteRange(lineDoc, 1);
130 void ContractionState::DeleteLines(int lineDoc, int lineCount) {
131 for (int l = 0; l < lineCount; l++) {
132 DeleteLine(lineDoc);
134 Check();
137 bool ContractionState::GetVisible(int lineDoc) const {
138 if (OneToOne()) {
139 return true;
140 } else {
141 if (lineDoc >= visible->Length())
142 return true;
143 return visible->ValueAt(lineDoc) == 1;
147 bool ContractionState::SetVisible(int lineDocStart, int lineDocEnd, bool visible_) {
148 if (OneToOne() && visible_) {
149 return false;
150 } else {
151 EnsureData();
152 int delta = 0;
153 Check();
154 if ((lineDocStart <= lineDocEnd) && (lineDocStart >= 0) && (lineDocEnd < LinesInDoc())) {
155 for (int line = lineDocStart; line <= lineDocEnd; line++) {
156 if (GetVisible(line) != visible_) {
157 int difference = visible_ ? heights->ValueAt(line) : -heights->ValueAt(line);
158 visible->SetValueAt(line, visible_ ? 1 : 0);
159 displayLines->InsertText(line, difference);
160 delta += difference;
163 } else {
164 return false;
166 Check();
167 return delta != 0;
171 bool ContractionState::HiddenLines() const {
172 if (OneToOne()) {
173 return false;
174 } else {
175 return !visible->AllSameAs(1);
179 bool ContractionState::GetExpanded(int lineDoc) const {
180 if (OneToOne()) {
181 return true;
182 } else {
183 Check();
184 return expanded->ValueAt(lineDoc) == 1;
188 bool ContractionState::SetExpanded(int lineDoc, bool expanded_) {
189 if (OneToOne() && expanded_) {
190 return false;
191 } else {
192 EnsureData();
193 if (expanded_ != (expanded->ValueAt(lineDoc) == 1)) {
194 expanded->SetValueAt(lineDoc, expanded_ ? 1 : 0);
195 Check();
196 return true;
197 } else {
198 Check();
199 return false;
204 int ContractionState::ContractedNext(int lineDocStart) const {
205 if (OneToOne()) {
206 return -1;
207 } else {
208 Check();
209 if (!expanded->ValueAt(lineDocStart)) {
210 return lineDocStart;
211 } else {
212 int lineDocNextChange = expanded->EndRun(lineDocStart);
213 if (lineDocNextChange < LinesInDoc())
214 return lineDocNextChange;
215 else
216 return -1;
221 int ContractionState::GetHeight(int lineDoc) const {
222 if (OneToOne()) {
223 return 1;
224 } else {
225 return heights->ValueAt(lineDoc);
229 // Set the number of display lines needed for this line.
230 // Return true if this is a change.
231 bool ContractionState::SetHeight(int lineDoc, int height) {
232 if (OneToOne() && (height == 1)) {
233 return false;
234 } else if (lineDoc < LinesInDoc()) {
235 EnsureData();
236 if (GetHeight(lineDoc) != height) {
237 if (GetVisible(lineDoc)) {
238 displayLines->InsertText(lineDoc, height - GetHeight(lineDoc));
240 heights->SetValueAt(lineDoc, height);
241 Check();
242 return true;
243 } else {
244 Check();
245 return false;
247 } else {
248 return false;
252 void ContractionState::ShowAll() {
253 int lines = LinesInDoc();
254 Clear();
255 linesInDocument = lines;
258 // Debugging checks
260 void ContractionState::Check() const {
261 #ifdef CHECK_CORRECTNESS
262 for (int vline = 0; vline < LinesDisplayed(); vline++) {
263 const int lineDoc = DocFromDisplay(vline);
264 PLATFORM_ASSERT(GetVisible(lineDoc));
266 for (int lineDoc = 0; lineDoc < LinesInDoc(); lineDoc++) {
267 const int displayThis = DisplayFromDoc(lineDoc);
268 const int displayNext = DisplayFromDoc(lineDoc + 1);
269 const int height = displayNext - displayThis;
270 PLATFORM_ASSERT(height >= 0);
271 if (GetVisible(lineDoc)) {
272 PLATFORM_ASSERT(GetHeight(lineDoc) == height);
273 } else {
274 PLATFORM_ASSERT(0 == height);
277 #endif