Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/custom/DefaultContent.d @ 113:fb3aa8075988
D2 support for the linux port.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Wed, 06 Apr 2011 21:57:23 +0200 |
parents | c01d033c633a |
children | 536e43f63c81 |
rev | line source |
---|---|
25 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.custom.DefaultContent; | |
14 | |
15 import org.eclipse.swt.SWT; | |
16 import org.eclipse.swt.SWTException; | |
17 import org.eclipse.swt.internal.Compatibility; | |
18 import org.eclipse.swt.widgets.TypedListener; | |
19 import org.eclipse.swt.custom.StyledTextContent; | |
20 import org.eclipse.swt.custom.TextChangeListener; | |
21 import org.eclipse.swt.custom.StyledTextEvent; | |
22 import org.eclipse.swt.custom.StyledTextListener; | |
23 import org.eclipse.swt.custom.StyledText; | |
24 import java.lang.all; | |
25 | |
48 | 26 version(Tango){ |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
27 static import tango.io.model.IFile; |
48 | 28 } else { // Phobos |
50 | 29 static import std.string; |
48 | 30 } |
25 | 31 |
32 | |
33 class DefaultContent : StyledTextContent { | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
34 version(Tango){ |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
35 private const static String LineDelimiter = tango.io.model.IFile.FileConst.NewlineString; |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
36 } else { // Phobos |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
37 private const static String LineDelimiter = std.string.newline; |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
38 } |
25 | 39 |
40 StyledTextListener[] textListeners; // stores text listeners for event sending | |
41 char[] textStore; // stores the actual text | |
42 int gapStart = -1; // the character position start of the gap | |
43 int gapEnd = -1; // the character position after the end of the gap | |
44 int gapLine = -1; // the line on which the gap exists, the gap will always be associated with one line | |
45 int highWatermark = 300; | |
46 int lowWatermark = 50; | |
47 | |
48 int[][] lines; // array of character positions and lengths representing the lines of text | |
49 int lineCount_ = 0; // the number of lines of text | |
50 int expandExp = 1; // the expansion exponent, used to increase the lines array exponentially | |
51 int replaceExpandExp = 1; // the expansion exponent, used to increase the lines array exponentially | |
52 | |
53 /** | |
54 * Creates a new DefaultContent and initializes it. A <code>StyledTextContent</> will always have | |
55 * at least one empty line. | |
56 */ | |
57 this() { | |
58 lines = new int[][]( 50, 2 ); | |
59 setText(""); | |
60 } | |
61 /** | |
62 * Adds a line to the end of the line indexes array. Increases the size of the array if necessary. | |
63 * <code>lineCount</code> is updated to reflect the new entry. | |
64 * <p> | |
65 * | |
66 * @param start the start of the line | |
67 * @param length the length of the line | |
68 */ | |
69 void addLineIndex(int start, int length) { | |
70 int size = lines.length; | |
71 if (lineCount_ is size) { | |
72 // expand the lines by powers of 2 | |
73 int[][] newLines = new int[][]( size+Compatibility.pow2(expandExp), 2 ); | |
74 System.arraycopy(lines, 0, newLines, 0, size); | |
75 lines = newLines; | |
76 expandExp++; | |
77 } | |
78 int[] range = [start, length]; | |
79 lines[lineCount_] = range; | |
80 lineCount_++; | |
81 } | |
82 /** | |
83 * Adds a line index to the end of <code>linesArray</code>. Increases the | |
84 * size of the array if necessary and returns a new array. | |
85 * <p> | |
86 * | |
87 * @param start the start of the line | |
88 * @param length the length of the line | |
89 * @param linesArray the array to which to add the line index | |
90 * @param count the position at which to add the line | |
91 * @return a new array of line indexes | |
92 */ | |
93 int[][] addLineIndex(int start, int length, int[][] linesArray, int count) { | |
94 int size = linesArray.length; | |
95 int[][] newLines = linesArray; | |
96 if (count is size) { | |
97 newLines = new int[][]( size+Compatibility.pow2(replaceExpandExp), 2 ); | |
98 replaceExpandExp++; | |
99 System.arraycopy(linesArray, 0, newLines, 0, size); | |
100 } | |
101 int[] range = [start, length]; | |
102 newLines[count] = range; | |
103 return newLines; | |
104 } | |
105 /** | |
106 * Adds a <code>TextChangeListener</code> listening for | |
107 * <code>TextChangingEvent</code> and <code>TextChangedEvent</code>. A | |
108 * <code>TextChangingEvent</code> is sent before changes to the text occur. | |
109 * A <code>TextChangedEvent</code> is sent after changes to the text | |
110 * occurred. | |
111 * <p> | |
112 * | |
113 * @param listener the listener | |
114 * @exception IllegalArgumentException <ul> | |
115 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
116 * </ul> | |
117 */ | |
118 public void addTextChangeListener(TextChangeListener listener) { | |
119 if (listener is null) error(SWT.ERROR_NULL_ARGUMENT); | |
120 StyledTextListener typedListener = new StyledTextListener(listener); | |
121 textListeners ~= typedListener; | |
122 } | |
123 /** | |
124 * Adjusts the gap to accommodate a text change that is occurring. | |
125 * <p> | |
126 * | |
127 * @param position the position at which a change is occurring | |
128 * @param sizeHint the size of the change | |
129 * @param line the line where the gap will go | |
130 */ | |
131 void adjustGap(int position, int sizeHint, int line) { | |
132 if (position is gapStart) { | |
133 // text is being inserted at the gap position | |
134 int size = (gapEnd - gapStart) - sizeHint; | |
135 if (lowWatermark <= size && size <= highWatermark) | |
136 return; | |
137 } else if ((position + sizeHint is gapStart) && (sizeHint < 0)) { | |
138 // text is being deleted at the gap position | |
139 int size = (gapEnd - gapStart) - sizeHint; | |
140 if (lowWatermark <= size && size <= highWatermark) | |
141 return; | |
142 } | |
143 moveAndResizeGap(position, sizeHint, line); | |
144 } | |
145 /** | |
146 * Calculates the indexes of each line in the text store. Assumes no gap exists. | |
147 * Optimized to do less checking. | |
148 */ | |
149 void indexLines(){ | |
150 int start = 0; | |
151 lineCount_ = 0; | |
152 int textLength = textStore.length; | |
153 int i; | |
154 for (i = start; i < textLength; i++) { | |
155 char ch = textStore[i]; | |
156 if (ch is SWT.CR) { | |
157 // see if the next character is a LF | |
158 if (i + 1 < textLength) { | |
159 ch = textStore[i+1]; | |
160 if (ch is SWT.LF) { | |
161 i++; | |
162 } | |
163 } | |
164 addLineIndex(start, i - start + 1); | |
165 start = i + 1; | |
166 } else if (ch is SWT.LF) { | |
167 addLineIndex(start, i - start + 1); | |
168 start = i + 1; | |
169 } | |
170 } | |
171 addLineIndex(start, i - start); | |
172 } | |
173 /** | |
174 * Returns whether or not the given character is a line delimiter. Both CR and LF | |
175 * are valid line delimiters. | |
176 * <p> | |
177 * | |
178 * @param ch the character to test | |
179 * @return true if ch is a delimiter, false otherwise | |
180 */ | |
181 bool isDelimiter(char ch) { | |
182 if (ch is SWT.CR) return true; | |
183 if (ch is SWT.LF) return true; | |
184 return false; | |
185 } | |
186 /** | |
187 * Determine whether or not the replace operation is valid. DefaultContent will not allow | |
188 * the /r/n line delimiter to be split or partially deleted. | |
189 * <p> | |
190 * | |
191 * @param start start offset of text to replace | |
192 * @param replaceLength start offset of text to replace | |
193 * @param newText start offset of text to replace | |
194 * @return a bool specifying whether or not the replace operation is valid | |
195 */ | |
196 protected bool isValidReplace(int start, int replaceLength, String newText){ | |
197 if (replaceLength is 0) { | |
198 // inserting text, see if the \r\n line delimiter is being split | |
199 if (start is 0) return true; | |
200 if (start is getCharCount()) return true; | |
201 char before = getTextRange(start - 1, 1)[0]; | |
202 if (before is '\r') { | |
203 char after = getTextRange(start, 1)[0]; | |
204 if (after is '\n') return false; | |
205 } | |
206 } else { | |
207 // deleting text, see if part of a \r\n line delimiter is being deleted | |
208 char startChar = getTextRange(start, 1)[0]; | |
209 if (startChar is '\n') { | |
210 // see if char before delete position is \r | |
211 if (start !is 0) { | |
212 char before = getTextRange(start - 1, 1)[0]; | |
213 if (before is '\r') return false; | |
214 } | |
215 } | |
216 char endChar = getTextRange(start + replaceLength - 1, 1)[0]; | |
217 if (endChar is '\r') { | |
218 // see if char after delete position is \n | |
219 if (start + replaceLength !is getCharCount()) { | |
220 char after = getTextRange(start + replaceLength, 1)[0]; | |
221 if (after is '\n') return false; | |
222 } | |
223 } | |
224 } | |
225 return true; | |
226 } | |
227 /** | |
228 * Calculates the indexes of each line of text in the given range. | |
229 * <p> | |
230 * | |
231 * @param offset the logical start offset of the text lineate | |
232 * @param length the length of the text to lineate, includes gap | |
233 * @param numLines the number of lines to initially allocate for the line index array, | |
234 * passed in for efficiency (the exact number of lines may be known) | |
235 * @return a line indexes array where each line is identified by a start offset and | |
236 * a length | |
237 */ | |
238 int[][] indexLines(int offset, int length, int numLines){ | |
239 int[][] indexedLines = new int[][]( numLines, 2 ); | |
240 int start = 0; | |
241 int lineCount_ = 0; | |
242 int i; | |
243 replaceExpandExp = 1; | |
244 for (i = start; i < length; i++) { | |
245 int location = i + offset; | |
246 if ((location >= gapStart) && (location < gapEnd)) { | |
247 // ignore the gap | |
248 } else { | |
249 char ch = textStore[location]; | |
250 if (ch is SWT.CR) { | |
251 // see if the next character is a LF | |
252 if (location+1 < textStore.length) { | |
253 ch = textStore[location+1]; | |
254 if (ch is SWT.LF) { | |
255 i++; | |
256 } | |
257 } | |
258 indexedLines = addLineIndex(start, i - start + 1, indexedLines, lineCount_); | |
259 lineCount_++; | |
260 start = i + 1; | |
261 } else if (ch is SWT.LF) { | |
262 indexedLines = addLineIndex(start, i - start + 1, indexedLines, lineCount_); | |
263 lineCount_++; | |
264 start = i + 1; | |
265 } | |
266 } | |
267 } | |
268 int[][] newLines = new int[][]( lineCount_+1, 2 ); | |
269 System.arraycopy(indexedLines, 0, newLines, 0, lineCount_); | |
270 int[] range = [start, i - start]; | |
271 newLines[lineCount_] = range; | |
272 return newLines; | |
273 } | |
274 /** | |
275 * Inserts text. | |
276 * <p> | |
277 * | |
278 * @param position the position at which to insert the text | |
279 * @param text the text to insert | |
280 */ | |
281 void insert(int position, String text) { | |
282 if (text.length is 0) return; | |
283 | |
284 int startLine = getLineAtOffset(position); | |
285 int change = text.length; | |
286 bool endInsert = position is getCharCount(); | |
287 adjustGap(position, change, startLine); | |
288 | |
289 // during an insert the gap will be adjusted to start at | |
290 // position and it will be associated with startline, the | |
291 // inserted text will be placed in the gap | |
292 int startLineOffset = getOffsetAtLine(startLine); | |
293 // at this point, startLineLength will include the start line | |
294 // and all of the newly inserted text | |
295 int startLineLength = getPhysicalLine(startLine).length; | |
296 | |
297 if (change > 0) { | |
298 // shrink gap | |
299 gapStart += (change); | |
300 for (int i = 0; i < text.length; i++) { | |
301 textStore[position + i]= text[i]; | |
302 } | |
303 } | |
304 | |
305 // figure out the number of new lines that have been inserted | |
306 int [][] newLines = indexLines(startLineOffset, startLineLength, 10); | |
307 // only insert an empty line if it is the last line in the text | |
308 int numNewLines = newLines.length - 1; | |
309 if (newLines[numNewLines][1] is 0) { | |
310 // last inserted line is a new line | |
311 if (endInsert) { | |
312 // insert happening at end of the text, leave numNewLines as | |
313 // is since the last new line will not be concatenated with another | |
314 // line | |
315 numNewLines += 1; | |
316 } else { | |
317 numNewLines -= 1; | |
318 } | |
319 } | |
320 | |
321 // make room for the new lines | |
322 expandLinesBy(numNewLines); | |
323 // shift down the lines after the replace line | |
324 for (int i = lineCount_ - 1; i > startLine; i--) { | |
325 lines[i + numNewLines]=lines[i]; | |
326 } | |
327 // insert the new lines | |
328 for (int i = 0; i < numNewLines; i++) { | |
329 newLines[i][0] += startLineOffset; | |
330 lines[startLine + i]=newLines[i]; | |
331 } | |
332 // update the last inserted line | |
333 if (numNewLines < newLines.length) { | |
334 newLines[numNewLines][0] += startLineOffset; | |
335 lines[startLine + numNewLines] = newLines[numNewLines]; | |
336 } | |
337 | |
338 lineCount_ += numNewLines; | |
339 gapLine = getLineAtPhysicalOffset(gapStart); | |
340 } | |
341 /** | |
342 * Moves the gap and adjusts its size in anticipation of a text change. | |
343 * The gap is resized to actual size + the specified size and moved to the given | |
344 * position. | |
345 * <p> | |
346 * | |
347 * @param position the position at which a change is occurring | |
348 * @param size the size of the change | |
349 * @param newGapLine the line where the gap should be put | |
350 */ | |
351 void moveAndResizeGap(int position, int size, int newGapLine) { | |
352 char[] content = null; | |
353 int oldSize = gapEnd - gapStart; | |
354 int newSize; | |
355 if (size > 0) { | |
356 newSize = highWatermark + size; | |
357 } else { | |
358 newSize = lowWatermark - size; | |
359 } | |
360 // remove the old gap from the lines information | |
361 if (gapExists()) { | |
362 // adjust the line length | |
363 lines[gapLine][1] = lines[gapLine][1] - oldSize; | |
364 // adjust the offsets of the lines after the gapLine | |
365 for (int i = gapLine + 1; i < lineCount_; i++) { | |
366 lines[i][0] = lines[i][0] - oldSize; | |
367 } | |
368 } | |
369 | |
370 if (newSize < 0) { | |
371 if (oldSize > 0) { | |
372 // removing the gap | |
373 content = new char[textStore.length - oldSize]; | |
374 System.arraycopy(textStore, 0, content, 0, gapStart); | |
375 System.arraycopy(textStore, gapEnd, content, gapStart, content.length - gapStart); | |
376 textStore = content; | |
377 } | |
378 gapStart = gapEnd = position; | |
379 return; | |
380 } | |
381 content = new char[textStore.length + (newSize - oldSize)]; | |
382 int newGapStart = position; | |
383 int newGapEnd = newGapStart + newSize; | |
384 if (oldSize is 0) { | |
385 System.arraycopy(textStore, 0, content, 0, newGapStart); | |
386 System.arraycopy(textStore, newGapStart, content, newGapEnd, content.length - newGapEnd); | |
387 } else if (newGapStart < gapStart) { | |
388 int delta = gapStart - newGapStart; | |
389 System.arraycopy(textStore, 0, content, 0, newGapStart); | |
390 System.arraycopy(textStore, newGapStart, content, newGapEnd, delta); | |
391 System.arraycopy(textStore, gapEnd, content, newGapEnd + delta, textStore.length - gapEnd); | |
392 } else { | |
393 int delta = newGapStart - gapStart; | |
394 System.arraycopy(textStore, 0, content, 0, gapStart); | |
395 System.arraycopy(textStore, gapEnd, content, gapStart, delta); | |
396 System.arraycopy(textStore, gapEnd + delta, content, newGapEnd, content.length - newGapEnd); | |
397 } | |
398 textStore = content; | |
399 gapStart = newGapStart; | |
400 gapEnd = newGapEnd; | |
401 | |
402 // add the new gap to the lines information | |
403 if (gapExists()) { | |
404 gapLine = newGapLine; | |
405 // adjust the line length | |
406 int gapLength = gapEnd - gapStart; | |
407 lines[gapLine][1] = lines[gapLine][1] + (gapLength); | |
408 // adjust the offsets of the lines after the gapLine | |
409 for (int i = gapLine + 1; i < lineCount_; i++) { | |
410 lines[i][0] = lines[i][0] + gapLength; | |
411 } | |
412 } | |
413 } | |
414 /** | |
415 * Returns the number of lines that are in the specified text. | |
416 * <p> | |
417 * | |
418 * @param startOffset the start of the text to lineate | |
419 * @param length the length of the text to lineate | |
420 * @return number of lines | |
421 */ | |
422 int lineCount(int startOffset, int length){ | |
423 if (length is 0) { | |
424 return 0; | |
425 } | |
426 int lineCount_ = 0; | |
427 int count = 0; | |
428 int i = startOffset; | |
429 if (i >= gapStart) { | |
430 i += gapEnd - gapStart; | |
431 } | |
432 while (count < length) { | |
433 if ((i >= gapStart) && (i < gapEnd)) { | |
434 // ignore the gap | |
435 } else { | |
436 char ch = textStore[i]; | |
437 if (ch is SWT.CR) { | |
438 // see if the next character is a LF | |
439 if (i + 1 < textStore.length) { | |
440 ch = textStore[i+1]; | |
441 if (ch is SWT.LF) { | |
442 i++; | |
443 count++; | |
444 } | |
445 } | |
446 lineCount_++; | |
447 } else if (ch is SWT.LF) { | |
448 lineCount_++; | |
449 } | |
450 count++; | |
451 } | |
452 i++; | |
453 } | |
454 return lineCount_; | |
455 } | |
456 /** | |
457 * Returns the number of lines that are in the specified text. | |
458 * <p> | |
459 * | |
460 * @param text the text to lineate | |
461 * @return number of lines in the text | |
462 */ | |
463 int lineCount(String text){ | |
464 int lineCount_ = 0; | |
465 int length = text.length; | |
466 for (int i = 0; i < length; i++) { | |
467 char ch = text[i]; | |
468 if (ch is SWT.CR) { | |
469 if (i + 1 < length && text[i + 1] is SWT.LF) { | |
470 i++; | |
471 } | |
472 lineCount_++; | |
473 } else if (ch is SWT.LF) { | |
474 lineCount_++; | |
475 } | |
476 } | |
477 return lineCount_; | |
478 } | |
479 /** | |
480 * @return the logical length of the text store | |
481 */ | |
482 public int getCharCount() { | |
483 int length = gapEnd - gapStart; | |
484 return (textStore.length - length); | |
485 } | |
486 /** | |
487 * Returns the line at <code>index</code> without delimiters. | |
488 * <p> | |
489 * | |
490 * @param index the index of the line to return | |
491 * @return the logical line text (i.e., without the gap) | |
492 * @exception IllegalArgumentException <ul> | |
493 * <li>ERROR_INVALID_ARGUMENT when index is out of range</li> | |
494 * </ul> | |
495 */ | |
496 public String getLine(int index) { | |
497 if ((index >= lineCount_) || (index < 0)) error(SWT.ERROR_INVALID_ARGUMENT); | |
498 int start = lines[index][0]; | |
499 int length_ = lines[index][1]; | |
500 int end = start + length_ - 1; | |
501 if (!gapExists() || (end < gapStart) || (start >= gapEnd)) { | |
502 // line is before or after the gap | |
503 while ((length_ - 1 >= 0) && isDelimiter(textStore[start+length_-1])) { | |
504 length_--; | |
505 } | |
51 | 506 return textStore[ start .. start + length_]._idup(); |
25 | 507 } else { |
508 // gap is in the specified range, strip out the gap | |
509 StringBuffer buf = new StringBuffer(); | |
510 int gapLength = gapEnd - gapStart; | |
511 buf.append(textStore[ start .. gapStart ] ); | |
512 buf.append(textStore[ gapEnd .. gapEnd + length_ - gapLength - (gapStart - start) ]); | |
513 length_ = buf.length; | |
514 while ((length_ - 1 >=0) && isDelimiter(buf.slice[length_ - 1])) { | |
515 length_--; | |
516 } | |
51 | 517 return buf.toString()[ 0 .. length_ ]._idup(); |
25 | 518 } |
519 } | |
520 /** | |
521 * Returns the line delimiter that should be used by the StyledText | |
522 * widget when inserting new lines. This delimiter may be different than the | |
523 * delimiter that is used by the <code>StyledTextContent</code> interface. | |
524 * <p> | |
525 * | |
526 * @return the platform line delimiter as specified in the line.separator | |
527 * system property. | |
528 */ | |
529 public String getLineDelimiter() { | |
530 return LineDelimiter; | |
531 } | |
532 /** | |
533 * Returns the line at the given index with delimiters. | |
534 * <p> | |
535 * @param index the index of the line to return | |
536 * @return the logical line text (i.e., without the gap) with delimiters | |
537 */ | |
538 String getFullLine(int index) { | |
539 int start = lines[index][0]; | |
540 int length_ = lines[index][1]; | |
541 int end = start + length_ - 1; | |
542 if (!gapExists() || (end < gapStart) || (start >= gapEnd)) { | |
543 // line is before or after the gap | |
51 | 544 return textStore[ start .. start + length_ ]._idup(); |
25 | 545 } else { |
546 // gap is in the specified range, strip out the gap | |
547 StringBuffer buffer = new StringBuffer(); | |
548 int gapLength = gapEnd - gapStart; | |
549 buffer.append(textStore[ start .. gapStart ]); | |
550 buffer.append(textStore[ gapEnd .. gapEnd + length_ - gapLength - (gapStart - start) ]); | |
51 | 551 return buffer.toString()._idup(); |
25 | 552 } |
553 } | |
554 /** | |
555 * Returns the physical line at the given index (i.e., with delimiters and the gap). | |
556 * <p> | |
557 * | |
558 * @param index the line index | |
559 * @return the physical line | |
560 */ | |
561 String getPhysicalLine(int index) { | |
562 int start = lines[index][0]; | |
563 int length_ = lines[index][1]; | |
564 return getPhysicalText(start, length_); | |
565 } | |
566 /** | |
567 * @return the number of lines in the text store | |
568 */ | |
569 public int getLineCount(){ | |
570 return lineCount_; | |
571 } | |
572 /** | |
573 * Returns the line at the given offset. | |
574 * <p> | |
575 * | |
576 * @param charPosition logical character offset (i.e., does not include gap) | |
577 * @return the line index | |
578 * @exception IllegalArgumentException <ul> | |
579 * <li>ERROR_INVALID_ARGUMENT when charPosition is out of range</li> | |
580 * </ul> | |
581 */ | |
582 public int getLineAtOffset(int charPosition){ | |
583 if ((charPosition > getCharCount()) || (charPosition < 0)) error(SWT.ERROR_INVALID_ARGUMENT); | |
584 int position; | |
585 if (charPosition < gapStart) { | |
586 // position is before the gap | |
587 position = charPosition; | |
588 } else { | |
589 // position includes the gap | |
590 position = charPosition + (gapEnd - gapStart); | |
591 } | |
592 | |
593 // if last line and the line is not empty you can ask for | |
594 // a position that doesn't exist (the one to the right of the | |
595 // last character) - for inserting | |
596 if (lineCount_ > 0) { | |
597 int lastLine = lineCount_ - 1; | |
598 if (position is lines[lastLine][0] + lines[lastLine][1]) | |
599 return lastLine; | |
600 } | |
601 | |
602 int high = lineCount_; | |
603 int low = -1; | |
604 int index = lineCount_; | |
605 while (high - low > 1) { | |
606 index = (high + low) / 2; | |
607 int lineStart = lines[index][0]; | |
608 int lineEnd = lineStart + lines[index][1] - 1; | |
609 if (position <= lineStart) { | |
610 high = index; | |
611 } else if (position <= lineEnd) { | |
612 high = index; | |
613 break; | |
614 } else { | |
615 low = index; | |
616 } | |
617 } | |
618 return high; | |
619 } | |
620 /** | |
621 * Returns the line index at the given physical offset. | |
622 * <p> | |
623 * | |
624 * @param position physical character offset (i.e., includes gap) | |
625 * @return the line index | |
626 */ | |
627 int getLineAtPhysicalOffset(int position){ | |
628 int high = lineCount_; | |
629 int low = -1; | |
630 int index = lineCount_; | |
631 while (high - low > 1) { | |
632 index = (high + low) / 2; | |
633 int lineStart = lines[index][0]; | |
634 int lineEnd = lineStart + lines[index][1] - 1; | |
635 if (position <= lineStart) { | |
636 high = index; | |
637 } else if (position <= lineEnd) { | |
638 high = index; | |
639 break; | |
640 } else { | |
641 low = index; | |
642 } | |
643 } | |
644 return high; | |
645 } | |
646 /** | |
647 * Returns the logical offset of the given line. | |
648 * <p> | |
649 * | |
650 * @param lineIndex index of line | |
651 * @return the logical starting offset of the line. When there are not any lines, | |
652 * getOffsetAtLine(0) is a valid call that should answer 0. | |
653 * @exception IllegalArgumentException <ul> | |
654 * <li>ERROR_INVALID_ARGUMENT when lineIndex is out of range</li> | |
655 * </ul> | |
656 */ | |
657 public int getOffsetAtLine(int lineIndex) { | |
658 if (lineIndex is 0) return 0; | |
659 if ((lineIndex >= lineCount_) || (lineIndex < 0)) error(SWT.ERROR_INVALID_ARGUMENT); | |
660 int start = lines[lineIndex][0]; | |
661 if (start > gapEnd) { | |
662 return start - (gapEnd - gapStart); | |
663 } else { | |
664 return start; | |
665 } | |
666 } | |
667 /** | |
668 * Increases the line indexes array to accommodate more lines. | |
669 * <p> | |
670 * | |
671 * @param numLines the number to increase the array by | |
672 */ | |
673 void expandLinesBy(int numLines) { | |
674 int size = lines.length; | |
675 if (size - lineCount_ >= numLines) { | |
676 return; | |
677 } | |
678 int[][] newLines = new int[][]( size+Math.max(10, numLines), 2 ); | |
679 System.arraycopy(lines, 0, newLines, 0, size); | |
680 lines = newLines; | |
681 } | |
682 /** | |
683 * Reports an SWT error. | |
684 * <p> | |
685 * | |
686 * @param code the error code | |
687 */ | |
688 void error (int code) { | |
689 SWT.error(code); | |
690 } | |
691 /** | |
692 * Returns whether or not a gap exists in the text store. | |
693 * <p> | |
694 * | |
695 * @return true if gap exists, false otherwise | |
696 */ | |
697 bool gapExists() { | |
698 return gapStart !is gapEnd; | |
699 } | |
700 /** | |
701 * Returns a string representing the continuous content of | |
702 * the text store. | |
703 * <p> | |
704 * | |
705 * @param start the physical start offset of the text to return | |
706 * @param length the physical length of the text to return | |
707 * @return the text | |
708 */ | |
709 String getPhysicalText(int start, int length_) { | |
51 | 710 return textStore[ start .. start + length_ ]._idup(); |
25 | 711 } |
712 /** | |
713 * Returns a string representing the logical content of | |
714 * the text store (i.e., gap stripped out). | |
715 * <p> | |
716 * | |
717 * @param start the logical start offset of the text to return | |
718 * @param length the logical length of the text to return | |
719 * @return the text | |
720 */ | |
721 public String getTextRange(int start, int length_) { | |
722 if (textStore is null) | |
723 return ""; | |
724 if (length_ is 0) | |
725 return ""; | |
726 int end= start + length_; | |
727 if (!gapExists() || (end < gapStart)) | |
51 | 728 return textStore[ start .. start + length_]._idup(); |
25 | 729 if (gapStart < start) { |
730 int gapLength= gapEnd - gapStart; | |
51 | 731 return textStore[ start + gapLength .. start + gapLength + length_ ]._idup(); |
25 | 732 } |
733 StringBuffer buf = new StringBuffer(); | |
734 buf.append(textStore[ start .. start + gapStart - start ] ); | |
735 buf.append(textStore[ gapEnd .. gapEnd + end - gapStart ] ); | |
51 | 736 return buf.toString()._idup(); |
25 | 737 } |
738 /** | |
739 * Removes the specified <code>TextChangeListener</code>. | |
740 * <p> | |
741 * | |
742 * @param listener the listener which should no longer be notified | |
743 * | |
744 * @exception IllegalArgumentException <ul> | |
745 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
746 * </ul> | |
747 */ | |
748 public void removeTextChangeListener(TextChangeListener listener){ | |
749 if (listener is null) error(SWT.ERROR_NULL_ARGUMENT); | |
750 for (int i = 0; i < textListeners.length; i++) { | |
751 TypedListener typedListener = cast(TypedListener) textListeners[i]; | |
752 if (typedListener.getEventListener () is listener) { | |
753 textListeners = textListeners[ 0 .. i ] ~ textListeners[ i+1 .. $ ]; | |
754 break; | |
755 } | |
756 } | |
757 } | |
758 /** | |
759 * Replaces the text with <code>newText</code> starting at position <code>start</code> | |
760 * for a length of <code>replaceLength</code>. Notifies the appropriate listeners. | |
761 * <p> | |
762 * | |
763 * When sending the TextChangingEvent, <code>newLineCount</code> is the number of | |
764 * lines that are going to be inserted and <code>replaceLineCount</code> is | |
765 * the number of lines that are going to be deleted, based on the change | |
766 * that occurs visually. For example: | |
767 * <ul> | |
768 * <li>(replaceText,newText) is> (replaceLineCount,newLineCount) | |
769 * <li>("","\n") is> (0,1) | |
770 * <li>("\n\n","a") is> (2,0) | |
771 * </ul> | |
772 * </p> | |
773 * | |
774 * @param start start offset of text to replace | |
775 * @param replaceLength start offset of text to replace | |
776 * @param newText start offset of text to replace | |
777 * | |
778 * @exception SWTException <ul> | |
779 * <li>ERROR_INVALID_ARGUMENT when the text change results in a multi byte | |
780 * line delimiter being split or partially deleted. Splitting a line | |
781 * delimiter by inserting text between the CR and LF characters of the | |
782 * \r\n delimiter or deleting part of this line delimiter is not supported</li> | |
783 * </ul> | |
784 */ | |
785 public void replaceTextRange(int start, int replaceLength, String newText){ | |
786 // check for invalid replace operations | |
787 if (!isValidReplace(start, replaceLength, newText)) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
788 | |
789 // inform listeners | |
790 StyledTextEvent event = new StyledTextEvent(this); | |
791 event.type = StyledText.TextChanging; | |
792 event.start = start; | |
793 event.replaceLineCount = lineCount(start, replaceLength); | |
794 event.text = newText; | |
795 event.newLineCount = lineCount(newText); | |
796 event.replaceCharCount = replaceLength; | |
797 event.newCharCount = newText.length; | |
798 sendTextEvent(event); | |
799 | |
800 // first delete the text to be replaced | |
801 delete_(start, replaceLength, event.replaceLineCount + 1); | |
802 // then insert the new text | |
803 insert(start, newText); | |
804 // inform listeners | |
805 event = new StyledTextEvent(this); | |
806 event.type = StyledText.TextChanged; | |
807 sendTextEvent(event); | |
808 } | |
809 /** | |
810 * Sends the text listeners the TextChanged event. | |
811 */ | |
812 void sendTextEvent(StyledTextEvent event) { | |
813 for (int i = 0; i < textListeners.length; i++) { | |
814 (cast(StyledTextListener)textListeners[i]).handleEvent(event); | |
815 } | |
816 } | |
817 /** | |
818 * Sets the content to text and removes the gap since there are no sensible predictions | |
819 * about where the next change will occur. | |
820 * <p> | |
821 * | |
822 * @param text the text | |
823 */ | |
824 public void setText (String text){ | |
825 textStore = text.dup; | |
826 gapStart = -1; | |
827 gapEnd = -1; | |
828 expandExp = 1; | |
829 indexLines(); | |
830 StyledTextEvent event = new StyledTextEvent(this); | |
831 event.type = StyledText.TextSet; | |
832 event.text = ""; | |
833 sendTextEvent(event); | |
834 } | |
835 /** | |
836 * Deletes text. | |
837 * <p> | |
838 * @param position the position at which the text to delete starts | |
839 * @param length the length of the text to delete | |
840 * @param numLines the number of lines that are being deleted | |
841 */ | |
842 void delete_(int position, int length_, int numLines) { | |
843 if (length_ is 0) return; | |
844 | |
845 int startLine = getLineAtOffset(position); | |
846 int startLineOffset = getOffsetAtLine(startLine); | |
847 int endLine = getLineAtOffset(position + length_); | |
848 | |
849 String endText = ""; | |
850 bool splittingDelimiter = false; | |
851 if (position + length_ < getCharCount()) { | |
852 endText = getTextRange(position + length_ - 1, 2); | |
853 if ((endText[0] is SWT.CR) && (endText[1] is SWT.LF)) { | |
854 splittingDelimiter = true; | |
855 } | |
856 } | |
857 | |
858 adjustGap(position + length_, -length_, startLine); | |
859 int [][] oldLines = indexLines(position, length_ + (gapEnd - gapStart), numLines); | |
860 | |
861 // enlarge the gap - the gap can be enlarged either to the | |
862 // right or left | |
863 if (position + length_ is gapStart) { | |
864 gapStart -= length_; | |
865 } else { | |
866 gapEnd += length_; | |
867 } | |
868 | |
869 // figure out the length of the new concatenated line, do so by | |
870 // finding the first line delimiter after position | |
871 int j = position; | |
872 bool eol = false; | |
873 while (j < textStore.length && !eol) { | |
874 if (j < gapStart || j >= gapEnd) { | |
875 char ch = textStore[j]; | |
876 if (isDelimiter(ch)) { | |
877 if (j + 1 < textStore.length) { | |
878 if (ch is SWT.CR && (textStore[j+1] is SWT.LF)) { | |
879 j++; | |
880 } | |
881 } | |
882 eol = true; | |
883 } | |
884 } | |
885 j++; | |
886 } | |
887 // update the line where the deletion started | |
888 lines[startLine][1] = (position - startLineOffset) + (j - position); | |
889 // figure out the number of lines that have been deleted | |
890 int numOldLines = oldLines.length - 1; | |
891 if (splittingDelimiter) numOldLines -= 1; | |
892 // shift up the lines after the last deleted line, no need to update | |
893 // the offset or length of the lines | |
894 for (int i = endLine + 1; i < lineCount_; i++) { | |
895 lines[i - numOldLines] = lines[i]; | |
896 } | |
897 lineCount_ -= numOldLines; | |
898 gapLine = getLineAtPhysicalOffset(gapStart); | |
899 } | |
900 | |
901 /++ | |
902 + SWT extension | |
903 +/ | |
904 int utf8AdjustOffset( int offset ){ | |
905 if (textStore is null) | |
906 return offset; | |
907 if (offset is 0) | |
908 return offset; | |
909 if( offset >= textStore.length ){ | |
910 return offset; | |
911 } | |
912 if (!gapExists() || (offset < gapStart)){ | |
113 | 913 while( (textStore[offset] & 0xC0) is 0x80 ){ |
25 | 914 offset--; |
915 } | |
916 return offset; | |
917 } | |
918 int gapLength= gapEnd - gapStart; | |
919 if( offset+gapLength >= textStore.length ){ | |
920 return offset; | |
921 } | |
113 | 922 while( (textStore[offset+gapLength] & 0xC0) is 0x80 ){ |
25 | 923 offset--; |
924 } | |
925 return offset; | |
926 } | |
927 | |
928 | |
929 } |