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