1 // Scintilla source code edit control
2 /** @file ContractionState.cxx
3 ** Manages visibility of lines for folding and wrapping.
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.
16 #include "SplitVector.h"
17 #include "Partitioning.h"
18 #include "RunStyles.h"
19 #include "ContractionState.h"
22 using namespace Scintilla
;
25 ContractionState::ContractionState() : visible(0), expanded(0), heights(0), displayLines(0), linesInDocument(1) {
29 ContractionState::~ContractionState() {
33 void ContractionState::EnsureData() {
35 visible
= new RunStyles();
36 expanded
= new RunStyles();
37 heights
= new RunStyles();
38 displayLines
= new Partitioning(4);
39 InsertLines(0, linesInDocument
);
43 void ContractionState::Clear() {
55 int ContractionState::LinesInDoc() const {
57 return linesInDocument
;
59 return displayLines
->Partitions() - 1;
63 int ContractionState::LinesDisplayed() const {
65 return linesInDocument
;
67 return displayLines
->PositionFromPartition(LinesInDoc());
71 int ContractionState::DisplayFromDoc(int lineDoc
) const {
73 return (lineDoc
<= linesInDocument
) ? lineDoc
: linesInDocument
;
75 if (lineDoc
> displayLines
->Partitions())
76 lineDoc
= displayLines
->Partitions();
77 return displayLines
->PositionFromPartition(lineDoc
);
81 int ContractionState::DisplayLastFromDoc(int lineDoc
) const {
82 return DisplayFromDoc(lineDoc
) + GetHeight(lineDoc
) - 1;
85 int ContractionState::DocFromDisplay(int lineDisplay
) const {
89 if (lineDisplay
<= 0) {
92 if (lineDisplay
> LinesDisplayed()) {
93 return displayLines
->PartitionFromPosition(LinesDisplayed());
95 int lineDoc
= displayLines
->PartitionFromPosition(lineDisplay
);
96 PLATFORM_ASSERT(GetVisible(lineDoc
));
101 void ContractionState::InsertLine(int lineDoc
) {
105 visible
->InsertSpace(lineDoc
, 1);
106 visible
->SetValueAt(lineDoc
, 1);
107 expanded
->InsertSpace(lineDoc
, 1);
108 expanded
->SetValueAt(lineDoc
, 1);
109 heights
->InsertSpace(lineDoc
, 1);
110 heights
->SetValueAt(lineDoc
, 1);
111 int lineDisplay
= DisplayFromDoc(lineDoc
);
112 displayLines
->InsertPartition(lineDoc
, lineDisplay
);
113 displayLines
->InsertText(lineDoc
, 1);
117 void ContractionState::InsertLines(int lineDoc
, int lineCount
) {
118 for (int l
= 0; l
< lineCount
; l
++) {
119 InsertLine(lineDoc
+ l
);
124 void ContractionState::DeleteLine(int lineDoc
) {
128 if (GetVisible(lineDoc
)) {
129 displayLines
->InsertText(lineDoc
, -heights
->ValueAt(lineDoc
));
131 displayLines
->RemovePartition(lineDoc
);
132 visible
->DeleteRange(lineDoc
, 1);
133 expanded
->DeleteRange(lineDoc
, 1);
134 heights
->DeleteRange(lineDoc
, 1);
138 void ContractionState::DeleteLines(int lineDoc
, int lineCount
) {
139 for (int l
= 0; l
< lineCount
; l
++) {
145 bool ContractionState::GetVisible(int lineDoc
) const {
149 if (lineDoc
>= visible
->Length())
151 return visible
->ValueAt(lineDoc
) == 1;
155 bool ContractionState::SetVisible(int lineDocStart
, int lineDocEnd
, bool isVisible
) {
156 if (OneToOne() && isVisible
) {
162 if ((lineDocStart
<= lineDocEnd
) && (lineDocStart
>= 0) && (lineDocEnd
< LinesInDoc())) {
163 for (int line
= lineDocStart
; line
<= lineDocEnd
; line
++) {
164 if (GetVisible(line
) != isVisible
) {
165 int difference
= isVisible
? heights
->ValueAt(line
) : -heights
->ValueAt(line
);
166 visible
->SetValueAt(line
, isVisible
? 1 : 0);
167 displayLines
->InsertText(line
, difference
);
179 bool ContractionState::HiddenLines() const {
183 return !visible
->AllSameAs(1);
187 bool ContractionState::GetExpanded(int lineDoc
) const {
192 return expanded
->ValueAt(lineDoc
) == 1;
196 bool ContractionState::SetExpanded(int lineDoc
, bool isExpanded
) {
197 if (OneToOne() && isExpanded
) {
201 if (isExpanded
!= (expanded
->ValueAt(lineDoc
) == 1)) {
202 expanded
->SetValueAt(lineDoc
, isExpanded
? 1 : 0);
212 int ContractionState::ContractedNext(int lineDocStart
) const {
217 if (!expanded
->ValueAt(lineDocStart
)) {
220 int lineDocNextChange
= expanded
->EndRun(lineDocStart
);
221 if (lineDocNextChange
< LinesInDoc())
222 return lineDocNextChange
;
229 int ContractionState::GetHeight(int lineDoc
) const {
233 return heights
->ValueAt(lineDoc
);
237 // Set the number of display lines needed for this line.
238 // Return true if this is a change.
239 bool ContractionState::SetHeight(int lineDoc
, int height
) {
240 if (OneToOne() && (height
== 1)) {
242 } else if (lineDoc
< LinesInDoc()) {
244 if (GetHeight(lineDoc
) != height
) {
245 if (GetVisible(lineDoc
)) {
246 displayLines
->InsertText(lineDoc
, height
- GetHeight(lineDoc
));
248 heights
->SetValueAt(lineDoc
, height
);
260 void ContractionState::ShowAll() {
261 int lines
= LinesInDoc();
263 linesInDocument
= lines
;
268 void ContractionState::Check() const {
269 #ifdef CHECK_CORRECTNESS
270 for (int vline
= 0; vline
< LinesDisplayed(); vline
++) {
271 const int lineDoc
= DocFromDisplay(vline
);
272 PLATFORM_ASSERT(GetVisible(lineDoc
));
274 for (int lineDoc
= 0; lineDoc
< LinesInDoc(); lineDoc
++) {
275 const int displayThis
= DisplayFromDoc(lineDoc
);
276 const int displayNext
= DisplayFromDoc(lineDoc
+ 1);
277 const int height
= displayNext
- displayThis
;
278 PLATFORM_ASSERT(height
>= 0);
279 if (GetVisible(lineDoc
)) {
280 PLATFORM_ASSERT(GetHeight(lineDoc
) == height
);
282 PLATFORM_ASSERT(0 == height
);