]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/ContractionState.cxx
816d06aae708f19c45eb88153873701397b7515c
[wxWidgets.git] / src / stc / scintilla / src / ContractionState.cxx
1 // Scintilla source code edit control
2 // ContractionState.cxx - manages visibility of lines for folding
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 #include "Platform.h"
7
8 #include "ContractionState.h"
9
10 OneLine::OneLine() {
11 displayLine = 0;
12 docLine = 0;
13 visible = true;
14 expanded = true;
15 }
16
17 ContractionState::ContractionState() {
18 lines = 0;
19 size = 0;
20 linesInDoc = 1;
21 linesInDisplay = 1;
22 valid = false;
23 }
24
25 ContractionState::~ContractionState() {
26 Clear();
27 }
28
29 void ContractionState::MakeValid() const {
30 if (!valid) {
31 // Could be cleverer by keeping the index of the last still valid entry
32 // rather than invalidating all.
33 int linePrev = -1;
34 int lineDisplay = 0;
35 for (int line=0; line<linesInDoc; line++) {
36 lines[line].displayLine = lineDisplay;
37 if (lines[line].visible) {
38 lines[lineDisplay].docLine = line;
39 lineDisplay++;
40 }
41 }
42 valid = true;
43 }
44 }
45
46 void ContractionState::Clear() {
47 delete []lines;
48 lines = 0;
49 size = 0;
50 linesInDoc = 1;
51 linesInDisplay = 1;
52 }
53
54 int ContractionState::LinesInDoc() const {
55 return linesInDoc;
56 }
57
58 int ContractionState::LinesDisplayed() const {
59 return linesInDisplay;
60 }
61
62 int ContractionState::DisplayFromDoc(int lineDoc) const {
63 if (size == 0) {
64 return lineDoc;
65 }
66 MakeValid();
67 if ((lineDoc >= 0) && (lineDoc < linesInDoc)) {
68 return lines[lineDoc].displayLine;
69 }
70 return -1;
71 }
72
73 int ContractionState::DocFromDisplay(int lineDisplay) const {
74 if (lineDisplay <= 0)
75 return 0;
76 if (lineDisplay >= linesInDisplay)
77 return linesInDoc-1;
78 if (size == 0)
79 return lineDisplay;
80 MakeValid();
81 return lines[lineDisplay].docLine;
82 }
83
84 void ContractionState::Grow(int sizeNew) {
85 OneLine *linesNew = new OneLine[sizeNew];
86 if (linesNew) {
87 int i = 0;
88 for (; i < size; i++) {
89 linesNew[i] = lines[i];
90 }
91 for (; i < sizeNew; i++) {
92 linesNew[i].displayLine = i;
93 }
94 delete []lines;
95 lines = linesNew;
96 size = sizeNew;
97 valid = false;
98 } else {
99 Platform::DebugPrintf("No memory available\n");
100 // TODO: Blow up
101 }
102 }
103
104 void ContractionState::InsertLines(int lineDoc, int lineCount) {
105 if (size == 0) {
106 linesInDoc += lineCount;
107 linesInDisplay += lineCount;
108 return;
109 }
110 //Platform::DebugPrintf("InsertLine[%d] = %d\n", lineDoc);
111 if ((linesInDoc + lineCount + 2) >= size) {
112 Grow(linesInDoc + lineCount + growSize);
113 }
114 linesInDoc += lineCount;
115 linesInDisplay += lineCount;
116 for (int i = linesInDoc + 1; i >= lineDoc + lineCount; i--) {
117 lines[i].visible = lines[i - lineCount].visible;
118 lines[i].expanded = lines[i - lineCount].expanded;
119 }
120 for (int d=0;d<lineCount;d++) {
121 lines[lineDoc+d].visible = true; // Should inherit visibility from context ?
122 lines[lineDoc+d].expanded = true;
123 }
124 valid = false;
125 }
126
127 void ContractionState::DeleteLines(int lineDoc, int lineCount) {
128 if (size == 0) {
129 linesInDoc -= lineCount;
130 linesInDisplay -= lineCount;
131 return;
132 }
133 int delta = 0;
134 for (int d=0;d<lineCount;d++)
135 if (lines[lineDoc+d].visible)
136 delta--;
137 for (int i = lineDoc; i < linesInDoc-lineCount; i++) {
138 lines[i].visible = lines[i + lineCount].visible;
139 lines[i].expanded = lines[i + lineCount].expanded;
140 }
141 linesInDoc -= lineCount;
142 linesInDisplay += delta;
143 valid = false;
144 }
145
146 bool ContractionState::GetVisible(int lineDoc) const {
147 if (size == 0)
148 return true;
149 if ((lineDoc >= 0) && (lineDoc < linesInDoc)) {
150 return lines[lineDoc].visible;
151 } else {
152 return false;
153 }
154 }
155
156 bool ContractionState::SetVisible(int lineDocStart, int lineDocEnd, bool visible) {
157 if (size == 0) {
158 Grow(linesInDoc + growSize);
159 }
160 // TODO: modify docLine members to mirror displayLine
161 int delta = 0;
162 // Change lineDocs
163 if ((lineDocStart <= lineDocEnd) && (lineDocStart >= 0) && (lineDocEnd < linesInDoc)) {
164 for (int line=lineDocStart; line <= lineDocEnd; line++) {
165 if (lines[line].visible != visible) {
166 delta += visible ? 1 : -1;
167 lines[line].visible = visible;
168 }
169 lines[line].displayLine += delta;
170 }
171 if (delta != 0) {
172 for (int line=lineDocEnd+1; line <= linesInDoc; line++) {
173 lines[line].displayLine += delta;
174 }
175 }
176 }
177 linesInDisplay += delta;
178 valid = false;
179 return delta != 0;
180 }
181
182 bool ContractionState::GetExpanded(int lineDoc) const {
183 if (size == 0)
184 return true;
185 if ((lineDoc >= 0) && (lineDoc < linesInDoc)) {
186 return lines[lineDoc].expanded;
187 } else {
188 return false;
189 }
190 }
191
192 bool ContractionState::SetExpanded(int lineDoc, bool expanded) {
193 if (size == 0) {
194 Grow(linesInDoc + growSize);
195 }
196 if ((lineDoc >= 0) && (lineDoc < linesInDoc)) {
197 if (lines[lineDoc].expanded != expanded) {
198 lines[lineDoc].expanded = expanded;
199 return true;
200 }
201 }
202 return false;
203 }