1
// Scintilla source code edit control
2
/** @file ContractionState.cxx
3
** Manages visibility of lines for folding.
5
// Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6
// The License.txt file describes the conditions under which this software may be distributed.
10
#include "ContractionState.h"
20
ContractionState::ContractionState() {
30
ContractionState::~ContractionState() {
34
void ContractionState::MakeValid() const {
36
// Could be cleverer by keeping the index of the last still valid entry
37
// rather than invalidating all.
39
for (int lineInDoc=0; lineInDoc<linesInDoc; lineInDoc++) {
40
lines[lineInDoc].displayLine = linesInDisplay;
41
if (lines[lineInDoc].visible) {
42
linesInDisplay += lines[lineInDoc].height;
45
if (sizeDocLines < linesInDisplay) {
47
int *docLinesNew = new int[linesInDisplay + growSize];
53
docLines = docLinesNew;
54
sizeDocLines = linesInDisplay + growSize;
58
for (int line=0; line<linesInDoc; line++) {
59
if (lines[line].visible) {
60
for (int linePiece=0; linePiece<lines[line].height; linePiece++) {
61
docLines[lineInDisplay] = line;
70
void ContractionState::Clear() {
81
int ContractionState::LinesInDoc() const {
85
int ContractionState::LinesDisplayed() const {
89
return linesInDisplay;
92
int ContractionState::DisplayFromDoc(int lineDoc) const {
97
if ((lineDoc >= 0) && (lineDoc < linesInDoc)) {
98
return lines[lineDoc].displayLine;
103
int ContractionState::DocFromDisplay(int lineDisplay) const {
104
if (lineDisplay <= 0)
106
if (lineDisplay >= linesInDisplay)
111
if (docLines) { // Valid allocation
112
return docLines[lineDisplay];
118
void ContractionState::Grow(int sizeNew) {
119
OneLine *linesNew = new OneLine[sizeNew];
122
for (; i < size; i++) {
123
linesNew[i] = lines[i];
125
for (; i < sizeNew; i++) {
126
linesNew[i].displayLine = i;
133
Platform::DebugPrintf("No memory available\n");
138
void ContractionState::InsertLines(int lineDoc, int lineCount) {
140
linesInDoc += lineCount;
141
linesInDisplay += lineCount;
144
//Platform::DebugPrintf("InsertLine[%d] = %d\n", lineDoc);
145
if ((linesInDoc + lineCount + 2) >= size) {
146
Grow(linesInDoc + lineCount + growSize);
148
linesInDoc += lineCount;
149
for (int i = linesInDoc; i >= lineDoc + lineCount; i--) {
150
lines[i].visible = lines[i - lineCount].visible;
151
lines[i].height = lines[i - lineCount].height;
152
linesInDisplay += lines[i].height;
153
lines[i].expanded = lines[i - lineCount].expanded;
155
for (int d=0;d<lineCount;d++) {
156
lines[lineDoc+d].visible = true; // Should inherit visibility from context ?
157
lines[lineDoc+d].height = 1;
158
lines[lineDoc+d].expanded = true;
163
void ContractionState::DeleteLines(int lineDoc, int lineCount) {
165
linesInDoc -= lineCount;
166
linesInDisplay -= lineCount;
169
int deltaDisplayed = 0;
170
for (int d=0;d<lineCount;d++) {
171
if (lines[lineDoc+d].visible)
172
deltaDisplayed -= lines[lineDoc+d].height;
174
for (int i = lineDoc; i < linesInDoc-lineCount; i++) {
175
if (i != 0) // Line zero is always visible
176
lines[i].visible = lines[i + lineCount].visible;
177
lines[i].expanded = lines[i + lineCount].expanded;
178
lines[i].height = lines[i + lineCount].height;
180
linesInDoc -= lineCount;
181
linesInDisplay += deltaDisplayed;
185
bool ContractionState::GetVisible(int lineDoc) const {
188
if ((lineDoc >= 0) && (lineDoc < linesInDoc)) {
189
return lines[lineDoc].visible;
195
bool ContractionState::SetVisible(int lineDocStart, int lineDocEnd, bool visible) {
196
if (lineDocStart == 0)
198
if (lineDocStart > lineDocEnd)
201
Grow(linesInDoc + growSize);
203
// TODO: modify docLine members to mirror displayLine
206
if ((lineDocStart <= lineDocEnd) && (lineDocStart >= 0) && (lineDocEnd < linesInDoc)) {
207
for (int line=lineDocStart; line <= lineDocEnd; line++) {
208
if (lines[line].visible != visible) {
209
delta += visible ? lines[line].height : -lines[line].height;
210
lines[line].visible = visible;
214
linesInDisplay += delta;
219
bool ContractionState::GetExpanded(int lineDoc) const {
222
if ((lineDoc >= 0) && (lineDoc < linesInDoc)) {
223
return lines[lineDoc].expanded;
229
bool ContractionState::SetExpanded(int lineDoc, bool expanded) {
232
// If in completely expanded state then setting
233
// one line to expanded has no effect.
236
Grow(linesInDoc + growSize);
238
if ((lineDoc >= 0) && (lineDoc < linesInDoc)) {
239
if (lines[lineDoc].expanded != expanded) {
240
lines[lineDoc].expanded = expanded;
247
int ContractionState::GetHeight(int lineDoc) const {
250
if ((lineDoc >= 0) && (lineDoc < linesInDoc)) {
251
return lines[lineDoc].height;
257
// Set the number of display lines needed for this line.
258
// Return true if this is a change.
259
bool ContractionState::SetHeight(int lineDoc, int height) {
260
if (lineDoc > linesInDoc)
264
// If in completely expanded state then all lines
265
// assumed to have height of one so no effect here.
268
Grow(linesInDoc + growSize);
270
if (lines[lineDoc].height != height) {
271
lines[lineDoc].height = height;
279
void ContractionState::ShowAll() {