comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/custom/DefaultContent.d @ 0:6dd524f61e62

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