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