Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/custom/StyledText.d @ 51:c01d033c633a
[swt lin]
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 27 Mar 2009 19:58:06 +0100 |
parents | 7a2dd761a8b2 |
children | fb3aa8075988 |
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.StyledText; | |
14 | |
15 | |
16 import org.eclipse.swt.SWT; | |
17 import org.eclipse.swt.SWTError; | |
18 import org.eclipse.swt.SWTException; | |
19 import org.eclipse.swt.accessibility.ACC; | |
20 import org.eclipse.swt.accessibility.Accessible; | |
21 import org.eclipse.swt.accessibility.AccessibleAdapter; | |
22 import org.eclipse.swt.accessibility.AccessibleControlAdapter; | |
23 import org.eclipse.swt.accessibility.AccessibleControlEvent; | |
24 import org.eclipse.swt.accessibility.AccessibleEvent; | |
25 import org.eclipse.swt.accessibility.AccessibleTextAdapter; | |
26 import org.eclipse.swt.accessibility.AccessibleTextEvent; | |
27 import org.eclipse.swt.dnd.Clipboard; | |
28 import org.eclipse.swt.dnd.DND; | |
29 import org.eclipse.swt.dnd.RTFTransfer; | |
30 import org.eclipse.swt.dnd.TextTransfer; | |
31 import org.eclipse.swt.dnd.Transfer; | |
32 import org.eclipse.swt.events.ModifyListener; | |
33 import org.eclipse.swt.events.SelectionEvent; | |
34 import org.eclipse.swt.events.SelectionListener; | |
35 import org.eclipse.swt.events.VerifyListener; | |
36 import org.eclipse.swt.graphics.Color; | |
37 import org.eclipse.swt.graphics.Cursor; | |
38 import org.eclipse.swt.graphics.Font; | |
39 import org.eclipse.swt.graphics.FontData; | |
40 import org.eclipse.swt.graphics.FontMetrics; | |
41 import org.eclipse.swt.graphics.GC; | |
42 import org.eclipse.swt.graphics.GlyphMetrics; | |
43 import org.eclipse.swt.graphics.Image; | |
44 import org.eclipse.swt.graphics.Device; | |
45 import org.eclipse.swt.graphics.Point; | |
46 import org.eclipse.swt.graphics.Rectangle; | |
47 import org.eclipse.swt.graphics.Resource; | |
48 import org.eclipse.swt.graphics.TextLayout; | |
49 import org.eclipse.swt.internal.BidiUtil; | |
50 import org.eclipse.swt.internal.Compatibility; | |
51 import org.eclipse.swt.printing.Printer; | |
52 import org.eclipse.swt.printing.PrinterData; | |
53 import org.eclipse.swt.widgets.Canvas; | |
54 import org.eclipse.swt.widgets.Caret; | |
55 import org.eclipse.swt.widgets.Composite; | |
56 import org.eclipse.swt.widgets.Control; | |
57 import org.eclipse.swt.widgets.Display; | |
58 import org.eclipse.swt.widgets.Event; | |
59 import org.eclipse.swt.widgets.IME; | |
60 import org.eclipse.swt.widgets.Label; | |
61 import org.eclipse.swt.widgets.Listener; | |
62 import org.eclipse.swt.widgets.ScrollBar; | |
63 import org.eclipse.swt.widgets.TypedListener; | |
64 import org.eclipse.swt.custom.StyledTextContent; | |
65 import org.eclipse.swt.custom.TextChangeListener; | |
66 import org.eclipse.swt.custom.StyledTextRenderer; | |
67 import org.eclipse.swt.custom.StyledTextPrintOptions; | |
68 import org.eclipse.swt.custom.ExtendedModifyListener; | |
69 import org.eclipse.swt.custom.BidiSegmentListener; | |
70 import org.eclipse.swt.custom.LineBackgroundListener; | |
71 import org.eclipse.swt.custom.LineStyleListener; | |
72 import org.eclipse.swt.custom.PaintObjectListener; | |
73 import org.eclipse.swt.custom.VerifyKeyListener; | |
74 import org.eclipse.swt.custom.MovementListener; | |
75 import org.eclipse.swt.custom.Bullet; | |
76 import org.eclipse.swt.custom.StyledTextEvent; | |
77 import org.eclipse.swt.custom.StyleRange; | |
78 import org.eclipse.swt.custom.TextChangedEvent; | |
79 import org.eclipse.swt.custom.TextChangingEvent; | |
80 import org.eclipse.swt.custom.DefaultContent; | |
81 import org.eclipse.swt.custom.StyledTextDropTargetEffect; | |
82 import org.eclipse.swt.custom.StyledTextListener; | |
83 import org.eclipse.swt.custom.ST; | |
84 | |
48 | 85 version(Tango){ |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
86 static import tango.io.model.IFile; |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
87 import tango.util.Convert; |
48 | 88 } else { // Phobos |
51 | 89 import std.conv; |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
90 static import std.string; |
48 | 91 } |
25 | 92 import java.lang.all; |
93 | |
94 | |
95 /** | |
96 * A StyledText is an editable user interface object that displays lines | |
97 * of text. The following style attributes can be defined for the text: | |
98 * <ul> | |
99 * <li>foreground color | |
100 * <li>background color | |
101 * <li>font style (bold, italic, bold-italic, regular) | |
102 * <li>underline | |
103 * <li>strikeout | |
104 * </ul> | |
105 * <p> | |
106 * In addition to text style attributes, the background color of a line may | |
107 * be specified. | |
108 * </p><p> | |
109 * There are two ways to use this widget when specifying text style information. | |
110 * You may use the API that is defined for StyledText or you may define your own | |
111 * LineStyleListener. If you define your own listener, you will be responsible | |
112 * for maintaining the text style information for the widget. IMPORTANT: You may | |
113 * not define your own listener and use the StyledText API. The following | |
114 * StyledText API is not supported if you have defined a LineStyleListener: | |
115 * <ul> | |
116 * <li>getStyleRangeAtOffset(int) | |
117 * <li>getStyleRanges() | |
118 * <li>replaceStyleRanges(int,int,StyleRange[]) | |
119 * <li>setStyleRange(StyleRange) | |
120 * <li>setStyleRanges(StyleRange[]) | |
121 * </ul> | |
122 * </p><p> | |
123 * There are two ways to use this widget when specifying line background colors. | |
124 * You may use the API that is defined for StyledText or you may define your own | |
125 * LineBackgroundListener. If you define your own listener, you will be responsible | |
126 * for maintaining the line background color information for the widget. | |
127 * IMPORTANT: You may not define your own listener and use the StyledText API. | |
128 * The following StyledText API is not supported if you have defined a | |
129 * LineBackgroundListener: | |
130 * <ul> | |
131 * <li>getLineBackground(int) | |
132 * <li>setLineBackground(int,int,Color) | |
133 * </ul> | |
134 * </p><p> | |
135 * The content implementation for this widget may also be user-defined. To do so, | |
136 * you must implement the StyledTextContent interface and use the StyledText API | |
137 * setContent(StyledTextContent) to initialize the widget. | |
138 * </p><p> | |
139 * <dl> | |
140 * <dt><b>Styles:</b><dd>FULL_SELECTION, MULTI, READ_ONLY, SINGLE, WRAP | |
141 * <dt><b>Events:</b><dd>ExtendedModify, LineGetBackground, LineGetSegments, LineGetStyle, Modify, Selection, Verify, VerifyKey | |
142 * </dl> | |
143 * </p><p> | |
144 * IMPORTANT: This class is <em>not</em> intended to be subclassed. | |
145 * </p> | |
146 * | |
147 * @see <a href="http://www.eclipse.org/swt/snippets/#styledtext">StyledText snippets</a> | |
148 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Examples: CustomControlExample, TextEditor</a> | |
149 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
150 */ | |
151 public class StyledText : Canvas { | |
152 alias Canvas.computeSize computeSize; | |
153 | |
154 static const char TAB = '\t'; | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
155 version(Tango){ |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
156 static const String PlatformLineDelimiter = tango.io.model.IFile.FileConst.NewlineString; |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
157 } else { // Phobos |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
158 static const String PlatformLineDelimiter = std.string.newline; |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
159 } |
25 | 160 static const int BIDI_CARET_WIDTH = 3; |
161 static const int DEFAULT_WIDTH = 64; | |
162 static const int DEFAULT_HEIGHT = 64; | |
163 static const int V_SCROLL_RATE = 50; | |
164 static const int H_SCROLL_RATE = 10; | |
165 | |
166 static const int ExtendedModify = 3000; | |
167 static const int LineGetBackground = 3001; | |
168 static const int LineGetStyle = 3002; | |
169 static const int TextChanging = 3003; | |
170 static const int TextSet = 3004; | |
171 static const int VerifyKey = 3005; | |
172 static const int TextChanged = 3006; | |
173 static const int LineGetSegments = 3007; | |
174 static const int PaintObject = 3008; | |
175 static const int WordNext = 3009; | |
176 static const int WordPrevious = 3010; | |
177 | |
178 static const int PREVIOUS_OFFSET_TRAILING = 0; | |
179 static const int OFFSET_LEADING = 1; | |
180 | |
181 Color selectionBackground; // selection background color | |
182 Color selectionForeground; // selection foreground color | |
183 StyledTextContent content; // native content (default or user specified) | |
184 StyledTextRenderer renderer; | |
185 Listener listener; | |
186 TextChangeListener textChangeListener; // listener for TextChanging, TextChanged and TextSet events from StyledTextContent | |
187 int verticalScrollOffset = 0; // pixel based | |
188 int horizontalScrollOffset = 0; // pixel based | |
189 int topIndex = 0; // top visible line | |
190 int topIndexY; | |
191 int clientAreaHeight = 0; // the client area height. Needed to calculate content width for new visible lines during Resize callback | |
192 int clientAreaWidth = 0; // the client area width. Needed during Resize callback to determine if line wrap needs to be recalculated | |
193 int tabLength = 4; // number of characters in a tab | |
194 int leftMargin; | |
195 int topMargin; | |
196 int rightMargin; | |
197 int bottomMargin; | |
198 int columnX; // keep track of the horizontal caret position when changing lines/pages. Fixes bug 5935 | |
199 int caretOffset = 0; | |
200 int caretAlignment; | |
201 Point selection; // x and y are start and end caret offsets of selection | |
202 Point clipboardSelection; // x and y are start and end caret offsets of previous selection | |
203 int selectionAnchor; // position of selection anchor. 0 based offset from beginning of text | |
204 Point doubleClickSelection; // selection after last mouse double click | |
205 bool editable = true; | |
206 bool wordWrap = false; | |
207 bool doubleClickEnabled = true; // see getDoubleClickEnabled | |
208 bool overwrite = false; // insert/overwrite edit mode | |
209 int textLimit = -1; // limits the number of characters the user can type in the widget. Unlimited by default. | |
210 int[int] keyActionMap; | |
211 Color background = null; // workaround for bug 4791 | |
212 Color foreground = null; // | |
213 Clipboard clipboard; | |
214 int clickCount; | |
215 int autoScrollDirection = SWT.NULL; // the direction of autoscrolling (up, down, right, left) | |
216 int autoScrollDistance = 0; | |
217 int lastTextChangeStart; // cache data of the | |
218 int lastTextChangeNewLineCount; // last text changing | |
219 int lastTextChangeNewCharCount; // event for use in the | |
220 int lastTextChangeReplaceLineCount; // text changed handler | |
221 int lastTextChangeReplaceCharCount; | |
222 int lastLineBottom; // the bottom pixel of the last line been replaced | |
223 bool isMirrored_; | |
224 bool bidiColoring = false; // apply the BIDI algorithm on text segments of the same color | |
225 Image leftCaretBitmap = null; | |
226 Image rightCaretBitmap = null; | |
227 int caretDirection = SWT.NULL; | |
228 int caretWidth = 0; | |
229 Caret defaultCaret = null; | |
230 bool updateCaretDirection = true; | |
231 bool fixedLineHeight; | |
232 bool dragDetect_ = true; | |
233 IME ime; | |
234 | |
235 int alignment; | |
236 bool justify; | |
237 int indent; | |
238 int lineSpacing; | |
239 | |
240 const static bool IS_CARBON, IS_GTK, IS_MOTIF; | |
241 static this(){ | |
242 String platform = SWT.getPlatform(); | |
243 IS_CARBON = ("carbon" == platform); | |
244 IS_GTK = ("gtk" == platform); | |
245 IS_MOTIF = ("motif" == platform); | |
246 } | |
247 | |
248 /** | |
249 * The Printing class : printing of a range of text. | |
250 * An instance of <code>Printing</code> is returned in the | |
251 * StyledText#print(Printer) API. The run() method may be | |
252 * invoked from any thread. | |
253 */ | |
254 static class Printing : Runnable { | |
255 const static int LEFT = 0; // left aligned header/footer segment | |
256 const static int CENTER = 1; // centered header/footer segment | |
257 const static int RIGHT = 2; // right aligned header/footer segment | |
258 | |
259 Printer printer; | |
260 StyledTextRenderer printerRenderer; | |
261 StyledTextPrintOptions printOptions; | |
262 Rectangle clientArea; | |
263 FontData fontData; | |
264 Font printerFont; | |
265 Resource[Resource] resources; | |
266 int tabLength; | |
267 GC gc; // printer GC | |
268 int pageWidth; // width of a printer page in pixels | |
269 int startPage; // first page to print | |
270 int endPage; // last page to print | |
271 int startLine; // first (wrapped) line to print | |
272 int endLine; // last (wrapped) line to print | |
273 bool singleLine; // widget single line mode | |
274 Point selection = null; // selected text | |
275 bool mirrored; // indicates the printing gc should be mirrored | |
276 int lineSpacing; | |
277 int printMargin; | |
278 | |
279 /** | |
280 * Creates an instance of <code>Printing</code>. | |
281 * Copies the widget content and rendering data that needs | |
282 * to be requested from listeners. | |
283 * </p> | |
284 * @param parent StyledText widget to print. | |
285 * @param printer printer device to print on. | |
286 * @param printOptions print options | |
287 */ | |
288 this(StyledText styledText, Printer printer, StyledTextPrintOptions printOptions) { | |
289 this.printer = printer; | |
290 this.printOptions = printOptions; | |
291 this.mirrored = (styledText.getStyle() & SWT.MIRRORED) !is 0; | |
292 singleLine = styledText.isSingleLine(); | |
293 startPage = 1; | |
294 endPage = int.max; | |
295 PrinterData data = printer.getPrinterData(); | |
296 if (data.scope_ is PrinterData.PAGE_RANGE) { | |
297 startPage = data.startPage; | |
298 endPage = data.endPage; | |
299 if (endPage < startPage) { | |
300 int temp = endPage; | |
301 endPage = startPage; | |
302 startPage = temp; | |
303 } | |
304 } else if (data.scope_ is PrinterData.SELECTION) { | |
305 selection = styledText.getSelectionRange(); | |
306 } | |
307 printerRenderer = new StyledTextRenderer(printer, null); | |
308 printerRenderer.setContent(copyContent(styledText.getContent())); | |
309 cacheLineData(styledText); | |
310 } | |
311 /** | |
312 * Caches all line data that needs to be requested from a listener. | |
313 * </p> | |
314 * @param printerContent <code>StyledTextContent</code> to request | |
315 * line data for. | |
316 */ | |
317 void cacheLineData(StyledText styledText) { | |
318 StyledTextRenderer renderer = styledText.renderer; | |
319 renderer.copyInto(printerRenderer); | |
320 fontData = styledText.getFont().getFontData()[0]; | |
321 tabLength = styledText.tabLength; | |
322 int lineCount = printerRenderer.lineCount; | |
323 if (styledText.isListening(LineGetBackground) || (styledText.isBidi() && styledText.isListening(LineGetSegments)) || styledText.isListening(LineGetStyle)) { | |
324 StyledTextContent content = printerRenderer.content; | |
325 for (int i = 0; i < lineCount; i++) { | |
326 String line = content.getLine(i); | |
327 int lineOffset = content.getOffsetAtLine(i); | |
328 StyledTextEvent event = styledText.getLineBackgroundData(lineOffset, line); | |
329 if (event !is null && event.lineBackground !is null) { | |
330 printerRenderer.setLineBackground(i, 1, event.lineBackground); | |
331 } | |
332 if (styledText.isBidi()) { | |
333 int[] segments = styledText.getBidiSegments(lineOffset, line); | |
334 printerRenderer.setLineSegments(i, 1, segments); | |
335 } | |
336 event = styledText.getLineStyleData(lineOffset, line); | |
337 if (event !is null) { | |
338 printerRenderer.setLineIndent(i, 1, event.indent); | |
339 printerRenderer.setLineAlignment(i, 1, event.alignment); | |
340 printerRenderer.setLineJustify(i, 1, event.justify); | |
341 printerRenderer.setLineBullet(i, 1, event.bullet); | |
342 StyleRange[] styles = event.styles; | |
343 if (styles !is null && styles.length > 0) { | |
344 printerRenderer.setStyleRanges(event.ranges, styles); | |
345 } | |
346 } | |
347 } | |
348 } | |
349 Point screenDPI = styledText.getDisplay().getDPI(); | |
350 Point printerDPI = printer.getDPI(); | |
351 resources = null; | |
352 for (int i = 0; i < lineCount; i++) { | |
353 Color color = printerRenderer.getLineBackground(i, null); | |
354 if (color !is null) { | |
355 if (printOptions.printLineBackground) { | |
356 Color printerColor; | |
357 if ( auto p = color in resources ) { | |
358 printerColor = cast(Color)*p; | |
359 } | |
360 else { | |
361 printerColor = new Color (printer, color.getRGB()); | |
362 resources[color]=printerColor; | |
363 } | |
364 printerRenderer.setLineBackground(i, 1, printerColor); | |
365 } else { | |
366 printerRenderer.setLineBackground(i, 1, null); | |
367 } | |
368 } | |
369 int indent = printerRenderer.getLineIndent(i, 0); | |
370 if (indent !is 0) { | |
371 printerRenderer.setLineIndent(i, 1, indent * printerDPI.x / screenDPI.x); | |
372 } | |
373 } | |
374 StyleRange[] styles = printerRenderer.styles; | |
375 for (int i = 0; i < printerRenderer.styleCount; i++) { | |
376 StyleRange style = styles[i]; | |
377 Font font = style.font; | |
378 if (style.font !is null) { | |
379 Font printerFont; | |
380 if ( auto p = font in resources ) { | |
381 printerFont = cast(Font)*p; | |
382 } | |
383 else { | |
384 printerFont = new Font (printer, font.getFontData()); | |
385 resources[font]= printerFont; | |
386 } | |
387 style.font = printerFont; | |
388 } | |
389 Color color = style.foreground; | |
390 if (color !is null) { | |
391 if (printOptions.printTextForeground) { | |
392 Color printerColor; | |
393 if ( auto p = color in resources ) { | |
394 printerColor = cast(Color)*p; | |
395 } | |
396 else { | |
397 printerColor = new Color (printer, color.getRGB()); | |
398 resources[color]=printerColor; | |
399 } | |
400 style.foreground = printerColor; | |
401 } else { | |
402 style.foreground = null; | |
403 } | |
404 } | |
405 color = style.background; | |
406 if (color !is null) { | |
407 if (printOptions.printTextBackground) { | |
408 Color printerColor; | |
409 if ( auto p = color in resources ) { | |
410 printerColor = cast(Color)*p; | |
411 } | |
412 else { | |
413 printerColor = new Color (printer, color.getRGB()); | |
414 resources[color]=printerColor; | |
415 } | |
416 style.background = printerColor; | |
417 } else { | |
418 style.background = null; | |
419 } | |
420 } | |
421 if (!printOptions.printTextFontStyle) { | |
422 style.fontStyle = SWT.NORMAL; | |
423 } | |
424 style.rise = style.rise * printerDPI.y / screenDPI.y; | |
425 GlyphMetrics metrics = style.metrics; | |
426 if (metrics !is null) { | |
427 metrics.ascent = metrics.ascent * printerDPI.y / screenDPI.y; | |
428 metrics.descent = metrics.descent * printerDPI.y / screenDPI.y; | |
429 metrics.width = metrics.width * printerDPI.x / screenDPI.x; | |
430 } | |
431 } | |
432 lineSpacing = styledText.lineSpacing * printerDPI.y / screenDPI.y; | |
433 if (printOptions.printLineNumbers) { | |
434 printMargin = 3 * printerDPI.x / screenDPI.x; | |
435 } | |
436 } | |
437 /** | |
438 * Copies the text of the specified <code>StyledTextContent</code>. | |
439 * </p> | |
440 * @param original the <code>StyledTextContent</code> to copy. | |
441 */ | |
442 StyledTextContent copyContent(StyledTextContent original) { | |
443 StyledTextContent printerContent = new DefaultContent(); | |
444 int insertOffset = 0; | |
445 for (int i = 0; i < original.getLineCount(); i++) { | |
446 int insertEndOffset; | |
447 if (i < original.getLineCount() - 1) { | |
448 insertEndOffset = original.getOffsetAtLine(i + 1); | |
449 } else { | |
450 insertEndOffset = original.getCharCount(); | |
451 } | |
452 printerContent.replaceTextRange(insertOffset, 0, original.getTextRange(insertOffset, insertEndOffset - insertOffset)); | |
453 insertOffset = insertEndOffset; | |
454 } | |
455 return printerContent; | |
456 } | |
457 /** | |
458 * Disposes of the resources and the <code>PrintRenderer</code>. | |
459 */ | |
460 void dispose() { | |
461 if (gc !is null) { | |
462 gc.dispose(); | |
463 gc = null; | |
464 } | |
465 foreach( resource; resources.values ){ | |
466 resource.dispose(); | |
467 } | |
468 resources = null; | |
469 if (printerFont !is null) { | |
470 printerFont.dispose(); | |
471 printerFont = null; | |
472 } | |
473 if (printerRenderer !is null) { | |
474 printerRenderer.dispose(); | |
475 printerRenderer = null; | |
476 } | |
477 } | |
478 void init_() { | |
479 Rectangle trim = printer.computeTrim(0, 0, 0, 0); | |
480 Point dpi = printer.getDPI(); | |
481 | |
482 printerFont = new Font( cast(Device)printer, fontData.getName(), fontData.getHeight(), SWT.NORMAL); | |
483 clientArea = printer.getClientArea(); | |
484 pageWidth = clientArea.width; | |
485 // one inch margin around text | |
486 clientArea.x = dpi.x + trim.x; | |
487 clientArea.y = dpi.y + trim.y; | |
488 clientArea.width -= (clientArea.x + trim.width); | |
489 clientArea.height -= (clientArea.y + trim.height); | |
490 | |
491 int style = mirrored ? SWT.RIGHT_TO_LEFT : SWT.LEFT_TO_RIGHT; | |
492 gc = new GC(printer, style); | |
493 gc.setFont(printerFont); | |
494 printerRenderer.setFont(printerFont, tabLength); | |
495 int lineHeight = printerRenderer.getLineHeight(); | |
496 if (printOptions.header !is null) { | |
497 clientArea.y += lineHeight * 2; | |
498 clientArea.height -= lineHeight * 2; | |
499 } | |
500 if (printOptions.footer !is null) { | |
501 clientArea.height -= lineHeight * 2; | |
502 } | |
503 | |
504 // TODO not wrapped | |
505 StyledTextContent content = printerRenderer.content; | |
506 startLine = 0; | |
507 endLine = singleLine ? 0 : content.getLineCount() - 1; | |
508 PrinterData data = printer.getPrinterData(); | |
509 if (data.scope_ is PrinterData.PAGE_RANGE) { | |
510 int pageSize = clientArea.height / lineHeight;//WRONG | |
511 startLine = (startPage - 1) * pageSize; | |
512 } else if (data.scope_ is PrinterData.SELECTION) { | |
513 startLine = content.getLineAtOffset(selection.x); | |
514 if (selection.y > 0) { | |
515 endLine = content.getLineAtOffset(selection.x + selection.y - 1); | |
516 } else { | |
517 endLine = startLine - 1; | |
518 } | |
519 } | |
520 } | |
521 /** | |
522 * Prints the lines in the specified page range. | |
523 */ | |
524 void print() { | |
525 Color background = gc.getBackground(); | |
526 Color foreground = gc.getForeground(); | |
527 int paintY = clientArea.y; | |
528 int paintX = clientArea.x; | |
529 int width = clientArea.width; | |
530 int page = startPage; | |
531 int pageBottom = clientArea.y + clientArea.height; | |
532 int orientation = gc.getStyle() & (SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT); | |
533 TextLayout printLayout = null; | |
534 if (printOptions.printLineNumbers || printOptions.header !is null || printOptions.footer !is null) { | |
535 printLayout = new TextLayout(printer); | |
536 printLayout.setFont(printerFont); | |
537 } | |
538 if (printOptions.printLineNumbers) { | |
539 int numberingWidth = 0; | |
540 int count = endLine - startLine + 1; | |
541 String[] lineLabels = printOptions.lineLabels; | |
542 if (lineLabels !is null) { | |
543 for (int i = startLine; i < Math.min(count, lineLabels.length); i++) { | |
544 if (lineLabels[i] !is null) { | |
545 printLayout.setText(lineLabels[i]); | |
546 int lineWidth = printLayout.getBounds().width; | |
547 numberingWidth = Math.max(numberingWidth, lineWidth); | |
548 } | |
549 } | |
550 } else { | |
551 StringBuffer buffer = new StringBuffer("0"); | |
552 while ((count /= 10) > 0) buffer.append("0"); | |
553 printLayout.setText(buffer.toString()); | |
554 numberingWidth = printLayout.getBounds().width; | |
555 } | |
556 numberingWidth += printMargin; | |
557 if (numberingWidth > width) numberingWidth = width; | |
558 paintX += numberingWidth; | |
559 width -= numberingWidth; | |
560 } | |
561 for (int i = startLine; i <= endLine && page <= endPage; i++) { | |
562 if (paintY is clientArea.y) { | |
563 printer.startPage(); | |
564 printDecoration(page, true, printLayout); | |
565 } | |
566 TextLayout layout = printerRenderer.getTextLayout(i, orientation, width, lineSpacing); | |
567 Color lineBackground = printerRenderer.getLineBackground(i, background); | |
568 int paragraphBottom = paintY + layout.getBounds().height; | |
569 if (paragraphBottom <= pageBottom) { | |
570 //normal case, the whole paragraph fits in the current page | |
571 printLine(paintX, paintY, gc, foreground, lineBackground, layout, printLayout, i); | |
572 paintY = paragraphBottom; | |
573 } else { | |
574 int lineCount = layout.getLineCount(); | |
575 while (paragraphBottom > pageBottom && lineCount > 0) { | |
576 lineCount--; | |
577 paragraphBottom -= layout.getLineBounds(lineCount).height + layout.getSpacing(); | |
578 } | |
579 if (lineCount is 0) { | |
580 //the whole paragraph goes to the next page | |
581 printDecoration(page, false, printLayout); | |
582 printer.endPage(); | |
583 page++; | |
584 if (page <= endPage) { | |
585 printer.startPage(); | |
586 printDecoration(page, true, printLayout); | |
587 paintY = clientArea.y; | |
588 printLine(paintX, paintY, gc, foreground, lineBackground, layout, printLayout, i); | |
589 paintY += layout.getBounds().height; | |
590 } | |
591 } else { | |
592 //draw paragraph top in the current page and paragraph bottom in the next | |
593 int height = paragraphBottom - paintY; | |
594 gc.setClipping(clientArea.x, paintY, clientArea.width, height); | |
595 printLine(paintX, paintY, gc, foreground, lineBackground, layout, printLayout, i); | |
596 gc.setClipping(cast(Rectangle)null); | |
597 printDecoration(page, false, printLayout); | |
598 printer.endPage(); | |
599 page++; | |
600 if (page <= endPage) { | |
601 printer.startPage(); | |
602 printDecoration(page, true, printLayout); | |
603 paintY = clientArea.y - height; | |
604 int layoutHeight = layout.getBounds().height; | |
605 gc.setClipping(clientArea.x, clientArea.y, clientArea.width, layoutHeight - height); | |
606 printLine(paintX, paintY, gc, foreground, lineBackground, layout, printLayout, i); | |
607 gc.setClipping(cast(Rectangle)null); | |
608 paintY += layoutHeight; | |
609 } | |
610 } | |
611 } | |
612 printerRenderer.disposeTextLayout(layout); | |
613 } | |
614 if (page <= endPage && paintY > clientArea.y) { | |
615 // close partial page | |
616 printDecoration(page, false, printLayout); | |
617 printer.endPage(); | |
618 } | |
619 if (printLayout !is null) printLayout.dispose(); | |
620 } | |
621 /** | |
622 * Print header or footer decorations. | |
623 * | |
624 * @param page page number to print, if specified in the StyledTextPrintOptions header or footer. | |
625 * @param header true = print the header, false = print the footer | |
626 */ | |
627 void printDecoration(int page, bool header, TextLayout layout) { | |
628 String text = header ? printOptions.header : printOptions.footer; | |
629 if (text is null) return; | |
630 int lastSegmentIndex = 0; | |
631 for (int i = 0; i < 3; i++) { | |
632 int segmentIndex = text.indexOf( StyledTextPrintOptions.SEPARATOR, lastSegmentIndex); | |
633 String segment; | |
634 if (segmentIndex is -1 ) { | |
635 segment = text.substring(lastSegmentIndex); | |
636 printDecorationSegment(segment, i, page, header, layout); | |
637 break; | |
638 } else { | |
639 segment = text.substring(lastSegmentIndex, segmentIndex); | |
640 printDecorationSegment(segment, i, page, header, layout); | |
641 lastSegmentIndex = segmentIndex + StyledTextPrintOptions.SEPARATOR.length; | |
642 } | |
643 } | |
644 } | |
645 /** | |
646 * Print one segment of a header or footer decoration. | |
647 * Headers and footers have three different segments. | |
648 * One each for left aligned, centered, and right aligned text. | |
649 * | |
650 * @param segment decoration segment to print | |
651 * @param alignment alignment of the segment. 0=left, 1=center, 2=right | |
652 * @param page page number to print, if specified in the decoration segment. | |
653 * @param header true = print the header, false = print the footer | |
654 */ | |
655 void printDecorationSegment(String segment, int alignment, int page, bool header, TextLayout layout) { | |
656 int pageIndex = segment.indexOf(StyledTextPrintOptions.PAGE_TAG); | |
657 if (pageIndex !is -1 ) { | |
658 int pageTagLength = StyledTextPrintOptions.PAGE_TAG.length; | |
659 StringBuffer buffer = new StringBuffer(segment.substring (0, pageIndex)); | |
660 buffer.append (page); | |
661 buffer.append (segment.substring(pageIndex + pageTagLength)); | |
51 | 662 segment = buffer.toString()._idup(); |
25 | 663 } |
664 if (segment.length > 0) { | |
665 layout.setText(segment); | |
666 int segmentWidth = layout.getBounds().width; | |
667 int segmentHeight = printerRenderer.getLineHeight(); | |
668 int drawX = 0, drawY; | |
669 if (alignment is LEFT) { | |
670 drawX = clientArea.x; | |
671 } else if (alignment is CENTER) { | |
672 drawX = (pageWidth - segmentWidth) / 2; | |
673 } else if (alignment is RIGHT) { | |
674 drawX = clientArea.x + clientArea.width - segmentWidth; | |
675 } | |
676 if (header) { | |
677 drawY = clientArea.y - segmentHeight * 2; | |
678 } else { | |
679 drawY = clientArea.y + clientArea.height + segmentHeight; | |
680 } | |
681 layout.draw(gc, drawX, drawY); | |
682 } | |
683 } | |
684 void printLine(int x, int y, GC gc, Color foreground, Color background, TextLayout layout, TextLayout printLayout, int index) { | |
685 if (background !is null) { | |
686 Rectangle rect = layout.getBounds(); | |
687 gc.setBackground(background); | |
688 gc.fillRectangle(x, y, rect.width, rect.height); | |
689 | |
690 // int lineCount = layout.getLineCount(); | |
691 // for (int i = 0; i < lineCount; i++) { | |
692 // Rectangle rect = layout.getLineBounds(i); | |
693 // rect.x += paintX; | |
694 // rect.y += paintY + layout.getSpacing(); | |
695 // rect.width = width;//layout bounds | |
696 // gc.fillRectangle(rect); | |
697 // } | |
698 } | |
699 if (printOptions.printLineNumbers) { | |
700 FontMetrics metrics = layout.getLineMetrics(0); | |
701 printLayout.setAscent(metrics.getAscent() + metrics.getLeading()); | |
702 printLayout.setDescent(metrics.getDescent()); | |
703 String[] lineLabels = printOptions.lineLabels; | |
704 if (lineLabels !is null) { | |
705 if (0 <= index && index < lineLabels.length && lineLabels[index] !is null) { | |
706 printLayout.setText(lineLabels[index]); | |
707 } else { | |
708 printLayout.setText(""); | |
709 } | |
710 } else { | |
711 printLayout.setText(to!(String)(index)); | |
712 } | |
713 int paintX = x - printMargin - printLayout.getBounds().width; | |
714 printLayout.draw(gc, paintX, y); | |
715 printLayout.setAscent(-1); | |
716 printLayout.setDescent(-1); | |
717 } | |
718 gc.setForeground(foreground); | |
719 layout.draw(gc, x, y); | |
720 } | |
721 /** | |
722 * Starts a print job and prints the pages specified in the constructor. | |
723 */ | |
724 public void run() { | |
725 String jobName = printOptions.jobName; | |
726 if (jobName is null) { | |
727 jobName = "Printing"; | |
728 } | |
729 if (printer.startJob(jobName)) { | |
730 init_(); | |
731 print(); | |
732 dispose(); | |
733 printer.endJob(); | |
734 } | |
735 } | |
736 } | |
737 /** | |
738 * The <code>RTFWriter</code> class is used to write widget content as | |
739 * rich text. The implementation complies with the RTF specification | |
740 * version 1.5. | |
741 * <p> | |
742 * toString() is guaranteed to return a valid RTF string only after | |
743 * close() has been called. | |
744 * </p><p> | |
745 * Whole and partial lines and line breaks can be written. Lines will be | |
746 * formatted using the styles queried from the LineStyleListener, if | |
747 * set, or those set directly in the widget. All styles are applied to | |
748 * the RTF stream like they are rendered by the widget. In addition, the | |
749 * widget font name and size is used for the whole text. | |
750 * </p> | |
751 */ | |
752 class RTFWriter : TextWriter { | |
753 | |
754 alias TextWriter.write write; | |
755 | |
756 static const int DEFAULT_FOREGROUND = 0; | |
757 static const int DEFAULT_BACKGROUND = 1; | |
758 Color[] colorTable; | |
759 Font[] fontTable; | |
760 bool WriteUnicode; | |
761 | |
762 /** | |
763 * Creates a RTF writer that writes content starting at offset "start" | |
764 * in the document. <code>start</code> and <code>length</code>can be set to specify partial | |
765 * lines. | |
766 * | |
767 * @param start start offset of content to write, 0 based from | |
768 * beginning of document | |
769 * @param length length of content to write | |
770 */ | |
771 public this(int start, int length) { | |
772 super(start, length); | |
773 colorTable ~= getForeground(); | |
774 colorTable ~= getBackground(); | |
775 fontTable ~= getFont(); | |
776 setUnicode(); | |
777 } | |
778 /** | |
779 * Closes the RTF writer. Once closed no more content can be written. | |
780 * <b>NOTE:</b> <code>toString()</code> does not return a valid RTF string until | |
781 * <code>close()</code> has been called. | |
782 */ | |
783 public override void close() { | |
784 if (!isClosed()) { | |
785 writeHeader(); | |
786 write("\n}}\0"); | |
787 super.close(); | |
788 } | |
789 } | |
790 /** | |
791 * Returns the index of the specified color in the RTF color table. | |
792 * | |
793 * @param color the color | |
794 * @param defaultIndex return value if color is null | |
795 * @return the index of the specified color in the RTF color table | |
796 * or "defaultIndex" if "color" is null. | |
797 */ | |
798 int getColorIndex(Color color, int defaultIndex) { | |
799 if (color is null) return defaultIndex; | |
800 int index = -1; | |
801 foreach( i, col; colorTable ){ | |
802 if( col == color ){ | |
803 index = i; | |
804 break; | |
805 } | |
806 } | |
807 if (index is -1) { | |
808 index = colorTable.length; | |
809 colorTable ~= color; | |
810 } | |
811 return index; | |
812 } | |
813 /** | |
814 * Returns the index of the specified color in the RTF color table. | |
815 * | |
816 * @param color the color | |
817 * @param defaultIndex return value if color is null | |
818 * @return the index of the specified color in the RTF color table | |
819 * or "defaultIndex" if "color" is null. | |
820 */ | |
821 int getFontIndex(Font font) { | |
822 int index = -1; | |
823 foreach( i, f; colorTable ){ | |
824 if( f == font ){ | |
825 index = i; | |
826 break; | |
827 } | |
828 } | |
829 if (index is -1) { | |
830 index = fontTable.length; | |
831 fontTable ~= font; | |
832 } | |
833 return index; | |
834 } | |
835 /** | |
836 * Determines if Unicode RTF should be written. | |
837 * Don't write Unicode RTF on Windows 95/98/ME or NT. | |
838 */ | |
839 void setUnicode() { | |
840 // const String Win95 = "windows 95"; | |
841 // const String Win98 = "windows 98"; | |
842 // const String WinME = "windows me"; | |
843 // const String WinNT = "windows nt"; | |
844 // String osName = System.getProperty("os.name").toLowerCase(); | |
845 // String osVersion = System.getProperty("os.version"); | |
846 // int majorVersion = 0; | |
847 // | |
848 // if (osName.startsWith(WinNT) && osVersion !is null) { | |
849 // int majorIndex = osVersion.indexOf('.'); | |
850 // if (majorIndex !is -1) { | |
851 // osVersion = osVersion.substring(0, majorIndex); | |
852 // try { | |
853 // majorVersion = Integer.parseInt(osVersion); | |
854 // } catch (NumberFormatException exception) { | |
855 // // ignore exception. version number remains unknown. | |
856 // // will write without Unicode | |
857 // } | |
858 // } | |
859 // } | |
860 // WriteUnicode = !osName.startsWith(Win95) && | |
861 // !osName.startsWith(Win98) && | |
862 // !osName.startsWith(WinME) && | |
863 // (!osName.startsWith(WinNT) || majorVersion > 4); | |
864 WriteUnicode = true; // we are on linux-gtk | |
865 } | |
866 /** | |
867 * Appends the specified segment of "string" to the RTF data. | |
868 * Copy from <code>start</code> up to, but excluding, <code>end</code>. | |
869 * | |
870 * @param string string to copy a segment from. Must not contain | |
871 * line breaks. Line breaks should be written using writeLineDelimiter() | |
872 * @param start start offset of segment. 0 based. | |
873 * @param end end offset of segment | |
874 */ | |
875 void write(String string, int start, int end) { | |
876 start = 0; | |
877 end = string.length; | |
878 int incr = 1; | |
879 for (int index = start; index < end; index+=incr) { | |
880 dchar ch = firstCodePoint( string[index .. $], incr ); | |
881 if (ch > 0xFF && WriteUnicode) { | |
882 // write the sub string from the last escaped character | |
883 // to the current one. Fixes bug 21698. | |
884 if (index > start) { | |
885 write( string[start .. index ] ); | |
886 } | |
887 write("\\u"); | |
888 write( to!(String)( cast(short)ch )); | |
889 write(' '); // control word delimiter | |
890 start = index + incr; | |
891 } else if (ch is '}' || ch is '{' || ch is '\\') { | |
892 // write the sub string from the last escaped character | |
893 // to the current one. Fixes bug 21698. | |
894 if (index > start) { | |
895 write(string[start .. index]); | |
896 } | |
897 write('\\'); | |
898 write(cast(char)ch); // ok because one of {}\ | |
899 start = index + 1; | |
900 } | |
901 } | |
902 // write from the last escaped character to the end. | |
903 // Fixes bug 21698. | |
904 if (start < end) { | |
905 write(string[ start .. end]); | |
906 } | |
907 } | |
908 /** | |
909 * Writes the RTF header including font table and color table. | |
910 */ | |
911 void writeHeader() { | |
912 StringBuffer header = new StringBuffer(); | |
913 FontData fontData = getFont().getFontData()[0]; | |
914 header.append("{\\rtf1\\ansi"); | |
915 // specify code page, necessary for copy to work in bidi | |
916 // systems that don't support Unicode RTF. | |
917 // PORTING_TODO: String cpg = System.getProperty("file.encoding").toLowerCase(); | |
918 String cpg = "UTF16"; | |
919 /+ | |
920 if (cpg.startsWith("cp") || cpg.startsWith("ms")) { | |
921 cpg = cpg.substring(2, cpg.length()); | |
922 header.append("\\ansicpg"); | |
923 header.append(cpg); | |
924 } | |
925 +/ | |
926 header.append("\\uc0\\deff0{\\fonttbl{\\f0\\fnil "); | |
927 header.append(fontData.getName()); | |
928 header.append(";"); | |
929 for (int i = 1; i < fontTable.length; i++) { | |
930 header.append("\\f"); | |
931 header.append(i); | |
932 header.append(" "); | |
933 FontData fd = (cast(Font)fontTable[i]).getFontData()[0]; | |
934 header.append(fd.getName()); | |
935 header.append(";"); | |
936 } | |
937 header.append("}}\n{\\colortbl"); | |
938 for (int i = 0; i < colorTable.length; i++) { | |
939 Color color = cast(Color) colorTable[i]; | |
940 header.append("\\red"); | |
941 header.append(color.getRed()); | |
942 header.append("\\green"); | |
943 header.append(color.getGreen()); | |
944 header.append("\\blue"); | |
945 header.append(color.getBlue()); | |
946 header.append(";"); | |
947 } | |
948 // some RTF readers ignore the deff0 font tag. Explicitly | |
949 // set the font for the whole document to work around this. | |
950 header.append("}\n{\\f0\\fs"); | |
951 // font size is specified in half points | |
952 header.append(fontData.getHeight() * 2); | |
953 header.append(" "); | |
954 write(header.toString(), 0); | |
955 } | |
956 /** | |
957 * Appends the specified line text to the RTF data. Lines will be formatted | |
958 * using the styles queried from the LineStyleListener, if set, or those set | |
959 * directly in the widget. | |
960 * | |
961 * @param line line text to write as RTF. Must not contain line breaks | |
962 * Line breaks should be written using writeLineDelimiter() | |
963 * @param lineOffset offset of the line. 0 based from the start of the | |
964 * widget document. Any text occurring before the start offset or after the | |
965 * end offset specified during object creation is ignored. | |
966 * @exception SWTException <ul> | |
967 * <li>ERROR_IO when the writer is closed.</li> | |
968 * </ul> | |
969 */ | |
970 public override void writeLine(String line, int lineOffset) { | |
971 if (isClosed()) { | |
972 SWT.error(SWT.ERROR_IO); | |
973 } | |
974 int lineIndex = content.getLineAtOffset(lineOffset); | |
975 int lineAlignment, lineIndent; | |
976 bool lineJustify; | |
977 int[] ranges; | |
978 StyleRange[] styles; | |
979 StyledTextEvent event = getLineStyleData(lineOffset, line); | |
980 if (event !is null) { | |
981 lineAlignment = event.alignment; | |
982 lineIndent = event.indent; | |
983 lineJustify = event.justify; | |
984 ranges = event.ranges; | |
985 styles = event.styles; | |
986 } else { | |
987 lineAlignment = renderer.getLineAlignment(lineIndex, alignment); | |
988 lineIndent = renderer.getLineIndent(lineIndex, indent); | |
989 lineJustify = renderer.getLineJustify(lineIndex, justify); | |
990 ranges = renderer.getRanges(lineOffset, line.length); | |
991 styles = renderer.getStyleRanges(lineOffset, line.length, false); | |
992 } | |
993 if (styles is null) styles = new StyleRange[0]; | |
994 Color lineBackground = renderer.getLineBackground(lineIndex, null); | |
995 event = getLineBackgroundData(lineOffset, line); | |
996 if (event !is null && event.lineBackground !is null) lineBackground = event.lineBackground; | |
997 writeStyledLine(line, lineOffset, ranges, styles, lineBackground, lineIndent, lineAlignment, lineJustify); | |
998 } | |
999 /** | |
1000 * Appends the specified line delimiter to the RTF data. | |
1001 * | |
1002 * @param lineDelimiter line delimiter to write as RTF. | |
1003 * @exception SWTException <ul> | |
1004 * <li>ERROR_IO when the writer is closed.</li> | |
1005 * </ul> | |
1006 */ | |
1007 public override void writeLineDelimiter(String lineDelimiter) { | |
1008 if (isClosed()) { | |
1009 SWT.error(SWT.ERROR_IO); | |
1010 } | |
1011 write(lineDelimiter, 0, lineDelimiter.length); | |
1012 write("\\par "); | |
1013 } | |
1014 /** | |
1015 * Appends the specified line text to the RTF data. | |
1016 * <p> | |
1017 * Use the colors and font styles specified in "styles" and "lineBackground". | |
1018 * Formatting is written to reflect the text rendering by the text widget. | |
1019 * Style background colors take precedence over the line background color. | |
1020 * Background colors are written using the \highlight tag (vs. the \cb tag). | |
1021 * </p> | |
1022 * | |
1023 * @param line line text to write as RTF. Must not contain line breaks | |
1024 * Line breaks should be written using writeLineDelimiter() | |
1025 * @param lineOffset offset of the line. 0 based from the start of the | |
1026 * widget document. Any text occurring before the start offset or after the | |
1027 * end offset specified during object creation is ignored. | |
1028 * @param styles styles to use for formatting. Must not be null. | |
1029 * @param lineBackground line background color to use for formatting. | |
1030 * May be null. | |
1031 */ | |
1032 void writeStyledLine(String line, int lineOffset, int ranges[], StyleRange[] styles, Color lineBackground, int indent, int alignment, bool justify) { | |
1033 int lineLength = line.length; | |
1034 int startOffset = getStart(); | |
1035 int writeOffset = startOffset - lineOffset; | |
1036 if (writeOffset >= lineLength) return; | |
1037 int lineIndex = Math.max(0, writeOffset); | |
1038 | |
1039 write("\\fi"); | |
1040 write(indent); | |
1041 switch (alignment) { | |
1042 case SWT.LEFT: write("\\ql"); break; | |
1043 case SWT.CENTER: write("\\qc"); break; | |
1044 case SWT.RIGHT: write("\\qr"); break; | |
1045 default: | |
1046 } | |
1047 if (justify) write("\\qj"); | |
1048 write(" "); | |
1049 | |
1050 if (lineBackground !is null) { | |
1051 write("{\\highlight"); | |
1052 write(getColorIndex(lineBackground, DEFAULT_BACKGROUND)); | |
1053 write(" "); | |
1054 } | |
1055 int endOffset = startOffset + super.getCharCount(); | |
1056 int lineEndOffset = Math.min(lineLength, endOffset - lineOffset); | |
1057 for (int i = 0; i < styles.length; i++) { | |
1058 StyleRange style = styles[i]; | |
1059 int start, end; | |
1060 if (ranges !is null) { | |
1061 start = ranges[i << 1] - lineOffset; | |
1062 end = start + ranges[(i << 1) + 1]; | |
1063 } else { | |
1064 start = style.start - lineOffset; | |
1065 end = start + style.length; | |
1066 } | |
1067 // skip over partial first line | |
1068 if (end < writeOffset) { | |
1069 continue; | |
1070 } | |
1071 // style starts beyond line end or RTF write end | |
1072 if (start >= lineEndOffset) { | |
1073 break; | |
1074 } | |
1075 // write any unstyled text | |
1076 if (lineIndex < start) { | |
1077 // copy to start of style | |
1078 // style starting beyond end of write range or end of line | |
1079 // is guarded against above. | |
1080 write(line, lineIndex, start); | |
1081 lineIndex = start; | |
1082 } | |
1083 // write styled text | |
1084 write("{\\cf"); | |
1085 write(getColorIndex(style.foreground, DEFAULT_FOREGROUND)); | |
1086 int colorIndex = getColorIndex(style.background, DEFAULT_BACKGROUND); | |
1087 if (colorIndex !is DEFAULT_BACKGROUND) { | |
1088 write("\\highlight"); | |
1089 write(colorIndex); | |
1090 } | |
1091 Font font = style.font; | |
1092 if (font !is null) { | |
1093 int fontIndex = getFontIndex(font); | |
1094 write("\\f"); | |
1095 write(fontIndex); | |
1096 FontData fontData = font.getFontData()[0]; | |
1097 write("\\fs"); | |
1098 write(fontData.getHeight() * 2); | |
1099 } else { | |
1100 if ((style.fontStyle & SWT.BOLD) !is 0) { | |
1101 write("\\b"); | |
1102 } | |
1103 if ((style.fontStyle & SWT.ITALIC) !is 0) { | |
1104 write("\\i"); | |
1105 } | |
1106 } | |
1107 if (style.underline) { | |
1108 write("\\ul"); | |
1109 } | |
1110 if (style.strikeout) { | |
1111 write("\\strike"); | |
1112 } | |
1113 write(" "); | |
1114 // copy to end of style or end of write range or end of line | |
1115 int copyEnd = Math.min(end, lineEndOffset); | |
1116 // guard against invalid styles and let style processing continue | |
1117 copyEnd = Math.max(copyEnd, lineIndex); | |
1118 write(line, lineIndex, copyEnd); | |
1119 if (font is null) { | |
1120 if ((style.fontStyle & SWT.BOLD) !is 0) { | |
1121 write("\\b0"); | |
1122 } | |
1123 if ((style.fontStyle & SWT.ITALIC) !is 0) { | |
1124 write("\\i0"); | |
1125 } | |
1126 } | |
1127 if (style.underline) { | |
1128 write("\\ul0"); | |
1129 } | |
1130 if (style.strikeout) { | |
1131 write("\\strike0"); | |
1132 } | |
1133 write("}"); | |
1134 lineIndex = copyEnd; | |
1135 } | |
1136 // write unstyled text at the end of the line | |
1137 if (lineIndex < lineEndOffset) { | |
1138 write(line, lineIndex, lineEndOffset); | |
1139 } | |
1140 if (lineBackground !is null) write("}"); | |
1141 } | |
1142 } | |
1143 /** | |
1144 * The <code>TextWriter</code> class is used to write widget content to | |
1145 * a string. Whole and partial lines and line breaks can be written. To write | |
1146 * partial lines, specify the start and length of the desired segment | |
1147 * during object creation. | |
1148 * <p> | |
1149 * </b>NOTE:</b> <code>toString()</code> is guaranteed to return a valid string only after close() | |
1150 * has been called. | |
1151 * </p> | |
1152 */ | |
1153 class TextWriter { | |
1154 private StringBuffer buffer; | |
1155 private int startOffset; // offset of first character that will be written | |
1156 private int endOffset; // offset of last character that will be written. | |
1157 // 0 based from the beginning of the widget text. | |
1158 private bool isClosed_ = false; | |
1159 | |
1160 /** | |
1161 * Creates a writer that writes content starting at offset "start" | |
1162 * in the document. <code>start</code> and <code>length</code> can be set to specify partial lines. | |
1163 * | |
1164 * @param start start offset of content to write, 0 based from beginning of document | |
1165 * @param length length of content to write | |
1166 */ | |
1167 public this(int start, int length) { | |
1168 buffer = new StringBuffer(length); | |
1169 startOffset = start; | |
1170 endOffset = start + length; | |
1171 } | |
1172 /** | |
1173 * Closes the writer. Once closed no more content can be written. | |
1174 * <b>NOTE:</b> <code>toString()</code> is not guaranteed to return a valid string unless | |
1175 * the writer is closed. | |
1176 */ | |
1177 public void close() { | |
1178 if (!isClosed_) { | |
1179 isClosed_ = true; | |
1180 } | |
1181 } | |
1182 /** | |
1183 * Returns the number of characters to write. | |
1184 * @return the integer number of characters to write | |
1185 */ | |
1186 public int getCharCount() { | |
1187 return endOffset - startOffset; | |
1188 } | |
1189 /** | |
1190 * Returns the offset where writing starts. 0 based from the start of | |
1191 * the widget text. Used to write partial lines. | |
1192 * @return the integer offset where writing starts | |
1193 */ | |
1194 public int getStart() { | |
1195 return startOffset; | |
1196 } | |
1197 /** | |
1198 * Returns whether the writer is closed. | |
1199 * @return a bool specifying whether or not the writer is closed | |
1200 */ | |
1201 public bool isClosed() { | |
1202 return isClosed_; | |
1203 } | |
1204 /** | |
1205 * Returns the string. <code>close()</code> must be called before <code>toString()</code> | |
1206 * is guaranteed to return a valid string. | |
1207 * | |
1208 * @return the string | |
1209 */ | |
1210 public override String toString() { | |
1211 return buffer.toString(); | |
1212 } | |
1213 /** | |
1214 * Appends the given string to the data. | |
1215 */ | |
1216 void write(String string) { | |
1217 buffer.append(string); | |
1218 } | |
1219 /** | |
1220 * Inserts the given string to the data at the specified offset. | |
1221 * <p> | |
1222 * Do nothing if "offset" is < 0 or > getCharCount() | |
1223 * </p> | |
1224 * | |
1225 * @param string text to insert | |
1226 * @param offset offset in the existing data to insert "string" at. | |
1227 */ | |
1228 void write(String string, int offset) { | |
1229 if (offset < 0 || offset > buffer.length()) { | |
1230 return; | |
1231 } | |
1232 buffer.insert( offset, string ); | |
1233 } | |
1234 /** | |
1235 * Appends the given int to the data. | |
1236 */ | |
1237 void write(int i) { | |
1238 buffer.append(i); | |
1239 } | |
1240 /** | |
1241 * Appends the given character to the data. | |
1242 */ | |
1243 void write(char i) { | |
1244 buffer.append(i); | |
1245 } | |
1246 /** | |
1247 * Appends the specified line text to the data. | |
1248 * | |
1249 * @param line line text to write. Must not contain line breaks | |
1250 * Line breaks should be written using writeLineDelimiter() | |
1251 * @param lineOffset offset of the line. 0 based from the start of the | |
1252 * widget document. Any text occurring before the start offset or after the | |
1253 * end offset specified during object creation is ignored. | |
1254 * @exception SWTException <ul> | |
1255 * <li>ERROR_IO when the writer is closed.</li> | |
1256 * </ul> | |
1257 */ | |
1258 public void writeLine(String line, int lineOffset) { | |
1259 if (isClosed_) { | |
1260 SWT.error(SWT.ERROR_IO); | |
1261 } | |
1262 int writeOffset = startOffset - lineOffset; | |
1263 int lineLength = line.length; | |
1264 int lineIndex; | |
1265 if (writeOffset >= lineLength) { | |
1266 return; // whole line is outside write range | |
1267 } else if (writeOffset > 0) { | |
1268 lineIndex = writeOffset; // line starts before write start | |
1269 } else { | |
1270 lineIndex = 0; | |
1271 } | |
1272 int copyEnd = Math.min(lineLength, endOffset - lineOffset); | |
1273 if (lineIndex < copyEnd) { | |
1274 write(line.substring(lineIndex, copyEnd)); | |
1275 } | |
1276 } | |
1277 /** | |
1278 * Appends the specified line delimiter to the data. | |
1279 * | |
1280 * @param lineDelimiter line delimiter to write | |
1281 * @exception SWTException <ul> | |
1282 * <li>ERROR_IO when the writer is closed.</li> | |
1283 * </ul> | |
1284 */ | |
1285 public void writeLineDelimiter(String lineDelimiter) { | |
1286 if (isClosed_) { | |
1287 SWT.error(SWT.ERROR_IO); | |
1288 } | |
1289 write(lineDelimiter); | |
1290 } | |
1291 } | |
1292 | |
1293 /** | |
1294 * Constructs a new instance of this class given its parent | |
1295 * and a style value describing its behavior and appearance. | |
1296 * <p> | |
1297 * The style value is either one of the style constants defined in | |
1298 * class <code>SWT</code> which is applicable to instances of this | |
1299 * class, or must be built by <em>bitwise OR</em>'ing together | |
1300 * (that is, using the <code>int</code> "|" operator) two or more | |
1301 * of those <code>SWT</code> style constants. The class description | |
1302 * lists the style constants that are applicable to the class. | |
1303 * Style bits are also inherited from superclasses. | |
1304 * </p> | |
1305 * | |
1306 * @param parent a widget which will be the parent of the new instance (cannot be null) | |
1307 * @param style the style of widget to construct | |
1308 * | |
1309 * @exception IllegalArgumentException <ul> | |
1310 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
1311 * </ul> | |
1312 * @exception SWTException <ul> | |
1313 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
1314 * </ul> | |
1315 * | |
1316 * @see SWT#FULL_SELECTION | |
1317 * @see SWT#MULTI | |
1318 * @see SWT#READ_ONLY | |
1319 * @see SWT#SINGLE | |
1320 * @see SWT#WRAP | |
1321 * @see #getStyle | |
1322 */ | |
1323 public this(Composite parent, int style) { | |
1324 selection = new Point(0, 0); | |
1325 super(parent, checkStyle(style)); | |
1326 // set the fg in the OS to ensure that these are the same as StyledText, necessary | |
1327 // for ensuring that the bg/fg the IME box uses is the same as what StyledText uses | |
1328 super.setForeground(getForeground()); | |
1329 super.setDragDetect(false); | |
1330 Display display = getDisplay(); | |
1331 isMirrored_ = (super.getStyle() & SWT.MIRRORED) !is 0; | |
1332 fixedLineHeight = true; | |
1333 if ((style & SWT.READ_ONLY) !is 0) { | |
1334 setEditable(false); | |
1335 } | |
1336 leftMargin = rightMargin = isBidiCaret() ? BIDI_CARET_WIDTH - 1: 0; | |
1337 if ((style & SWT.SINGLE) !is 0 && (style & SWT.BORDER) !is 0) { | |
1338 leftMargin = topMargin = rightMargin = bottomMargin = 2; | |
1339 } | |
1340 alignment = style & (SWT.LEFT | SWT.RIGHT | SWT.CENTER); | |
1341 if (alignment is 0) alignment = SWT.LEFT; | |
1342 clipboard = new Clipboard(display); | |
1343 installDefaultContent(); | |
1344 renderer = new StyledTextRenderer(getDisplay(), this); | |
1345 renderer.setContent(content); | |
1346 renderer.setFont(getFont(), tabLength); | |
1347 ime = new IME(this, SWT.NONE); | |
1348 defaultCaret = new Caret(this, SWT.NONE); | |
1349 if ((style & SWT.WRAP) !is 0) { | |
1350 setWordWrap(true); | |
1351 } | |
1352 if (isBidiCaret()) { | |
1353 createCaretBitmaps(); | |
1354 Runnable runnable = new class() Runnable { | |
1355 public void run() { | |
1356 int direction = BidiUtil.getKeyboardLanguage() is BidiUtil.KEYBOARD_BIDI ? SWT.RIGHT : SWT.LEFT; | |
1357 if (direction is caretDirection) return; | |
1358 if (getCaret() !is defaultCaret) return; | |
1359 Point newCaretPos = getPointAtOffset(caretOffset); | |
1360 setCaretLocation(newCaretPos, direction); | |
1361 } | |
1362 }; | |
1363 BidiUtil.addLanguageListener(this, runnable); | |
1364 } | |
1365 setCaret(defaultCaret); | |
1366 calculateScrollBars(); | |
1367 createKeyBindings(); | |
1368 setCursor(display.getSystemCursor(SWT.CURSOR_IBEAM)); | |
1369 installListeners(); | |
1370 initializeAccessible(); | |
1371 setData("DEFAULT_DROP_TARGET_EFFECT", new StyledTextDropTargetEffect(this)); | |
1372 } | |
1373 /** | |
1374 * Adds an extended modify listener. An ExtendedModify event is sent by the | |
1375 * widget when the widget text has changed. | |
1376 * | |
1377 * @param extendedModifyListener the listener | |
1378 * @exception SWTException <ul> | |
1379 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1380 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1381 * </ul> | |
1382 * @exception IllegalArgumentException <ul> | |
1383 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
1384 * </ul> | |
1385 */ | |
1386 public void addExtendedModifyListener(ExtendedModifyListener extendedModifyListener) { | |
1387 checkWidget(); | |
1388 if (extendedModifyListener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1389 StyledTextListener typedListener = new StyledTextListener(extendedModifyListener); | |
1390 addListener(ExtendedModify, typedListener); | |
1391 } | |
1392 /** | |
1393 * Adds a bidirectional segment listener. | |
1394 * <p> | |
1395 * A BidiSegmentEvent is sent | |
1396 * whenever a line of text is measured or rendered. The user can | |
1397 * specify text ranges in the line that should be treated as if they | |
1398 * had a different direction than the surrounding text. | |
1399 * This may be used when adjacent segments of right-to-left text should | |
1400 * not be reordered relative to each other. | |
1401 * E.g., Multiple Java string literals in a right-to-left language | |
1402 * should generally remain in logical order to each other, that is, the | |
1403 * way they are stored. | |
1404 * </p> | |
1405 * | |
1406 * @param listener the listener | |
1407 * @exception SWTException <ul> | |
1408 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1409 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1410 * </ul> | |
1411 * @exception IllegalArgumentException <ul> | |
1412 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
1413 * </ul> | |
1414 * @see BidiSegmentEvent | |
1415 * @since 2.0 | |
1416 */ | |
1417 public void addBidiSegmentListener(BidiSegmentListener listener) { | |
1418 checkWidget(); | |
1419 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1420 addListener(LineGetSegments, new StyledTextListener(listener)); | |
1421 } | |
1422 /** | |
1423 * Adds a line background listener. A LineGetBackground event is sent by the | |
1424 * widget to determine the background color for a line. | |
1425 * | |
1426 * @param listener the listener | |
1427 * @exception SWTException <ul> | |
1428 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1429 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1430 * </ul> | |
1431 * @exception IllegalArgumentException <ul> | |
1432 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
1433 * </ul> | |
1434 */ | |
1435 public void addLineBackgroundListener(LineBackgroundListener listener) { | |
1436 checkWidget(); | |
1437 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1438 if (!isListening(LineGetBackground)) { | |
1439 renderer.clearLineBackground(0, content.getLineCount()); | |
1440 } | |
1441 addListener(LineGetBackground, new StyledTextListener(listener)); | |
1442 } | |
1443 /** | |
1444 * Adds a line style listener. A LineGetStyle event is sent by the widget to | |
1445 * determine the styles for a line. | |
1446 * | |
1447 * @param listener the listener | |
1448 * @exception SWTException <ul> | |
1449 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1450 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1451 * </ul> | |
1452 * @exception IllegalArgumentException <ul> | |
1453 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
1454 * </ul> | |
1455 */ | |
1456 public void addLineStyleListener(LineStyleListener listener) { | |
1457 checkWidget(); | |
1458 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1459 if (!isListening(LineGetStyle)) { | |
1460 setStyleRanges(0, 0, null, null, true); | |
1461 renderer.clearLineStyle(0, content.getLineCount()); | |
1462 } | |
1463 addListener(LineGetStyle, new StyledTextListener(listener)); | |
1464 } | |
1465 /** | |
1466 * Adds a modify listener. A Modify event is sent by the widget when the widget text | |
1467 * has changed. | |
1468 * | |
1469 * @param modifyListener the listener | |
1470 * @exception SWTException <ul> | |
1471 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1472 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1473 * </ul> | |
1474 * @exception IllegalArgumentException <ul> | |
1475 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
1476 * </ul> | |
1477 */ | |
1478 public void addModifyListener(ModifyListener modifyListener) { | |
1479 checkWidget(); | |
1480 if (modifyListener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1481 addListener(SWT.Modify, new TypedListener(modifyListener)); | |
1482 } | |
1483 /** | |
1484 * Adds a paint object listener. A paint object event is sent by the widget when an object | |
1485 * needs to be drawn. | |
1486 * | |
1487 * @param listener the listener | |
1488 * @exception SWTException <ul> | |
1489 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1490 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1491 * </ul> | |
1492 * @exception IllegalArgumentException <ul> | |
1493 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
1494 * </ul> | |
1495 * | |
1496 * @since 3.2 | |
1497 * | |
1498 * @see PaintObjectListener | |
1499 * @see PaintObjectEvent | |
1500 */ | |
1501 public void addPaintObjectListener(PaintObjectListener listener) { | |
1502 checkWidget(); | |
1503 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1504 addListener(PaintObject, new StyledTextListener(listener)); | |
1505 } | |
1506 /** | |
1507 * Adds a selection listener. A Selection event is sent by the widget when the | |
1508 * user changes the selection. | |
1509 * <p> | |
1510 * When <code>widgetSelected</code> is called, the event x and y fields contain | |
1511 * the start and end caret indices of the selection. | |
1512 * <code>widgetDefaultSelected</code> is not called for StyledTexts. | |
1513 * </p> | |
1514 * | |
1515 * @param listener the listener which should be notified when the user changes the receiver's selection | |
1516 | |
1517 * @exception IllegalArgumentException <ul> | |
1518 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
1519 * </ul> | |
1520 * @exception SWTException <ul> | |
1521 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1522 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1523 * </ul> | |
1524 * | |
1525 * @see SelectionListener | |
1526 * @see #removeSelectionListener | |
1527 * @see SelectionEvent | |
1528 */ | |
1529 public void addSelectionListener(SelectionListener listener) { | |
1530 checkWidget(); | |
1531 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1532 addListener(SWT.Selection, new TypedListener(listener)); | |
1533 } | |
1534 /** | |
1535 * Adds a verify key listener. A VerifyKey event is sent by the widget when a key | |
1536 * is pressed. The widget ignores the key press if the listener sets the doit field | |
1537 * of the event to false. | |
1538 * | |
1539 * @param listener the listener | |
1540 * @exception SWTException <ul> | |
1541 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1542 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1543 * </ul> | |
1544 * @exception IllegalArgumentException <ul> | |
1545 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
1546 * </ul> | |
1547 */ | |
1548 public void addVerifyKeyListener(VerifyKeyListener listener) { | |
1549 checkWidget(); | |
1550 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1551 addListener(VerifyKey, new StyledTextListener(listener)); | |
1552 } | |
1553 /** | |
1554 * Adds a verify listener. A Verify event is sent by the widget when the widget text | |
1555 * is about to change. The listener can set the event text and the doit field to | |
1556 * change the text that is set in the widget or to force the widget to ignore the | |
1557 * text change. | |
1558 * | |
1559 * @param verifyListener the listener | |
1560 * @exception SWTException <ul> | |
1561 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1562 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1563 * </ul> | |
1564 * @exception IllegalArgumentException <ul> | |
1565 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
1566 * </ul> | |
1567 */ | |
1568 public void addVerifyListener(VerifyListener verifyListener) { | |
1569 checkWidget(); | |
1570 if (verifyListener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1571 addListener(SWT.Verify, new TypedListener(verifyListener)); | |
1572 } | |
1573 /** | |
1574 * Adds a word movement listener. A movement event is sent when the boundary | |
1575 * of a word is needed. For example, this occurs during word next and word | |
1576 * previous actions. | |
1577 * | |
1578 * @param movementListener the listener | |
1579 * @exception SWTException <ul> | |
1580 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1581 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1582 * </ul> | |
1583 * @exception IllegalArgumentException <ul> | |
1584 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
1585 * </ul> | |
1586 * | |
1587 * @see MovementEvent | |
1588 * @see MovementListener | |
1589 * @see #removeWordMovementListener | |
1590 * | |
1591 * @since 3.3 | |
1592 */ | |
1593 public void addWordMovementListener(MovementListener movementListener) { | |
1594 checkWidget(); | |
1595 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1596 addListener(WordNext, new StyledTextListener(movementListener)); | |
1597 addListener(WordPrevious, new StyledTextListener(movementListener)); | |
1598 } | |
1599 /** | |
1600 * Appends a string to the text at the end of the widget. | |
1601 * | |
1602 * @param string the string to be appended | |
1603 * @see #replaceTextRange(int,int,String) | |
1604 * @exception SWTException <ul> | |
1605 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1606 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1607 * </ul> | |
1608 */ | |
1609 public void append(String string) { | |
1610 checkWidget(); | |
1611 // SWT extension: allow null for zero length string | |
1612 // if (string is null) { | |
1613 // SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
1614 // } | |
1615 int lastChar = Math.max(getCharCount(), 0); | |
1616 replaceTextRange(lastChar, 0, string); | |
1617 } | |
1618 /** | |
1619 * Calculates the scroll bars | |
1620 */ | |
1621 void calculateScrollBars() { | |
1622 ScrollBar horizontalBar = getHorizontalBar(); | |
1623 ScrollBar verticalBar = getVerticalBar(); | |
1624 setScrollBars(true); | |
1625 if (verticalBar !is null) { | |
1626 verticalBar.setIncrement(getVerticalIncrement()); | |
1627 } | |
1628 if (horizontalBar !is null) { | |
1629 horizontalBar.setIncrement(getHorizontalIncrement()); | |
1630 } | |
1631 } | |
1632 /** | |
1633 * Calculates the top index based on the current vertical scroll offset. | |
1634 * The top index is the index of the topmost fully visible line or the | |
1635 * topmost partially visible line if no line is fully visible. | |
1636 * The top index starts at 0. | |
1637 */ | |
1638 void calculateTopIndex(int delta) { | |
1639 int oldTopIndex = topIndex; | |
1640 int oldTopIndexY = topIndexY; | |
1641 if (isFixedLineHeight()) { | |
1642 int verticalIncrement = getVerticalIncrement(); | |
1643 if (verticalIncrement is 0) { | |
1644 return; | |
1645 } | |
1646 topIndex = Compatibility.ceil(getVerticalScrollOffset(), verticalIncrement); | |
1647 // Set top index to partially visible top line if no line is fully | |
1648 // visible but at least some of the widget client area is visible. | |
1649 // Fixes bug 15088. | |
1650 if (topIndex > 0) { | |
1651 if (clientAreaHeight > 0) { | |
1652 int bottomPixel = getVerticalScrollOffset() + clientAreaHeight; | |
1653 int fullLineTopPixel = topIndex * verticalIncrement; | |
1654 int fullLineVisibleHeight = bottomPixel - fullLineTopPixel; | |
1655 // set top index to partially visible line if no line fully fits in | |
1656 // client area or if space is available but not used (the latter should | |
1657 // never happen because we use claimBottomFreeSpace) | |
1658 if (fullLineVisibleHeight < verticalIncrement) { | |
1659 topIndex--; | |
1660 } | |
1661 } else if (topIndex >= content.getLineCount()) { | |
1662 topIndex = content.getLineCount() - 1; | |
1663 } | |
1664 } | |
1665 } else { | |
1666 if (delta >= 0) { | |
1667 delta -= topIndexY; | |
1668 int lineIndex = topIndex; | |
1669 int lineCount = content.getLineCount(); | |
1670 while (lineIndex < lineCount) { | |
1671 if (delta <= 0) break; | |
1672 delta -= renderer.getLineHeight(lineIndex++); | |
1673 } | |
1674 if (lineIndex < lineCount && -delta + renderer.getLineHeight(lineIndex) <= clientAreaHeight - topMargin - bottomMargin) { | |
1675 topIndex = lineIndex; | |
1676 topIndexY = -delta; | |
1677 } else { | |
1678 topIndex = lineIndex - 1; | |
1679 topIndexY = -renderer.getLineHeight(topIndex) - delta; | |
1680 } | |
1681 } else { | |
1682 delta -= topIndexY; | |
1683 int lineIndex = topIndex; | |
1684 while (lineIndex > 0) { | |
1685 int lineHeight = renderer.getLineHeight(lineIndex - 1); | |
1686 if (delta + lineHeight > 0) break; | |
1687 delta += lineHeight; | |
1688 lineIndex--; | |
1689 } | |
1690 if (lineIndex is 0 || -delta + renderer.getLineHeight(lineIndex) <= clientAreaHeight - topMargin - bottomMargin) { | |
1691 topIndex = lineIndex; | |
1692 topIndexY = - delta; | |
1693 } else { | |
1694 topIndex = lineIndex - 1; | |
1695 topIndexY = - renderer.getLineHeight(topIndex) - delta; | |
1696 } | |
1697 } | |
1698 } | |
1699 if (topIndex !is oldTopIndex || oldTopIndexY !is topIndexY) { | |
1700 renderer.calculateClientArea(); | |
1701 setScrollBars(false); | |
1702 } | |
1703 } | |
1704 /** | |
1705 * Hides the scroll bars if widget is created in single line mode. | |
1706 */ | |
1707 static int checkStyle(int style) { | |
1708 if ((style & SWT.SINGLE) !is 0) { | |
1709 style &= ~(SWT.H_SCROLL | SWT.V_SCROLL | SWT.WRAP | SWT.MULTI); | |
1710 } else { | |
1711 style |= SWT.MULTI; | |
1712 if ((style & SWT.WRAP) !is 0) { | |
1713 style &= ~SWT.H_SCROLL; | |
1714 } | |
1715 } | |
1716 style |= SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND; | |
1717 return style; | |
1718 } | |
1719 /** | |
1720 * Scrolls down the text to use new space made available by a resize or by | |
1721 * deleted lines. | |
1722 */ | |
1723 void claimBottomFreeSpace() { | |
1724 int clientAreaHeight = this.clientAreaHeight - topMargin - bottomMargin; | |
1725 if (isFixedLineHeight()) { | |
1726 int lineHeight = renderer.getLineHeight(); | |
1727 int newVerticalOffset = Math.max(0, content.getLineCount() * lineHeight - clientAreaHeight); | |
1728 if (newVerticalOffset < getVerticalScrollOffset()) { | |
1729 scrollVertical(newVerticalOffset - getVerticalScrollOffset(), true); | |
1730 } | |
1731 } else { | |
1732 int bottomIndex = getPartialBottomIndex(); | |
1733 int height = getLinePixel(bottomIndex + 1); | |
1734 if (clientAreaHeight > height) { | |
1735 scrollVertical(-getAvailableHeightAbove(clientAreaHeight - height), true); | |
1736 } | |
1737 } | |
1738 } | |
1739 /** | |
1740 * Scrolls text to the right to use new space made available by a resize. | |
1741 */ | |
1742 void claimRightFreeSpace() { | |
1743 int newHorizontalOffset = Math.max(0, renderer.getWidth() - (clientAreaWidth - leftMargin - rightMargin)); | |
1744 if (newHorizontalOffset < horizontalScrollOffset) { | |
1745 // item is no longer drawn past the right border of the client area | |
1746 // align the right end of the item with the right border of the | |
1747 // client area (window is scrolled right). | |
1748 scrollHorizontal(newHorizontalOffset - horizontalScrollOffset, true); | |
1749 } | |
1750 } | |
1751 /** | |
1752 * Removes the widget selection. | |
1753 * | |
1754 * @param sendEvent a Selection event is sent when set to true and when the selection is actually reset. | |
1755 */ | |
1756 void clearSelection(bool sendEvent) { | |
1757 int selectionStart = selection.x; | |
1758 int selectionEnd = selection.y; | |
1759 resetSelection(); | |
1760 // redraw old selection, if any | |
1761 if (selectionEnd - selectionStart > 0) { | |
1762 int length = content.getCharCount(); | |
1763 // called internally to remove selection after text is removed | |
1764 // therefore make sure redraw range is valid. | |
1765 int redrawStart = Math.min(selectionStart, length); | |
1766 int redrawEnd = Math.min(selectionEnd, length); | |
1767 if (redrawEnd - redrawStart > 0) { | |
1768 internalRedrawRange(redrawStart, redrawEnd - redrawStart); | |
1769 } | |
1770 if (sendEvent) { | |
1771 sendSelectionEvent(); | |
1772 } | |
1773 } | |
1774 } | |
1775 public override Point computeSize (int wHint, int hHint, bool changed) { | |
1776 checkWidget(); | |
1777 int lineCount = (getStyle() & SWT.SINGLE) !is 0 ? 1 : content.getLineCount(); | |
1778 int width = 0; | |
1779 int height = 0; | |
1780 if (wHint is SWT.DEFAULT || hHint is SWT.DEFAULT) { | |
1781 Display display = getDisplay(); | |
1782 int maxHeight = display.getClientArea().height; | |
1783 for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) { | |
1784 TextLayout layout = renderer.getTextLayout(lineIndex); | |
1785 int wrapWidth = layout.getWidth(); | |
1786 if (wordWrap) layout.setWidth(wHint is 0 ? 1 : wHint); | |
1787 Rectangle rect = layout.getBounds(); | |
1788 height += rect.height; | |
1789 width = Math.max(width, rect.width); | |
1790 layout.setWidth(wrapWidth); | |
1791 renderer.disposeTextLayout(layout); | |
1792 if (isFixedLineHeight() && height > maxHeight) break; | |
1793 } | |
1794 if (isFixedLineHeight()) { | |
1795 height = lineCount * renderer.getLineHeight(); | |
1796 } | |
1797 } | |
1798 // Use default values if no text is defined. | |
1799 if (width is 0) width = DEFAULT_WIDTH; | |
1800 if (height is 0) height = DEFAULT_HEIGHT; | |
1801 if (wHint !is SWT.DEFAULT) width = wHint; | |
1802 if (hHint !is SWT.DEFAULT) height = hHint; | |
1803 int wTrim = leftMargin + rightMargin + getCaretWidth(); | |
1804 int hTrim = topMargin + bottomMargin; | |
1805 Rectangle rect = computeTrim(0, 0, width + wTrim, height + hTrim); | |
1806 return new Point (rect.width, rect.height); | |
1807 } | |
1808 /** | |
1809 * Copies the selected text to the <code>DND.CLIPBOARD</code> clipboard. | |
1810 * <p> | |
1811 * The text will be put on the clipboard in plain text format and RTF format. | |
1812 * The <code>DND.CLIPBOARD</code> clipboard is used for data that is | |
1813 * transferred by keyboard accelerator (such as Ctrl+C/Ctrl+V) or | |
1814 * by menu action. | |
1815 * </p> | |
1816 * | |
1817 * @exception SWTException <ul> | |
1818 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1819 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1820 * </ul> | |
1821 */ | |
1822 public void copy() { | |
1823 checkWidget(); | |
1824 copy(DND.CLIPBOARD); | |
1825 } | |
1826 /** | |
1827 * Copies the selected text to the specified clipboard. The text will be put in the | |
1828 * clipboard in plain text format and RTF format. | |
1829 * <p> | |
1830 * The clipboardType is one of the clipboard constants defined in class | |
1831 * <code>DND</code>. The <code>DND.CLIPBOARD</code> clipboard is | |
1832 * used for data that is transferred by keyboard accelerator (such as Ctrl+C/Ctrl+V) | |
1833 * or by menu action. The <code>DND.SELECTION_CLIPBOARD</code> | |
1834 * clipboard is used for data that is transferred by selecting text and pasting | |
1835 * with the middle mouse button. | |
1836 * </p> | |
1837 * | |
1838 * @param clipboardType indicates the type of clipboard | |
1839 * | |
1840 * @exception SWTException <ul> | |
1841 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1842 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1843 * </ul> | |
1844 * | |
1845 * @since 3.1 | |
1846 */ | |
1847 public void copy(int clipboardType) { | |
1848 checkWidget(); | |
1849 if (clipboardType !is DND.CLIPBOARD && clipboardType !is DND.SELECTION_CLIPBOARD) return; | |
1850 int length = selection.y - selection.x; | |
1851 if (length > 0) { | |
1852 try { | |
1853 setClipboardContent(selection.x, length, clipboardType); | |
1854 } catch (SWTError error) { | |
1855 // Copy to clipboard failed. This happens when another application | |
1856 // is accessing the clipboard while we copy. Ignore the error. | |
1857 // Fixes 1GDQAVN | |
1858 // Rethrow all other errors. Fixes bug 17578. | |
1859 if (error.code !is DND.ERROR_CANNOT_SET_CLIPBOARD) { | |
1860 throw error; | |
1861 } | |
1862 } | |
1863 } | |
1864 } | |
1865 /** | |
1866 * Returns the alignment of the widget. | |
1867 * | |
1868 * @return the alignment | |
1869 * | |
1870 * @exception SWTException <ul> | |
1871 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1872 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1873 * </ul> | |
1874 * | |
1875 * @see #getLineAlignment(int) | |
1876 * | |
1877 * @since 3.2 | |
1878 */ | |
1879 public int getAlignment() { | |
1880 checkWidget(); | |
1881 return alignment; | |
1882 } | |
1883 int getAvailableHeightAbove(int height) { | |
1884 int maxHeight = verticalScrollOffset; | |
1885 if (maxHeight is -1) { | |
1886 int lineIndex = topIndex - 1; | |
1887 maxHeight = -topIndexY; | |
1888 if (topIndexY > 0) { | |
1889 maxHeight += renderer.getLineHeight(lineIndex--); | |
1890 } | |
1891 while (height > maxHeight && lineIndex >= 0) { | |
1892 maxHeight += renderer.getLineHeight(lineIndex--); | |
1893 } | |
1894 } | |
1895 return Math.min(height, maxHeight); | |
1896 } | |
1897 int getAvailableHeightBellow(int height) { | |
1898 int partialBottomIndex = getPartialBottomIndex(); | |
1899 int topY = getLinePixel(partialBottomIndex); | |
1900 int lineHeight = renderer.getLineHeight(partialBottomIndex); | |
1901 int availableHeight = 0; | |
1902 int clientAreaHeight = this.clientAreaHeight - topMargin - bottomMargin; | |
1903 if (topY + lineHeight > clientAreaHeight) { | |
1904 availableHeight = lineHeight - (clientAreaHeight - topY); | |
1905 } | |
1906 int lineIndex = partialBottomIndex + 1; | |
1907 int lineCount = content.getLineCount(); | |
1908 while (height > availableHeight && lineIndex < lineCount) { | |
1909 availableHeight += renderer.getLineHeight(lineIndex++); | |
1910 } | |
1911 return Math.min(height, availableHeight); | |
1912 } | |
1913 /** | |
1914 * Returns a string that uses only the line delimiter specified by the | |
1915 * StyledTextContent implementation. | |
1916 * <p> | |
1917 * Returns only the first line if the widget has the SWT.SINGLE style. | |
1918 * </p> | |
1919 * | |
1920 * @param text the text that may have line delimiters that don't | |
1921 * match the model line delimiter. Possible line delimiters | |
1922 * are CR ('\r'), LF ('\n'), CR/LF ("\r\n") | |
1923 * @return the converted text that only uses the line delimiter | |
1924 * specified by the model. Returns only the first line if the widget | |
1925 * has the SWT.SINGLE style. | |
1926 */ | |
1927 String getModelDelimitedText(String text) { | |
1928 int length = text.length; | |
1929 if (length is 0) { | |
1930 return text; | |
1931 } | |
1932 int crIndex = 0; | |
1933 int lfIndex = 0; | |
1934 int i = 0; | |
1935 StringBuffer convertedText = new StringBuffer(length); | |
1936 String delimiter = getLineDelimiter(); | |
1937 while (i < length) { | |
1938 if (crIndex !is -1) { | |
1939 crIndex = text.indexOf (SWT.CR, i); | |
1940 } | |
1941 if (lfIndex !is -1) { | |
1942 lfIndex = text.indexOf (SWT.LF, i); | |
1943 } | |
1944 if (lfIndex is -1 && crIndex is -1) { // no more line breaks? | |
1945 break; | |
1946 } else if ((crIndex < lfIndex && crIndex !is -1) || lfIndex is -1) { | |
1947 convertedText.append(text.substring(i, crIndex)); | |
1948 if (lfIndex is crIndex + 1) { // CR/LF combination? | |
1949 i = lfIndex + 1; | |
1950 } else { | |
1951 i = crIndex + 1; | |
1952 } | |
1953 } else { // LF occurs before CR! | |
1954 convertedText.append(text.substring(i, lfIndex)); | |
1955 i = lfIndex + 1; | |
1956 } | |
1957 if (isSingleLine()) { | |
1958 break; | |
1959 } | |
1960 convertedText.append(delimiter); | |
1961 } | |
1962 // copy remaining text if any and if not in single line mode or no | |
1963 // text copied thus far (because there only is one line) | |
1964 if (i < length && (!isSingleLine() || convertedText.length() is 0)) { | |
1965 convertedText.append(text.substring(i)); | |
1966 } | |
1967 return convertedText.toString(); | |
1968 } | |
1969 bool checkDragDetect(Event event) { | |
1970 if (!isListening(SWT.DragDetect)) return false; | |
1971 if (IS_MOTIF) { | |
1972 if (event.button !is 2) return false; | |
1973 } else { | |
1974 if (event.button !is 1) return false; | |
1975 } | |
1976 if (selection.x is selection.y) return false; | |
1977 int offset = getOffsetAtPoint(event.x, event.y, null, true); | |
1978 if (selection.x <= offset && offset < selection.y) { | |
1979 return dragDetect(event); | |
1980 } | |
1981 return false; | |
1982 } | |
1983 /** | |
1984 * Creates default key bindings. | |
1985 */ | |
1986 void createKeyBindings() { | |
1987 int nextKey = isMirrored() ? SWT.ARROW_LEFT : SWT.ARROW_RIGHT; | |
1988 int previousKey = isMirrored() ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT; | |
1989 | |
1990 // Navigation | |
1991 setKeyBinding(SWT.ARROW_UP, ST.LINE_UP); | |
1992 setKeyBinding(SWT.ARROW_DOWN, ST.LINE_DOWN); | |
1993 if (IS_CARBON) { | |
1994 setKeyBinding(previousKey | SWT.MOD1, ST.LINE_START); | |
1995 setKeyBinding(nextKey | SWT.MOD1, ST.LINE_END); | |
1996 setKeyBinding(SWT.HOME, ST.TEXT_START); | |
1997 setKeyBinding(SWT.END, ST.TEXT_END); | |
1998 setKeyBinding(SWT.ARROW_UP | SWT.MOD1, ST.TEXT_START); | |
1999 setKeyBinding(SWT.ARROW_DOWN | SWT.MOD1, ST.TEXT_END); | |
2000 setKeyBinding(nextKey | SWT.MOD3, ST.WORD_NEXT); | |
2001 setKeyBinding(previousKey | SWT.MOD3, ST.WORD_PREVIOUS); | |
2002 } else { | |
2003 setKeyBinding(SWT.HOME, ST.LINE_START); | |
2004 setKeyBinding(SWT.END, ST.LINE_END); | |
2005 setKeyBinding(SWT.HOME | SWT.MOD1, ST.TEXT_START); | |
2006 setKeyBinding(SWT.END | SWT.MOD1, ST.TEXT_END); | |
2007 setKeyBinding(nextKey | SWT.MOD1, ST.WORD_NEXT); | |
2008 setKeyBinding(previousKey | SWT.MOD1, ST.WORD_PREVIOUS); | |
2009 } | |
2010 setKeyBinding(SWT.PAGE_UP, ST.PAGE_UP); | |
2011 setKeyBinding(SWT.PAGE_DOWN, ST.PAGE_DOWN); | |
2012 setKeyBinding(SWT.PAGE_UP | SWT.MOD1, ST.WINDOW_START); | |
2013 setKeyBinding(SWT.PAGE_DOWN | SWT.MOD1, ST.WINDOW_END); | |
2014 setKeyBinding(nextKey, ST.COLUMN_NEXT); | |
2015 setKeyBinding(previousKey, ST.COLUMN_PREVIOUS); | |
2016 | |
2017 // Selection | |
2018 setKeyBinding(SWT.ARROW_UP | SWT.MOD2, ST.SELECT_LINE_UP); | |
2019 setKeyBinding(SWT.ARROW_DOWN | SWT.MOD2, ST.SELECT_LINE_DOWN); | |
2020 if (IS_CARBON) { | |
2021 setKeyBinding(previousKey | SWT.MOD1 | SWT.MOD2, ST.SELECT_LINE_START); | |
2022 setKeyBinding(nextKey | SWT.MOD1 | SWT.MOD2, ST.SELECT_LINE_END); | |
2023 setKeyBinding(SWT.HOME | SWT.MOD2, ST.SELECT_TEXT_START); | |
2024 setKeyBinding(SWT.END | SWT.MOD2, ST.SELECT_TEXT_END); | |
2025 setKeyBinding(SWT.ARROW_UP | SWT.MOD1 | SWT.MOD2, ST.SELECT_TEXT_START); | |
2026 setKeyBinding(SWT.ARROW_DOWN | SWT.MOD1 | SWT.MOD2, ST.SELECT_TEXT_END); | |
2027 setKeyBinding(nextKey | SWT.MOD2 | SWT.MOD3, ST.SELECT_WORD_NEXT); | |
2028 setKeyBinding(previousKey | SWT.MOD2 | SWT.MOD3, ST.SELECT_WORD_PREVIOUS); | |
2029 } else { | |
2030 setKeyBinding(SWT.HOME | SWT.MOD2, ST.SELECT_LINE_START); | |
2031 setKeyBinding(SWT.END | SWT.MOD2, ST.SELECT_LINE_END); | |
2032 setKeyBinding(SWT.HOME | SWT.MOD1 | SWT.MOD2, ST.SELECT_TEXT_START); | |
2033 setKeyBinding(SWT.END | SWT.MOD1 | SWT.MOD2, ST.SELECT_TEXT_END); | |
2034 setKeyBinding(nextKey | SWT.MOD1 | SWT.MOD2, ST.SELECT_WORD_NEXT); | |
2035 setKeyBinding(previousKey | SWT.MOD1 | SWT.MOD2, ST.SELECT_WORD_PREVIOUS); | |
2036 } | |
2037 setKeyBinding(SWT.PAGE_UP | SWT.MOD2, ST.SELECT_PAGE_UP); | |
2038 setKeyBinding(SWT.PAGE_DOWN | SWT.MOD2, ST.SELECT_PAGE_DOWN); | |
2039 setKeyBinding(SWT.PAGE_UP | SWT.MOD1 | SWT.MOD2, ST.SELECT_WINDOW_START); | |
2040 setKeyBinding(SWT.PAGE_DOWN | SWT.MOD1 | SWT.MOD2, ST.SELECT_WINDOW_END); | |
2041 setKeyBinding(nextKey | SWT.MOD2, ST.SELECT_COLUMN_NEXT); | |
2042 setKeyBinding(previousKey | SWT.MOD2, ST.SELECT_COLUMN_PREVIOUS); | |
2043 | |
2044 // Modification | |
2045 // Cut, Copy, Paste | |
2046 setKeyBinding('X' | SWT.MOD1, ST.CUT); | |
2047 setKeyBinding('C' | SWT.MOD1, ST.COPY); | |
2048 setKeyBinding('V' | SWT.MOD1, ST.PASTE); | |
2049 if (IS_CARBON) { | |
2050 setKeyBinding(SWT.DEL | SWT.MOD2, ST.DELETE_NEXT); | |
2051 setKeyBinding(SWT.BS | SWT.MOD3, ST.DELETE_WORD_PREVIOUS); | |
2052 setKeyBinding(SWT.DEL | SWT.MOD3, ST.DELETE_WORD_NEXT); | |
2053 } else { | |
2054 // Cut, Copy, Paste Wordstar style | |
2055 setKeyBinding(SWT.DEL | SWT.MOD2, ST.CUT); | |
2056 setKeyBinding(SWT.INSERT | SWT.MOD1, ST.COPY); | |
2057 setKeyBinding(SWT.INSERT | SWT.MOD2, ST.PASTE); | |
2058 } | |
2059 setKeyBinding(SWT.BS | SWT.MOD2, ST.DELETE_PREVIOUS); | |
2060 setKeyBinding(SWT.BS, ST.DELETE_PREVIOUS); | |
2061 setKeyBinding(SWT.DEL, ST.DELETE_NEXT); | |
2062 setKeyBinding(SWT.BS | SWT.MOD1, ST.DELETE_WORD_PREVIOUS); | |
2063 setKeyBinding(SWT.DEL | SWT.MOD1, ST.DELETE_WORD_NEXT); | |
2064 | |
2065 // Miscellaneous | |
2066 setKeyBinding(SWT.INSERT, ST.TOGGLE_OVERWRITE); | |
2067 } | |
2068 /** | |
2069 * Create the bitmaps to use for the caret in bidi mode. This | |
2070 * method only needs to be called upon widget creation and when the | |
2071 * font changes (the caret bitmap height needs to match font height). | |
2072 */ | |
2073 void createCaretBitmaps() { | |
2074 int caretWidth = BIDI_CARET_WIDTH; | |
2075 Display display = getDisplay(); | |
2076 if (leftCaretBitmap !is null) { | |
2077 if (defaultCaret !is null && leftCaretBitmap==/*eq*/defaultCaret.getImage()) { | |
2078 defaultCaret.setImage(null); | |
2079 } | |
2080 leftCaretBitmap.dispose(); | |
2081 } | |
2082 int lineHeight = renderer.getLineHeight(); | |
2083 leftCaretBitmap = new Image(display, caretWidth, lineHeight); | |
2084 GC gc = new GC (leftCaretBitmap); | |
2085 gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); | |
2086 gc.fillRectangle(0, 0, caretWidth, lineHeight); | |
2087 gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); | |
2088 gc.drawLine(0,0,0,lineHeight); | |
2089 gc.drawLine(0,0,caretWidth-1,0); | |
2090 gc.drawLine(0,1,1,1); | |
2091 gc.dispose(); | |
2092 | |
2093 if (rightCaretBitmap !is null) { | |
2094 if (defaultCaret !is null && rightCaretBitmap==/*eq*/defaultCaret.getImage()) { | |
2095 defaultCaret.setImage(null); | |
2096 } | |
2097 rightCaretBitmap.dispose(); | |
2098 } | |
2099 rightCaretBitmap = new Image(display, caretWidth, lineHeight); | |
2100 gc = new GC (rightCaretBitmap); | |
2101 gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); | |
2102 gc.fillRectangle(0, 0, caretWidth, lineHeight); | |
2103 gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); | |
2104 gc.drawLine(caretWidth-1,0,caretWidth-1,lineHeight); | |
2105 gc.drawLine(0,0,caretWidth-1,0); | |
2106 gc.drawLine(caretWidth-1,1,1,1); | |
2107 gc.dispose(); | |
2108 } | |
2109 /** | |
2110 * Moves the selected text to the clipboard. The text will be put in the | |
2111 * clipboard in plain text format and RTF format. | |
2112 * | |
2113 * @exception SWTException <ul> | |
2114 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
2115 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
2116 * </ul> | |
2117 */ | |
2118 public void cut(){ | |
2119 checkWidget(); | |
2120 int length = selection.y - selection.x; | |
2121 if (length > 0) { | |
2122 try { | |
2123 setClipboardContent(selection.x, length, DND.CLIPBOARD); | |
2124 } catch (SWTError error) { | |
2125 // Copy to clipboard failed. This happens when another application | |
2126 // is accessing the clipboard while we copy. Ignore the error. | |
2127 // Fixes 1GDQAVN | |
2128 // Rethrow all other errors. Fixes bug 17578. | |
2129 if (error.code !is DND.ERROR_CANNOT_SET_CLIPBOARD) { | |
2130 throw error; | |
2131 } | |
2132 // Abort cut operation if copy to clipboard fails. | |
2133 // Fixes bug 21030. | |
2134 return; | |
2135 } | |
2136 doDelete(); | |
2137 } | |
2138 } | |
2139 /** | |
2140 * A mouse move event has occurred. See if we should start autoscrolling. If | |
2141 * the move position is outside of the client area, initiate autoscrolling. | |
2142 * Otherwise, we've moved back into the widget so end autoscrolling. | |
2143 */ | |
2144 void doAutoScroll(Event event) { | |
2145 if (event.y > clientAreaHeight) { | |
2146 doAutoScroll(SWT.DOWN, event.y - clientAreaHeight); | |
2147 } else if (event.y < 0) { | |
2148 doAutoScroll(SWT.UP, -event.y); | |
2149 } else if (event.x < leftMargin && !wordWrap) { | |
2150 doAutoScroll(ST.COLUMN_PREVIOUS, leftMargin - event.x); | |
2151 } else if (event.x > clientAreaWidth - leftMargin - rightMargin && !wordWrap) { | |
2152 doAutoScroll(ST.COLUMN_NEXT, event.x - (clientAreaWidth - leftMargin - rightMargin)); | |
2153 } else { | |
2154 endAutoScroll(); | |
2155 } | |
2156 } | |
2157 /** | |
2158 * Initiates autoscrolling. | |
2159 * | |
2160 * @param direction SWT.UP, SWT.DOWN, SWT.COLUMN_NEXT, SWT.COLUMN_PREVIOUS | |
2161 */ | |
2162 void doAutoScroll(int direction, int distance) { | |
2163 autoScrollDistance = distance; | |
2164 // If we're already autoscrolling in the given direction do nothing | |
2165 if (autoScrollDirection is direction) { | |
2166 return; | |
2167 } | |
2168 | |
2169 Runnable timer = null; | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
2170 Display disp = getDisplay(); |
25 | 2171 // Set a timer that will simulate the user pressing and holding |
2172 // down a cursor key (i.e., arrowUp, arrowDown). | |
2173 if (direction is SWT.UP) { | |
2174 timer = new class(disp) Runnable { | |
2175 Display display; | |
2176 this( Display d ){ this.display = d; } | |
2177 public void run() { | |
2178 if (autoScrollDirection is SWT.UP) { | |
2179 doSelectionPageUp(autoScrollDistance); | |
2180 display.timerExec(V_SCROLL_RATE, this); | |
2181 } | |
2182 } | |
2183 }; | |
2184 autoScrollDirection = direction; | |
2185 display.timerExec(V_SCROLL_RATE, timer); | |
2186 } else if (direction is SWT.DOWN) { | |
2187 timer = new class(disp) Runnable { | |
2188 Display display; | |
2189 this( Display d ){ this.display = d; } | |
2190 public void run() { | |
2191 if (autoScrollDirection is SWT.DOWN) { | |
2192 doSelectionPageDown(autoScrollDistance); | |
2193 display.timerExec(V_SCROLL_RATE, this); | |
2194 } | |
2195 } | |
2196 }; | |
2197 autoScrollDirection = direction; | |
2198 display.timerExec(V_SCROLL_RATE, timer); | |
2199 } else if (direction is ST.COLUMN_NEXT) { | |
2200 timer = new class(disp) Runnable { | |
2201 Display display; | |
2202 this( Display d ){ this.display = d; } | |
2203 public void run() { | |
2204 if (autoScrollDirection is ST.COLUMN_NEXT) { | |
2205 doVisualNext(); | |
2206 setMouseWordSelectionAnchor(); | |
2207 doMouseSelection(); | |
2208 display.timerExec(H_SCROLL_RATE, this); | |
2209 } | |
2210 } | |
2211 }; | |
2212 autoScrollDirection = direction; | |
2213 display.timerExec(H_SCROLL_RATE, timer); | |
2214 } else if (direction is ST.COLUMN_PREVIOUS) { | |
2215 timer = new class(disp) Runnable { | |
2216 Display display; | |
2217 this( Display d ){ this.display = d; } | |
2218 public void run() { | |
2219 if (autoScrollDirection is ST.COLUMN_PREVIOUS) { | |
2220 doVisualPrevious(); | |
2221 setMouseWordSelectionAnchor(); | |
2222 doMouseSelection(); | |
2223 display.timerExec(H_SCROLL_RATE, this); | |
2224 } | |
2225 } | |
2226 }; | |
2227 autoScrollDirection = direction; | |
2228 display.timerExec(H_SCROLL_RATE, timer); | |
2229 } | |
2230 } | |
2231 /** | |
2232 * Deletes the previous character. Delete the selected text if any. | |
2233 * Move the caret in front of the deleted text. | |
2234 */ | |
2235 void doBackspace() { | |
2236 Event event = new Event(); | |
2237 event.text = ""; | |
2238 if (selection.x !is selection.y) { | |
2239 event.start = selection.x; | |
2240 event.end = selection.y; | |
2241 sendKeyEvent(event); | |
2242 } else if (caretOffset > 0) { | |
2243 int lineIndex = content.getLineAtOffset(caretOffset); | |
2244 int lineOffset = content.getOffsetAtLine(lineIndex); | |
2245 if (caretOffset is lineOffset) { | |
2246 // SWT: on line start, delete line break | |
2247 lineOffset = content.getOffsetAtLine(lineIndex - 1); | |
2248 event.start = lineOffset + content.getLine(lineIndex - 1).length; | |
2249 event.end = caretOffset; | |
2250 } else { | |
2251 TextLayout layout = renderer.getTextLayout(lineIndex); | |
2252 int start = layout.getPreviousOffset(caretOffset - lineOffset, SWT.MOVEMENT_CLUSTER); | |
2253 renderer.disposeTextLayout(layout); | |
2254 event.start = start + lineOffset; | |
2255 event.end = caretOffset; | |
2256 } | |
2257 sendKeyEvent(event); | |
2258 } | |
2259 } | |
2260 /** | |
2261 * Replaces the selection with the character or insert the character at the | |
2262 * current caret position if no selection exists. | |
2263 * <p> | |
2264 * If a carriage return was typed replace it with the line break character | |
2265 * used by the widget on this platform. | |
2266 * </p> | |
2267 * | |
2268 * @param key the character typed by the user | |
2269 */ | |
2270 void doContent(dchar key) { | |
2271 Event event = new Event(); | |
2272 event.start = selection.x; | |
2273 event.end = selection.y; | |
2274 // replace a CR line break with the widget line break | |
2275 // CR does not make sense on Windows since most (all?) applications | |
2276 // don't recognize CR as a line break. | |
2277 if (key is SWT.CR || key is SWT.LF) { | |
2278 if (!isSingleLine()) { | |
2279 event.text = getLineDelimiter(); | |
2280 } | |
2281 } else if (selection.x is selection.y && overwrite && key !is TAB) { | |
2282 // no selection and overwrite mode is on and the typed key is not a | |
2283 // tab character (tabs are always inserted without overwriting)? | |
2284 int lineIndex = content.getLineAtOffset(event.end); | |
2285 int lineOffset = content.getOffsetAtLine(lineIndex); | |
2286 String line = content.getLine(lineIndex); | |
2287 // replace character at caret offset if the caret is not at the | |
2288 // end of the line | |
2289 if (event.end < lineOffset + line.length) { | |
2290 event.end+=dcharToString( key ).length; | |
2291 } | |
2292 event.text = dcharToString( key ); | |
2293 } else { | |
2294 event.text = dcharToString( key ); | |
2295 } | |
2296 if (event.text !is null) { | |
2297 if (textLimit > 0 && content.getCharCount() - (event.end - event.start) >= textLimit) { | |
2298 return; | |
2299 } | |
2300 sendKeyEvent(event); | |
2301 } | |
2302 } | |
2303 /** | |
2304 * Moves the caret after the last character of the widget content. | |
2305 */ | |
2306 void doContentEnd() { | |
2307 // place caret at end of first line if receiver is in single | |
2308 // line mode. fixes 4820. | |
2309 if (isSingleLine()) { | |
2310 doLineEnd(); | |
2311 } else { | |
2312 int length = content.getCharCount(); | |
2313 if (caretOffset < length) { | |
2314 caretOffset = length; | |
2315 showCaret(); | |
2316 } | |
2317 } | |
2318 } | |
2319 /** | |
2320 * Moves the caret in front of the first character of the widget content. | |
2321 */ | |
2322 void doContentStart() { | |
2323 if (caretOffset > 0) { | |
2324 caretOffset = 0; | |
2325 showCaret(); | |
2326 } | |
2327 } | |
2328 /** | |
2329 * Moves the caret to the start of the selection if a selection exists. | |
2330 * Otherwise, if no selection exists move the cursor according to the | |
2331 * cursor selection rules. | |
2332 * | |
2333 * @see #doSelectionCursorPrevious | |
2334 */ | |
2335 void doCursorPrevious() { | |
2336 if (selection.y - selection.x > 0) { | |
2337 caretOffset = selection.x; | |
2338 caretAlignment = OFFSET_LEADING; | |
2339 showCaret(); | |
2340 } else { | |
2341 doSelectionCursorPrevious(); | |
2342 } | |
2343 } | |
2344 /** | |
2345 * Moves the caret to the end of the selection if a selection exists. | |
2346 * Otherwise, if no selection exists move the cursor according to the | |
2347 * cursor selection rules. | |
2348 * | |
2349 * @see #doSelectionCursorNext | |
2350 */ | |
2351 void doCursorNext() { | |
2352 if (selection.y - selection.x > 0) { | |
2353 caretOffset = selection.y; | |
2354 caretAlignment = PREVIOUS_OFFSET_TRAILING; | |
2355 showCaret(); | |
2356 } else { | |
2357 doSelectionCursorNext(); | |
2358 } | |
2359 } | |
2360 /** | |
2361 * Deletes the next character. Delete the selected text if any. | |
2362 */ | |
2363 void doDelete() { | |
2364 Event event = new Event(); | |
2365 event.text = ""; | |
2366 if (selection.x !is selection.y) { | |
2367 event.start = selection.x; | |
2368 event.end = selection.y; | |
2369 sendKeyEvent(event); | |
2370 } else if (caretOffset < content.getCharCount()) { | |
2371 int line = content.getLineAtOffset(caretOffset); | |
2372 int lineOffset = content.getOffsetAtLine(line); | |
2373 int lineLength = content.getLine(line).length; | |
2374 if (caretOffset is lineOffset + lineLength) { | |
2375 event.start = caretOffset; | |
2376 event.end = content.getOffsetAtLine(line + 1); | |
2377 } else { | |
2378 event.start = caretOffset; | |
2379 event.end = getClusterNext(caretOffset, line); | |
2380 } | |
2381 sendKeyEvent(event); | |
2382 } | |
2383 } | |
2384 /** | |
2385 * Deletes the next word. | |
2386 */ | |
2387 void doDeleteWordNext() { | |
2388 if (selection.x !is selection.y) { | |
2389 // if a selection exists, treat the as if | |
2390 // only the delete key was pressed | |
2391 doDelete(); | |
2392 } else { | |
2393 Event event = new Event(); | |
2394 event.text = ""; | |
2395 event.start = caretOffset; | |
2396 event.end = getWordNext(caretOffset, SWT.MOVEMENT_WORD); | |
2397 sendKeyEvent(event); | |
2398 } | |
2399 } | |
2400 /** | |
2401 * Deletes the previous word. | |
2402 */ | |
2403 void doDeleteWordPrevious() { | |
2404 if (selection.x !is selection.y) { | |
2405 // if a selection exists, treat as if | |
2406 // only the backspace key was pressed | |
2407 doBackspace(); | |
2408 } else { | |
2409 Event event = new Event(); | |
2410 event.text = ""; | |
2411 event.start = getWordPrevious(caretOffset, SWT.MOVEMENT_WORD); | |
2412 event.end = caretOffset; | |
2413 sendKeyEvent(event); | |
2414 } | |
2415 } | |
2416 /** | |
2417 * Moves the caret one line down and to the same character offset relative | |
2418 * to the beginning of the line. Move the caret to the end of the new line | |
2419 * if the new line is shorter than the character offset. | |
2420 */ | |
2421 void doLineDown(bool select) { | |
2422 int caretLine = getCaretLine(); | |
2423 int lineCount = content.getLineCount(); | |
2424 int y = 0; | |
2425 bool lastLine = false; | |
2426 if (wordWrap) { | |
2427 int lineOffset = content.getOffsetAtLine(caretLine); | |
2428 int offsetInLine = caretOffset - lineOffset; | |
2429 TextLayout layout = renderer.getTextLayout(caretLine); | |
2430 int lineIndex = getVisualLineIndex(layout, offsetInLine); | |
2431 int layoutLineCount = layout.getLineCount(); | |
2432 if (lineIndex is layoutLineCount - 1) { | |
2433 lastLine = caretLine is lineCount - 1; | |
2434 caretLine++; | |
2435 } else { | |
2436 y = layout.getLineBounds(lineIndex + 1).y; | |
2437 } | |
2438 renderer.disposeTextLayout(layout); | |
2439 } else { | |
2440 lastLine = caretLine is lineCount - 1; | |
2441 caretLine++; | |
2442 } | |
2443 if (lastLine) { | |
2444 if (select) caretOffset = content.getCharCount(); | |
2445 } else { | |
2446 caretOffset = getOffsetAtPoint(columnX, y, caretLine); | |
2447 } | |
2448 int oldColumnX = columnX; | |
2449 int oldHScrollOffset = horizontalScrollOffset; | |
2450 if (select) { | |
2451 setMouseWordSelectionAnchor(); | |
2452 // select first and then scroll to reduce flash when key | |
2453 // repeat scrolls lots of lines | |
2454 doSelection(ST.COLUMN_NEXT); | |
2455 } | |
2456 showCaret(); | |
2457 int hScrollChange = oldHScrollOffset - horizontalScrollOffset; | |
2458 columnX = oldColumnX + hScrollChange; | |
2459 } | |
2460 /** | |
2461 * Moves the caret to the end of the line. | |
2462 */ | |
2463 void doLineEnd() { | |
2464 int caretLine = getCaretLine(); | |
2465 int lineOffset = content.getOffsetAtLine(caretLine); | |
2466 int lineEndOffset; | |
2467 if (wordWrap) { | |
2468 TextLayout layout = renderer.getTextLayout(caretLine); | |
2469 int offsetInLine = caretOffset - lineOffset; | |
2470 int lineIndex = getVisualLineIndex(layout, offsetInLine); | |
2471 int[] offsets = layout.getLineOffsets(); | |
2472 lineEndOffset = lineOffset + offsets[lineIndex + 1]; | |
2473 renderer.disposeTextLayout(layout); | |
2474 } else { | |
2475 int lineLength = content.getLine(caretLine).length; | |
2476 lineEndOffset = lineOffset + lineLength; | |
2477 } | |
2478 if (caretOffset < lineEndOffset) { | |
2479 caretOffset = lineEndOffset; | |
2480 caretAlignment = PREVIOUS_OFFSET_TRAILING; | |
2481 showCaret(); | |
2482 } | |
2483 } | |
2484 /** | |
2485 * Moves the caret to the beginning of the line. | |
2486 */ | |
2487 void doLineStart() { | |
2488 int caretLine = getCaretLine(); | |
2489 int lineOffset = content.getOffsetAtLine(caretLine); | |
2490 if (wordWrap) { | |
2491 TextLayout layout = renderer.getTextLayout(caretLine); | |
2492 int offsetInLine = caretOffset - lineOffset; | |
2493 int lineIndex = getVisualLineIndex(layout, offsetInLine); | |
2494 int[] offsets = layout.getLineOffsets(); | |
2495 lineOffset += offsets[lineIndex]; | |
2496 renderer.disposeTextLayout(layout); | |
2497 } | |
2498 if (caretOffset > lineOffset) { | |
2499 caretOffset = lineOffset; | |
2500 caretAlignment = OFFSET_LEADING; | |
2501 showCaret(); | |
2502 } | |
2503 } | |
2504 /** | |
2505 * Moves the caret one line up and to the same character offset relative | |
2506 * to the beginning of the line. Move the caret to the end of the new line | |
2507 * if the new line is shorter than the character offset. | |
2508 */ | |
2509 void doLineUp(bool select) { | |
2510 int caretLine = getCaretLine(), y = 0; | |
2511 bool firstLine = false; | |
2512 if (wordWrap) { | |
2513 int lineOffset = content.getOffsetAtLine(caretLine); | |
2514 int offsetInLine = caretOffset - lineOffset; | |
2515 TextLayout layout = renderer.getTextLayout(caretLine); | |
2516 int lineIndex = getVisualLineIndex(layout, offsetInLine); | |
2517 if (lineIndex is 0) { | |
2518 firstLine = caretLine is 0; | |
2519 if (!firstLine) { | |
2520 caretLine--; | |
2521 y = renderer.getLineHeight(caretLine) - 1; | |
2522 } | |
2523 } else { | |
2524 y = layout.getLineBounds(lineIndex - 1).y; | |
2525 } | |
2526 renderer.disposeTextLayout(layout); | |
2527 } else { | |
2528 firstLine = caretLine is 0; | |
2529 caretLine--; | |
2530 } | |
2531 if (firstLine) { | |
2532 if (select) caretOffset = 0; | |
2533 } else { | |
2534 caretOffset = getOffsetAtPoint(columnX, y, caretLine); | |
2535 } | |
2536 int oldColumnX = columnX; | |
2537 int oldHScrollOffset = horizontalScrollOffset; | |
2538 if (select) setMouseWordSelectionAnchor(); | |
2539 showCaret(); | |
2540 if (select) doSelection(ST.COLUMN_PREVIOUS); | |
2541 int hScrollChange = oldHScrollOffset - horizontalScrollOffset; | |
2542 columnX = oldColumnX + hScrollChange; | |
2543 } | |
2544 /** | |
2545 * Moves the caret to the specified location. | |
2546 * | |
2547 * @param x x location of the new caret position | |
2548 * @param y y location of the new caret position | |
2549 * @param select the location change is a selection operation. | |
2550 * include the line delimiter in the selection | |
2551 */ | |
2552 void doMouseLocationChange(int x, int y, bool select) { | |
2553 int line = getLineIndex(y); | |
2554 | |
2555 updateCaretDirection = true; | |
2556 // allow caret to be placed below first line only if receiver is | |
2557 // not in single line mode. fixes 4820. | |
2558 if (line < 0 || (isSingleLine() && line > 0)) { | |
2559 return; | |
2560 } | |
2561 int oldCaretAlignment = caretAlignment; | |
2562 int newCaretOffset = getOffsetAtPoint(x, y); | |
2563 | |
2564 if (doubleClickEnabled && clickCount > 1) { | |
2565 newCaretOffset = doMouseWordSelect(x, newCaretOffset, line); | |
2566 } | |
2567 | |
2568 int newCaretLine = content.getLineAtOffset(newCaretOffset); | |
2569 | |
2570 // Is the mouse within the left client area border or on | |
2571 // a different line? If not the autoscroll selection | |
2572 // could be incorrectly reset. Fixes 1GKM3XS | |
2573 if (0 <= y && y < clientAreaHeight && | |
2574 (0 <= x && x < clientAreaWidth || wordWrap || | |
2575 newCaretLine !is content.getLineAtOffset(caretOffset))) { | |
2576 if (newCaretOffset !is caretOffset || caretAlignment !is oldCaretAlignment) { | |
2577 caretOffset = newCaretOffset; | |
2578 if (select) doMouseSelection(); | |
2579 showCaret(); | |
2580 } | |
2581 } | |
2582 if (!select) { | |
2583 caretOffset = newCaretOffset; | |
2584 clearSelection(true); | |
2585 } | |
2586 } | |
2587 /** | |
2588 * Updates the selection based on the caret position | |
2589 */ | |
2590 void doMouseSelection() { | |
2591 if (caretOffset <= selection.x || | |
2592 (caretOffset > selection.x && | |
2593 caretOffset < selection.y && selectionAnchor is selection.x)) { | |
2594 doSelection(ST.COLUMN_PREVIOUS); | |
2595 } else { | |
2596 doSelection(ST.COLUMN_NEXT); | |
2597 } | |
2598 } | |
2599 /** | |
2600 * Returns the offset of the word at the specified offset. | |
2601 * If the current selection extends from high index to low index | |
2602 * (i.e., right to left, or caret is at left border of selection on | |
2603 * non-bidi platforms) the start offset of the word preceding the | |
2604 * selection is returned. If the current selection extends from | |
2605 * low index to high index the end offset of the word following | |
2606 * the selection is returned. | |
2607 * | |
2608 * @param x mouse x location | |
2609 * @param newCaretOffset caret offset of the mouse cursor location | |
2610 * @param line line index of the mouse cursor location | |
2611 */ | |
2612 int doMouseWordSelect(int x, int newCaretOffset, int line) { | |
2613 // flip selection anchor based on word selection direction from | |
2614 // base double click. Always do this here (and don't rely on doAutoScroll) | |
2615 // because auto scroll only does not cover all possible mouse selections | |
2616 // (e.g., mouse x < 0 && mouse y > caret line y) | |
2617 if (newCaretOffset < selectionAnchor && selectionAnchor is selection.x) { | |
2618 selectionAnchor = doubleClickSelection.y; | |
2619 } else if (newCaretOffset > selectionAnchor && selectionAnchor is selection.y) { | |
2620 selectionAnchor = doubleClickSelection.x; | |
2621 } | |
2622 if (0 <= x && x < clientAreaWidth) { | |
2623 bool wordSelect = (clickCount & 1) is 0; | |
2624 if (caretOffset is selection.x) { | |
2625 if (wordSelect) { | |
2626 newCaretOffset = getWordPrevious(newCaretOffset, SWT.MOVEMENT_WORD_START); | |
2627 } else { | |
2628 newCaretOffset = content.getOffsetAtLine(line); | |
2629 } | |
2630 } else { | |
2631 if (wordSelect) { | |
2632 newCaretOffset = getWordNext(newCaretOffset, SWT.MOVEMENT_WORD_END); | |
2633 } else { | |
2634 int lineEnd = content.getCharCount(); | |
2635 if (line + 1 < content.getLineCount()) { | |
2636 lineEnd = content.getOffsetAtLine(line + 1); | |
2637 } | |
2638 newCaretOffset = lineEnd; | |
2639 } | |
2640 } | |
2641 } | |
2642 return newCaretOffset; | |
2643 } | |
2644 /** | |
2645 * Scrolls one page down so that the last line (truncated or whole) | |
2646 * of the current page becomes the fully visible top line. | |
2647 * <p> | |
2648 * The caret is scrolled the same number of lines so that its location | |
2649 * relative to the top line remains the same. The exception is the end | |
2650 * of the text where a full page scroll is not possible. In this case | |
2651 * the caret is moved after the last character. | |
2652 * </p> | |
2653 * | |
2654 * @param select whether or not to select the page | |
2655 */ | |
2656 void doPageDown(bool select, int height) { | |
2657 if (isSingleLine()) return; | |
2658 int oldColumnX = columnX; | |
2659 int oldHScrollOffset = horizontalScrollOffset; | |
2660 if (isFixedLineHeight()) { | |
2661 int lineCount = content.getLineCount(); | |
2662 int caretLine = getCaretLine(); | |
2663 if (caretLine < lineCount - 1) { | |
2664 int lineHeight = renderer.getLineHeight(); | |
2665 int lines = (height is -1 ? clientAreaHeight : height) / lineHeight; | |
2666 int scrollLines = Math.min(lineCount - caretLine - 1, lines); | |
2667 // ensure that scrollLines never gets negative and at least one | |
2668 // line is scrolled. fixes bug 5602. | |
2669 scrollLines = Math.max(1, scrollLines); | |
2670 caretOffset = getOffsetAtPoint(columnX, getLinePixel(caretLine + scrollLines)); | |
2671 if (select) { | |
2672 doSelection(ST.COLUMN_NEXT); | |
2673 } | |
2674 // scroll one page down or to the bottom | |
2675 int verticalMaximum = lineCount * getVerticalIncrement(); | |
2676 int pageSize = clientAreaHeight; | |
2677 int verticalScrollOffset = getVerticalScrollOffset(); | |
2678 int scrollOffset = verticalScrollOffset + scrollLines * getVerticalIncrement(); | |
2679 if (scrollOffset + pageSize > verticalMaximum) { | |
2680 scrollOffset = verticalMaximum - pageSize; | |
2681 } | |
2682 if (scrollOffset > verticalScrollOffset) { | |
2683 scrollVertical(scrollOffset - verticalScrollOffset, true); | |
2684 } | |
2685 } | |
2686 } else { | |
2687 int lineCount = content.getLineCount(); | |
2688 int caretLine = getCaretLine(); | |
2689 int lineIndex, lineHeight; | |
2690 if (height is -1) { | |
2691 lineIndex = getPartialBottomIndex(); | |
2692 int topY = getLinePixel(lineIndex); | |
2693 lineHeight = renderer.getLineHeight(lineIndex); | |
2694 height = topY; | |
2695 if (topY + lineHeight <= clientAreaHeight) { | |
2696 height += lineHeight; | |
2697 } else { | |
2698 if (wordWrap) { | |
2699 TextLayout layout = renderer.getTextLayout(lineIndex); | |
2700 int y = clientAreaHeight - topY; | |
2701 for (int i = 0; i < layout.getLineCount(); i++) { | |
2702 Rectangle bounds = layout.getLineBounds(i); | |
2703 if (bounds.contains(bounds.x, y)) { | |
2704 height += bounds.y; | |
2705 break; | |
2706 } | |
2707 } | |
2708 renderer.disposeTextLayout(layout); | |
2709 } | |
2710 } | |
2711 } else { | |
2712 lineIndex = getLineIndex(height); | |
2713 int topLineY = getLinePixel(lineIndex); | |
2714 if (wordWrap) { | |
2715 TextLayout layout = renderer.getTextLayout(lineIndex); | |
2716 int y = height - topLineY; | |
2717 for (int i = 0; i < layout.getLineCount(); i++) { | |
2718 Rectangle bounds = layout.getLineBounds(i); | |
2719 if (bounds.contains(bounds.x, y)) { | |
2720 height = topLineY + bounds.y + bounds.height; | |
2721 break; | |
2722 } | |
2723 } | |
2724 renderer.disposeTextLayout(layout); | |
2725 } else { | |
2726 height = topLineY + renderer.getLineHeight(lineIndex); | |
2727 } | |
2728 } | |
2729 int caretHeight = height; | |
2730 if (wordWrap) { | |
2731 TextLayout layout = renderer.getTextLayout(caretLine); | |
2732 int offsetInLine = caretOffset - content.getOffsetAtLine(caretLine); | |
2733 lineIndex = getVisualLineIndex(layout, offsetInLine); | |
2734 caretHeight += layout.getLineBounds(lineIndex).y; | |
2735 renderer.disposeTextLayout(layout); | |
2736 } | |
2737 lineIndex = caretLine; | |
2738 lineHeight = renderer.getLineHeight(lineIndex); | |
2739 while (caretHeight - lineHeight >= 0 && lineIndex < lineCount - 1) { | |
2740 caretHeight -= lineHeight; | |
2741 lineHeight = renderer.getLineHeight(++lineIndex); | |
2742 } | |
2743 caretOffset = getOffsetAtPoint(columnX, caretHeight, lineIndex); | |
2744 if (select) doSelection(ST.COLUMN_NEXT); | |
2745 height = getAvailableHeightBellow(height); | |
2746 scrollVertical(height, true); | |
2747 if (height is 0) setCaretLocation(); | |
2748 } | |
2749 showCaret(); | |
2750 int hScrollChange = oldHScrollOffset - horizontalScrollOffset; | |
2751 columnX = oldColumnX + hScrollChange; | |
2752 } | |
2753 /** | |
2754 * Moves the cursor to the end of the last fully visible line. | |
2755 */ | |
2756 void doPageEnd() { | |
2757 // go to end of line if in single line mode. fixes 5673 | |
2758 if (isSingleLine()) { | |
2759 doLineEnd(); | |
2760 } else { | |
2761 int bottomOffset; | |
2762 if (wordWrap) { | |
2763 int lineIndex = getPartialBottomIndex(); | |
2764 TextLayout layout = renderer.getTextLayout(lineIndex); | |
2765 int y = (clientAreaHeight - bottomMargin) - getLinePixel(lineIndex); | |
2766 int index = layout.getLineCount() - 1; | |
2767 while (index >= 0) { | |
2768 Rectangle bounds = layout.getLineBounds(index); | |
2769 if (y >= bounds.y + bounds.height) break; | |
2770 index--; | |
2771 } | |
2772 if (index is -1 && lineIndex > 0) { | |
2773 bottomOffset = content.getOffsetAtLine(lineIndex - 1) + content.getLine(lineIndex - 1).length; | |
2774 } else { | |
2775 bottomOffset = content.getOffsetAtLine(lineIndex) + Math.max(0, layout.getLineOffsets()[index + 1] - 1); | |
2776 } | |
2777 renderer.disposeTextLayout(layout); | |
2778 } else { | |
2779 int lineIndex = getBottomIndex(); | |
2780 bottomOffset = content.getOffsetAtLine(lineIndex) + content.getLine(lineIndex).length; | |
2781 } | |
2782 if (caretOffset < bottomOffset) { | |
2783 caretOffset = bottomOffset; | |
2784 caretAlignment = OFFSET_LEADING; | |
2785 showCaret(); | |
2786 } | |
2787 } | |
2788 } | |
2789 /** | |
2790 * Moves the cursor to the beginning of the first fully visible line. | |
2791 */ | |
2792 void doPageStart() { | |
2793 int topOffset; | |
2794 if (wordWrap) { | |
2795 int y, lineIndex; | |
2796 if (topIndexY > 0) { | |
2797 lineIndex = topIndex - 1; | |
2798 y = renderer.getLineHeight(lineIndex) - topIndexY; | |
2799 } else { | |
2800 lineIndex = topIndex; | |
2801 y = -topIndexY; | |
2802 } | |
2803 TextLayout layout = renderer.getTextLayout(lineIndex); | |
2804 int index = 0; | |
2805 int lineCount = layout.getLineCount(); | |
2806 while (index < lineCount) { | |
2807 Rectangle bounds = layout.getLineBounds(index); | |
2808 if (y <= bounds.y) break; | |
2809 index++; | |
2810 } | |
2811 if (index is lineCount) { | |
2812 topOffset = content.getOffsetAtLine(lineIndex + 1); | |
2813 } else { | |
2814 topOffset = content.getOffsetAtLine(lineIndex) + layout.getLineOffsets()[index]; | |
2815 } | |
2816 renderer.disposeTextLayout(layout); | |
2817 } else { | |
2818 topOffset = content.getOffsetAtLine(topIndex); | |
2819 } | |
2820 if (caretOffset > topOffset) { | |
2821 caretOffset = topOffset; | |
2822 caretAlignment = OFFSET_LEADING; | |
2823 showCaret(); | |
2824 } | |
2825 } | |
2826 /** | |
2827 * Scrolls one page up so that the first line (truncated or whole) | |
2828 * of the current page becomes the fully visible last line. | |
2829 * The caret is scrolled the same number of lines so that its location | |
2830 * relative to the top line remains the same. The exception is the beginning | |
2831 * of the text where a full page scroll is not possible. In this case the | |
2832 * caret is moved in front of the first character. | |
2833 */ | |
2834 void doPageUp(bool select, int height) { | |
2835 if (isSingleLine()) return; | |
2836 int oldHScrollOffset = horizontalScrollOffset; | |
2837 int oldColumnX = columnX; | |
2838 if (isFixedLineHeight()) { | |
2839 int caretLine = getCaretLine(); | |
2840 if (caretLine > 0) { | |
2841 int lineHeight = renderer.getLineHeight(); | |
2842 int lines = (height is -1 ? clientAreaHeight : height) / lineHeight; | |
2843 int scrollLines = Math.max(1, Math.min(caretLine, lines)); | |
2844 caretLine -= scrollLines; | |
2845 caretOffset = getOffsetAtPoint(columnX, getLinePixel(caretLine)); | |
2846 if (select) { | |
2847 doSelection(ST.COLUMN_PREVIOUS); | |
2848 } | |
2849 int verticalScrollOffset = getVerticalScrollOffset(); | |
2850 int scrollOffset = Math.max(0, verticalScrollOffset - scrollLines * getVerticalIncrement()); | |
2851 if (scrollOffset < verticalScrollOffset) { | |
2852 scrollVertical(scrollOffset - verticalScrollOffset, true); | |
2853 } | |
2854 } | |
2855 } else { | |
2856 int caretLine = getCaretLine(); | |
2857 int lineHeight, lineIndex; | |
2858 if (height is -1) { | |
2859 if (topIndexY is 0) { | |
2860 height = clientAreaHeight; | |
2861 } else { | |
2862 int y; | |
2863 if (topIndex > 0) { | |
2864 lineIndex = topIndex - 1; | |
2865 lineHeight = renderer.getLineHeight(lineIndex); | |
2866 height = clientAreaHeight - topIndexY; | |
2867 y = lineHeight - topIndexY; | |
2868 } else { | |
2869 lineIndex = topIndex; | |
2870 lineHeight = renderer.getLineHeight(lineIndex); | |
2871 height = clientAreaHeight - (lineHeight + topIndexY); | |
2872 y = -topIndexY; | |
2873 } | |
2874 if (wordWrap) { | |
2875 TextLayout layout = renderer.getTextLayout(lineIndex); | |
2876 for (int i = 0; i < layout.getLineCount(); i++) { | |
2877 Rectangle bounds = layout.getLineBounds(i); | |
2878 if (bounds.contains(bounds.x, y)) { | |
2879 height += lineHeight - (bounds.y + bounds.height); | |
2880 break; | |
2881 } | |
2882 } | |
2883 renderer.disposeTextLayout(layout); | |
2884 } | |
2885 } | |
2886 } else { | |
2887 lineIndex = getLineIndex(clientAreaHeight - height); | |
2888 int topLineY = getLinePixel(lineIndex); | |
2889 if (wordWrap) { | |
2890 TextLayout layout = renderer.getTextLayout(lineIndex); | |
2891 int y = topLineY; | |
2892 for (int i = 0; i < layout.getLineCount(); i++) { | |
2893 Rectangle bounds = layout.getLineBounds(i); | |
2894 if (bounds.contains(bounds.x, y)) { | |
2895 height = clientAreaHeight - (topLineY + bounds.y); | |
2896 break; | |
2897 } | |
2898 } | |
2899 renderer.disposeTextLayout(layout); | |
2900 } else { | |
2901 height = clientAreaHeight - topLineY; | |
2902 } | |
2903 } | |
2904 int caretHeight = height; | |
2905 if (wordWrap) { | |
2906 TextLayout layout = renderer.getTextLayout(caretLine); | |
2907 int offsetInLine = caretOffset - content.getOffsetAtLine(caretLine); | |
2908 lineIndex = getVisualLineIndex(layout, offsetInLine); | |
2909 caretHeight += layout.getBounds().height - layout.getLineBounds(lineIndex).y; | |
2910 renderer.disposeTextLayout(layout); | |
2911 } | |
2912 lineIndex = caretLine; | |
2913 lineHeight = renderer.getLineHeight(lineIndex); | |
2914 while (caretHeight - lineHeight >= 0 && lineIndex > 0) { | |
2915 caretHeight -= lineHeight; | |
2916 lineHeight = renderer.getLineHeight(--lineIndex); | |
2917 } | |
2918 lineHeight = renderer.getLineHeight(lineIndex); | |
2919 caretOffset = getOffsetAtPoint(columnX, lineHeight - caretHeight, lineIndex); | |
2920 if (select) doSelection(ST.COLUMN_PREVIOUS); | |
2921 height = getAvailableHeightAbove(height); | |
2922 scrollVertical(-height, true); | |
2923 if (height is 0) setCaretLocation(); | |
2924 } | |
2925 showCaret(); | |
2926 int hScrollChange = oldHScrollOffset - horizontalScrollOffset; | |
2927 columnX = oldColumnX + hScrollChange; | |
2928 } | |
2929 /** | |
2930 * Updates the selection to extend to the current caret position. | |
2931 */ | |
2932 void doSelection(int direction) { | |
2933 int redrawStart = -1; | |
2934 int redrawEnd = -1; | |
2935 if (selectionAnchor is -1) { | |
2936 selectionAnchor = selection.x; | |
2937 } | |
2938 if (direction is ST.COLUMN_PREVIOUS) { | |
2939 if (caretOffset < selection.x) { | |
2940 // grow selection | |
2941 redrawEnd = selection.x; | |
2942 redrawStart = selection.x = caretOffset; | |
2943 // check if selection has reversed direction | |
2944 if (selection.y !is selectionAnchor) { | |
2945 redrawEnd = selection.y; | |
2946 selection.y = selectionAnchor; | |
2947 } | |
2948 // test whether selection actually changed. Fixes 1G71EO1 | |
2949 } else if (selectionAnchor is selection.x && caretOffset < selection.y) { | |
2950 // caret moved towards selection anchor (left side of selection). | |
2951 // shrink selection | |
2952 redrawEnd = selection.y; | |
2953 redrawStart = selection.y = caretOffset; | |
2954 } | |
2955 } else { | |
2956 if (caretOffset > selection.y) { | |
2957 // grow selection | |
2958 redrawStart = selection.y; | |
2959 redrawEnd = selection.y = caretOffset; | |
2960 // check if selection has reversed direction | |
2961 if (selection.x !is selectionAnchor) { | |
2962 redrawStart = selection.x; | |
2963 selection.x = selectionAnchor; | |
2964 } | |
2965 // test whether selection actually changed. Fixes 1G71EO1 | |
2966 } else if (selectionAnchor is selection.y && caretOffset > selection.x) { | |
2967 // caret moved towards selection anchor (right side of selection). | |
2968 // shrink selection | |
2969 redrawStart = selection.x; | |
2970 redrawEnd = selection.x = caretOffset; | |
2971 } | |
2972 } | |
2973 if (redrawStart !is -1 && redrawEnd !is -1) { | |
2974 internalRedrawRange(redrawStart, redrawEnd - redrawStart); | |
2975 sendSelectionEvent(); | |
2976 } | |
2977 } | |
2978 /** | |
2979 * Moves the caret to the next character or to the beginning of the | |
2980 * next line if the cursor is at the end of a line. | |
2981 */ | |
2982 void doSelectionCursorNext() { | |
2983 int caretLine = getCaretLine(); | |
2984 int lineOffset = content.getOffsetAtLine(caretLine); | |
2985 int offsetInLine = caretOffset - lineOffset; | |
2986 if (offsetInLine < content.getLine(caretLine).length) { | |
2987 TextLayout layout = renderer.getTextLayout(caretLine); | |
2988 offsetInLine = layout.getNextOffset(offsetInLine, SWT.MOVEMENT_CLUSTER); | |
2989 int lineStart = layout.getLineOffsets()[layout.getLineIndex(offsetInLine)]; | |
2990 renderer.disposeTextLayout(layout); | |
2991 caretOffset = offsetInLine + lineOffset; | |
2992 caretAlignment = offsetInLine is lineStart ? OFFSET_LEADING : PREVIOUS_OFFSET_TRAILING; | |
2993 showCaret(); | |
2994 } else if (caretLine < content.getLineCount() - 1 && !isSingleLine()) { | |
2995 caretLine++; | |
2996 caretOffset = content.getOffsetAtLine(caretLine); | |
2997 caretAlignment = PREVIOUS_OFFSET_TRAILING; | |
2998 showCaret(); | |
2999 } | |
3000 } | |
3001 /** | |
3002 * Moves the caret to the previous character or to the end of the previous | |
3003 * line if the cursor is at the beginning of a line. | |
3004 */ | |
3005 void doSelectionCursorPrevious() { | |
3006 int caretLine = getCaretLine(); | |
3007 int lineOffset = content.getOffsetAtLine(caretLine); | |
3008 int offsetInLine = caretOffset - lineOffset; | |
3009 caretAlignment = OFFSET_LEADING; | |
3010 | |
3011 if (offsetInLine > 0) { | |
3012 caretOffset = getClusterPrevious(caretOffset, caretLine); | |
3013 showCaret(); | |
3014 } else if (caretLine > 0) { | |
3015 caretLine--; | |
3016 lineOffset = content.getOffsetAtLine(caretLine); | |
3017 caretOffset = lineOffset + content.getLine(caretLine).length; | |
3018 showCaret(); | |
3019 } | |
3020 } | |
3021 /** | |
3022 * Moves the caret one line down and to the same character offset relative | |
3023 * to the beginning of the line. Moves the caret to the end of the new line | |
3024 * if the new line is shorter than the character offset. | |
3025 * Moves the caret to the end of the text if the caret already is on the | |
3026 * last line. | |
3027 * Adjusts the selection according to the caret change. This can either add | |
3028 * to or subtract from the old selection, depending on the previous selection | |
3029 * direction. | |
3030 */ | |
3031 void doSelectionLineDown() { | |
3032 int oldColumnX = columnX = getPointAtOffset(caretOffset).x; | |
3033 doLineDown(true); | |
3034 columnX = oldColumnX; | |
3035 } | |
3036 /** | |
3037 * Moves the caret one line up and to the same character offset relative | |
3038 * to the beginning of the line. Moves the caret to the end of the new line | |
3039 * if the new line is shorter than the character offset. | |
3040 * Moves the caret to the beginning of the document if it is already on the | |
3041 * first line. | |
3042 * Adjusts the selection according to the caret change. This can either add | |
3043 * to or subtract from the old selection, depending on the previous selection | |
3044 * direction. | |
3045 */ | |
3046 void doSelectionLineUp() { | |
3047 int oldColumnX = columnX = getPointAtOffset(caretOffset).x; | |
3048 doLineUp(true); | |
3049 columnX = oldColumnX; | |
3050 } | |
3051 /** | |
3052 * Scrolls one page down so that the last line (truncated or whole) | |
3053 * of the current page becomes the fully visible top line. | |
3054 * <p> | |
3055 * The caret is scrolled the same number of lines so that its location | |
3056 * relative to the top line remains the same. The exception is the end | |
3057 * of the text where a full page scroll is not possible. In this case | |
3058 * the caret is moved after the last character. | |
3059 * <p></p> | |
3060 * Adjusts the selection according to the caret change. This can either add | |
3061 * to or subtract from the old selection, depending on the previous selection | |
3062 * direction. | |
3063 * </p> | |
3064 */ | |
3065 void doSelectionPageDown(int pixels) { | |
3066 int oldColumnX = columnX = getPointAtOffset(caretOffset).x; | |
3067 doPageDown(true, pixels); | |
3068 columnX = oldColumnX; | |
3069 } | |
3070 /** | |
3071 * Scrolls one page up so that the first line (truncated or whole) | |
3072 * of the current page becomes the fully visible last line. | |
3073 * <p> | |
3074 * The caret is scrolled the same number of lines so that its location | |
3075 * relative to the top line remains the same. The exception is the beginning | |
3076 * of the text where a full page scroll is not possible. In this case the | |
3077 * caret is moved in front of the first character. | |
3078 * </p><p> | |
3079 * Adjusts the selection according to the caret change. This can either add | |
3080 * to or subtract from the old selection, depending on the previous selection | |
3081 * direction. | |
3082 * </p> | |
3083 */ | |
3084 void doSelectionPageUp(int pixels) { | |
3085 int oldColumnX = columnX = getPointAtOffset(caretOffset).x; | |
3086 doPageUp(true, pixels); | |
3087 columnX = oldColumnX; | |
3088 } | |
3089 /** | |
3090 * Moves the caret to the end of the next word . | |
3091 */ | |
3092 void doSelectionWordNext() { | |
3093 int newCaretOffset = getWordNext(caretOffset, SWT.MOVEMENT_WORD); | |
3094 // Force symmetrical movement for word next and previous. Fixes 14536 | |
3095 caretAlignment = OFFSET_LEADING; | |
3096 // don't change caret position if in single line mode and the cursor | |
3097 // would be on a different line. fixes 5673 | |
3098 if (!isSingleLine() || | |
3099 content.getLineAtOffset(caretOffset) is content.getLineAtOffset(newCaretOffset)) { | |
3100 caretOffset = newCaretOffset; | |
3101 showCaret(); | |
3102 } | |
3103 } | |
3104 /** | |
3105 * Moves the caret to the start of the previous word. | |
3106 */ | |
3107 void doSelectionWordPrevious() { | |
3108 caretAlignment = OFFSET_LEADING; | |
3109 caretOffset = getWordPrevious(caretOffset, SWT.MOVEMENT_WORD); | |
3110 int caretLine = content.getLineAtOffset(caretOffset); | |
3111 // word previous always comes from bottom line. when | |
3112 // wrapping lines, stay on bottom line when on line boundary | |
3113 if (wordWrap && caretLine < content.getLineCount() - 1 && | |
3114 caretOffset is content.getOffsetAtLine(caretLine + 1)) { | |
3115 caretLine++; | |
3116 } | |
3117 showCaret(); | |
3118 } | |
3119 /** | |
3120 * Moves the caret one character to the left. Do not go to the previous line. | |
3121 * When in a bidi locale and at a R2L character the caret is moved to the | |
3122 * beginning of the R2L segment (visually right) and then one character to the | |
3123 * left (visually left because it's now in a L2R segment). | |
3124 */ | |
3125 void doVisualPrevious() { | |
3126 caretOffset = getClusterPrevious(caretOffset, getCaretLine()); | |
3127 showCaret(); | |
3128 } | |
3129 /** | |
3130 * Moves the caret one character to the right. Do not go to the next line. | |
3131 * When in a bidi locale and at a R2L character the caret is moved to the | |
3132 * end of the R2L segment (visually left) and then one character to the | |
3133 * right (visually right because it's now in a L2R segment). | |
3134 */ | |
3135 void doVisualNext() { | |
3136 caretOffset = getClusterNext(caretOffset, getCaretLine()); | |
3137 showCaret(); | |
3138 } | |
3139 /** | |
3140 * Moves the caret to the end of the next word. | |
3141 * If a selection exists, move the caret to the end of the selection | |
3142 * and remove the selection. | |
3143 */ | |
3144 void doWordNext() { | |
3145 if (selection.y - selection.x > 0) { | |
3146 caretOffset = selection.y; | |
3147 showCaret(); | |
3148 } else { | |
3149 doSelectionWordNext(); | |
3150 } | |
3151 } | |
3152 /** | |
3153 * Moves the caret to the start of the previous word. | |
3154 * If a selection exists, move the caret to the start of the selection | |
3155 * and remove the selection. | |
3156 */ | |
3157 void doWordPrevious() { | |
3158 if (selection.y - selection.x > 0) { | |
3159 caretOffset = selection.x; | |
3160 showCaret(); | |
3161 } else { | |
3162 doSelectionWordPrevious(); | |
3163 } | |
3164 } | |
3165 /** | |
3166 * Ends the autoscroll process. | |
3167 */ | |
3168 void endAutoScroll() { | |
3169 autoScrollDirection = SWT.NULL; | |
3170 } | |
3171 public override Color getBackground() { | |
3172 checkWidget(); | |
3173 if (background is null) { | |
3174 return getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND); | |
3175 } | |
3176 return background; | |
3177 } | |
3178 /** | |
3179 * Returns the baseline, in pixels. | |
3180 * | |
3181 * Note: this API should not be used if a StyleRange attribute causes lines to | |
3182 * have different heights (i.e. different fonts, rise, etc). | |
3183 * | |
3184 * @return baseline the baseline | |
3185 * @exception SWTException <ul> | |
3186 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3187 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3188 * </ul> | |
3189 * @since 3.0 | |
3190 * | |
3191 * @see #getBaseline(int) | |
3192 */ | |
3193 public int getBaseline() { | |
3194 checkWidget(); | |
3195 return renderer.getBaseline(); | |
3196 } | |
3197 /** | |
3198 * Returns the baseline at the given offset, in pixels. | |
3199 * | |
3200 * @param offset the offset | |
3201 * | |
3202 * @return baseline the baseline | |
3203 * | |
3204 * @exception SWTException <ul> | |
3205 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3206 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3207 * </ul> | |
3208 * @exception IllegalArgumentException <ul> | |
3209 * <li>ERROR_INVALID_RANGE when the offset is outside the valid range (< 0 or > getCharCount())</li> | |
3210 * </ul> | |
3211 * | |
3212 * @since 3.2 | |
3213 */ | |
3214 public int getBaseline(int offset) { | |
3215 checkWidget(); | |
3216 if (!(0 <= offset && offset <= content.getCharCount())) { | |
3217 SWT.error(SWT.ERROR_INVALID_RANGE); | |
3218 } | |
3219 if (isFixedLineHeight()) { | |
3220 return renderer.getBaseline(); | |
3221 } | |
3222 int lineIndex = content.getLineAtOffset(offset); | |
3223 int lineOffset = content.getOffsetAtLine(lineIndex); | |
3224 TextLayout layout = renderer.getTextLayout(lineIndex); | |
3225 int lineInParagraph = layout.getLineIndex(Math.min(offset - lineOffset, layout.getText().length)); | |
3226 FontMetrics metrics = layout.getLineMetrics(lineInParagraph); | |
3227 renderer.disposeTextLayout(layout); | |
3228 return metrics.getAscent() + metrics.getLeading(); | |
3229 } | |
3230 /** | |
3231 * Gets the BIDI coloring mode. When true the BIDI text display | |
3232 * algorithm is applied to segments of text that are the same | |
3233 * color. | |
3234 * | |
3235 * @return the current coloring mode | |
3236 * @exception SWTException <ul> | |
3237 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3238 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3239 * </ul> | |
3240 * | |
3241 * @deprecated use BidiSegmentListener instead. | |
3242 */ | |
3243 public bool getBidiColoring() { | |
3244 checkWidget(); | |
3245 return bidiColoring; | |
3246 } | |
3247 /** | |
3248 * Returns the index of the last fully visible line. | |
3249 * | |
3250 * @return index of the last fully visible line. | |
3251 */ | |
3252 int getBottomIndex() { | |
3253 int bottomIndex; | |
3254 if (isFixedLineHeight()) { | |
3255 int lineCount = 1; | |
3256 int lineHeight = renderer.getLineHeight(); | |
3257 if (lineHeight !is 0) { | |
3258 // calculate the number of lines that are fully visible | |
3259 int partialTopLineHeight = topIndex * lineHeight - getVerticalScrollOffset(); | |
3260 lineCount = (clientAreaHeight - partialTopLineHeight) / lineHeight; | |
3261 } | |
3262 bottomIndex = Math.min(content.getLineCount() - 1, topIndex + Math.max(0, lineCount - 1)); | |
3263 } else { | |
3264 int clientAreaHeight = this.clientAreaHeight - bottomMargin; | |
3265 bottomIndex = getLineIndex(clientAreaHeight); | |
3266 if (bottomIndex > 0) { | |
3267 int linePixel = getLinePixel(bottomIndex); | |
3268 int lineHeight = renderer.getLineHeight(bottomIndex); | |
3269 if (linePixel + lineHeight > clientAreaHeight) { | |
3270 if (getLinePixel(bottomIndex - 1) >= topMargin) { | |
3271 bottomIndex--; | |
3272 } | |
3273 } | |
3274 } | |
3275 } | |
3276 return bottomIndex; | |
3277 } | |
3278 Rectangle getBoundsAtOffset(int offset) { | |
3279 int lineIndex = content.getLineAtOffset(offset); | |
3280 int lineOffset = content.getOffsetAtLine(lineIndex); | |
3281 String line = content.getLine(lineIndex); | |
3282 Rectangle bounds; | |
3283 if (line.length !is 0) { | |
3284 int offsetInLine = offset - lineOffset; | |
3285 TextLayout layout = renderer.getTextLayout(lineIndex); | |
3286 bounds = layout.getBounds(offsetInLine, offsetInLine); | |
3287 renderer.disposeTextLayout(layout); | |
3288 } else { | |
3289 bounds = new Rectangle (0, 0, 0, renderer.getLineHeight()); | |
3290 } | |
3291 if (offset is caretOffset) { | |
3292 int lineEnd = lineOffset + line.length; | |
3293 if (offset is lineEnd && caretAlignment is PREVIOUS_OFFSET_TRAILING) { | |
3294 bounds.width += getCaretWidth(); | |
3295 } | |
3296 } | |
3297 bounds.x += leftMargin - horizontalScrollOffset; | |
3298 bounds.y += getLinePixel(lineIndex); | |
3299 return bounds; | |
3300 } | |
3301 /** | |
3302 * Returns the caret position relative to the start of the text. | |
3303 * | |
3304 * @return the caret position relative to the start of the text. | |
3305 * @exception SWTException <ul> | |
3306 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3307 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3308 * </ul> | |
3309 */ | |
3310 public int getCaretOffset() { | |
3311 checkWidget(); | |
3312 return caretOffset; | |
3313 } | |
3314 /** | |
3315 * Returns the caret width. | |
3316 * | |
3317 * @return the caret width, 0 if caret is null. | |
3318 */ | |
3319 int getCaretWidth() { | |
3320 Caret caret = getCaret(); | |
3321 if (caret is null) return 0; | |
3322 return caret.getSize().x; | |
3323 } | |
3324 Object getClipboardContent(int clipboardType) { | |
3325 TextTransfer plainTextTransfer = TextTransfer.getInstance(); | |
3326 return clipboard.getContents(plainTextTransfer, clipboardType); | |
3327 } | |
3328 int getClusterNext(int offset, int lineIndex) { | |
3329 int lineOffset = content.getOffsetAtLine(lineIndex); | |
3330 TextLayout layout = renderer.getTextLayout(lineIndex); | |
3331 offset -= lineOffset; | |
3332 offset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER); | |
3333 offset += lineOffset; | |
3334 renderer.disposeTextLayout(layout); | |
3335 return offset; | |
3336 } | |
3337 int getClusterPrevious(int offset, int lineIndex) { | |
3338 int lineOffset = content.getOffsetAtLine(lineIndex); | |
3339 TextLayout layout = renderer.getTextLayout(lineIndex); | |
3340 offset -= lineOffset; | |
3341 offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_CLUSTER); | |
3342 offset += lineOffset; | |
3343 renderer.disposeTextLayout(layout); | |
3344 return offset; | |
3345 } | |
3346 /** | |
3347 * Returns the content implementation that is used for text storage. | |
3348 * | |
3349 * @return content the user defined content implementation that is used for | |
3350 * text storage or the default content implementation if no user defined | |
3351 * content implementation has been set. | |
3352 * @exception SWTException <ul> | |
3353 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3354 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3355 * </ul> | |
3356 */ | |
3357 public StyledTextContent getContent() { | |
3358 checkWidget(); | |
3359 return content; | |
3360 } | |
3361 public override bool getDragDetect () { | |
3362 checkWidget (); | |
3363 return dragDetect_; | |
3364 } | |
3365 /** | |
3366 * Returns whether the widget implements double click mouse behavior. | |
3367 * | |
3368 * @return true if double clicking a word selects the word, false if double clicks | |
3369 * have the same effect as regular mouse clicks | |
3370 * @exception SWTException <ul> | |
3371 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3372 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3373 * </ul> | |
3374 */ | |
3375 public bool getDoubleClickEnabled() { | |
3376 checkWidget(); | |
3377 return doubleClickEnabled; | |
3378 } | |
3379 /** | |
3380 * Returns whether the widget content can be edited. | |
3381 * | |
3382 * @return true if content can be edited, false otherwise | |
3383 * @exception SWTException <ul> | |
3384 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3385 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3386 * </ul> | |
3387 */ | |
3388 public bool getEditable() { | |
3389 checkWidget(); | |
3390 return editable; | |
3391 } | |
3392 public override Color getForeground() { | |
3393 checkWidget(); | |
3394 if (foreground is null) { | |
3395 return getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND); | |
3396 } | |
3397 return foreground; | |
3398 } | |
3399 /** | |
3400 * Returns the horizontal scroll increment. | |
3401 * | |
3402 * @return horizontal scroll increment. | |
3403 */ | |
3404 int getHorizontalIncrement() { | |
3405 return renderer.averageCharWidth; | |
3406 } | |
3407 /** | |
3408 * Returns the horizontal scroll offset relative to the start of the line. | |
3409 * | |
3410 * @return horizontal scroll offset relative to the start of the line, | |
3411 * measured in character increments starting at 0, if > 0 the content is scrolled | |
3412 * @exception SWTException <ul> | |
3413 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3414 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3415 * </ul> | |
3416 */ | |
3417 public int getHorizontalIndex() { | |
3418 checkWidget(); | |
3419 return horizontalScrollOffset / getHorizontalIncrement(); | |
3420 } | |
3421 /** | |
3422 * Returns the horizontal scroll offset relative to the start of the line. | |
3423 * | |
3424 * @return the horizontal scroll offset relative to the start of the line, | |
3425 * measured in pixel starting at 0, if > 0 the content is scrolled. | |
3426 * @exception SWTException <ul> | |
3427 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3428 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3429 * </ul> | |
3430 */ | |
3431 public int getHorizontalPixel() { | |
3432 checkWidget(); | |
3433 return horizontalScrollOffset; | |
3434 } | |
3435 /** | |
3436 * Returns the line indentation of the widget. | |
3437 * | |
3438 * @return the line indentation | |
3439 * | |
3440 * @exception SWTException <ul> | |
3441 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3442 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3443 * </ul> | |
3444 * | |
3445 * @see #getLineIndent(int) | |
3446 * | |
3447 * @since 3.2 | |
3448 */ | |
3449 public int getIndent() { | |
3450 checkWidget(); | |
3451 return indent; | |
3452 } | |
3453 /** | |
3454 * Returns whether the widget justifies lines. | |
3455 * | |
3456 * @return whether lines are justified | |
3457 * | |
3458 * @exception SWTException <ul> | |
3459 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3460 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3461 * </ul> | |
3462 * | |
3463 * @see #getLineJustify(int) | |
3464 * | |
3465 * @since 3.2 | |
3466 */ | |
3467 public bool getJustify() { | |
3468 checkWidget(); | |
3469 return justify; | |
3470 } | |
3471 /** | |
3472 * Returns the action assigned to the key. | |
3473 * Returns SWT.NULL if there is no action associated with the key. | |
3474 * | |
3475 * @param key a key code defined in SWT.java or a character. | |
3476 * Optionally ORd with a state mask. Preferred state masks are one or more of | |
3477 * SWT.MOD1, SWT.MOD2, SWT.MOD3, since these masks account for modifier platform | |
3478 * differences. However, there may be cases where using the specific state masks | |
3479 * (i.e., SWT.CTRL, SWT.SHIFT, SWT.ALT, SWT.COMMAND) makes sense. | |
3480 * @return one of the predefined actions defined in ST.java or SWT.NULL | |
3481 * if there is no action associated with the key. | |
3482 * @exception SWTException <ul> | |
3483 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3484 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3485 * </ul> | |
3486 */ | |
3487 public int getKeyBinding(int key) { | |
3488 checkWidget(); | |
3489 if( auto p = key in keyActionMap ){ | |
3490 return *p; | |
3491 } | |
3492 return SWT.NULL; | |
3493 } | |
3494 /** | |
3495 * Gets the number of characters. | |
3496 * | |
3497 * @return number of characters in the widget | |
3498 * @exception SWTException <ul> | |
3499 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3500 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3501 * </ul> | |
3502 */ | |
3503 public int getCharCount() { | |
3504 checkWidget(); | |
3505 return content.getCharCount(); | |
3506 } | |
3507 /** | |
3508 * Returns the line at the given line index without delimiters. | |
3509 * Index 0 is the first line of the content. When there are not | |
3510 * any lines, getLine(0) is a valid call that answers an empty string. | |
3511 * <p> | |
3512 * | |
3513 * @param lineIndex index of the line to return. | |
3514 * @return the line text without delimiters | |
3515 * | |
3516 * @exception SWTException <ul> | |
3517 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3518 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3519 * </ul> | |
3520 * @exception IllegalArgumentException <ul> | |
3521 * <li>ERROR_INVALID_RANGE when the line index is outside the valid range (< 0 or >= getLineCount())</li> | |
3522 * </ul> | |
3523 * @since 3.4 | |
3524 */ | |
3525 public String getLine(int lineIndex) { | |
3526 checkWidget(); | |
3527 if (lineIndex < 0 || | |
3528 (lineIndex > 0 && lineIndex >= content.getLineCount())) { | |
3529 SWT.error(SWT.ERROR_INVALID_RANGE); | |
3530 } | |
3531 return content.getLine(lineIndex); | |
3532 } | |
3533 /** | |
3534 * Returns the alignment of the line at the given index. | |
3535 * | |
3536 * @param index the index of the line | |
3537 * | |
3538 * @return the line alignment | |
3539 * | |
3540 * @exception SWTException <ul> | |
3541 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3542 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3543 * </ul> | |
3544 * @exception IllegalArgumentException <ul> | |
3545 * <li>ERROR_INVALID_ARGUMENT when the index is invalid</li> | |
3546 * </ul> | |
3547 * | |
3548 * @see #getAlignment() | |
3549 * | |
3550 * @since 3.2 | |
3551 */ | |
3552 public int getLineAlignment(int index) { | |
3553 checkWidget(); | |
3554 if (index < 0 || index > content.getLineCount()) { | |
3555 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
3556 } | |
3557 return renderer.getLineAlignment(index, alignment); | |
3558 } | |
3559 /** | |
3560 * Returns the line at the specified offset in the text | |
3561 * where 0 < offset < getCharCount() so that getLineAtOffset(getCharCount()) | |
3562 * returns the line of the insert location. | |
3563 * | |
3564 * @param offset offset relative to the start of the content. | |
3565 * 0 <= offset <= getCharCount() | |
3566 * @return line at the specified offset in the text | |
3567 * @exception SWTException <ul> | |
3568 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3569 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3570 * </ul> | |
3571 * @exception IllegalArgumentException <ul> | |
3572 * <li>ERROR_INVALID_RANGE when the offset is outside the valid range (< 0 or > getCharCount())</li> | |
3573 * </ul> | |
3574 */ | |
3575 public int getLineAtOffset(int offset) { | |
3576 checkWidget(); | |
3577 if (offset < 0 || offset > getCharCount()) { | |
3578 SWT.error(SWT.ERROR_INVALID_RANGE); | |
3579 } | |
3580 return content.getLineAtOffset(offset); | |
3581 } | |
3582 /** | |
3583 * Returns the background color of the line at the given index. | |
3584 * Returns null if a LineBackgroundListener has been set or if no background | |
3585 * color has been specified for the line. Should not be called if a | |
3586 * LineBackgroundListener has been set since the listener maintains the | |
3587 * line background colors. | |
3588 * | |
3589 * @param index the index of the line | |
3590 * @return the background color of the line at the given index. | |
3591 * | |
3592 * @exception SWTException <ul> | |
3593 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3594 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3595 * </ul> | |
3596 * @exception IllegalArgumentException <ul> | |
3597 * <li>ERROR_INVALID_ARGUMENT when the index is invalid</li> | |
3598 * </ul> | |
3599 */ | |
3600 public Color getLineBackground(int index) { | |
3601 checkWidget(); | |
3602 if (index < 0 || index > content.getLineCount()) { | |
3603 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
3604 } | |
3605 return isListening(LineGetBackground) ? null : renderer.getLineBackground(index, null); | |
3606 } | |
3607 /** | |
3608 * Returns the bullet of the line at the given index. | |
3609 * | |
3610 * @param index the index of the line | |
3611 * | |
3612 * @return the line bullet | |
3613 * | |
3614 * @exception SWTException <ul> | |
3615 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3616 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3617 * </ul> | |
3618 * @exception IllegalArgumentException <ul> | |
3619 * <li>ERROR_INVALID_ARGUMENT when the index is invalid</li> | |
3620 * </ul> | |
3621 * | |
3622 * @since 3.2 | |
3623 */ | |
3624 public Bullet getLineBullet(int index) { | |
3625 checkWidget(); | |
3626 if (index < 0 || index > content.getLineCount()) { | |
3627 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
3628 } | |
3629 return isListening(LineGetStyle) ? null : renderer.getLineBullet(index, null); | |
3630 } | |
3631 /** | |
3632 * Returns the line background data for the given line or null if | |
3633 * there is none. | |
3634 * | |
3635 * @param lineOffset offset of the line start relative to the start | |
3636 * of the content. | |
3637 * @param line line to get line background data for | |
3638 * @return line background data for the given line. | |
3639 */ | |
3640 StyledTextEvent getLineBackgroundData(int lineOffset, String line) { | |
3641 return sendLineEvent(LineGetBackground, lineOffset, line); | |
3642 } | |
3643 /** | |
3644 * Gets the number of text lines. | |
3645 * | |
3646 * @return the number of lines in the widget | |
3647 * @exception SWTException <ul> | |
3648 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3649 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3650 * </ul> | |
3651 */ | |
3652 public int getLineCount() { | |
3653 checkWidget(); | |
3654 return content.getLineCount(); | |
3655 } | |
3656 /** | |
3657 * Returns the number of lines that can be completely displayed in the | |
3658 * widget client area. | |
3659 * | |
3660 * @return number of lines that can be completely displayed in the widget | |
3661 * client area. | |
3662 */ | |
3663 int getLineCountWhole() { | |
3664 if (isFixedLineHeight()) { | |
3665 int lineHeight = renderer.getLineHeight(); | |
3666 return lineHeight !is 0 ? clientAreaHeight / lineHeight : 1; | |
3667 } | |
3668 return getBottomIndex() - topIndex + 1; | |
3669 } | |
3670 /** | |
3671 * Returns the line delimiter used for entering new lines by key down | |
3672 * or paste operation. | |
3673 * | |
3674 * @return line delimiter used for entering new lines by key down | |
3675 * or paste operation. | |
3676 * @exception SWTException <ul> | |
3677 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3678 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3679 * </ul> | |
3680 */ | |
3681 public String getLineDelimiter() { | |
3682 checkWidget(); | |
3683 return content.getLineDelimiter(); | |
3684 } | |
3685 /** | |
3686 * Returns the line height. | |
3687 * <p> | |
3688 * Note: this API should not be used if a StyleRange attribute causes lines to | |
3689 * have different heights (i.e. different fonts, rise, etc). | |
3690 * </p> | |
3691 * | |
3692 * @return line height in pixel. | |
3693 * @exception SWTException <ul> | |
3694 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3695 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3696 * </ul> | |
3697 * @see #getLineHeight(int) | |
3698 */ | |
3699 public int getLineHeight() { | |
3700 checkWidget(); | |
3701 return renderer.getLineHeight(); | |
3702 } | |
3703 /** | |
3704 * Returns the line height at the given offset. | |
3705 * | |
3706 * @param offset the offset | |
3707 * | |
3708 * @return line height in pixels | |
3709 * | |
3710 * @exception SWTException <ul> | |
3711 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3712 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3713 * </ul> | |
3714 * @exception IllegalArgumentException <ul> | |
3715 * <li>ERROR_INVALID_RANGE when the offset is outside the valid range (< 0 or > getCharCount())</li> | |
3716 * </ul> | |
3717 * | |
3718 * @since 3.2 | |
3719 */ | |
3720 public int getLineHeight(int offset) { | |
3721 checkWidget(); | |
3722 if (!(0 <= offset && offset <= content.getCharCount())) { | |
3723 SWT.error(SWT.ERROR_INVALID_RANGE); | |
3724 } | |
3725 if (isFixedLineHeight()) { | |
3726 return renderer.getLineHeight(); | |
3727 } | |
3728 int lineIndex = content.getLineAtOffset(offset); | |
3729 int lineOffset = content.getOffsetAtLine(lineIndex); | |
3730 TextLayout layout = renderer.getTextLayout(lineIndex); | |
3731 int lineInParagraph = layout.getLineIndex(Math.min(offset - lineOffset, layout.getText().length)); | |
3732 int height = layout.getLineBounds(lineInParagraph).height; | |
3733 renderer.disposeTextLayout(layout); | |
3734 return height; | |
3735 } | |
3736 /** | |
3737 * Returns the indentation of the line at the given index. | |
3738 * | |
3739 * @param index the index of the line | |
3740 * | |
3741 * @return the line indentation | |
3742 * | |
3743 * @exception SWTException <ul> | |
3744 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3745 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3746 * </ul> | |
3747 * @exception IllegalArgumentException <ul> | |
3748 * <li>ERROR_INVALID_ARGUMENT when the index is invalid</li> | |
3749 * </ul> | |
3750 * | |
3751 * @see #getIndent() | |
3752 * | |
3753 * @since 3.2 | |
3754 */ | |
3755 public int getLineIndent(int index) { | |
3756 checkWidget(); | |
3757 if (index < 0 || index > content.getLineCount()) { | |
3758 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
3759 } | |
3760 return isListening(LineGetStyle) ? 0 : renderer.getLineIndent(index, indent); | |
3761 } | |
3762 /** | |
3763 * Returns whether the line at the given index is justified. | |
3764 * | |
3765 * @param index the index of the line | |
3766 * | |
3767 * @return whether the line is justified | |
3768 * | |
3769 * @exception SWTException <ul> | |
3770 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3771 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3772 * </ul> | |
3773 * @exception IllegalArgumentException <ul> | |
3774 * <li>ERROR_INVALID_ARGUMENT when the index is invalid</li> | |
3775 * </ul> | |
3776 * | |
3777 * @see #getJustify() | |
3778 * | |
3779 * @since 3.2 | |
3780 */ | |
3781 public bool getLineJustify(int index) { | |
3782 checkWidget(); | |
3783 if (index < 0 || index > content.getLineCount()) { | |
3784 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
3785 } | |
3786 return isListening(LineGetStyle) ? false : renderer.getLineJustify(index, justify); | |
3787 } | |
3788 /** | |
3789 * Returns the line spacing of the widget. | |
3790 * | |
3791 * @return the line spacing | |
3792 * | |
3793 * @exception SWTException <ul> | |
3794 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3795 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3796 * </ul> | |
3797 * | |
3798 * @since 3.2 | |
3799 */ | |
3800 public int getLineSpacing() { | |
3801 checkWidget(); | |
3802 return lineSpacing; | |
3803 } | |
3804 /** | |
3805 * Returns the line style data for the given line or null if there is | |
3806 * none. | |
3807 * <p> | |
3808 * If there is a LineStyleListener but it does not set any styles, | |
3809 * the StyledTextEvent.styles field will be initialized to an empty | |
3810 * array. | |
3811 * </p> | |
3812 * | |
3813 * @param lineOffset offset of the line start relative to the start of | |
3814 * the content. | |
3815 * @param line line to get line styles for | |
3816 * @return line style data for the given line. Styles may start before | |
3817 * line start and end after line end | |
3818 */ | |
3819 StyledTextEvent getLineStyleData(int lineOffset, String line) { | |
3820 return sendLineEvent(LineGetStyle, lineOffset, line); | |
3821 } | |
3822 /** | |
3823 * Returns the top pixel, relative to the client area, of a given line. | |
3824 * Clamps out of ranges index. | |
3825 * | |
3826 * @param lineIndex the line index, the max value is lineCount. If | |
3827 * lineIndex is lineCount it returns the bottom pixel of the last line. | |
3828 * It means this function can be used to retrieve the bottom pixel of any line. | |
3829 * | |
3830 * @return the top pixel of a given line index | |
3831 * | |
3832 * @since 3.2 | |
3833 */ | |
3834 public int getLinePixel(int lineIndex) { | |
3835 checkWidget(); | |
3836 int lineCount = content.getLineCount(); | |
3837 lineIndex = Math.max(0, Math.min(lineCount, lineIndex)); | |
3838 if (isFixedLineHeight()) { | |
3839 int lineHeight = renderer.getLineHeight(); | |
3840 return lineIndex * lineHeight - getVerticalScrollOffset() + topMargin; | |
3841 } | |
3842 if (lineIndex is topIndex) return topIndexY + topMargin; | |
3843 int height = topIndexY; | |
3844 if (lineIndex > topIndex) { | |
3845 for (int i = topIndex; i < lineIndex; i++) { | |
3846 height += renderer.getLineHeight(i); | |
3847 } | |
3848 } else { | |
3849 for (int i = topIndex - 1; i >= lineIndex; i--) { | |
3850 height -= renderer.getLineHeight(i); | |
3851 } | |
3852 } | |
3853 return height + topMargin; | |
3854 } | |
3855 /** | |
3856 * Returns the line index for a y, relative to the client area. | |
3857 * The line index returned is always in the range 0..lineCount - 1. | |
3858 * | |
3859 * @param y the y-coordinate pixel | |
3860 * | |
3861 * @return the line index for a given y-coordinate pixel | |
3862 * | |
3863 * @since 3.2 | |
3864 */ | |
3865 public int getLineIndex(int y) { | |
3866 checkWidget(); | |
3867 y -= topMargin; | |
3868 if (isFixedLineHeight()) { | |
3869 int lineHeight = renderer.getLineHeight(); | |
3870 int lineIndex = (y + getVerticalScrollOffset()) / lineHeight; | |
3871 int lineCount = content.getLineCount(); | |
3872 lineIndex = Math.max(0, Math.min(lineCount - 1, lineIndex)); | |
3873 return lineIndex; | |
3874 } | |
3875 if (y is topIndexY) return topIndex; | |
3876 int line = topIndex; | |
3877 if (y < topIndexY) { | |
3878 while (y < topIndexY && line > 0) { | |
3879 y += renderer.getLineHeight(--line); | |
3880 } | |
3881 } else { | |
3882 int lineCount = content.getLineCount(); | |
3883 int lineHeight = renderer.getLineHeight(line); | |
3884 while (y - lineHeight >= topIndexY && line < lineCount - 1) { | |
3885 y -= lineHeight; | |
3886 lineHeight = renderer.getLineHeight(++line); | |
3887 } | |
3888 } | |
3889 return line; | |
3890 } | |
3891 /** | |
3892 * Returns the x, y location of the upper left corner of the character | |
3893 * bounding box at the specified offset in the text. The point is | |
3894 * relative to the upper left corner of the widget client area. | |
3895 * | |
3896 * @param offset offset relative to the start of the content. | |
3897 * 0 <= offset <= getCharCount() | |
3898 * @return x, y location of the upper left corner of the character | |
3899 * bounding box at the specified offset in the text. | |
3900 * @exception SWTException <ul> | |
3901 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3902 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3903 * </ul> | |
3904 * @exception IllegalArgumentException <ul> | |
3905 * <li>ERROR_INVALID_RANGE when the offset is outside the valid range (< 0 or > getCharCount())</li> | |
3906 * </ul> | |
3907 */ | |
3908 public Point getLocationAtOffset(int offset) { | |
3909 checkWidget(); | |
3910 if (offset < 0 || offset > getCharCount()) { | |
3911 SWT.error(SWT.ERROR_INVALID_RANGE); | |
3912 } | |
3913 return getPointAtOffset(offset); | |
3914 } | |
3915 /** | |
3916 * Returns the character offset of the first character of the given line. | |
3917 * | |
3918 * @param lineIndex index of the line, 0 based relative to the first | |
3919 * line in the content. 0 <= lineIndex < getLineCount(), except | |
3920 * lineIndex may always be 0 | |
3921 * @return offset offset of the first character of the line, relative to | |
3922 * the beginning of the document. The first character of the document is | |
3923 * at offset 0. | |
3924 * When there are not any lines, getOffsetAtLine(0) is a valid call that | |
3925 * answers 0. | |
3926 * @exception SWTException <ul> | |
3927 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3928 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3929 * </ul> | |
3930 * @exception IllegalArgumentException <ul> | |
3931 * <li>ERROR_INVALID_RANGE when the line index is outside the valid range (< 0 or >= getLineCount())</li> | |
3932 * </ul> | |
3933 * @since 2.0 | |
3934 */ | |
3935 public int getOffsetAtLine(int lineIndex) { | |
3936 checkWidget(); | |
3937 if (lineIndex < 0 || | |
3938 (lineIndex > 0 && lineIndex >= content.getLineCount())) { | |
3939 SWT.error(SWT.ERROR_INVALID_RANGE); | |
3940 } | |
3941 return content.getOffsetAtLine(lineIndex); | |
3942 } | |
3943 /** | |
3944 * Returns the offset of the character at the given location relative | |
3945 * to the first character in the document. | |
3946 * <p> | |
3947 * The return value reflects the character offset that the caret will | |
3948 * be placed at if a mouse click occurred at the specified location. | |
3949 * If the x coordinate of the location is beyond the center of a character | |
3950 * the returned offset will be behind the character. | |
3951 * </p> | |
3952 * | |
3953 * @param point the origin of character bounding box relative to | |
3954 * the origin of the widget client area. | |
3955 * @return offset of the character at the given location relative | |
3956 * to the first character in the document. | |
3957 * @exception SWTException <ul> | |
3958 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
3959 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
3960 * </ul> | |
3961 * @exception IllegalArgumentException <ul> | |
3962 * <li>ERROR_NULL_ARGUMENT when point is null</li> | |
3963 * <li>ERROR_INVALID_ARGUMENT when there is no character at the specified location</li> | |
3964 * </ul> | |
3965 */ | |
3966 public int getOffsetAtLocation(Point point) { | |
3967 checkWidget(); | |
3968 if (point is null) { | |
3969 SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
3970 } | |
3971 int[] trailing = new int[1]; | |
3972 int offset = getOffsetAtPoint(point.x, point.y, trailing, true); | |
3973 if (offset is -1) { | |
3974 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
3975 } | |
3976 return offset + trailing[0]; | |
3977 } | |
3978 int getOffsetAtPoint(int x, int y) { | |
3979 int lineIndex = getLineIndex(y); | |
3980 y -= getLinePixel(lineIndex); | |
3981 return getOffsetAtPoint(x, y, lineIndex); | |
3982 } | |
3983 /** | |
3984 * Returns the offset at the specified x location in the specified line. | |
3985 * | |
3986 * @param x x location of the mouse location | |
3987 * @param line line the mouse location is in | |
3988 * @return the offset at the specified x location in the specified line, | |
3989 * relative to the beginning of the document | |
3990 */ | |
3991 int getOffsetAtPoint(int x, int y, int lineIndex) { | |
3992 TextLayout layout = renderer.getTextLayout(lineIndex); | |
3993 x += horizontalScrollOffset - leftMargin; | |
3994 int[1] trailing; | |
3995 int offsetInLine = layout.getOffset(x, y, trailing); | |
3996 caretAlignment = OFFSET_LEADING; | |
3997 if (trailing[0] !is 0) { | |
3998 int lineInParagraph = layout.getLineIndex(offsetInLine + trailing[0]); | |
3999 int lineStart = layout.getLineOffsets()[lineInParagraph]; | |
4000 if (offsetInLine + trailing[0] is lineStart) { | |
4001 offsetInLine += trailing[0]; | |
4002 caretAlignment = PREVIOUS_OFFSET_TRAILING; | |
4003 } else { | |
4004 String line = content.getLine(lineIndex); | |
4005 int level; | |
4006 int offset = offsetInLine; | |
51 | 4007 while (offset > 0 && Character.isDigit(line.dcharAt(offset))) offset--; |
4008 if (offset is 0 && Character.isDigit(line.dcharAt(offset))) { | |
25 | 4009 level = isMirrored() ? 1 : 0; |
4010 } else { | |
4011 level = layout.getLevel(offset) & 0x1; | |
4012 } | |
4013 offsetInLine += trailing[0]; | |
4014 int trailingLevel = layout.getLevel(offsetInLine) & 0x1; | |
4015 if ((level ^ trailingLevel) !is 0) { | |
4016 caretAlignment = PREVIOUS_OFFSET_TRAILING; | |
4017 } else { | |
4018 caretAlignment = OFFSET_LEADING; | |
4019 } | |
4020 } | |
4021 } | |
4022 renderer.disposeTextLayout(layout); | |
4023 return offsetInLine + content.getOffsetAtLine(lineIndex); | |
4024 } | |
4025 int getOffsetAtPoint(int x, int y, int[] trailing, bool inTextOnly) { | |
4026 if (inTextOnly && y + getVerticalScrollOffset() < 0 || x + horizontalScrollOffset < 0) { | |
4027 return -1; | |
4028 } | |
4029 int bottomIndex = getPartialBottomIndex(); | |
4030 int height = getLinePixel(bottomIndex + 1); | |
4031 if (inTextOnly && y > height) { | |
4032 return -1; | |
4033 } | |
4034 int lineIndex = getLineIndex(y); | |
4035 int lineOffset = content.getOffsetAtLine(lineIndex); | |
4036 TextLayout layout = renderer.getTextLayout(lineIndex); | |
4037 x += horizontalScrollOffset - leftMargin ; | |
4038 y -= getLinePixel(lineIndex); | |
4039 int offset = layout.getOffset(x, y, trailing); | |
4040 Rectangle rect = layout.getLineBounds(layout.getLineIndex(offset)); | |
4041 renderer.disposeTextLayout(layout); | |
4042 if (inTextOnly && !(rect.x <= x && x <= rect.x + rect.width)) { | |
4043 return -1; | |
4044 } | |
4045 return offset + lineOffset; | |
4046 } | |
4047 /** | |
4048 * Returns the orientation of the receiver. | |
4049 * | |
4050 * @return the orientation style | |
4051 * | |
4052 * @exception SWTException <ul> | |
4053 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4054 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4055 * </ul> | |
4056 * | |
4057 * @since 2.1.2 | |
4058 */ | |
4059 public int getOrientation () { | |
4060 checkWidget(); | |
4061 return isMirrored() ? SWT.RIGHT_TO_LEFT : SWT.LEFT_TO_RIGHT; | |
4062 } | |
4063 /** | |
4064 * Returns the index of the last partially visible line. | |
4065 * | |
4066 * @return index of the last partially visible line. | |
4067 */ | |
4068 int getPartialBottomIndex() { | |
4069 if (isFixedLineHeight()) { | |
4070 int lineHeight = renderer.getLineHeight(); | |
4071 int partialLineCount = Compatibility.ceil(clientAreaHeight, lineHeight); | |
4072 return Math.max(0, Math.min(content.getLineCount(), topIndex + partialLineCount) - 1); | |
4073 } | |
4074 return getLineIndex(clientAreaHeight - bottomMargin); | |
4075 } | |
4076 /** | |
4077 * Returns the index of the first partially visible line. | |
4078 * | |
4079 * @return index of the first partially visible line. | |
4080 */ | |
4081 int getPartialTopIndex() { | |
4082 if (isFixedLineHeight()) { | |
4083 int lineHeight = renderer.getLineHeight(); | |
4084 return getVerticalScrollOffset() / lineHeight; | |
4085 } | |
4086 return topIndexY <= 0 ? topIndex : topIndex - 1; | |
4087 } | |
4088 /** | |
4089 * Returns the content in the specified range using the platform line | |
4090 * delimiter to separate lines. | |
4091 * | |
4092 * @param writer the TextWriter to write line text into | |
4093 * @return the content in the specified range using the platform line | |
4094 * delimiter to separate lines as written by the specified TextWriter. | |
4095 */ | |
4096 String getPlatformDelimitedText(TextWriter writer) { | |
4097 int end = writer.getStart() + writer.getCharCount(); | |
4098 int startLine = content.getLineAtOffset(writer.getStart()); | |
4099 int endLine = content.getLineAtOffset(end); | |
4100 String endLineText = content.getLine(endLine); | |
4101 int endLineOffset = content.getOffsetAtLine(endLine); | |
4102 | |
4103 for (int i = startLine; i <= endLine; i++) { | |
4104 writer.writeLine(content.getLine(i), content.getOffsetAtLine(i)); | |
4105 if (i < endLine) { | |
4106 writer.writeLineDelimiter(PlatformLineDelimiter); | |
4107 } | |
4108 } | |
4109 if (end > endLineOffset + endLineText.length) { | |
4110 writer.writeLineDelimiter(PlatformLineDelimiter); | |
4111 } | |
4112 writer.close(); | |
4113 return writer.toString(); | |
4114 } | |
4115 /** | |
4116 * Returns all the ranges of text that have an associated StyleRange. | |
4117 * Returns an empty array if a LineStyleListener has been set. | |
4118 * Should not be called if a LineStyleListener has been set since the | |
4119 * listener maintains the styles. | |
4120 * <p> | |
4121 * The ranges array contains start and length pairs. Each pair refers to | |
4122 * the corresponding style in the styles array. For example, the pair | |
4123 * that starts at ranges[n] with length ranges[n+1] uses the style | |
4124 * at styles[n/2] returned by <code>getStyleRanges(int, int, bool)</code>. | |
4125 * </p> | |
4126 * | |
4127 * @return the ranges or an empty array if a LineStyleListener has been set. | |
4128 * | |
4129 * @exception SWTException <ul> | |
4130 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4131 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4132 * </ul> | |
4133 * | |
4134 * @since 3.2 | |
4135 * | |
4136 * @see #getStyleRanges(bool) | |
4137 */ | |
4138 public int[] getRanges() { | |
4139 checkWidget(); | |
4140 if (!isListening(LineGetStyle)) { | |
4141 int[] ranges = renderer.getRanges(0, content.getCharCount()); | |
4142 if (ranges !is null) return ranges; | |
4143 } | |
4144 return new int[0]; | |
4145 } | |
4146 /** | |
4147 * Returns the ranges of text that have an associated StyleRange. | |
4148 * Returns an empty array if a LineStyleListener has been set. | |
4149 * Should not be called if a LineStyleListener has been set since the | |
4150 * listener maintains the styles. | |
4151 * <p> | |
4152 * The ranges array contains start and length pairs. Each pair refers to | |
4153 * the corresponding style in the styles array. For example, the pair | |
4154 * that starts at ranges[n] with length ranges[n+1] uses the style | |
4155 * at styles[n/2] returned by <code>getStyleRanges(int, int, bool)</code>. | |
4156 * </p> | |
4157 * | |
4158 * @param start the start offset of the style ranges to return | |
4159 * @param length the number of style ranges to return | |
4160 * | |
4161 * @return the ranges or an empty array if a LineStyleListener has been set. | |
4162 * | |
4163 * @exception SWTException <ul> | |
4164 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4165 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4166 * </ul> | |
4167 * @exception IllegalArgumentException <ul> | |
4168 * <li>ERROR_INVALID_RANGE if start or length are outside the widget content</li> | |
4169 * </ul> | |
4170 * | |
4171 * @since 3.2 | |
4172 * | |
4173 * @see #getStyleRanges(int, int, bool) | |
4174 */ | |
4175 public int[] getRanges(int start, int length) { | |
4176 checkWidget(); | |
4177 int contentLength = getCharCount(); | |
4178 int end = start + length; | |
4179 if (start > end || start < 0 || end > contentLength) { | |
4180 SWT.error(SWT.ERROR_INVALID_RANGE); | |
4181 } | |
4182 if (!isListening(LineGetStyle)) { | |
4183 int[] ranges = renderer.getRanges(start, length); | |
4184 if (ranges !is null) return ranges; | |
4185 } | |
4186 return new int[0]; | |
4187 } | |
4188 /** | |
4189 * Returns the selection. | |
4190 * <p> | |
4191 * Text selections are specified in terms of caret positions. In a text | |
4192 * widget that contains N characters, there are N+1 caret positions, | |
4193 * ranging from 0..N | |
4194 * </p> | |
4195 * | |
4196 * @return start and end of the selection, x is the offset of the first | |
4197 * selected character, y is the offset after the last selected character. | |
4198 * The selection values returned are visual (i.e., x will always always be | |
4199 * <= y). To determine if a selection is right-to-left (RtoL) vs. left-to-right | |
4200 * (LtoR), compare the caretOffset to the start and end of the selection | |
4201 * (e.g., caretOffset is start of selection implies that the selection is RtoL). | |
4202 * @see #getSelectionRange | |
4203 * @exception SWTException <ul> | |
4204 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4205 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4206 * </ul> | |
4207 */ | |
4208 public Point getSelection() { | |
4209 checkWidget(); | |
4210 return new Point(selection.x, selection.y); | |
4211 } | |
4212 /** | |
4213 * Returns the selection. | |
4214 * | |
4215 * @return start and length of the selection, x is the offset of the | |
4216 * first selected character, relative to the first character of the | |
4217 * widget content. y is the length of the selection. | |
4218 * The selection values returned are visual (i.e., length will always always be | |
4219 * positive). To determine if a selection is right-to-left (RtoL) vs. left-to-right | |
4220 * (LtoR), compare the caretOffset to the start and end of the selection | |
4221 * (e.g., caretOffset is start of selection implies that the selection is RtoL). | |
4222 * @exception SWTException <ul> | |
4223 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4224 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4225 * </ul> | |
4226 */ | |
4227 public Point getSelectionRange() { | |
4228 checkWidget(); | |
4229 return new Point(selection.x, selection.y - selection.x); | |
4230 } | |
4231 /** | |
4232 * Returns the receiver's selection background color. | |
4233 * | |
4234 * @return the selection background color | |
4235 * | |
4236 * @exception SWTException <ul> | |
4237 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4238 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4239 * </ul> | |
4240 * @since 2.1 | |
4241 */ | |
4242 public Color getSelectionBackground() { | |
4243 checkWidget(); | |
4244 if (selectionBackground is null) { | |
4245 return getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION); | |
4246 } | |
4247 return selectionBackground; | |
4248 } | |
4249 /** | |
4250 * Gets the number of selected characters. | |
4251 * | |
4252 * @return the number of selected characters. | |
4253 * @exception SWTException <ul> | |
4254 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4255 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4256 * </ul> | |
4257 */ | |
4258 public int getSelectionCount() { | |
4259 checkWidget(); | |
4260 return getSelectionRange().y; | |
4261 } | |
4262 /** | |
4263 * Returns the receiver's selection foreground color. | |
4264 * | |
4265 * @return the selection foreground color | |
4266 * | |
4267 * @exception SWTException <ul> | |
4268 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4269 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4270 * </ul> | |
4271 * @since 2.1 | |
4272 */ | |
4273 public Color getSelectionForeground() { | |
4274 checkWidget(); | |
4275 if (selectionForeground is null) { | |
4276 return getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT); | |
4277 } | |
4278 return selectionForeground; | |
4279 } | |
4280 /** | |
4281 * Returns the selected text. | |
4282 * | |
4283 * @return selected text, or an empty String if there is no selection. | |
4284 * @exception SWTException <ul> | |
4285 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4286 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4287 * </ul> | |
4288 */ | |
4289 public String getSelectionText() { | |
4290 checkWidget(); | |
4291 return content.getTextRange(selection.x, selection.y - selection.x); | |
4292 } | |
4293 public override int getStyle() { | |
4294 int style = super.getStyle(); | |
4295 style &= ~(SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT | SWT.MIRRORED); | |
4296 if (isMirrored()) { | |
4297 style |= SWT.RIGHT_TO_LEFT | SWT.MIRRORED; | |
4298 } else { | |
4299 style |= SWT.LEFT_TO_RIGHT; | |
4300 } | |
4301 return style; | |
4302 } | |
4303 | |
4304 /** | |
4305 * Returns the text segments that should be treated as if they | |
4306 * had a different direction than the surrounding text. | |
4307 * | |
4308 * @param lineOffset offset of the first character in the line. | |
4309 * 0 based from the beginning of the document. | |
4310 * @param line text of the line to specify bidi segments for | |
4311 * @return text segments that should be treated as if they had a | |
4312 * different direction than the surrounding text. Only the start | |
4313 * index of a segment is specified, relative to the start of the | |
4314 * line. Always starts with 0 and ends with the line length. | |
4315 * @exception IllegalArgumentException <ul> | |
4316 * <li>ERROR_INVALID_ARGUMENT - if the segment indices returned | |
4317 * by the listener do not start with 0, are not in ascending order, | |
4318 * exceed the line length or have duplicates</li> | |
4319 * </ul> | |
4320 */ | |
4321 int [] getBidiSegments(int lineOffset, String line) { | |
4322 if (!isBidi()) return null; | |
4323 if (!isListening(LineGetSegments)) { | |
4324 return getBidiSegmentsCompatibility(line, lineOffset); | |
4325 } | |
4326 StyledTextEvent event = sendLineEvent(LineGetSegments, lineOffset, line); | |
4327 int lineLength = line.length; | |
4328 int[] segments; | |
4329 if (event is null || event.segments is null || event.segments.length is 0) { | |
4330 segments = [0, lineLength]; | |
4331 } else { | |
4332 int segmentCount = event.segments.length; | |
4333 | |
4334 // test segment index consistency | |
4335 if (event.segments[0] !is 0) { | |
4336 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
4337 } | |
4338 for (int i = 1; i < segmentCount; i++) { | |
4339 if (event.segments[i] <= event.segments[i - 1] || event.segments[i] > lineLength) { | |
4340 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
4341 } | |
4342 } | |
4343 // ensure that last segment index is line end offset | |
4344 if (event.segments[segmentCount - 1] !is lineLength) { | |
4345 segments = new int[segmentCount + 1]; | |
4346 System.arraycopy(event.segments, 0, segments, 0, segmentCount); | |
4347 segments[segmentCount] = lineLength; | |
4348 } else { | |
4349 segments = event.segments; | |
4350 } | |
4351 } | |
4352 return segments; | |
4353 } | |
4354 /** | |
4355 * @see #getBidiSegments | |
4356 * Supports deprecated setBidiColoring API. Remove when API is removed. | |
4357 */ | |
4358 int [] getBidiSegmentsCompatibility(String line, int lineOffset) { | |
4359 int lineLength = line.length; | |
4360 if (!bidiColoring) { | |
4361 return [0, lineLength]; | |
4362 } | |
4363 StyleRange [] styles = null; | |
4364 StyledTextEvent event = getLineStyleData(lineOffset, line); | |
4365 if (event !is null) { | |
4366 styles = event.styles; | |
4367 } else { | |
4368 styles = renderer.getStyleRanges(lineOffset, lineLength, true); | |
4369 } | |
4370 if (styles is null || styles.length is 0) { | |
4371 return [0, lineLength]; | |
4372 } | |
4373 int k=0, count = 1; | |
4374 while (k < styles.length && styles[k].start is 0 && styles[k].length is lineLength) { | |
4375 k++; | |
4376 } | |
4377 int[] offsets = new int[(styles.length - k) * 2 + 2]; | |
4378 for (int i = k; i < styles.length; i++) { | |
4379 StyleRange style = styles[i]; | |
4380 int styleLineStart = Math.max(style.start - lineOffset, 0); | |
4381 int styleLineEnd = Math.max(style.start + style.length - lineOffset, styleLineStart); | |
4382 styleLineEnd = Math.min (styleLineEnd, line.length ); | |
4383 if (i > 0 && count > 1 && | |
4384 ((styleLineStart >= offsets[count-2] && styleLineStart <= offsets[count-1]) || | |
4385 (styleLineEnd >= offsets[count-2] && styleLineEnd <= offsets[count-1])) && | |
4386 style.similarTo(styles[i-1])) { | |
4387 offsets[count-2] = Math.min(offsets[count-2], styleLineStart); | |
4388 offsets[count-1] = Math.max(offsets[count-1], styleLineEnd); | |
4389 } else { | |
4390 if (styleLineStart > offsets[count - 1]) { | |
4391 offsets[count] = styleLineStart; | |
4392 count++; | |
4393 } | |
4394 offsets[count] = styleLineEnd; | |
4395 count++; | |
4396 } | |
4397 } | |
4398 // add offset for last non-colored segment in line, if any | |
4399 if (lineLength > offsets[count-1]) { | |
4400 offsets [count] = lineLength; | |
4401 count++; | |
4402 } | |
4403 if (count is offsets.length) { | |
4404 return offsets; | |
4405 } | |
4406 int [] result = new int [count]; | |
4407 System.arraycopy (offsets, 0, result, 0, count); | |
4408 return result; | |
4409 } | |
4410 /** | |
4411 * Returns the style range at the given offset. | |
4412 * <p> | |
4413 * Returns null if a LineStyleListener has been set or if a style is not set | |
4414 * for the offset. | |
4415 * Should not be called if a LineStyleListener has been set since the | |
4416 * listener maintains the styles. | |
4417 * </p> | |
4418 * | |
4419 * @param offset the offset to return the style for. | |
4420 * 0 <= offset < getCharCount() must be true. | |
4421 * @return a StyleRange with start is offset and length is 1, indicating | |
4422 * the style at the given offset. null if a LineStyleListener has been set | |
4423 * or if a style is not set for the given offset. | |
4424 * @exception SWTException <ul> | |
4425 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4426 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4427 * </ul> | |
4428 * @exception IllegalArgumentException <ul> | |
4429 * <li>ERROR_INVALID_ARGUMENT when the offset is invalid</li> | |
4430 * </ul> | |
4431 */ | |
4432 public StyleRange getStyleRangeAtOffset(int offset) { | |
4433 checkWidget(); | |
4434 if (offset < 0 || offset >= getCharCount()) { | |
4435 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
4436 } | |
4437 if (!isListening(LineGetStyle)) { | |
4438 StyleRange[] ranges = renderer.getStyleRanges(offset, 1, true); | |
4439 if (ranges !is null) return ranges[0]; | |
4440 } | |
4441 return null; | |
4442 } | |
4443 /** | |
4444 * Returns the styles. | |
4445 * <p> | |
4446 * Returns an empty array if a LineStyleListener has been set. | |
4447 * Should not be called if a LineStyleListener has been set since the | |
4448 * listener maintains the styles. | |
4449 * <p></p> | |
4450 * Note: Because a StyleRange includes the start and length, the | |
4451 * same instance cannot occur multiple times in the array of styles. | |
4452 * If the same style attributes, such as font and color, occur in | |
4453 * multiple StyleRanges, <code>getStyleRanges(bool)</code> | |
4454 * can be used to get the styles without the ranges. | |
4455 * </p> | |
4456 * | |
4457 * @return the styles or an empty array if a LineStyleListener has been set. | |
4458 * | |
4459 * @exception SWTException <ul> | |
4460 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4461 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4462 * </ul> | |
4463 * | |
4464 * @see #getStyleRanges(bool) | |
4465 */ | |
4466 public StyleRange[] getStyleRanges() { | |
4467 checkWidget(); | |
4468 return getStyleRanges(0, content.getCharCount(), true); | |
4469 } | |
4470 /** | |
4471 * Returns the styles. | |
4472 * <p> | |
4473 * Returns an empty array if a LineStyleListener has been set. | |
4474 * Should not be called if a LineStyleListener has been set since the | |
4475 * listener maintains the styles. | |
4476 * </p><p> | |
4477 * Note: When <code>includeRanges</code> is true, the start and length | |
4478 * fields of each StyleRange will be valid, however the StyleRange | |
4479 * objects may need to be cloned. When <code>includeRanges</code> is | |
4480 * false, <code>getRanges(int, int)</code> can be used to get the | |
4481 * associated ranges. | |
4482 * </p> | |
4483 * | |
4484 * @param includeRanges whether the start and length field of the StyleRanges should be set. | |
4485 * | |
4486 * @return the styles or an empty array if a LineStyleListener has been set. | |
4487 * | |
4488 * @exception SWTException <ul> | |
4489 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4490 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4491 * </ul> | |
4492 * | |
4493 * @since 3.2 | |
4494 * | |
4495 * @see #getRanges(int, int) | |
4496 * @see #setStyleRanges(int[], StyleRange[]) | |
4497 */ | |
4498 public StyleRange[] getStyleRanges(bool includeRanges) { | |
4499 checkWidget(); | |
4500 return getStyleRanges(0, content.getCharCount(), includeRanges); | |
4501 } | |
4502 /** | |
4503 * Returns the styles for the given text range. | |
4504 * <p> | |
4505 * Returns an empty array if a LineStyleListener has been set. | |
4506 * Should not be called if a LineStyleListener has been set since the | |
4507 * listener maintains the styles. | |
4508 * </p><p> | |
4509 * Note: Because the StyleRange includes the start and length, the | |
4510 * same instance cannot occur multiple times in the array of styles. | |
4511 * If the same style attributes, such as font and color, occur in | |
4512 * multiple StyleRanges, <code>getStyleRanges(int, int, bool)</code> | |
4513 * can be used to get the styles without the ranges. | |
4514 * </p> | |
4515 * @param start the start offset of the style ranges to return | |
4516 * @param length the number of style ranges to return | |
4517 * | |
4518 * @return the styles or an empty array if a LineStyleListener has | |
4519 * been set. The returned styles will reflect the given range. The first | |
4520 * returned <code>StyleRange</code> will have a starting offset >= start | |
4521 * and the last returned <code>StyleRange</code> will have an ending | |
4522 * offset <= start + length - 1 | |
4523 * | |
4524 * @exception SWTException <ul> | |
4525 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4526 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4527 * </ul> | |
4528 * @exception IllegalArgumentException <ul> | |
4529 * <li>ERROR_INVALID_RANGE when start and/or end are outside the widget content</li> | |
4530 * </ul> | |
4531 * | |
4532 * @see #getStyleRanges(int, int, bool) | |
4533 * | |
4534 * @since 3.0 | |
4535 */ | |
4536 public StyleRange[] getStyleRanges(int start, int length) { | |
4537 checkWidget(); | |
4538 return getStyleRanges(start, length, true); | |
4539 } | |
4540 /** | |
4541 * Returns the styles for the given text range. | |
4542 * <p> | |
4543 * Returns an empty array if a LineStyleListener has been set. | |
4544 * Should not be called if a LineStyleListener has been set since the | |
4545 * listener maintains the styles. | |
4546 * </p><p> | |
4547 * Note: When <code>includeRanges</code> is true, the start and length | |
4548 * fields of each StyleRange will be valid, however the StyleRange | |
4549 * objects may need to be cloned. When <code>includeRanges</code> is | |
4550 * false, <code>getRanges(int, int)</code> can be used to get the | |
4551 * associated ranges. | |
4552 * </p> | |
4553 * | |
4554 * @param start the start offset of the style ranges to return | |
4555 * @param length the number of style ranges to return | |
4556 * @param includeRanges whether the start and length field of the StyleRanges should be set. | |
4557 * | |
4558 * @return the styles or an empty array if a LineStyleListener has | |
4559 * been set. The returned styles will reflect the given range. The first | |
4560 * returned <code>StyleRange</code> will have a starting offset >= start | |
4561 * and the last returned <code>StyleRange</code> will have an ending | |
4562 * offset <= start + length - 1 | |
4563 * | |
4564 * @exception SWTException <ul> | |
4565 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4566 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4567 * </ul> | |
4568 * @exception IllegalArgumentException <ul> | |
4569 * <li>ERROR_INVALID_RANGE when start and/or end are outside the widget content</li> | |
4570 * </ul> | |
4571 * | |
4572 * @since 3.2 | |
4573 * | |
4574 * @see #getRanges(int, int) | |
4575 * @see #setStyleRanges(int[], StyleRange[]) | |
4576 */ | |
4577 public StyleRange[] getStyleRanges(int start, int length, bool includeRanges) { | |
4578 checkWidget(); | |
4579 int contentLength = getCharCount(); | |
4580 int end = start + length; | |
4581 if (start > end || start < 0 || end > contentLength) { | |
4582 SWT.error(SWT.ERROR_INVALID_RANGE); | |
4583 } | |
4584 if (!isListening(LineGetStyle)) { | |
4585 StyleRange[] ranges = renderer.getStyleRanges(start, length, includeRanges); | |
4586 if (ranges !is null) return ranges; | |
4587 } | |
4588 return new StyleRange[0]; | |
4589 } | |
4590 /** | |
4591 * Returns the tab width measured in characters. | |
4592 * | |
4593 * @return tab width measured in characters | |
4594 * @exception SWTException <ul> | |
4595 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4596 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4597 * </ul> | |
4598 */ | |
4599 public int getTabs() { | |
4600 checkWidget(); | |
4601 return tabLength; | |
4602 } | |
4603 /** | |
4604 * Returns a copy of the widget content. | |
4605 * | |
4606 * @return copy of the widget content | |
4607 * @exception SWTException <ul> | |
4608 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4609 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4610 * </ul> | |
4611 */ | |
4612 public String getText() { | |
4613 checkWidget(); | |
4614 return content.getTextRange(0, getCharCount()); | |
4615 } | |
4616 /** | |
4617 * Returns the widget content between the two offsets. | |
4618 * | |
4619 * @param start offset of the first character in the returned String | |
4620 * @param end offset of the last character in the returned String | |
4621 * @return widget content starting at start and ending at end | |
4622 * @see #getTextRange(int,int) | |
4623 * @exception SWTException <ul> | |
4624 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4625 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4626 * </ul> | |
4627 * @exception IllegalArgumentException <ul> | |
4628 * <li>ERROR_INVALID_RANGE when start and/or end are outside the widget content</li> | |
4629 * </ul> | |
4630 */ | |
4631 public String getText(int start, int end) { | |
4632 checkWidget(); | |
4633 int contentLength = getCharCount(); | |
4634 if (start < 0 || start >= contentLength || end < 0 || end >= contentLength || start > end) { | |
4635 SWT.error(SWT.ERROR_INVALID_RANGE); | |
4636 } | |
4637 return content.getTextRange(start, end - start + 1); | |
4638 } | |
4639 /** | |
4640 * Returns the smallest bounding rectangle that includes the characters between two offsets. | |
4641 * | |
4642 * @param start offset of the first character included in the bounding box | |
4643 * @param end offset of the last character included in the bounding box | |
4644 * @return bounding box of the text between start and end | |
4645 * @exception SWTException <ul> | |
4646 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4647 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4648 * </ul> | |
4649 * @exception IllegalArgumentException <ul> | |
4650 * <li>ERROR_INVALID_RANGE when start and/or end are outside the widget content</li> | |
4651 * </ul> | |
4652 * @since 3.1 | |
4653 */ | |
4654 public Rectangle getTextBounds(int start, int end) { | |
4655 checkWidget(); | |
4656 int contentLength = getCharCount(); | |
4657 if (start < 0 || start >= contentLength || end < 0 || end >= contentLength || start > end) { | |
4658 SWT.error(SWT.ERROR_INVALID_RANGE); | |
4659 } | |
4660 int lineStart = content.getLineAtOffset(start); | |
4661 int lineEnd = content.getLineAtOffset(end); | |
4662 Rectangle rect; | |
4663 int y = getLinePixel(lineStart); | |
4664 int height = 0; | |
4665 int left = 0x7fffffff, right = 0; | |
4666 for (int i = lineStart; i <= lineEnd; i++) { | |
4667 int lineOffset = content.getOffsetAtLine(i); | |
4668 TextLayout layout = renderer.getTextLayout(i); | |
4669 int length = layout.getText().length; | |
4670 if (length > 0) { | |
4671 if (i is lineStart) { | |
4672 if (i is lineEnd) { | |
4673 rect = layout.getBounds(start - lineOffset, end - lineOffset); | |
4674 } else { | |
4675 rect = layout.getBounds(start - lineOffset, length); | |
4676 } | |
4677 y += rect.y; | |
4678 } else if (i is lineEnd) { | |
4679 rect = layout.getBounds(0, end - lineOffset); | |
4680 } else { | |
4681 rect = layout.getBounds(); | |
4682 } | |
4683 left = Math.min(left, rect.x); | |
4684 right = Math.max(right, rect.x + rect.width); | |
4685 height += rect.height; | |
4686 } else { | |
4687 height += renderer.getLineHeight(); | |
4688 } | |
4689 renderer.disposeTextLayout(layout); | |
4690 } | |
4691 rect = new Rectangle (left, y, right-left, height); | |
4692 rect.x += leftMargin - horizontalScrollOffset; | |
4693 return rect; | |
4694 } | |
4695 /** | |
4696 * Returns the widget content starting at start for length characters. | |
4697 * | |
4698 * @param start offset of the first character in the returned String | |
4699 * @param length number of characters to return | |
4700 * @return widget content starting at start and extending length characters. | |
4701 * @exception SWTException <ul> | |
4702 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4703 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4704 * </ul> | |
4705 * @exception IllegalArgumentException <ul> | |
4706 * <li>ERROR_INVALID_RANGE when start and/or length are outside the widget content</li> | |
4707 * </ul> | |
4708 */ | |
4709 public String getTextRange(int start, int length) { | |
4710 checkWidget(); | |
4711 int contentLength = getCharCount(); | |
4712 int end = start + length; | |
4713 if (start > end || start < 0 || end > contentLength) { | |
4714 SWT.error(SWT.ERROR_INVALID_RANGE); | |
4715 } | |
4716 return content.getTextRange(start, length); | |
4717 } | |
4718 /** | |
4719 * Returns the maximum number of characters that the receiver is capable of holding. | |
4720 * | |
4721 * @return the text limit | |
4722 * | |
4723 * @exception SWTException <ul> | |
4724 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4725 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4726 * </ul> | |
4727 */ | |
4728 public int getTextLimit() { | |
4729 checkWidget(); | |
4730 return textLimit; | |
4731 } | |
4732 /** | |
4733 * Gets the top index. | |
4734 * <p> | |
4735 * The top index is the index of the fully visible line that is currently | |
4736 * at the top of the widget or the topmost partially visible line if no line is fully visible. | |
4737 * The top index changes when the widget is scrolled. Indexing is zero based. | |
4738 * </p> | |
4739 * | |
4740 * @return the index of the top line | |
4741 * @exception SWTException <ul> | |
4742 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4743 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4744 * </ul> | |
4745 */ | |
4746 public int getTopIndex() { | |
4747 checkWidget(); | |
4748 return topIndex; | |
4749 } | |
4750 /** | |
4751 * Gets the top pixel. | |
4752 * <p> | |
4753 * The top pixel is the pixel position of the line that is | |
4754 * currently at the top of the widget. The text widget can be scrolled by pixels | |
4755 * by dragging the scroll thumb so that a partial line may be displayed at the top | |
4756 * the widget. The top pixel changes when the widget is scrolled. The top pixel | |
4757 * does not include the widget trimming. | |
4758 * </p> | |
4759 * | |
4760 * @return pixel position of the top line | |
4761 * @exception SWTException <ul> | |
4762 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4763 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4764 * </ul> | |
4765 */ | |
4766 public int getTopPixel() { | |
4767 checkWidget(); | |
4768 return getVerticalScrollOffset(); | |
4769 } | |
4770 /** | |
4771 * Returns the vertical scroll increment. | |
4772 * | |
4773 * @return vertical scroll increment. | |
4774 */ | |
4775 int getVerticalIncrement() { | |
4776 return renderer.getLineHeight(); | |
4777 } | |
4778 int getVerticalScrollOffset() { | |
4779 if (verticalScrollOffset is -1) { | |
4780 renderer.calculate(0, topIndex); | |
4781 int height = 0; | |
4782 for (int i = 0; i < topIndex; i++) { | |
4783 height += renderer.getLineHeight(i); | |
4784 } | |
4785 height -= topIndexY; | |
4786 verticalScrollOffset = height; | |
4787 } | |
4788 return verticalScrollOffset; | |
4789 } | |
4790 int getVisualLineIndex(TextLayout layout, int offsetInLine) { | |
4791 int lineIndex = layout.getLineIndex(offsetInLine); | |
4792 int[] offsets = layout.getLineOffsets(); | |
4793 if (lineIndex !is 0 && offsetInLine is offsets[lineIndex]) { | |
4794 int lineY = layout.getLineBounds(lineIndex).y; | |
4795 int caretY = getCaret().getLocation().y - topMargin - getLinePixel(getCaretLine()); | |
4796 if (lineY > caretY) lineIndex--; | |
4797 } | |
4798 return lineIndex; | |
4799 } | |
4800 int getCaretDirection() { | |
4801 if (!isBidiCaret()) return SWT.DEFAULT; | |
4802 if (ime.getCompositionOffset() !is -1) return SWT.DEFAULT; | |
4803 if (!updateCaretDirection && caretDirection !is SWT.NULL) return caretDirection; | |
4804 updateCaretDirection = false; | |
4805 int caretLine = getCaretLine(); | |
4806 int lineOffset = content.getOffsetAtLine(caretLine); | |
4807 String line = content.getLine(caretLine); | |
4808 int offset = caretOffset - lineOffset; | |
4809 int lineLength = line.length; | |
4810 if (lineLength is 0) return isMirrored() ? SWT.RIGHT : SWT.LEFT; | |
4811 if (caretAlignment is PREVIOUS_OFFSET_TRAILING && offset > 0) offset--; | |
4812 if (offset is lineLength && offset > 0) offset--; | |
51 | 4813 while (offset > 0 && Character.isDigit(line.dcharAt(offset))) offset--; |
4814 if (offset is 0 && Character.isDigit(line.dcharAt(offset))) { | |
25 | 4815 return isMirrored() ? SWT.RIGHT : SWT.LEFT; |
4816 } | |
4817 TextLayout layout = renderer.getTextLayout(caretLine); | |
4818 int level = layout.getLevel(offset); | |
4819 renderer.disposeTextLayout(layout); | |
4820 return ((level & 1) !is 0) ? SWT.RIGHT : SWT.LEFT; | |
4821 } | |
4822 /* | |
4823 * Returns the index of the line the caret is on. | |
4824 */ | |
4825 int getCaretLine() { | |
4826 return content.getLineAtOffset(caretOffset); | |
4827 } | |
4828 int getWrapWidth () { | |
4829 if (wordWrap && !isSingleLine()) { | |
4830 int width = clientAreaWidth - leftMargin - rightMargin - getCaretWidth(); | |
4831 return width > 0 ? width : 1; | |
4832 } | |
4833 return -1; | |
4834 } | |
4835 int getWordNext (int offset, int movement) { | |
4836 int newOffset, lineOffset; | |
4837 String lineText; | |
4838 if (offset >= getCharCount()) { | |
4839 newOffset = offset; | |
4840 int lineIndex = content.getLineCount() - 1; | |
4841 lineOffset = content.getOffsetAtLine(lineIndex); | |
4842 lineText = content.getLine(lineIndex); | |
4843 } else { | |
4844 int lineIndex = content.getLineAtOffset(offset); | |
4845 lineOffset = content.getOffsetAtLine(lineIndex); | |
4846 lineText = content.getLine(lineIndex); | |
4847 int lineLength = lineText.length; | |
4848 if (offset is lineOffset + lineLength) { | |
4849 newOffset = content.getOffsetAtLine(lineIndex + 1); | |
4850 } else { | |
4851 TextLayout layout = renderer.getTextLayout(lineIndex); | |
4852 newOffset = lineOffset + layout.getNextOffset(offset - lineOffset, movement); | |
4853 renderer.disposeTextLayout(layout); | |
4854 } | |
4855 } | |
4856 return sendWordBoundaryEvent(WordNext, movement, offset, newOffset, lineText, lineOffset); | |
4857 } | |
4858 int getWordPrevious(int offset, int movement) { | |
4859 int newOffset, lineOffset; | |
4860 String lineText; | |
4861 if (offset <= 0) { | |
4862 newOffset = 0; | |
4863 int lineIndex = content.getLineAtOffset(newOffset); | |
4864 lineOffset = content.getOffsetAtLine(lineIndex); | |
4865 lineText = content.getLine(lineIndex); | |
4866 } else { | |
4867 int lineIndex = content.getLineAtOffset(offset); | |
4868 lineOffset = content.getOffsetAtLine(lineIndex); | |
4869 lineText = content.getLine(lineIndex); | |
4870 if (offset is lineOffset) { | |
4871 String nextLineText = content.getLine(lineIndex - 1); | |
4872 int nextLineOffset = content.getOffsetAtLine(lineIndex - 1); | |
4873 newOffset = nextLineOffset + nextLineText.length; | |
4874 } else { | |
4875 TextLayout layout = renderer.getTextLayout(lineIndex); | |
4876 newOffset = lineOffset + layout.getPreviousOffset(offset - lineOffset, movement); | |
4877 renderer.disposeTextLayout(layout); | |
4878 } | |
4879 } | |
4880 return sendWordBoundaryEvent(WordPrevious, movement, offset, newOffset, lineText, lineOffset); | |
4881 } | |
4882 /** | |
4883 * Returns whether the widget wraps lines. | |
4884 * | |
4885 * @return true if widget wraps lines, false otherwise | |
4886 * @since 2.0 | |
4887 */ | |
4888 public bool getWordWrap() { | |
4889 checkWidget(); | |
4890 return wordWrap; | |
4891 } | |
4892 /** | |
4893 * Returns the location of the given offset. | |
4894 * <p> | |
4895 * <b>NOTE:</b> Does not return correct values for true italic fonts (vs. slanted fonts). | |
4896 * </p> | |
4897 * | |
4898 * @return location of the character at the given offset in the line. | |
4899 */ | |
4900 Point getPointAtOffset(int offset) { | |
4901 int lineIndex = content.getLineAtOffset(offset); | |
4902 String line = content.getLine(lineIndex); | |
4903 int lineOffset = content.getOffsetAtLine(lineIndex); | |
4904 int offsetInLine = offset - lineOffset; | |
4905 int lineLength = line.length; | |
4906 if (lineIndex < content.getLineCount() - 1) { | |
4907 int endLineOffset = content.getOffsetAtLine(lineIndex + 1) - 1; | |
4908 if (lineLength < offsetInLine && offsetInLine <= endLineOffset) { | |
4909 offsetInLine = lineLength; | |
4910 } | |
4911 } | |
4912 Point point; | |
4913 TextLayout layout = renderer.getTextLayout(lineIndex); | |
4914 if (lineLength !is 0 && offsetInLine <= lineLength) { | |
4915 if (offsetInLine is lineLength) { | |
4916 // SWT: Instead of go back one byte, go back one codepoint | |
4917 int offsetInLine_m1 = layout.getPreviousOffset(offsetInLine, SWT.MOVEMENT_CLUSTER); | |
4918 point = layout.getLocation(offsetInLine_m1, true); | |
4919 } else { | |
4920 switch (caretAlignment) { | |
4921 case OFFSET_LEADING: | |
4922 point = layout.getLocation(offsetInLine, false); | |
4923 break; | |
4924 case PREVIOUS_OFFSET_TRAILING: | |
4925 default: | |
4926 if (offsetInLine is 0) { | |
4927 point = layout.getLocation(offsetInLine, false); | |
4928 } else { | |
4929 // SWT: Instead of go back one byte, go back one codepoint | |
4930 int offsetInLine_m1 = layout.getPreviousOffset(offsetInLine, SWT.MOVEMENT_CLUSTER); | |
4931 point = layout.getLocation(offsetInLine_m1, true); | |
4932 } | |
4933 break; | |
4934 } | |
4935 } | |
4936 } else { | |
4937 point = new Point(layout.getIndent(), 0); | |
4938 } | |
4939 renderer.disposeTextLayout(layout); | |
4940 point.x += leftMargin - horizontalScrollOffset; | |
4941 point.y += getLinePixel(lineIndex); | |
4942 return point; | |
4943 } | |
4944 /** | |
4945 * Inserts a string. The old selection is replaced with the new text. | |
4946 * | |
4947 * @param string the string | |
4948 * @see #replaceTextRange(int,int,String) | |
4949 * @exception SWTException <ul> | |
4950 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
4951 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
4952 * </ul> | |
4953 */ | |
4954 public void insert(String string) { | |
4955 checkWidget(); | |
4956 // SWT extension: allow null for zero length string | |
4957 // if (string is null) { | |
4958 // SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
4959 // } | |
4960 Point sel = getSelectionRange(); | |
4961 replaceTextRange(sel.x, sel.y, string); | |
4962 } | |
4963 /** | |
4964 * Creates content change listeners and set the default content model. | |
4965 */ | |
4966 void installDefaultContent() { | |
4967 textChangeListener = new class() TextChangeListener { | |
4968 public void textChanging(TextChangingEvent event) { | |
4969 handleTextChanging(event); | |
4970 } | |
4971 public void textChanged(TextChangedEvent event) { | |
4972 handleTextChanged(event); | |
4973 } | |
4974 public void textSet(TextChangedEvent event) { | |
4975 handleTextSet(event); | |
4976 } | |
4977 }; | |
4978 content = new DefaultContent(); | |
4979 content.addTextChangeListener(textChangeListener); | |
4980 } | |
4981 /** | |
4982 * Adds event listeners | |
4983 */ | |
4984 void installListeners() { | |
4985 ScrollBar verticalBar = getVerticalBar(); | |
4986 ScrollBar horizontalBar = getHorizontalBar(); | |
4987 | |
4988 listener = new class() Listener { | |
4989 public void handleEvent(Event event) { | |
4990 switch (event.type) { | |
4991 case SWT.Dispose: handleDispose(event); break; | |
4992 case SWT.KeyDown: handleKeyDown(event); break; | |
4993 case SWT.KeyUp: handleKeyUp(event); break; | |
4994 case SWT.MouseDown: handleMouseDown(event); break; | |
4995 case SWT.MouseUp: handleMouseUp(event); break; | |
4996 case SWT.MouseMove: handleMouseMove(event); break; | |
4997 case SWT.Paint: handlePaint(event); break; | |
4998 case SWT.Resize: handleResize(event); break; | |
4999 case SWT.Traverse: handleTraverse(event); break; | |
5000 default: | |
5001 } | |
5002 } | |
5003 }; | |
5004 addListener(SWT.Dispose, listener); | |
5005 addListener(SWT.KeyDown, listener); | |
5006 addListener(SWT.KeyUp, listener); | |
5007 addListener(SWT.MouseDown, listener); | |
5008 addListener(SWT.MouseUp, listener); | |
5009 addListener(SWT.MouseMove, listener); | |
5010 addListener(SWT.Paint, listener); | |
5011 addListener(SWT.Resize, listener); | |
5012 addListener(SWT.Traverse, listener); | |
5013 ime.addListener(SWT.ImeComposition, new class() Listener { | |
5014 public void handleEvent(Event event) { | |
5015 switch (event.detail) { | |
5016 case SWT.COMPOSITION_SELECTION: handleCompositionSelection(event); break; | |
5017 case SWT.COMPOSITION_CHANGED: handleCompositionChanged(event); break; | |
5018 case SWT.COMPOSITION_OFFSET: handleCompositionOffset(event); break; | |
5019 default: | |
5020 } | |
5021 } | |
5022 }); | |
5023 if (verticalBar !is null) { | |
5024 verticalBar.addListener(SWT.Selection, new class() Listener { | |
5025 public void handleEvent(Event event) { | |
5026 handleVerticalScroll(event); | |
5027 } | |
5028 }); | |
5029 } | |
5030 if (horizontalBar !is null) { | |
5031 horizontalBar.addListener(SWT.Selection, new class() Listener { | |
5032 public void handleEvent(Event event) { | |
5033 handleHorizontalScroll(event); | |
5034 } | |
5035 }); | |
5036 } | |
5037 } | |
5038 void internalRedrawRange(int start, int length) { | |
5039 if (length <= 0) return; | |
5040 int end = start + length; | |
5041 int startLine = content.getLineAtOffset(start); | |
5042 int endLine = content.getLineAtOffset(end); | |
5043 int partialBottomIndex = getPartialBottomIndex(); | |
5044 int partialTopIndex = getPartialTopIndex(); | |
5045 if (startLine > partialBottomIndex || endLine < partialTopIndex) { | |
5046 return; | |
5047 } | |
5048 if (partialTopIndex > startLine) { | |
5049 startLine = partialTopIndex; | |
5050 start = 0; | |
5051 } else { | |
5052 start -= content.getOffsetAtLine(startLine); | |
5053 } | |
5054 if (partialBottomIndex < endLine) { | |
5055 endLine = partialBottomIndex + 1; | |
5056 end = 0; | |
5057 } else { | |
5058 end -= content.getOffsetAtLine(endLine); | |
5059 } | |
5060 | |
5061 TextLayout layout = renderer.getTextLayout(startLine); | |
5062 int lineX = leftMargin - horizontalScrollOffset, startLineY = getLinePixel(startLine); | |
5063 int[] offsets = layout.getLineOffsets(); | |
5064 int startIndex = layout.getLineIndex(Math.min(start, layout.getText().length)); | |
5065 | |
5066 /* Redraw end of line before start line if wrapped and start offset is first char */ | |
5067 if (wordWrap && startIndex > 0 && offsets[startIndex] is start) { | |
5068 Rectangle rect = layout.getLineBounds(startIndex - 1); | |
5069 rect.x = rect.width; | |
5070 rect.width = clientAreaWidth - rightMargin - rect.x; | |
5071 rect.x += lineX; | |
5072 rect.y += startLineY; | |
5073 super.redraw(rect.x, rect.y, rect.width, rect.height, false); | |
5074 } | |
5075 | |
5076 if (startLine is endLine) { | |
5077 int endIndex = layout.getLineIndex(Math.min(end, layout.getText().length)); | |
5078 if (startIndex is endIndex) { | |
5079 /* Redraw rect between start and end offset if start and end offsets are in same wrapped line */ | |
5080 Rectangle rect = layout.getBounds(start, end - 1); | |
5081 rect.x += lineX; | |
5082 rect.y += startLineY; | |
5083 super.redraw(rect.x, rect.y, rect.width, rect.height, false); | |
5084 renderer.disposeTextLayout(layout); | |
5085 return; | |
5086 } | |
5087 } | |
5088 | |
5089 /* Redraw start line from the start offset to the end of client area */ | |
5090 Rectangle startRect = layout.getBounds(start, offsets[startIndex + 1] - 1); | |
5091 if (startRect.height is 0) { | |
5092 Rectangle bounds = layout.getLineBounds(startIndex); | |
5093 startRect.x = bounds.width; | |
5094 startRect.y = bounds.y; | |
5095 startRect.height = bounds.height; | |
5096 } | |
5097 startRect.x += lineX; | |
5098 startRect.y += startLineY; | |
5099 startRect.width = clientAreaWidth - rightMargin - startRect.x; | |
5100 super.redraw(startRect.x, startRect.y, startRect.width, startRect.height, false); | |
5101 | |
5102 /* Redraw end line from the beginning of the line to the end offset */ | |
5103 if (startLine !is endLine) { | |
5104 renderer.disposeTextLayout(layout); | |
5105 layout = renderer.getTextLayout(endLine); | |
5106 offsets = layout.getLineOffsets(); | |
5107 } | |
5108 int endIndex = layout.getLineIndex(Math.min(end, layout.getText().length)); | |
5109 Rectangle endRect = layout.getBounds(offsets[endIndex], end - 1); | |
5110 if (endRect.height is 0) { | |
5111 Rectangle bounds = layout.getLineBounds(endIndex); | |
5112 endRect.y = bounds.y; | |
5113 endRect.height = bounds.height; | |
5114 } | |
5115 endRect.x += lineX; | |
5116 endRect.y += getLinePixel(endLine); | |
5117 super.redraw(endRect.x, endRect.y, endRect.width, endRect.height, false); | |
5118 renderer.disposeTextLayout(layout); | |
5119 | |
5120 /* Redraw all lines in between start and end line */ | |
5121 int y = startRect.y + startRect.height; | |
5122 if (endRect.y > y) { | |
5123 super.redraw(leftMargin, y, clientAreaWidth - rightMargin - leftMargin, endRect.y - y, false); | |
5124 } | |
5125 } | |
5126 void handleCompositionOffset (Event event) { | |
5127 int[] trailing = new int [1]; | |
5128 event.index = getOffsetAtPoint(event.x, event.y, trailing, true); | |
5129 event.count = trailing[0]; | |
5130 } | |
5131 void handleCompositionSelection (Event event) { | |
5132 event.start = selection.x; | |
5133 event.end = selection.y; | |
5134 event.text = getSelectionText(); | |
5135 } | |
5136 void handleCompositionChanged(Event event) { | |
5137 String text = event.text; | |
5138 int start = event.start; | |
5139 int end = event.end; | |
5140 int length = text.length; | |
5141 if (length is ime.getCommitCount()) { | |
5142 content.replaceTextRange(start, end - start, ""); | |
5143 caretOffset = ime.getCompositionOffset(); | |
5144 caretWidth = 0; | |
5145 caretDirection = SWT.NULL; | |
5146 } else { | |
5147 content.replaceTextRange(start, end - start, text); | |
5148 caretOffset = ime.getCaretOffset(); | |
5149 if (ime.getWideCaret()) { | |
5150 start = ime.getCompositionOffset(); | |
5151 int lineIndex = getCaretLine(); | |
5152 int lineOffset = content.getOffsetAtLine(lineIndex); | |
5153 TextLayout layout = renderer.getTextLayout(lineIndex); | |
5154 caretWidth = layout.getBounds(start - lineOffset, start + length - 1 - lineOffset).width; | |
5155 renderer.disposeTextLayout(layout); | |
5156 } | |
5157 } | |
5158 showCaret(); | |
5159 } | |
5160 /** | |
5161 * Frees resources. | |
5162 */ | |
5163 void handleDispose(Event event) { | |
5164 removeListener(SWT.Dispose, listener); | |
5165 notifyListeners(SWT.Dispose, event); | |
5166 event.type = SWT.None; | |
5167 | |
5168 clipboard.dispose(); | |
5169 if (renderer !is null) { | |
5170 renderer.dispose(); | |
5171 renderer = null; | |
5172 } | |
5173 if (content !is null) { | |
5174 content.removeTextChangeListener(textChangeListener); | |
5175 content = null; | |
5176 } | |
5177 if (defaultCaret !is null) { | |
5178 defaultCaret.dispose(); | |
5179 defaultCaret = null; | |
5180 } | |
5181 if (leftCaretBitmap !is null) { | |
5182 leftCaretBitmap.dispose(); | |
5183 leftCaretBitmap = null; | |
5184 } | |
5185 if (rightCaretBitmap !is null) { | |
5186 rightCaretBitmap.dispose(); | |
5187 rightCaretBitmap = null; | |
5188 } | |
5189 if (isBidiCaret()) { | |
5190 BidiUtil.removeLanguageListener(this); | |
5191 } | |
5192 selectionBackground = null; | |
5193 selectionForeground = null; | |
5194 textChangeListener = null; | |
5195 selection = null; | |
5196 doubleClickSelection = null; | |
5197 keyActionMap = null; | |
5198 background = null; | |
5199 foreground = null; | |
5200 clipboard = null; | |
5201 } | |
5202 /** | |
5203 * Scrolls the widget horizontally. | |
5204 */ | |
5205 void handleHorizontalScroll(Event event) { | |
5206 int scrollPixel = getHorizontalBar().getSelection() - horizontalScrollOffset; | |
5207 scrollHorizontal(scrollPixel, false); | |
5208 } | |
5209 /** | |
5210 * If an action has been registered for the key stroke execute the action. | |
5211 * Otherwise, if a character has been entered treat it as new content. | |
5212 * | |
5213 * @param event keyboard event | |
5214 */ | |
5215 void handleKey(Event event) { | |
5216 int action; | |
5217 caretAlignment = PREVIOUS_OFFSET_TRAILING; | |
5218 if (event.keyCode !is 0) { | |
5219 // special key pressed (e.g., F1) | |
5220 action = getKeyBinding(event.keyCode | event.stateMask); | |
5221 } else { | |
5222 // character key pressed | |
5223 action = getKeyBinding(event.character | event.stateMask); | |
5224 if (action is SWT.NULL) { | |
5225 // see if we have a control character | |
5226 if ((event.stateMask & SWT.CTRL) !is 0 && (event.character >= 0) && event.character <= 31) { | |
5227 // get the character from the CTRL+char sequence, the control | |
5228 // key subtracts 64 from the value of the key that it modifies | |
5229 int c = event.character + 64; | |
5230 action = getKeyBinding(c | event.stateMask); | |
5231 } | |
5232 } | |
5233 } | |
5234 if (action is SWT.NULL) { | |
5235 bool ignore = false; | |
5236 | |
5237 if (IS_CARBON) { | |
5238 // Ignore accelerator key combinations (we do not want to | |
5239 // insert a character in the text in this instance). Do not | |
5240 // ignore COMMAND+ALT combinations since that key sequence | |
5241 // produces characters on the mac. | |
5242 ignore = (event.stateMask ^ SWT.COMMAND) is 0 || | |
5243 (event.stateMask ^ (SWT.COMMAND | SWT.SHIFT)) is 0; | |
5244 } else if (IS_MOTIF) { | |
5245 // Ignore accelerator key combinations (we do not want to | |
5246 // insert a character in the text in this instance). Do not | |
5247 // ignore ALT combinations since this key sequence | |
5248 // produces characters on motif. | |
5249 ignore = (event.stateMask ^ SWT.CTRL) is 0 || | |
5250 (event.stateMask ^ (SWT.CTRL | SWT.SHIFT)) is 0; | |
5251 } else { | |
5252 // Ignore accelerator key combinations (we do not want to | |
5253 // insert a character in the text in this instance). Don't | |
5254 // ignore CTRL+ALT combinations since that is the Alt Gr | |
5255 // key on some keyboards. See bug 20953. | |
5256 ignore = (event.stateMask ^ SWT.ALT) is 0 || | |
5257 (event.stateMask ^ SWT.CTRL) is 0 || | |
5258 (event.stateMask ^ (SWT.ALT | SWT.SHIFT)) is 0 || | |
5259 (event.stateMask ^ (SWT.CTRL | SWT.SHIFT)) is 0; | |
5260 } | |
5261 // -ignore anything below SPACE except for line delimiter keys and tab. | |
5262 // -ignore DEL | |
5263 if (!ignore && event.character > 31 && event.character !is SWT.DEL || | |
5264 event.character is SWT.CR || event.character is SWT.LF || | |
5265 event.character is TAB) { | |
5266 doContent(event.character); | |
5267 update(); | |
5268 } | |
5269 } else { | |
5270 invokeAction(action); | |
5271 } | |
5272 } | |
5273 /** | |
5274 * If a VerifyKey listener exists, verify that the key that was entered | |
5275 * should be processed. | |
5276 * | |
5277 * @param event keyboard event | |
5278 */ | |
5279 void handleKeyDown(Event event) { | |
5280 if (clipboardSelection is null) { | |
5281 clipboardSelection = new Point(selection.x, selection.y); | |
5282 } | |
5283 | |
5284 Event verifyEvent = new Event(); | |
5285 verifyEvent.character = event.character; | |
5286 verifyEvent.keyCode = event.keyCode; | |
5287 verifyEvent.stateMask = event.stateMask; | |
5288 verifyEvent.doit = true; | |
5289 notifyListeners(VerifyKey, verifyEvent); | |
5290 if (verifyEvent.doit) { | |
5291 handleKey(event); | |
5292 } | |
5293 } | |
5294 /** | |
5295 * Update the Selection Clipboard. | |
5296 * | |
5297 * @param event keyboard event | |
5298 */ | |
5299 void handleKeyUp(Event event) { | |
5300 if (clipboardSelection !is null) { | |
5301 if (clipboardSelection.x !is selection.x || clipboardSelection.y !is selection.y) { | |
5302 try { | |
5303 if (selection.y - selection.x > 0) { | |
5304 setClipboardContent(selection.x, selection.y - selection.x, DND.SELECTION_CLIPBOARD); | |
5305 } | |
5306 } catch (SWTError error) { | |
5307 // Copy to clipboard failed. This happens when another application | |
5308 // is accessing the clipboard while we copy. Ignore the error. | |
5309 // Fixes 1GDQAVN | |
5310 // Rethrow all other errors. Fixes bug 17578. | |
5311 if (error.code !is DND.ERROR_CANNOT_SET_CLIPBOARD) { | |
5312 throw error; | |
5313 } | |
5314 } | |
5315 } | |
5316 } | |
5317 clipboardSelection = null; | |
5318 } | |
5319 /** | |
5320 * Updates the caret location and selection if mouse button 1 has been | |
5321 * pressed. | |
5322 */ | |
5323 void handleMouseDown(Event event) { | |
5324 //force focus (object support) | |
5325 forceFocus(); | |
5326 | |
5327 //drag detect | |
5328 if (dragDetect_ && checkDragDetect(event)) return; | |
5329 | |
5330 //paste clipboard selection | |
5331 if (event.button is 2) { | |
51 | 5332 String text = stringcast(getClipboardContent(DND.SELECTION_CLIPBOARD)); |
25 | 5333 if (text !is null && text.length > 0) { |
5334 // position cursor | |
5335 doMouseLocationChange(event.x, event.y, false); | |
5336 // insert text | |
5337 Event e = new Event(); | |
5338 e.start = selection.x; | |
5339 e.end = selection.y; | |
5340 e.text = getModelDelimitedText(text); | |
5341 sendKeyEvent(e); | |
5342 } | |
5343 } | |
5344 | |
5345 //set selection | |
5346 if ((event.button !is 1) || (IS_CARBON && (event.stateMask & SWT.MOD4) !is 0)) { | |
5347 return; | |
5348 } | |
5349 clickCount = event.count; | |
5350 if (clickCount is 1) { | |
5351 bool select = (event.stateMask & SWT.MOD2) !is 0; | |
5352 doMouseLocationChange(event.x, event.y, select); | |
5353 } else { | |
5354 if (doubleClickEnabled) { | |
5355 clearSelection(false); | |
5356 int offset = getOffsetAtPoint(event.x, event.y); | |
5357 int lineIndex = content.getLineAtOffset(offset); | |
5358 int lineOffset = content.getOffsetAtLine(lineIndex); | |
5359 int lineEnd = content.getCharCount(); | |
5360 if (lineIndex + 1 < content.getLineCount()) { | |
5361 lineEnd = content.getOffsetAtLine(lineIndex + 1); | |
5362 } | |
5363 int start, end; | |
5364 if ((clickCount & 1) is 0) { | |
5365 start = Math.max(0, getWordPrevious(offset, SWT.MOVEMENT_WORD_START)); | |
5366 end = Math.min(content.getCharCount(), getWordNext(start, SWT.MOVEMENT_WORD_END)); | |
5367 } else { | |
5368 start = lineOffset; | |
5369 end = lineEnd; | |
5370 } | |
5371 caretOffset = start; | |
5372 resetSelection(); | |
5373 caretOffset = end; | |
5374 showCaret(); | |
5375 doMouseSelection(); | |
5376 doubleClickSelection = new Point(selection.x, selection.y); | |
5377 } | |
5378 } | |
5379 } | |
5380 /** | |
5381 * Updates the caret location and selection if mouse button 1 is pressed | |
5382 * during the mouse move. | |
5383 */ | |
5384 void handleMouseMove(Event event) { | |
5385 if (clickCount is 0) return; | |
5386 doMouseLocationChange(event.x, event.y, true); | |
5387 update(); | |
5388 doAutoScroll(event); | |
5389 } | |
5390 /** | |
5391 * Autoscrolling ends when the mouse button is released. | |
5392 */ | |
5393 void handleMouseUp(Event event) { | |
5394 clickCount = 0; | |
5395 endAutoScroll(); | |
5396 if (event.button is 1) { | |
5397 try { | |
5398 if (selection.y - selection.x > 0) { | |
5399 setClipboardContent(selection.x, selection.y - selection.x, DND.SELECTION_CLIPBOARD); | |
5400 } | |
5401 } catch (SWTError error) { | |
5402 // Copy to clipboard failed. This happens when another application | |
5403 // is accessing the clipboard while we copy. Ignore the error. | |
5404 // Fixes 1GDQAVN | |
5405 // Rethrow all other errors. Fixes bug 17578. | |
5406 if (error.code !is DND.ERROR_CANNOT_SET_CLIPBOARD) { | |
5407 throw error; | |
5408 } | |
5409 } | |
5410 } | |
5411 } | |
5412 /** | |
5413 * Renders the invalidated area specified in the paint event. | |
5414 * | |
5415 * @param event paint event | |
5416 */ | |
5417 void handlePaint(Event event) { | |
5418 if (event.width is 0 || event.height is 0) return; | |
5419 if (clientAreaWidth is 0 || clientAreaHeight is 0) return; | |
5420 | |
5421 int startLine = getLineIndex(event.y); | |
5422 int y = getLinePixel(startLine); | |
5423 int endY = event.y + event.height; | |
5424 GC gc = event.gc; | |
5425 Color background = getBackground(); | |
5426 Color foreground = getForeground(); | |
5427 if (endY > 0) { | |
5428 int lineCount = isSingleLine() ? 1 : content.getLineCount(); | |
5429 int x = leftMargin - horizontalScrollOffset; | |
5430 for (int i = startLine; y < endY && i < lineCount; i++) { | |
5431 y += renderer.drawLine(i, x, y, gc, background, foreground); | |
5432 } | |
5433 if (y < endY) { | |
5434 gc.setBackground(background); | |
5435 drawBackground(gc, 0, y, clientAreaWidth, endY - y); | |
5436 } | |
5437 } | |
5438 // fill the margin background | |
5439 gc.setBackground(background); | |
5440 if (topMargin > 0) { | |
5441 drawBackground(gc, 0, 0, clientAreaWidth, topMargin); | |
5442 } | |
5443 if (bottomMargin > 0) { | |
5444 drawBackground(gc, 0, clientAreaHeight - bottomMargin, clientAreaWidth, bottomMargin); | |
5445 } | |
5446 if (leftMargin > 0) { | |
5447 drawBackground(gc, 0, 0, leftMargin, clientAreaHeight); | |
5448 } | |
5449 if (rightMargin > 0) { | |
5450 drawBackground(gc, clientAreaWidth - rightMargin, 0, rightMargin, clientAreaHeight); | |
5451 } | |
5452 } | |
5453 /** | |
5454 * Recalculates the scroll bars. Rewraps all lines when in word | |
5455 * wrap mode. | |
5456 * | |
5457 * @param event resize event | |
5458 */ | |
5459 void handleResize(Event event) { | |
5460 int oldHeight = clientAreaHeight; | |
5461 int oldWidth = clientAreaWidth; | |
5462 Rectangle clientArea = getClientArea(); | |
5463 clientAreaHeight = clientArea.height; | |
5464 clientAreaWidth = clientArea.width; | |
5465 /* Redraw the old or new right/bottom margin if needed */ | |
5466 if (oldWidth !is clientAreaWidth) { | |
5467 if (rightMargin > 0) { | |
5468 int x = (oldWidth < clientAreaWidth ? oldWidth : clientAreaWidth) - rightMargin; | |
5469 super.redraw(x, 0, rightMargin, oldHeight, false); | |
5470 } | |
5471 } | |
5472 if (oldHeight !is clientAreaHeight) { | |
5473 if (bottomMargin > 0) { | |
5474 int y = (oldHeight < clientAreaHeight ? oldHeight : clientAreaHeight) - bottomMargin; | |
5475 super.redraw(0, y, oldWidth, bottomMargin, false); | |
5476 } | |
5477 } | |
5478 if (wordWrap) { | |
5479 if (oldWidth !is clientAreaWidth) { | |
5480 renderer.reset(0, content.getLineCount()); | |
5481 verticalScrollOffset = -1; | |
5482 renderer.calculateIdle(); | |
5483 super.redraw(); | |
5484 } | |
5485 if (oldHeight !is clientAreaHeight) { | |
5486 if (oldHeight is 0) topIndexY = 0; | |
5487 setScrollBars(true); | |
5488 } | |
5489 setCaretLocation(); | |
5490 } else { | |
5491 renderer.calculateClientArea(); | |
5492 setScrollBars(true); | |
5493 claimRightFreeSpace(); | |
5494 // StyledText allows any value for horizontalScrollOffset when clientArea is zero | |
5495 // in setHorizontalPixel() and setHorisontalOffset(). Fixes bug 168429. | |
5496 if (clientAreaWidth !is 0) { | |
5497 ScrollBar horizontalBar = getHorizontalBar(); | |
5498 if (horizontalBar !is null && horizontalBar.getVisible()) { | |
5499 if (horizontalScrollOffset !is horizontalBar.getSelection()) { | |
5500 horizontalBar.setSelection(horizontalScrollOffset); | |
5501 horizontalScrollOffset = horizontalBar.getSelection(); | |
5502 } | |
5503 } | |
5504 } | |
5505 } | |
5506 claimBottomFreeSpace(); | |
5507 //TODO FIX TOP INDEX DURING RESIZE | |
5508 // if (oldHeight !is clientAreaHeight || wordWrap) { | |
5509 // calculateTopIndex(0); | |
5510 // } | |
5511 } | |
5512 /** | |
5513 * Updates the caret position and selection and the scroll bars to reflect | |
5514 * the content change. | |
5515 */ | |
5516 void handleTextChanged(TextChangedEvent event) { | |
5517 int offset = ime.getCompositionOffset(); | |
5518 if (offset !is -1 && lastTextChangeStart < offset) { | |
5519 ime.setCompositionOffset(offset + lastTextChangeNewCharCount - lastTextChangeReplaceCharCount); | |
5520 } | |
5521 int firstLine = content.getLineAtOffset(lastTextChangeStart); | |
5522 resetCache(firstLine, 0); | |
5523 if (!isFixedLineHeight() && topIndex > firstLine) { | |
5524 topIndex = firstLine; | |
5525 topIndexY = 0; | |
5526 super.redraw(); | |
5527 } else { | |
5528 int lastLine = firstLine + lastTextChangeNewLineCount; | |
5529 int firstLineTop = getLinePixel(firstLine); | |
5530 int newLastLineBottom = getLinePixel(lastLine + 1); | |
5531 if (lastLineBottom !is newLastLineBottom) { | |
5532 super.redraw(); | |
5533 } else { | |
5534 super.redraw(0, firstLineTop, clientAreaWidth, newLastLineBottom - firstLineTop, false); | |
5535 redrawLinesBullet(renderer.redrawLines); | |
5536 } | |
5537 } | |
5538 renderer.redrawLines = null; | |
5539 // update selection/caret location after styles have been changed. | |
5540 // otherwise any text measuring could be incorrect | |
5541 // | |
5542 // also, this needs to be done after all scrolling. Otherwise, | |
5543 // selection redraw would be flushed during scroll which is wrong. | |
5544 // in some cases new text would be drawn in scroll source area even | |
5545 // though the intent is to scroll it. | |
5546 updateSelection(lastTextChangeStart, lastTextChangeReplaceCharCount, lastTextChangeNewCharCount); | |
5547 if (lastTextChangeReplaceLineCount > 0 || wordWrap) { | |
5548 claimBottomFreeSpace(); | |
5549 } | |
5550 if (lastTextChangeReplaceCharCount > 0) { | |
5551 claimRightFreeSpace(); | |
5552 } | |
5553 } | |
5554 /** | |
5555 * Updates the screen to reflect a pending content change. | |
5556 * | |
5557 * @param event .start the start offset of the change | |
5558 * @param event .newText text that is going to be inserted or empty String | |
5559 * if no text will be inserted | |
5560 * @param event .replaceCharCount length of text that is going to be replaced | |
5561 * @param event .newCharCount length of text that is going to be inserted | |
5562 * @param event .replaceLineCount number of lines that are going to be replaced | |
5563 * @param event .newLineCount number of new lines that are going to be inserted | |
5564 */ | |
5565 void handleTextChanging(TextChangingEvent event) { | |
5566 if (event.replaceCharCount < 0) { | |
5567 event.start += event.replaceCharCount; | |
5568 event.replaceCharCount *= -1; | |
5569 } | |
5570 lastTextChangeStart = event.start; | |
5571 lastTextChangeNewLineCount = event.newLineCount; | |
5572 lastTextChangeNewCharCount = event.newCharCount; | |
5573 lastTextChangeReplaceLineCount = event.replaceLineCount; | |
5574 lastTextChangeReplaceCharCount = event.replaceCharCount; | |
5575 int lineIndex = content.getLineAtOffset(event.start); | |
5576 int srcY = getLinePixel(lineIndex + event.replaceLineCount + 1); | |
5577 int destY = getLinePixel(lineIndex + 1) + event.newLineCount * renderer.getLineHeight(); | |
5578 lastLineBottom = destY; | |
5579 if (srcY < 0 && destY < 0) { | |
5580 lastLineBottom += srcY - destY; | |
5581 verticalScrollOffset += destY - srcY; | |
5582 calculateTopIndex(destY - srcY); | |
5583 setScrollBars(true); | |
5584 } else { | |
5585 scrollText(srcY, destY); | |
5586 } | |
5587 | |
5588 renderer.textChanging(event); | |
5589 | |
5590 // Update the caret offset if it is greater than the length of the content. | |
5591 // This is necessary since style range API may be called between the | |
5592 // handleTextChanging and handleTextChanged events and this API sets the | |
5593 // caretOffset. | |
5594 int newEndOfText = content.getCharCount() - event.replaceCharCount + event.newCharCount; | |
5595 if (caretOffset > newEndOfText) caretOffset = newEndOfText; | |
5596 } | |
5597 /** | |
5598 * Called when the widget content is set programmatically, overwriting | |
5599 * the old content. Resets the caret position, selection and scroll offsets. | |
5600 * Recalculates the content width and scroll bars. Redraws the widget. | |
5601 * | |
5602 * @param event text change event. | |
5603 */ | |
5604 void handleTextSet(TextChangedEvent event) { | |
5605 reset(); | |
5606 } | |
5607 /** | |
5608 * Called when a traversal key is pressed. | |
5609 * Allow tab next traversal to occur when the widget is in single | |
5610 * line mode or in multi line and non-editable mode . | |
5611 * When in editable multi line mode we want to prevent the tab | |
5612 * traversal and receive the tab key event instead. | |
5613 * | |
5614 * @param event the event | |
5615 */ | |
5616 void handleTraverse(Event event) { | |
5617 switch (event.detail) { | |
5618 case SWT.TRAVERSE_ESCAPE: | |
5619 case SWT.TRAVERSE_PAGE_NEXT: | |
5620 case SWT.TRAVERSE_PAGE_PREVIOUS: | |
5621 event.doit = true; | |
5622 break; | |
5623 case SWT.TRAVERSE_RETURN: | |
5624 case SWT.TRAVERSE_TAB_NEXT: | |
5625 case SWT.TRAVERSE_TAB_PREVIOUS: | |
5626 if ((getStyle() & SWT.SINGLE) !is 0) { | |
5627 event.doit = true; | |
5628 } else { | |
5629 if (!editable || (event.stateMask & SWT.MODIFIER_MASK) !is 0) { | |
5630 event.doit = true; | |
5631 } | |
5632 } | |
5633 break; | |
5634 default: | |
5635 } | |
5636 } | |
5637 /** | |
5638 * Scrolls the widget vertically. | |
5639 */ | |
5640 void handleVerticalScroll(Event event) { | |
5641 int scrollPixel = getVerticalBar().getSelection() - getVerticalScrollOffset(); | |
5642 scrollVertical(scrollPixel, false); | |
5643 } | |
5644 /** | |
5645 * Add accessibility support for the widget. | |
5646 */ | |
5647 void initializeAccessible() { | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
5648 Accessible accessible = getAccessible(); |
25 | 5649 accessible.addAccessibleListener(new class() AccessibleAdapter { |
5650 public void getName (AccessibleEvent e) { | |
5651 String name = null; | |
5652 Label label = getAssociatedLabel (); | |
5653 if (label !is null) { | |
5654 name = stripMnemonic (label.getText()); | |
5655 } | |
5656 e.result = name; | |
5657 } | |
5658 public void getHelp(AccessibleEvent e) { | |
5659 e.result = getToolTipText(); | |
5660 } | |
5661 public void getKeyboardShortcut(AccessibleEvent e) { | |
5662 String shortcut = null; | |
5663 Label label = getAssociatedLabel (); | |
5664 if (label !is null) { | |
5665 String text = label.getText (); | |
5666 if (text !is null) { | |
5667 dchar mnemonic = _findMnemonic (text); | |
5668 if (mnemonic !is '\0') { | |
51 | 5669 shortcut = Format("Alt+{}", mnemonic ); //$NON-NLS-1$ |
25 | 5670 } |
5671 } | |
5672 } | |
5673 e.result = shortcut; | |
5674 } | |
5675 }); | |
5676 accessible.addAccessibleTextListener(new class() AccessibleTextAdapter { | |
5677 public void getCaretOffset(AccessibleTextEvent e) { | |
5678 e.offset = this.outer.getCaretOffset(); | |
5679 } | |
5680 public void getSelectionRange(AccessibleTextEvent e) { | |
5681 Point selection = this.outer.getSelectionRange(); | |
5682 e.offset = selection.x; | |
5683 e.length = selection.y; | |
5684 } | |
5685 }); | |
5686 accessible.addAccessibleControlListener(new class() AccessibleControlAdapter { | |
5687 public void getRole(AccessibleControlEvent e) { | |
5688 e.detail = ACC.ROLE_TEXT; | |
5689 } | |
5690 public void getState(AccessibleControlEvent e) { | |
5691 int state = 0; | |
5692 if (isEnabled()) state |= ACC.STATE_FOCUSABLE; | |
5693 if (isFocusControl()) state |= ACC.STATE_FOCUSED; | |
5694 if (!isVisible()) state |= ACC.STATE_INVISIBLE; | |
5695 if (!getEditable()) state |= ACC.STATE_READONLY; | |
5696 e.detail = state; | |
5697 } | |
5698 public void getValue(AccessibleControlEvent e) { | |
5699 e.result = this.outer.getText(); | |
5700 } | |
5701 }); | |
5702 addListener(SWT.FocusIn, new class(accessible) Listener { | |
5703 Accessible acc; | |
5704 this( Accessible acc ){ this.acc = acc; } | |
5705 public void handleEvent(Event event) { | |
5706 acc.setFocus(ACC.CHILDID_SELF); | |
5707 } | |
5708 }); | |
5709 } | |
5710 /* | |
5711 * Return the Label immediately preceding the receiver in the z-order, | |
5712 * or null if none. | |
5713 */ | |
5714 Label getAssociatedLabel () { | |
5715 Control[] siblings = getParent ().getChildren (); | |
5716 for (int i = 0; i < siblings.length; i++) { | |
5717 if (siblings [i] is this) { | |
5718 if (i > 0 && ( null !is cast(Label)siblings [i-1])) { | |
5719 return cast(Label) siblings [i-1]; | |
5720 } | |
5721 } | |
5722 } | |
5723 return null; | |
5724 } | |
5725 String stripMnemonic (String string) { | |
5726 int index = 0; | |
5727 int length_ = string.length; | |
5728 do { | |
5729 while ((index < length_) && (string[index] !is '&')) index++; | |
5730 if (++index >= length_) return string; | |
5731 if (string[index] !is '&') { | |
5732 return string.substring(0, index-1) ~ string.substring(index, length_); | |
5733 } | |
5734 index++; | |
5735 } while (index < length_); | |
5736 return string; | |
5737 } | |
5738 /* | |
5739 * Return the lowercase of the first non-'&' character following | |
5740 * an '&' character in the given string. If there are no '&' | |
5741 * characters in the given string, return '\0'. | |
5742 */ | |
5743 dchar _findMnemonic (String string) { | |
5744 if (string is null) return '\0'; | |
5745 int index = 0; | |
5746 int length_ = string.length; | |
5747 do { | |
5748 while (index < length_ && string[index] !is '&') index++; | |
5749 if (++index >= length_) return '\0'; | |
5750 if (string[index] !is '&') return CharacterFirstToLower(string[index .. $ ] ); | |
5751 index++; | |
5752 } while (index < length_); | |
5753 return '\0'; | |
5754 } | |
5755 /** | |
5756 * Executes the action. | |
5757 * | |
5758 * @param action one of the actions defined in ST.java | |
5759 */ | |
5760 public void invokeAction(int action) { | |
5761 checkWidget(); | |
5762 updateCaretDirection = true; | |
5763 switch (action) { | |
5764 // Navigation | |
5765 case ST.LINE_UP: | |
5766 doLineUp(false); | |
5767 clearSelection(true); | |
5768 break; | |
5769 case ST.LINE_DOWN: | |
5770 doLineDown(false); | |
5771 clearSelection(true); | |
5772 break; | |
5773 case ST.LINE_START: | |
5774 doLineStart(); | |
5775 clearSelection(true); | |
5776 break; | |
5777 case ST.LINE_END: | |
5778 doLineEnd(); | |
5779 clearSelection(true); | |
5780 break; | |
5781 case ST.COLUMN_PREVIOUS: | |
5782 doCursorPrevious(); | |
5783 clearSelection(true); | |
5784 break; | |
5785 case ST.COLUMN_NEXT: | |
5786 doCursorNext(); | |
5787 clearSelection(true); | |
5788 break; | |
5789 case ST.PAGE_UP: | |
5790 doPageUp(false, -1); | |
5791 clearSelection(true); | |
5792 break; | |
5793 case ST.PAGE_DOWN: | |
5794 doPageDown(false, -1); | |
5795 clearSelection(true); | |
5796 break; | |
5797 case ST.WORD_PREVIOUS: | |
5798 doWordPrevious(); | |
5799 clearSelection(true); | |
5800 break; | |
5801 case ST.WORD_NEXT: | |
5802 doWordNext(); | |
5803 clearSelection(true); | |
5804 break; | |
5805 case ST.TEXT_START: | |
5806 doContentStart(); | |
5807 clearSelection(true); | |
5808 break; | |
5809 case ST.TEXT_END: | |
5810 doContentEnd(); | |
5811 clearSelection(true); | |
5812 break; | |
5813 case ST.WINDOW_START: | |
5814 doPageStart(); | |
5815 clearSelection(true); | |
5816 break; | |
5817 case ST.WINDOW_END: | |
5818 doPageEnd(); | |
5819 clearSelection(true); | |
5820 break; | |
5821 // Selection | |
5822 case ST.SELECT_LINE_UP: | |
5823 doSelectionLineUp(); | |
5824 break; | |
5825 case ST.SELECT_ALL: | |
5826 selectAll(); | |
5827 break; | |
5828 case ST.SELECT_LINE_DOWN: | |
5829 doSelectionLineDown(); | |
5830 break; | |
5831 case ST.SELECT_LINE_START: | |
5832 doLineStart(); | |
5833 doSelection(ST.COLUMN_PREVIOUS); | |
5834 break; | |
5835 case ST.SELECT_LINE_END: | |
5836 doLineEnd(); | |
5837 doSelection(ST.COLUMN_NEXT); | |
5838 break; | |
5839 case ST.SELECT_COLUMN_PREVIOUS: | |
5840 doSelectionCursorPrevious(); | |
5841 doSelection(ST.COLUMN_PREVIOUS); | |
5842 break; | |
5843 case ST.SELECT_COLUMN_NEXT: | |
5844 doSelectionCursorNext(); | |
5845 doSelection(ST.COLUMN_NEXT); | |
5846 break; | |
5847 case ST.SELECT_PAGE_UP: | |
5848 doSelectionPageUp(-1); | |
5849 break; | |
5850 case ST.SELECT_PAGE_DOWN: | |
5851 doSelectionPageDown(-1); | |
5852 break; | |
5853 case ST.SELECT_WORD_PREVIOUS: | |
5854 doSelectionWordPrevious(); | |
5855 doSelection(ST.COLUMN_PREVIOUS); | |
5856 break; | |
5857 case ST.SELECT_WORD_NEXT: | |
5858 doSelectionWordNext(); | |
5859 doSelection(ST.COLUMN_NEXT); | |
5860 break; | |
5861 case ST.SELECT_TEXT_START: | |
5862 doContentStart(); | |
5863 doSelection(ST.COLUMN_PREVIOUS); | |
5864 break; | |
5865 case ST.SELECT_TEXT_END: | |
5866 doContentEnd(); | |
5867 doSelection(ST.COLUMN_NEXT); | |
5868 break; | |
5869 case ST.SELECT_WINDOW_START: | |
5870 doPageStart(); | |
5871 doSelection(ST.COLUMN_PREVIOUS); | |
5872 break; | |
5873 case ST.SELECT_WINDOW_END: | |
5874 doPageEnd(); | |
5875 doSelection(ST.COLUMN_NEXT); | |
5876 break; | |
5877 // Modification | |
5878 case ST.CUT: | |
5879 cut(); | |
5880 break; | |
5881 case ST.COPY: | |
5882 copy(); | |
5883 break; | |
5884 case ST.PASTE: | |
5885 paste(); | |
5886 break; | |
5887 case ST.DELETE_PREVIOUS: | |
5888 doBackspace(); | |
5889 break; | |
5890 case ST.DELETE_NEXT: | |
5891 doDelete(); | |
5892 break; | |
5893 case ST.DELETE_WORD_PREVIOUS: | |
5894 doDeleteWordPrevious(); | |
5895 break; | |
5896 case ST.DELETE_WORD_NEXT: | |
5897 doDeleteWordNext(); | |
5898 break; | |
5899 // Miscellaneous | |
5900 case ST.TOGGLE_OVERWRITE: | |
5901 overwrite = !overwrite; // toggle insert/overwrite mode | |
5902 break; | |
5903 default: | |
5904 } | |
5905 } | |
5906 /** | |
5907 * Temporary until SWT provides this | |
5908 */ | |
5909 bool isBidi() { | |
5910 return IS_GTK || IS_CARBON || BidiUtil.isBidiPlatform() || isMirrored_; | |
5911 } | |
5912 bool isBidiCaret() { | |
5913 return BidiUtil.isBidiPlatform(); | |
5914 } | |
5915 bool isFixedLineHeight() { | |
5916 return fixedLineHeight; | |
5917 } | |
5918 /** | |
5919 * Returns whether the given offset is inside a multi byte line delimiter. | |
5920 * Example: | |
5921 * "Line1\r\n" isLineDelimiter(5) is false but isLineDelimiter(6) is true | |
5922 * | |
5923 * @return true if the given offset is inside a multi byte line delimiter. | |
5924 * false if the given offset is before or after a line delimiter. | |
5925 */ | |
5926 bool isLineDelimiter(int offset) { | |
5927 int line = content.getLineAtOffset(offset); | |
5928 int lineOffset = content.getOffsetAtLine(line); | |
5929 int offsetInLine = offset - lineOffset; | |
5930 // offsetInLine will be greater than line length if the line | |
5931 // delimiter is longer than one character and the offset is set | |
5932 // in between parts of the line delimiter. | |
5933 return offsetInLine > content.getLine(line).length; | |
5934 } | |
5935 /** | |
5936 * Returns whether the widget is mirrored (right oriented/right to left | |
5937 * writing order). | |
5938 * | |
5939 * @return isMirrored true=the widget is right oriented, false=the widget | |
5940 * is left oriented | |
5941 */ | |
5942 bool isMirrored() { | |
5943 return isMirrored_; | |
5944 } | |
5945 /** | |
5946 * Returns whether the widget can have only one line. | |
5947 * | |
5948 * @return true if widget can have only one line, false if widget can have | |
5949 * multiple lines | |
5950 */ | |
5951 bool isSingleLine() { | |
5952 return (getStyle() & SWT.SINGLE) !is 0; | |
5953 } | |
5954 /** | |
5955 * Sends the specified verify event, replace/insert text as defined by | |
5956 * the event and send a modify event. | |
5957 * | |
5958 * @param event the text change event. | |
5959 * <ul> | |
5960 * <li>event.start - the replace start offset</li> | |
5961 * <li>event.end - the replace end offset</li> | |
5962 * <li>event.text - the new text</li> | |
5963 * </ul> | |
5964 * @param updateCaret whether or not he caret should be set behind | |
5965 * the new text | |
5966 */ | |
5967 void modifyContent(Event event, bool updateCaret) { | |
5968 event.doit = true; | |
5969 notifyListeners(SWT.Verify, event); | |
5970 if (event.doit) { | |
5971 StyledTextEvent styledTextEvent = null; | |
5972 int replacedLength = event.end - event.start; | |
5973 if (isListening(ExtendedModify)) { | |
5974 styledTextEvent = new StyledTextEvent(content); | |
5975 styledTextEvent.start = event.start; | |
5976 styledTextEvent.end = event.start + event.text.length; | |
5977 styledTextEvent.text = content.getTextRange(event.start, replacedLength); | |
5978 } | |
5979 if (updateCaret) { | |
5980 //Fix advancing flag for delete/backspace key on direction boundary | |
5981 if (event.text.length is 0) { | |
5982 int lineIndex = content.getLineAtOffset(event.start); | |
5983 int lineOffset = content.getOffsetAtLine(lineIndex); | |
5984 TextLayout layout = renderer.getTextLayout(lineIndex); | |
5985 int levelStart = layout.getLevel(event.start - lineOffset); | |
5986 int lineIndexEnd = content.getLineAtOffset(event.end); | |
5987 if (lineIndex !is lineIndexEnd) { | |
5988 renderer.disposeTextLayout(layout); | |
5989 lineOffset = content.getOffsetAtLine(lineIndexEnd); | |
5990 layout = renderer.getTextLayout(lineIndexEnd); | |
5991 } | |
5992 int levelEnd = layout.getLevel(event.end - lineOffset); | |
5993 renderer.disposeTextLayout(layout); | |
5994 if (levelStart !is levelEnd) { | |
5995 caretAlignment = PREVIOUS_OFFSET_TRAILING; | |
5996 } else { | |
5997 caretAlignment = OFFSET_LEADING; | |
5998 } | |
5999 } | |
6000 } | |
6001 content.replaceTextRange(event.start, replacedLength, event.text); | |
6002 // set the caret position prior to sending the modify event. | |
6003 // fixes 1GBB8NJ | |
6004 if (updateCaret) { | |
6005 // always update the caret location. fixes 1G8FODP | |
6006 setSelection(event.start + event.text.length, 0, true); | |
6007 showCaret(); | |
6008 } | |
6009 sendModifyEvent(event); | |
6010 if (isListening(ExtendedModify)) { | |
6011 notifyListeners(ExtendedModify, styledTextEvent); | |
6012 } | |
6013 } | |
6014 } | |
6015 void paintObject(GC gc, int x, int y, int ascent, int descent, StyleRange style, Bullet bullet, int bulletIndex) { | |
6016 if (isListening(PaintObject)) { | |
6017 StyledTextEvent event = new StyledTextEvent (content) ; | |
6018 event.gc = gc; | |
6019 event.x = x; | |
6020 event.y = y; | |
6021 event.ascent = ascent; | |
6022 event.descent = descent; | |
6023 event.style = style; | |
6024 event.bullet = bullet; | |
6025 event.bulletIndex = bulletIndex; | |
6026 notifyListeners(PaintObject, event); | |
6027 } | |
6028 } | |
6029 /** | |
6030 * Replaces the selection with the text on the <code>DND.CLIPBOARD</code> | |
6031 * clipboard or, if there is no selection, inserts the text at the current | |
6032 * caret offset. If the widget has the SWT.SINGLE style and the | |
6033 * clipboard text contains more than one line, only the first line without | |
6034 * line delimiters is inserted in the widget. | |
6035 * | |
6036 * @exception SWTException <ul> | |
6037 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6038 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6039 * </ul> | |
6040 */ | |
6041 public void paste(){ | |
6042 checkWidget(); | |
51 | 6043 String text = stringcast( getClipboardContent(DND.CLIPBOARD)); |
25 | 6044 if (text !is null && text.length > 0) { |
6045 Event event = new Event(); | |
6046 event.start = selection.x; | |
6047 event.end = selection.y; | |
6048 event.text = getModelDelimitedText(text); | |
6049 sendKeyEvent(event); | |
6050 } | |
6051 } | |
6052 /** | |
6053 * Prints the widget's text to the default printer. | |
6054 * | |
6055 * @exception SWTException <ul> | |
6056 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6057 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6058 * </ul> | |
6059 */ | |
6060 public void print() { | |
6061 checkWidget(); | |
6062 Printer printer = new Printer(); | |
6063 StyledTextPrintOptions options = new StyledTextPrintOptions(); | |
6064 options.printTextForeground = true; | |
6065 options.printTextBackground = true; | |
6066 options.printTextFontStyle = true; | |
6067 options.printLineBackground = true; | |
6068 (new Printing(this, printer, options)).run(); | |
6069 printer.dispose(); | |
6070 } | |
6071 /** | |
6072 * Returns a runnable that will print the widget's text | |
6073 * to the specified printer. | |
6074 * <p> | |
6075 * The runnable may be run in a non-UI thread. | |
6076 * </p> | |
6077 * | |
6078 * @param printer the printer to print to | |
6079 * | |
6080 * @return a <code>Runnable</code> for printing the receiver's text | |
6081 * | |
6082 * @exception SWTException <ul> | |
6083 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6084 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6085 * </ul> | |
6086 * @exception IllegalArgumentException <ul> | |
6087 * <li>ERROR_NULL_ARGUMENT when printer is null</li> | |
6088 * </ul> | |
6089 */ | |
6090 public Runnable print(Printer printer) { | |
6091 checkWidget(); | |
6092 if (printer is null) { | |
6093 SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6094 } | |
6095 StyledTextPrintOptions options = new StyledTextPrintOptions(); | |
6096 options.printTextForeground = true; | |
6097 options.printTextBackground = true; | |
6098 options.printTextFontStyle = true; | |
6099 options.printLineBackground = true; | |
6100 return print(printer, options); | |
6101 } | |
6102 /** | |
6103 * Returns a runnable that will print the widget's text | |
6104 * to the specified printer. | |
6105 * <p> | |
6106 * The runnable may be run in a non-UI thread. | |
6107 * </p> | |
6108 * | |
6109 * @param printer the printer to print to | |
6110 * @param options print options to use during printing | |
6111 * | |
6112 * @return a <code>Runnable</code> for printing the receiver's text | |
6113 * | |
6114 * @exception SWTException <ul> | |
6115 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6116 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6117 * </ul> | |
6118 * @exception IllegalArgumentException <ul> | |
6119 * <li>ERROR_NULL_ARGUMENT when printer or options is null</li> | |
6120 * </ul> | |
6121 * @since 2.1 | |
6122 */ | |
6123 public Runnable print(Printer printer, StyledTextPrintOptions options) { | |
6124 checkWidget(); | |
6125 if (printer is null || options is null) { | |
6126 SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6127 } | |
6128 return new Printing(this, printer, options); | |
6129 } | |
6130 /** | |
6131 * Causes the entire bounds of the receiver to be marked | |
6132 * as needing to be redrawn. The next time a paint request | |
6133 * is processed, the control will be completely painted. | |
6134 * <p> | |
6135 * Recalculates the content width for all lines in the bounds. | |
6136 * When a <code>LineStyleListener</code> is used a redraw call | |
6137 * is the only notification to the widget that styles have changed | |
6138 * and that the content width may have changed. | |
6139 * </p> | |
6140 * | |
6141 * @exception SWTException <ul> | |
6142 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6143 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6144 * </ul> | |
6145 * | |
6146 * @see Control#update() | |
6147 */ | |
6148 public override void redraw() { | |
6149 super.redraw(); | |
6150 int itemCount = getPartialBottomIndex() - topIndex + 1; | |
6151 renderer.reset(topIndex, itemCount); | |
6152 renderer.calculate(topIndex, itemCount); | |
6153 setScrollBars(false); | |
6154 } | |
6155 /** | |
6156 * Causes the rectangular area of the receiver specified by | |
6157 * the arguments to be marked as needing to be redrawn. | |
6158 * The next time a paint request is processed, that area of | |
6159 * the receiver will be painted. If the <code>all</code> flag | |
6160 * is <code>true</code>, any children of the receiver which | |
6161 * intersect with the specified area will also paint their | |
6162 * intersecting areas. If the <code>all</code> flag is | |
6163 * <code>false</code>, the children will not be painted. | |
6164 * <p> | |
6165 * Marks the content width of all lines in the specified rectangle | |
6166 * as unknown. Recalculates the content width of all visible lines. | |
6167 * When a <code>LineStyleListener</code> is used a redraw call | |
6168 * is the only notification to the widget that styles have changed | |
6169 * and that the content width may have changed. | |
6170 * </p> | |
6171 * | |
6172 * @param x the x coordinate of the area to draw | |
6173 * @param y the y coordinate of the area to draw | |
6174 * @param width the width of the area to draw | |
6175 * @param height the height of the area to draw | |
6176 * @param all <code>true</code> if children should redraw, and <code>false</code> otherwise | |
6177 * | |
6178 * @exception SWTException <ul> | |
6179 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6180 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6181 * </ul> | |
6182 * | |
6183 * @see Control#update() | |
6184 */ | |
6185 public override void redraw(int x, int y, int width, int height, bool all) { | |
6186 super.redraw(x, y, width, height, all); | |
6187 if (height > 0) { | |
6188 int firstLine = getLineIndex(y); | |
6189 int lastLine = getLineIndex(y + height); | |
6190 resetCache(firstLine, lastLine - firstLine + 1); | |
6191 } | |
6192 } | |
6193 void redrawLines(int startLine, int lineCount) { | |
6194 // do nothing if redraw range is completely invisible | |
6195 int partialBottomIndex = getPartialBottomIndex(); | |
6196 if (startLine > partialBottomIndex || startLine + lineCount - 1 < topIndex) { | |
6197 return; | |
6198 } | |
6199 // only redraw visible lines | |
6200 if (startLine < topIndex) { | |
6201 lineCount -= topIndex - startLine; | |
6202 startLine = topIndex; | |
6203 } | |
6204 if (startLine + lineCount - 1 > partialBottomIndex) { | |
6205 lineCount = partialBottomIndex - startLine + 1; | |
6206 } | |
6207 startLine -= topIndex; | |
6208 int redrawTop = getLinePixel(startLine); | |
6209 int redrawBottom = getLinePixel(startLine + lineCount); | |
6210 int redrawWidth = clientAreaWidth - leftMargin - rightMargin; | |
6211 super.redraw(leftMargin, redrawTop, redrawWidth, redrawBottom - redrawTop, true); | |
6212 } | |
6213 void redrawLinesBullet (int[] redrawLines) { | |
6214 if (redrawLines is null) return; | |
6215 int topIndex = getPartialTopIndex(); | |
6216 int bottomIndex = getPartialBottomIndex(); | |
6217 for (int i = 0; i < redrawLines.length; i++) { | |
6218 int lineIndex = redrawLines[i]; | |
6219 if (!(topIndex <= lineIndex && lineIndex <= bottomIndex)) continue; | |
6220 int width = -1; | |
6221 Bullet bullet = renderer.getLineBullet(lineIndex, null); | |
6222 if (bullet !is null) { | |
6223 StyleRange style = bullet.style; | |
6224 GlyphMetrics metrics = style.metrics; | |
6225 width = metrics.width; | |
6226 } | |
6227 if (width is -1) width = getClientArea().width; | |
6228 int height = renderer.getLineHeight(lineIndex); | |
6229 int y = getLinePixel(lineIndex); | |
6230 super.redraw(0, y, width, height, false); | |
6231 } | |
6232 } | |
6233 /** | |
6234 * Redraws the specified text range. | |
6235 * | |
6236 * @param start offset of the first character to redraw | |
6237 * @param length number of characters to redraw | |
6238 * @param clearBackground true if the background should be cleared as | |
6239 * part of the redraw operation. If true, the entire redraw range will | |
6240 * be cleared before anything is redrawn. If the redraw range includes | |
6241 * the last character of a line (i.e., the entire line is redrawn) the | |
6242 * line is cleared all the way to the right border of the widget. | |
6243 * The redraw operation will be faster and smoother if clearBackground | |
6244 * is set to false. Whether or not the flag can be set to false depends | |
6245 * on the type of change that has taken place. If font styles or | |
6246 * background colors for the redraw range have changed, clearBackground | |
6247 * should be set to true. If only foreground colors have changed for | |
6248 * the redraw range, clearBackground can be set to false. | |
6249 * @exception SWTException <ul> | |
6250 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6251 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6252 * </ul> | |
6253 * @exception IllegalArgumentException <ul> | |
6254 * <li>ERROR_INVALID_RANGE when start and/or end are outside the widget content</li> | |
6255 * </ul> | |
6256 */ | |
6257 public void redrawRange(int start, int length, bool clearBackground) { | |
6258 checkWidget(); | |
6259 int end = start + length; | |
6260 int contentLength = content.getCharCount(); | |
6261 if (start > end || start < 0 || end > contentLength) { | |
6262 SWT.error(SWT.ERROR_INVALID_RANGE); | |
6263 } | |
6264 int firstLine = content.getLineAtOffset(start); | |
6265 int lastLine = content.getLineAtOffset(end); | |
6266 resetCache(firstLine, lastLine - firstLine + 1); | |
6267 internalRedrawRange(start, length); | |
6268 } | |
6269 /** | |
6270 * Removes the specified bidirectional segment listener. | |
6271 * | |
6272 * @param listener the listener which should no longer be notified | |
6273 * | |
6274 * @exception SWTException <ul> | |
6275 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6276 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6277 * </ul> | |
6278 * @exception IllegalArgumentException <ul> | |
6279 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
6280 * </ul> | |
6281 * | |
6282 * @since 2.0 | |
6283 */ | |
6284 public void removeBidiSegmentListener(BidiSegmentListener listener) { | |
6285 checkWidget(); | |
6286 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6287 removeListener(LineGetSegments, listener); | |
6288 } | |
6289 /** | |
6290 * Removes the specified extended modify listener. | |
6291 * | |
6292 * @param extendedModifyListener the listener which should no longer be notified | |
6293 * | |
6294 * @exception SWTException <ul> | |
6295 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6296 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6297 * </ul> | |
6298 * @exception IllegalArgumentException <ul> | |
6299 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
6300 * </ul> | |
6301 */ | |
6302 public void removeExtendedModifyListener(ExtendedModifyListener extendedModifyListener) { | |
6303 checkWidget(); | |
6304 if (extendedModifyListener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6305 removeListener(ExtendedModify, extendedModifyListener); | |
6306 } | |
6307 /** | |
6308 * Removes the specified line background listener. | |
6309 * | |
6310 * @param listener the listener which should no longer be notified | |
6311 * | |
6312 * @exception SWTException <ul> | |
6313 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6314 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6315 * </ul> | |
6316 * @exception IllegalArgumentException <ul> | |
6317 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
6318 * </ul> | |
6319 */ | |
6320 public void removeLineBackgroundListener(LineBackgroundListener listener) { | |
6321 checkWidget(); | |
6322 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6323 removeListener(LineGetBackground, listener); | |
6324 } | |
6325 /** | |
6326 * Removes the specified line style listener. | |
6327 * | |
6328 * @param listener the listener which should no longer be notified | |
6329 * | |
6330 * @exception SWTException <ul> | |
6331 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6332 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6333 * </ul> | |
6334 * @exception IllegalArgumentException <ul> | |
6335 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
6336 * </ul> | |
6337 */ | |
6338 public void removeLineStyleListener(LineStyleListener listener) { | |
6339 checkWidget(); | |
6340 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6341 removeListener(LineGetStyle, listener); | |
6342 } | |
6343 /** | |
6344 * Removes the specified modify listener. | |
6345 * | |
6346 * @param modifyListener the listener which should no longer be notified | |
6347 * | |
6348 * @exception SWTException <ul> | |
6349 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6350 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6351 * </ul> | |
6352 * @exception IllegalArgumentException <ul> | |
6353 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
6354 * </ul> | |
6355 */ | |
6356 public void removeModifyListener(ModifyListener modifyListener) { | |
6357 checkWidget(); | |
6358 if (modifyListener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6359 removeListener(SWT.Modify, modifyListener); | |
6360 } | |
6361 /** | |
6362 * Removes the specified listener. | |
6363 * | |
6364 * @param listener the listener which should no longer be notified | |
6365 * | |
6366 * @exception SWTException <ul> | |
6367 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6368 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6369 * </ul> | |
6370 * @exception IllegalArgumentException <ul> | |
6371 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
6372 * </ul> | |
6373 * @since 3.2 | |
6374 */ | |
6375 public void removePaintObjectListener(PaintObjectListener listener) { | |
6376 checkWidget(); | |
6377 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6378 removeListener(PaintObject, listener); | |
6379 } | |
6380 /** | |
6381 * Removes the listener from the collection of listeners who will | |
6382 * be notified when the user changes the receiver's selection. | |
6383 * | |
6384 * @param listener the listener which should no longer be notified | |
6385 * | |
6386 * @exception IllegalArgumentException <ul> | |
6387 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
6388 * </ul> | |
6389 * @exception SWTException <ul> | |
6390 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6391 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6392 * </ul> | |
6393 * | |
6394 * @see SelectionListener | |
6395 * @see #addSelectionListener | |
6396 */ | |
6397 public void removeSelectionListener(SelectionListener listener) { | |
6398 checkWidget(); | |
6399 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6400 removeListener(SWT.Selection, listener); | |
6401 } | |
6402 /** | |
6403 * Removes the specified verify listener. | |
6404 * | |
6405 * @param verifyListener the listener which should no longer be notified | |
6406 * | |
6407 * @exception SWTException <ul> | |
6408 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6409 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6410 * </ul> | |
6411 * @exception IllegalArgumentException <ul> | |
6412 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
6413 * </ul> | |
6414 */ | |
6415 public void removeVerifyListener(VerifyListener verifyListener) { | |
6416 checkWidget(); | |
6417 if (verifyListener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6418 removeListener(SWT.Verify, verifyListener); | |
6419 } | |
6420 /** | |
6421 * Removes the specified key verify listener. | |
6422 * | |
6423 * @param listener the listener which should no longer be notified | |
6424 * | |
6425 * @exception SWTException <ul> | |
6426 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6427 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6428 * </ul> | |
6429 * @exception IllegalArgumentException <ul> | |
6430 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
6431 * </ul> | |
6432 */ | |
6433 public void removeVerifyKeyListener(VerifyKeyListener listener) { | |
6434 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6435 removeListener(VerifyKey, listener); | |
6436 } | |
6437 /** | |
6438 * Removes the specified word movement listener. | |
6439 * | |
6440 * @param listener the listener which should no longer be notified | |
6441 * | |
6442 * @exception SWTException <ul> | |
6443 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6444 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6445 * </ul> | |
6446 * @exception IllegalArgumentException <ul> | |
6447 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
6448 * </ul> | |
6449 * | |
6450 * @see MovementEvent | |
6451 * @see MovementListener | |
6452 * @see #addWordMovementListener | |
6453 * | |
6454 * @since 3.3 | |
6455 */ | |
6456 | |
6457 public void removeWordMovementListener(MovementListener listener) { | |
6458 checkWidget(); | |
6459 if (listener is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6460 removeListener(WordNext, listener); | |
6461 removeListener(WordPrevious, listener); | |
6462 } | |
6463 /** | |
6464 * Replaces the styles in the given range with new styles. This method | |
6465 * effectively deletes the styles in the given range and then adds the | |
6466 * the new styles. | |
6467 * <p> | |
6468 * Note: Because a StyleRange includes the start and length, the | |
6469 * same instance cannot occur multiple times in the array of styles. | |
6470 * If the same style attributes, such as font and color, occur in | |
6471 * multiple StyleRanges, <code>setStyleRanges(int, int, int[], StyleRange[])</code> | |
6472 * can be used to share styles and reduce memory usage. | |
6473 * </p><p> | |
6474 * Should not be called if a LineStyleListener has been set since the | |
6475 * listener maintains the styles. | |
6476 * </p> | |
6477 * | |
6478 * @param start offset of first character where styles will be deleted | |
6479 * @param length length of the range to delete styles in | |
6480 * @param ranges StyleRange objects containing the new style information. | |
6481 * The ranges should not overlap and should be within the specified start | |
6482 * and length. The style rendering is undefined if the ranges do overlap | |
6483 * or are ill-defined. Must not be null. | |
6484 * @exception SWTException <ul> | |
6485 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6486 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6487 * </ul> | |
6488 * @exception IllegalArgumentException <ul> | |
6489 * <li>ERROR_INVALID_RANGE when either start or end is outside the valid range (0 <= offset <= getCharCount())</li> | |
6490 * </ul> | |
6491 * | |
6492 * @since 2.0 | |
6493 * | |
6494 * @see #setStyleRanges(int, int, int[], StyleRange[]) | |
6495 */ | |
6496 public void replaceStyleRanges(int start, int length, StyleRange[] ranges) { | |
6497 checkWidget(); | |
6498 if (isListening(LineGetStyle)) return; | |
6499 // SWT extension: allow null for zero length string | |
6500 //if (ranges is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6501 setStyleRanges(start, length, null, ranges, false); | |
6502 } | |
6503 /** | |
6504 * Replaces the given text range with new text. | |
6505 * If the widget has the SWT.SINGLE style and "text" contains more than | |
6506 * one line, only the first line is rendered but the text is stored | |
6507 * unchanged. A subsequent call to getText will return the same text | |
6508 * that was set. Note that only a single line of text should be set when | |
6509 * the SWT.SINGLE style is used. | |
6510 * <p> | |
6511 * <b>NOTE:</b> During the replace operation the current selection is | |
6512 * changed as follows: | |
6513 * <ul> | |
6514 * <li>selection before replaced text: selection unchanged | |
6515 * <li>selection after replaced text: adjust the selection so that same text | |
6516 * remains selected | |
6517 * <li>selection intersects replaced text: selection is cleared and caret | |
6518 * is placed after inserted text | |
6519 * </ul> | |
6520 * </p> | |
6521 * | |
6522 * @param start offset of first character to replace | |
6523 * @param length number of characters to replace. Use 0 to insert text | |
6524 * @param text new text. May be empty to delete text. | |
6525 * @exception SWTException <ul> | |
6526 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6527 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6528 * </ul> | |
6529 * @exception IllegalArgumentException <ul> | |
6530 * <li>ERROR_INVALID_RANGE when either start or end is outside the valid range (0 <= offset <= getCharCount())</li> | |
6531 * <li>ERROR_INVALID_ARGUMENT when either start or end is inside a multi byte line delimiter. | |
6532 * Splitting a line delimiter for example by inserting text in between the CR and LF and deleting part of a line delimiter is not supported</li> | |
6533 * </ul> | |
6534 */ | |
6535 public void replaceTextRange(int start, int length, String text) { | |
6536 checkWidget(); | |
6537 // SWT extension: allow null for zero length string | |
6538 // if (text is null) { | |
6539 // SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
6540 // } | |
6541 int contentLength = getCharCount(); | |
6542 int end = start + length; | |
6543 if (start > end || start < 0 || end > contentLength) { | |
6544 SWT.error(SWT.ERROR_INVALID_RANGE); | |
6545 } | |
6546 Event event = new Event(); | |
6547 event.start = start; | |
6548 event.end = end; | |
6549 event.text = text; | |
6550 modifyContent(event, false); | |
6551 } | |
6552 /** | |
6553 * Resets the caret position, selection and scroll offsets. Recalculate | |
6554 * the content width and scroll bars. Redraw the widget. | |
6555 */ | |
6556 void reset() { | |
6557 ScrollBar verticalBar = getVerticalBar(); | |
6558 ScrollBar horizontalBar = getHorizontalBar(); | |
6559 caretOffset = 0; | |
6560 topIndex = 0; | |
6561 topIndexY = 0; | |
6562 verticalScrollOffset = 0; | |
6563 horizontalScrollOffset = 0; | |
6564 resetSelection(); | |
6565 renderer.setContent(content); | |
6566 if (verticalBar !is null) { | |
6567 verticalBar.setSelection(0); | |
6568 } | |
6569 if (horizontalBar !is null) { | |
6570 horizontalBar.setSelection(0); | |
6571 } | |
6572 resetCache(0, 0); | |
6573 setCaretLocation(); | |
6574 super.redraw(); | |
6575 } | |
6576 void resetCache(int firstLine, int count) { | |
6577 int maxLineIndex = renderer.maxWidthLineIndex; | |
6578 renderer.reset(firstLine, count); | |
6579 renderer.calculateClientArea(); | |
6580 if (0 <= maxLineIndex && maxLineIndex < content.getLineCount()) { | |
6581 renderer.calculate(maxLineIndex, 1); | |
6582 } | |
6583 setScrollBars(true); | |
6584 if (!isFixedLineHeight()) { | |
6585 if (topIndex > firstLine) { | |
6586 verticalScrollOffset = -1; | |
6587 } | |
6588 renderer.calculateIdle(); | |
6589 } | |
6590 } | |
6591 /** | |
6592 * Resets the selection. | |
6593 */ | |
6594 void resetSelection() { | |
6595 selection.x = selection.y = caretOffset; | |
6596 selectionAnchor = -1; | |
6597 } | |
6598 | |
6599 public override void scroll(int destX, int destY, int x, int y, int width, int height, bool all) { | |
6600 super.scroll(destX, destY, x, y, width, height, false); | |
6601 if (all) { | |
6602 int deltaX = destX - x, deltaY = destY - y; | |
6603 Control[] children = getChildren(); | |
6604 for (int i=0; i<children.length; i++) { | |
6605 Control child = children[i]; | |
6606 Rectangle rect = child.getBounds(); | |
6607 child.setLocation(rect.x + deltaX, rect.y + deltaY); | |
6608 } | |
6609 } | |
6610 } | |
6611 | |
6612 /** | |
6613 * Scrolls the widget horizontally. | |
6614 * | |
6615 * @param pixels number of pixels to scroll, > 0 = scroll left, | |
6616 * < 0 scroll right | |
6617 * @param adjustScrollBar | |
6618 * true= the scroll thumb will be moved to reflect the new scroll offset. | |
6619 * false = the scroll thumb will not be moved | |
6620 * @return | |
6621 * true=the widget was scrolled | |
6622 * false=the widget was not scrolled, the given offset is not valid. | |
6623 */ | |
6624 bool scrollHorizontal(int pixels, bool adjustScrollBar) { | |
6625 if (pixels is 0) { | |
6626 return false; | |
6627 } | |
6628 ScrollBar horizontalBar = getHorizontalBar(); | |
6629 if (horizontalBar !is null && adjustScrollBar) { | |
6630 horizontalBar.setSelection(horizontalScrollOffset + pixels); | |
6631 } | |
6632 int scrollHeight = clientAreaHeight - topMargin - bottomMargin; | |
6633 if (pixels > 0) { | |
6634 int sourceX = leftMargin + pixels; | |
6635 int scrollWidth = clientAreaWidth - sourceX - rightMargin; | |
6636 if (scrollWidth > 0) { | |
6637 scroll(leftMargin, topMargin, sourceX, topMargin, scrollWidth, scrollHeight, true); | |
6638 } | |
6639 if (sourceX > scrollWidth) { | |
6640 super.redraw(leftMargin + scrollWidth, topMargin, pixels - scrollWidth, scrollHeight, true); | |
6641 } | |
6642 } else { | |
6643 int destinationX = leftMargin - pixels; | |
6644 int scrollWidth = clientAreaWidth - destinationX - rightMargin; | |
6645 if (scrollWidth > 0) { | |
6646 scroll(destinationX, topMargin, leftMargin, topMargin, scrollWidth, scrollHeight, true); | |
6647 } | |
6648 if (destinationX > scrollWidth) { | |
6649 super.redraw(leftMargin + scrollWidth, topMargin, -pixels - scrollWidth, scrollHeight, true); | |
6650 } | |
6651 } | |
6652 horizontalScrollOffset += pixels; | |
6653 setCaretLocation(); | |
6654 return true; | |
6655 } | |
6656 /** | |
6657 * Scrolls the widget vertically. | |
6658 * | |
6659 * @param pixel the new vertical scroll offset | |
6660 * @param adjustScrollBar | |
6661 * true= the scroll thumb will be moved to reflect the new scroll offset. | |
6662 * false = the scroll thumb will not be moved | |
6663 * @return | |
6664 * true=the widget was scrolled | |
6665 * false=the widget was not scrolled | |
6666 */ | |
6667 bool scrollVertical(int pixels, bool adjustScrollBar) { | |
6668 if (pixels is 0) { | |
6669 return false; | |
6670 } | |
6671 if (verticalScrollOffset !is -1) { | |
6672 ScrollBar verticalBar = getVerticalBar(); | |
6673 if (verticalBar !is null && adjustScrollBar) { | |
6674 verticalBar.setSelection(verticalScrollOffset + pixels); | |
6675 } | |
6676 int scrollWidth = clientAreaWidth - leftMargin - rightMargin; | |
6677 if (pixels > 0) { | |
6678 int sourceY = topMargin + pixels; | |
6679 int scrollHeight = clientAreaHeight - sourceY - bottomMargin; | |
6680 if (scrollHeight > 0) { | |
6681 scroll(leftMargin, topMargin, leftMargin, sourceY, scrollWidth, scrollHeight, true); | |
6682 } | |
6683 if (sourceY > scrollHeight) { | |
6684 int redrawY = Math.max(0, topMargin + scrollHeight); | |
6685 int redrawHeight = Math.min(clientAreaHeight, pixels - scrollHeight); | |
6686 super.redraw(leftMargin, redrawY, scrollWidth, redrawHeight, true); | |
6687 } | |
6688 } else { | |
6689 int destinationY = topMargin - pixels; | |
6690 int scrollHeight = clientAreaHeight - destinationY - bottomMargin; | |
6691 if (scrollHeight > 0) { | |
6692 scroll(leftMargin, destinationY, leftMargin, topMargin, scrollWidth, scrollHeight, true); | |
6693 } | |
6694 if (destinationY > scrollHeight) { | |
6695 int redrawY = Math.max(0, topMargin + scrollHeight); | |
6696 int redrawHeight = Math.min(clientAreaHeight, -pixels - scrollHeight); | |
6697 super.redraw(leftMargin, redrawY, scrollWidth, redrawHeight, true); | |
6698 } | |
6699 } | |
6700 verticalScrollOffset += pixels; | |
6701 calculateTopIndex(pixels); | |
6702 } else { | |
6703 calculateTopIndex(pixels); | |
6704 super.redraw(); | |
6705 } | |
6706 setCaretLocation(); | |
6707 return true; | |
6708 } | |
6709 void scrollText(int srcY, int destY) { | |
6710 if (srcY is destY) return; | |
6711 int deltaY = destY - srcY; | |
6712 int scrollWidth = clientAreaWidth - leftMargin - rightMargin, scrollHeight; | |
6713 if (deltaY > 0) { | |
6714 scrollHeight = clientAreaHeight - srcY - bottomMargin; | |
6715 } else { | |
6716 scrollHeight = clientAreaHeight - destY - bottomMargin; | |
6717 } | |
6718 scroll(leftMargin, destY, leftMargin, srcY, scrollWidth, scrollHeight, true); | |
6719 if ((0 < srcY + scrollHeight) && (topMargin > srcY)) { | |
6720 super.redraw(leftMargin, deltaY, scrollWidth, topMargin, false); | |
6721 } | |
6722 if ((0 < destY + scrollHeight) && (topMargin > destY)) { | |
6723 super.redraw(leftMargin, 0, scrollWidth, topMargin, false); | |
6724 } | |
6725 if ((clientAreaHeight - bottomMargin < srcY + scrollHeight) && (clientAreaHeight > srcY)) { | |
6726 super.redraw(leftMargin, clientAreaHeight - bottomMargin + deltaY, scrollWidth, bottomMargin, false); | |
6727 } | |
6728 if ((clientAreaHeight - bottomMargin < destY + scrollHeight) && (clientAreaHeight > destY)) { | |
6729 super.redraw(leftMargin, clientAreaHeight - bottomMargin, scrollWidth, bottomMargin, false); | |
6730 } | |
6731 } | |
6732 /** | |
6733 * Selects all the text. | |
6734 * | |
6735 * @exception SWTException <ul> | |
6736 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6737 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6738 * </ul> | |
6739 */ | |
6740 public void selectAll() { | |
6741 checkWidget(); | |
6742 setSelection(0, Math.max(getCharCount(),0)); | |
6743 } | |
6744 /** | |
6745 * Replaces/inserts text as defined by the event. | |
6746 * | |
6747 * @param event the text change event. | |
6748 * <ul> | |
6749 * <li>event.start - the replace start offset</li> | |
6750 * <li>event.end - the replace end offset</li> | |
6751 * <li>event.text - the new text</li> | |
6752 * </ul> | |
6753 */ | |
6754 void sendKeyEvent(Event event) { | |
6755 if (editable) { | |
6756 modifyContent(event, true); | |
6757 } | |
6758 } | |
6759 /** | |
6760 * Returns a StyledTextEvent that can be used to request data such | |
6761 * as styles and background color for a line. | |
6762 * <p> | |
6763 * The specified line may be a visual (wrapped) line if in word | |
6764 * wrap mode. The returned object will always be for a logical | |
6765 * (unwrapped) line. | |
6766 * </p> | |
6767 * | |
6768 * @param lineOffset offset of the line. This may be the offset of | |
6769 * a visual line if the widget is in word wrap mode. | |
6770 * @param line line text. This may be the text of a visual line if | |
6771 * the widget is in word wrap mode. | |
6772 * @return StyledTextEvent that can be used to request line data | |
6773 * for the given line. | |
6774 */ | |
6775 StyledTextEvent sendLineEvent(int eventType, int lineOffset, String line) { | |
6776 StyledTextEvent event = null; | |
6777 if (isListening(eventType)) { | |
6778 event = new StyledTextEvent(content); | |
6779 event.detail = lineOffset; | |
6780 event.text = line; | |
6781 event.alignment = alignment; | |
6782 event.indent = indent; | |
6783 event.justify = justify; | |
6784 notifyListeners(eventType, event); | |
6785 } | |
6786 return event; | |
6787 } | |
6788 void sendModifyEvent(Event event) { | |
6789 Accessible accessible = getAccessible(); | |
6790 if (event.text.length is 0) { | |
6791 accessible.textChanged(ACC.TEXT_DELETE, event.start, event.end - event.start); | |
6792 } else { | |
6793 if (event.start is event.end) { | |
6794 accessible.textChanged(ACC.TEXT_INSERT, event.start, event.text.length); | |
6795 } else { | |
6796 accessible.textChanged(ACC.TEXT_DELETE, event.start, event.end - event.start); | |
6797 accessible.textChanged(ACC.TEXT_INSERT, event.start, event.text.length); | |
6798 } | |
6799 } | |
6800 notifyListeners(SWT.Modify, event); | |
6801 } | |
6802 /** | |
6803 * Sends the specified selection event. | |
6804 */ | |
6805 void sendSelectionEvent() { | |
6806 getAccessible().textSelectionChanged(); | |
6807 Event event = new Event(); | |
6808 event.x = selection.x; | |
6809 event.y = selection.y; | |
6810 notifyListeners(SWT.Selection, event); | |
6811 } | |
6812 int sendWordBoundaryEvent(int eventType, int movement, int offset, int newOffset, String lineText, int lineOffset) { | |
6813 if (isListening(eventType)) { | |
6814 StyledTextEvent event = new StyledTextEvent(content); | |
6815 event.detail = lineOffset; | |
6816 event.text = lineText; | |
6817 event.count = movement; | |
6818 event.start = offset; | |
6819 event.end = newOffset; | |
6820 notifyListeners(eventType, event); | |
6821 offset = event.end; | |
6822 if (offset !is newOffset) { | |
6823 int length = getCharCount(); | |
6824 if (offset < 0) { | |
6825 offset = 0; | |
6826 } else if (offset > length) { | |
6827 offset = length; | |
6828 } else { | |
6829 if (isLineDelimiter(offset)) { | |
6830 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
6831 } | |
6832 } | |
6833 } | |
6834 return offset; | |
6835 } | |
6836 return newOffset; | |
6837 } | |
6838 /** | |
6839 * Sets the alignment of the widget. The argument should be one of <code>SWT.LEFT</code>, | |
6840 * <code>SWT.CENTER</code> or <code>SWT.RIGHT</code>. The alignment applies for all lines. | |
6841 * </p><p> | |
6842 * Note that if <code>SWT.MULTI</code> is set, then <code>SWT.WRAP</code> must also be set | |
6843 * in order to stabilize the right edge before setting alignment. | |
6844 * </p> | |
6845 * | |
6846 * @param alignment the new alignment | |
6847 * | |
6848 * @exception SWTException <ul> | |
6849 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6850 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6851 * </ul> | |
6852 * | |
6853 * @see #setLineAlignment(int, int, int) | |
6854 * | |
6855 * @since 3.2 | |
6856 */ | |
6857 public void setAlignment(int alignment) { | |
6858 checkWidget(); | |
6859 alignment &= (SWT.LEFT | SWT.RIGHT | SWT.CENTER); | |
6860 if (alignment is 0 || this.alignment is alignment) return; | |
6861 this.alignment = alignment; | |
6862 resetCache(0, content.getLineCount()); | |
6863 setCaretLocation(); | |
6864 super.redraw(); | |
6865 } | |
6866 /** | |
6867 * @see Control#setBackground(Color) | |
6868 */ | |
6869 public override void setBackground(Color color) { | |
6870 checkWidget(); | |
6871 background = color; | |
6872 super.setBackground(color); | |
6873 super.redraw(); | |
6874 } | |
6875 /** | |
6876 * Sets the receiver's caret. Set the caret's height and location. | |
6877 * | |
6878 * </p> | |
6879 * @param caret the new caret for the receiver | |
6880 * | |
6881 * @exception SWTException <ul> | |
6882 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6883 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6884 * </ul> | |
6885 */ | |
6886 public override void setCaret(Caret caret) { | |
6887 checkWidget (); | |
6888 super.setCaret(caret); | |
6889 caretDirection = SWT.NULL; | |
6890 if (caret !is null) { | |
6891 setCaretLocation(); | |
6892 } | |
6893 } | |
6894 /** | |
6895 * Sets the BIDI coloring mode. When true the BIDI text display | |
6896 * algorithm is applied to segments of text that are the same | |
6897 * color. | |
6898 * | |
6899 * @param mode the new coloring mode | |
6900 * @exception SWTException <ul> | |
6901 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6902 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6903 * </ul> | |
6904 * | |
6905 * @deprecated use BidiSegmentListener instead. | |
6906 */ | |
6907 public void setBidiColoring(bool mode) { | |
6908 checkWidget(); | |
6909 bidiColoring = mode; | |
6910 } | |
6911 /** | |
6912 * Moves the Caret to the current caret offset. | |
6913 */ | |
6914 void setCaretLocation() { | |
6915 Point newCaretPos = getPointAtOffset(caretOffset); | |
6916 setCaretLocation(newCaretPos, getCaretDirection()); | |
6917 } | |
6918 void setCaretLocation(Point location, int direction) { | |
6919 Caret caret = getCaret(); | |
6920 if (caret !is null) { | |
6921 bool isDefaultCaret = caret is defaultCaret; | |
6922 int lineHeight = renderer.getLineHeight(); | |
6923 int caretHeight = lineHeight; | |
6924 if (!isFixedLineHeight() && isDefaultCaret) { | |
6925 caretHeight = getBoundsAtOffset(caretOffset).height; | |
6926 if (caretHeight !is lineHeight) { | |
6927 direction = SWT.DEFAULT; | |
6928 } | |
6929 } | |
6930 int imageDirection = direction; | |
6931 if (isMirrored()) { | |
6932 if (imageDirection is SWT.LEFT) { | |
6933 imageDirection = SWT.RIGHT; | |
6934 } else if (imageDirection is SWT.RIGHT) { | |
6935 imageDirection = SWT.LEFT; | |
6936 } | |
6937 } | |
6938 if (isDefaultCaret && imageDirection is SWT.RIGHT) { | |
6939 location.x -= (caret.getSize().x - 1); | |
6940 } | |
6941 if (isDefaultCaret) { | |
6942 caret.setBounds(location.x, location.y, caretWidth, caretHeight); | |
6943 } else { | |
6944 caret.setLocation(location); | |
6945 } | |
6946 getAccessible().textCaretMoved(getCaretOffset()); | |
6947 if (direction !is caretDirection) { | |
6948 caretDirection = direction; | |
6949 if (isDefaultCaret) { | |
6950 if (imageDirection is SWT.DEFAULT) { | |
6951 defaultCaret.setImage(null); | |
6952 } else if (imageDirection is SWT.LEFT) { | |
6953 defaultCaret.setImage(leftCaretBitmap); | |
6954 } else if (imageDirection is SWT.RIGHT) { | |
6955 defaultCaret.setImage(rightCaretBitmap); | |
6956 } | |
6957 } | |
6958 if (caretDirection is SWT.LEFT) { | |
6959 BidiUtil.setKeyboardLanguage(BidiUtil.KEYBOARD_NON_BIDI); | |
6960 } else if (caretDirection is SWT.RIGHT) { | |
6961 BidiUtil.setKeyboardLanguage(BidiUtil.KEYBOARD_BIDI); | |
6962 } | |
6963 } | |
6964 } | |
6965 columnX = location.x; | |
6966 } | |
6967 /** | |
6968 * Sets the caret offset. | |
6969 * | |
6970 * @param offset caret offset, relative to the first character in the text. | |
6971 * @exception SWTException <ul> | |
6972 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
6973 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
6974 * </ul> | |
6975 * @exception IllegalArgumentException <ul> | |
6976 * <li>ERROR_INVALID_ARGUMENT when either the start or the end of the selection range is inside a | |
6977 * multi byte line delimiter (and thus neither clearly in front of or after the line delimiter) | |
6978 * </ul> | |
6979 */ | |
6980 public void setCaretOffset(int offset) { | |
6981 checkWidget(); | |
6982 int length = getCharCount(); | |
6983 if (length > 0 && offset !is caretOffset) { | |
6984 if (offset < 0) { | |
6985 caretOffset = 0; | |
6986 } else if (offset > length) { | |
6987 caretOffset = length; | |
6988 } else { | |
6989 if (isLineDelimiter(offset)) { | |
6990 // offset is inside a multi byte line delimiter. This is an | |
6991 // illegal operation and an exception is thrown. Fixes 1GDKK3R | |
6992 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
6993 } | |
6994 caretOffset = offset; | |
6995 } | |
6996 caretAlignment = PREVIOUS_OFFSET_TRAILING; | |
6997 // clear the selection if the caret is moved. | |
6998 // don't notify listeners about the selection change. | |
6999 clearSelection(false); | |
7000 } | |
7001 setCaretLocation(); | |
7002 } | |
7003 /** | |
7004 * Copies the specified text range to the clipboard. The text will be placed | |
7005 * in the clipboard in plain text format and RTF format. | |
7006 * | |
7007 * @param start start index of the text | |
7008 * @param length length of text to place in clipboard | |
7009 * | |
7010 * @exception SWTError, see Clipboard.setContents | |
7011 * @see org.eclipse.swt.dnd.Clipboard#setContents | |
7012 */ | |
7013 void setClipboardContent(int start, int length, int clipboardType) { | |
7014 if (clipboardType is DND.SELECTION_CLIPBOARD && !(IS_MOTIF || IS_GTK)) return; | |
7015 TextTransfer plainTextTransfer = TextTransfer.getInstance(); | |
7016 TextWriter plainTextWriter = new TextWriter(start, length); | |
7017 String plainText = getPlatformDelimitedText(plainTextWriter); | |
7018 Object[] data; | |
7019 Transfer[] types; | |
7020 if (clipboardType is DND.SELECTION_CLIPBOARD) { | |
7021 data = [ cast(Object) new ArrayWrapperString(plainText) ]; | |
7022 types = [plainTextTransfer]; | |
7023 } else { | |
7024 RTFTransfer rtfTransfer = RTFTransfer.getInstance(); | |
7025 RTFWriter rtfWriter = new RTFWriter(start, length); | |
7026 String rtfText = getPlatformDelimitedText(rtfWriter); | |
7027 data = [ cast(Object) new ArrayWrapperString(rtfText), new ArrayWrapperString(plainText) ]; | |
7028 types = [ cast(Transfer)rtfTransfer, plainTextTransfer]; | |
7029 } | |
7030 clipboard.setContents(data, types, clipboardType); | |
7031 } | |
7032 /** | |
7033 * Sets the content implementation to use for text storage. | |
7034 * | |
7035 * @param newContent StyledTextContent implementation to use for text storage. | |
7036 * @exception SWTException <ul> | |
7037 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7038 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7039 * </ul> | |
7040 * @exception IllegalArgumentException <ul> | |
7041 * <li>ERROR_NULL_ARGUMENT when listener is null</li> | |
7042 * </ul> | |
7043 */ | |
7044 public void setContent(StyledTextContent newContent) { | |
7045 checkWidget(); | |
7046 if (newContent is null) { | |
7047 SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
7048 } | |
7049 if (content !is null) { | |
7050 content.removeTextChangeListener(textChangeListener); | |
7051 } | |
7052 content = newContent; | |
7053 content.addTextChangeListener(textChangeListener); | |
7054 reset(); | |
7055 } | |
7056 /** | |
7057 * Sets the receiver's cursor to the cursor specified by the | |
7058 * argument. Overridden to handle the null case since the | |
7059 * StyledText widget uses an ibeam as its default cursor. | |
7060 * | |
7061 * @see Control#setCursor(Cursor) | |
7062 */ | |
7063 public override void setCursor (Cursor cursor) { | |
7064 if (cursor is null) { | |
7065 Display display = getDisplay(); | |
7066 super.setCursor(display.getSystemCursor(SWT.CURSOR_IBEAM)); | |
7067 } else { | |
7068 super.setCursor(cursor); | |
7069 } | |
7070 } | |
7071 /** | |
7072 * Sets whether the widget : double click mouse behavior. | |
7073 * </p> | |
7074 * | |
7075 * @param enable if true double clicking a word selects the word, if false | |
7076 * double clicks have the same effect as regular mouse clicks. | |
7077 * @exception SWTException <ul> | |
7078 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7079 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7080 * </ul> | |
7081 */ | |
7082 public void setDoubleClickEnabled(bool enable) { | |
7083 checkWidget(); | |
7084 doubleClickEnabled = enable; | |
7085 } | |
7086 public override void setDragDetect (bool dragDetect_) { | |
7087 checkWidget (); | |
7088 this.dragDetect_ = dragDetect_; | |
7089 } | |
7090 /** | |
7091 * Sets whether the widget content can be edited. | |
7092 * </p> | |
7093 * | |
7094 * @param editable if true content can be edited, if false content can not be | |
7095 * edited | |
7096 * @exception SWTException <ul> | |
7097 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7098 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7099 * </ul> | |
7100 */ | |
7101 public void setEditable(bool editable) { | |
7102 checkWidget(); | |
7103 this.editable = editable; | |
7104 } | |
7105 /** | |
7106 * Sets a new font to render text with. | |
7107 * <p> | |
7108 * <b>NOTE:</b> Italic fonts are not supported unless they have no overhang | |
7109 * and the same baseline as regular fonts. | |
7110 * </p> | |
7111 * | |
7112 * @param font new font | |
7113 * @exception SWTException <ul> | |
7114 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7115 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7116 * </ul> | |
7117 */ | |
7118 public override void setFont(Font font) { | |
7119 checkWidget(); | |
7120 int oldLineHeight = renderer.getLineHeight(); | |
7121 super.setFont(font); | |
7122 renderer.setFont(getFont(), tabLength); | |
7123 // keep the same top line visible. fixes 5815 | |
7124 if (isFixedLineHeight()) { | |
7125 int lineHeight = renderer.getLineHeight(); | |
7126 if (lineHeight !is oldLineHeight) { | |
7127 int vscroll = (getVerticalScrollOffset() * lineHeight / oldLineHeight) - getVerticalScrollOffset(); | |
7128 scrollVertical(vscroll, true); | |
7129 } | |
7130 } | |
7131 resetCache(0, content.getLineCount()); | |
7132 claimBottomFreeSpace(); | |
7133 calculateScrollBars(); | |
7134 if (isBidiCaret()) createCaretBitmaps(); | |
7135 caretDirection = SWT.NULL; | |
7136 setCaretLocation(); | |
7137 super.redraw(); | |
7138 } | |
7139 public override void setForeground(Color color) { | |
7140 checkWidget(); | |
7141 foreground = color; | |
7142 super.setForeground(getForeground()); | |
7143 super.redraw(); | |
7144 } | |
7145 /** | |
7146 * Sets the horizontal scroll offset relative to the start of the line. | |
7147 * Do nothing if there is no text set. | |
7148 * <p> | |
7149 * <b>NOTE:</b> The horizontal index is reset to 0 when new text is set in the | |
7150 * widget. | |
7151 * </p> | |
7152 * | |
7153 * @param offset horizontal scroll offset relative to the start | |
7154 * of the line, measured in character increments starting at 0, if | |
7155 * equal to 0 the content is not scrolled, if > 0 = the content is scrolled. | |
7156 * @exception SWTException <ul> | |
7157 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7158 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7159 * </ul> | |
7160 */ | |
7161 public void setHorizontalIndex(int offset) { | |
7162 checkWidget(); | |
7163 if (getCharCount() is 0) { | |
7164 return; | |
7165 } | |
7166 if (offset < 0) { | |
7167 offset = 0; | |
7168 } | |
7169 offset *= getHorizontalIncrement(); | |
7170 // allow any value if client area width is unknown or 0. | |
7171 // offset will be checked in resize handler. | |
7172 // don't use isVisible since width is known even if widget | |
7173 // is temporarily invisible | |
7174 if (clientAreaWidth > 0) { | |
7175 int width = renderer.getWidth(); | |
7176 // prevent scrolling if the content fits in the client area. | |
7177 // align end of longest line with right border of client area | |
7178 // if offset is out of range. | |
7179 if (offset > width - clientAreaWidth) { | |
7180 offset = Math.max(0, width - clientAreaWidth); | |
7181 } | |
7182 } | |
7183 scrollHorizontal(offset - horizontalScrollOffset, true); | |
7184 } | |
7185 /** | |
7186 * Sets the horizontal pixel offset relative to the start of the line. | |
7187 * Do nothing if there is no text set. | |
7188 * <p> | |
7189 * <b>NOTE:</b> The horizontal pixel offset is reset to 0 when new text | |
7190 * is set in the widget. | |
7191 * </p> | |
7192 * | |
7193 * @param pixel horizontal pixel offset relative to the start | |
7194 * of the line. | |
7195 * @exception SWTException <ul> | |
7196 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7197 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7198 * </ul> | |
7199 * @since 2.0 | |
7200 */ | |
7201 public void setHorizontalPixel(int pixel) { | |
7202 checkWidget(); | |
7203 if (getCharCount() is 0) { | |
7204 return; | |
7205 } | |
7206 if (pixel < 0) { | |
7207 pixel = 0; | |
7208 } | |
7209 // allow any value if client area width is unknown or 0. | |
7210 // offset will be checked in resize handler. | |
7211 // don't use isVisible since width is known even if widget | |
7212 // is temporarily invisible | |
7213 if (clientAreaWidth > 0) { | |
7214 int width = renderer.getWidth(); | |
7215 // prevent scrolling if the content fits in the client area. | |
7216 // align end of longest line with right border of client area | |
7217 // if offset is out of range. | |
7218 if (pixel > width - clientAreaWidth) { | |
7219 pixel = Math.max(0, width - clientAreaWidth); | |
7220 } | |
7221 } | |
7222 scrollHorizontal(pixel - horizontalScrollOffset, true); | |
7223 } | |
7224 /** | |
7225 * Sets the line indentation of the widget. | |
7226 * <p> | |
7227 * It is the amount of blank space, in pixels, at the beginning of each line. | |
7228 * When a line wraps in several lines only the first one is indented. | |
7229 * </p> | |
7230 * | |
7231 * @param indent the new indent | |
7232 * | |
7233 * @exception SWTException <ul> | |
7234 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7235 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7236 * </ul> | |
7237 * | |
7238 * @see #setLineIndent(int, int, int) | |
7239 * | |
7240 * @since 3.2 | |
7241 */ | |
7242 public void setIndent(int indent) { | |
7243 checkWidget(); | |
7244 if (this.indent is indent || indent < 0) return; | |
7245 this.indent = indent; | |
7246 resetCache(0, content.getLineCount()); | |
7247 setCaretLocation(); | |
7248 super.redraw(); | |
7249 } | |
7250 /** | |
7251 * Sets whether the widget should justify lines. | |
7252 * | |
7253 * @param justify whether lines should be justified | |
7254 * | |
7255 * @exception SWTException <ul> | |
7256 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7257 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7258 * </ul> | |
7259 * | |
7260 * @see #setLineJustify(int, int, bool) | |
7261 * | |
7262 * @since 3.2 | |
7263 */ | |
7264 public void setJustify(bool justify) { | |
7265 checkWidget(); | |
7266 if (this.justify is justify) return; | |
7267 this.justify = justify; | |
7268 resetCache(0, content.getLineCount()); | |
7269 setCaretLocation(); | |
7270 super.redraw(); | |
7271 } | |
7272 /** | |
7273 * Maps a key to an action. | |
7274 * <p> | |
7275 * One action can be associated with N keys. However, each key can only | |
7276 * have one action (key:action is N:1 relation). | |
7277 * </p> | |
7278 * | |
7279 * @param key a key code defined in SWT.java or a character. | |
7280 * Optionally ORd with a state mask. Preferred state masks are one or more of | |
7281 * SWT.MOD1, SWT.MOD2, SWT.MOD3, since these masks account for modifier platform | |
7282 * differences. However, there may be cases where using the specific state masks | |
7283 * (i.e., SWT.CTRL, SWT.SHIFT, SWT.ALT, SWT.COMMAND) makes sense. | |
7284 * @param action one of the predefined actions defined in ST.java. | |
7285 * Use SWT.NULL to remove a key binding. | |
7286 * @exception SWTException <ul> | |
7287 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7288 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7289 * </ul> | |
7290 */ | |
7291 public void setKeyBinding(int key, int action) { | |
7292 checkWidget(); | |
7293 int modifierValue = key & SWT.MODIFIER_MASK; | |
7294 char keyChar = cast(char)(key & SWT.KEY_MASK); | |
7295 if (Compatibility.isLetter(keyChar)) { | |
7296 // make the keybinding case insensitive by adding it | |
7297 // in its upper and lower case form | |
7298 char ch = CharacterToUpper(keyChar); | |
7299 int newKey = ch | modifierValue; | |
7300 if (action is SWT.NULL) { | |
7301 keyActionMap.remove(newKey); | |
7302 } else { | |
7303 keyActionMap[newKey] = action; | |
7304 } | |
7305 ch = CharacterToLower(keyChar); | |
7306 newKey = ch | modifierValue; | |
7307 if (action is SWT.NULL) { | |
7308 keyActionMap.remove(newKey); | |
7309 } else { | |
7310 keyActionMap[newKey] = action; | |
7311 } | |
7312 } else { | |
7313 if (action is SWT.NULL) { | |
7314 keyActionMap.remove(key); | |
7315 } else { | |
7316 keyActionMap[key]=action; | |
7317 } | |
7318 } | |
7319 } | |
7320 /** | |
7321 * Sets the alignment of the specified lines. The argument should be one of <code>SWT.LEFT</code>, | |
7322 * <code>SWT.CENTER</code> or <code>SWT.RIGHT</code>. | |
7323 * <p><p> | |
7324 * Note that if <code>SWT.MULTI</code> is set, then <code>SWT.WRAP</code> must also be set | |
7325 * in order to stabilize the right edge before setting alignment. | |
7326 * </p> | |
7327 * Should not be called if a LineStyleListener has been set since the listener | |
7328 * maintains the line attributes. | |
7329 * </p><p> | |
7330 * All line attributes are maintained relative to the line text, not the | |
7331 * line index that is specified in this method call. | |
7332 * During text changes, when entire lines are inserted or removed, the line | |
7333 * attributes that are associated with the lines after the change | |
7334 * will "move" with their respective text. An entire line is defined as | |
7335 * extending from the first character on a line to the last and including the | |
7336 * line delimiter. | |
7337 * </p><p> | |
7338 * When two lines are joined by deleting a line delimiter, the top line | |
7339 * attributes take precedence and the attributes of the bottom line are deleted. | |
7340 * For all other text changes line attributes will remain unchanged. | |
7341 * | |
7342 * @param startLine first line the alignment is applied to, 0 based | |
7343 * @param lineCount number of lines the alignment applies to. | |
7344 * @param alignment line alignment | |
7345 * | |
7346 * @exception SWTException <ul> | |
7347 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7348 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7349 * </ul> | |
7350 * @exception IllegalArgumentException <ul> | |
7351 * <li>ERROR_INVALID_ARGUMENT when the specified line range is invalid</li> | |
7352 * </ul> | |
7353 * @see #setAlignment(int) | |
7354 * @since 3.2 | |
7355 */ | |
7356 public void setLineAlignment(int startLine, int lineCount, int alignment) { | |
7357 checkWidget(); | |
7358 if (isListening(LineGetStyle)) return; | |
7359 if (startLine < 0 || startLine + lineCount > content.getLineCount()) { | |
7360 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
7361 } | |
7362 | |
7363 renderer.setLineAlignment(startLine, lineCount, alignment); | |
7364 resetCache(startLine, lineCount); | |
7365 redrawLines(startLine, lineCount); | |
7366 int caretLine = getCaretLine(); | |
7367 if (startLine <= caretLine && caretLine < startLine + lineCount) { | |
7368 setCaretLocation(); | |
7369 } | |
7370 } | |
7371 /** | |
7372 * Sets the background color of the specified lines. | |
7373 * <p> | |
7374 * The background color is drawn for the width of the widget. All | |
7375 * line background colors are discarded when setText is called. | |
7376 * The text background color if defined in a StyleRange overlays the | |
7377 * line background color. | |
7378 * </p><p> | |
7379 * Should not be called if a LineBackgroundListener has been set since the | |
7380 * listener maintains the line backgrounds. | |
7381 * </p><p> | |
7382 * All line attributes are maintained relative to the line text, not the | |
7383 * line index that is specified in this method call. | |
7384 * During text changes, when entire lines are inserted or removed, the line | |
7385 * attributes that are associated with the lines after the change | |
7386 * will "move" with their respective text. An entire line is defined as | |
7387 * extending from the first character on a line to the last and including the | |
7388 * line delimiter. | |
7389 * </p><p> | |
7390 * When two lines are joined by deleting a line delimiter, the top line | |
7391 * attributes take precedence and the attributes of the bottom line are deleted. | |
7392 * For all other text changes line attributes will remain unchanged. | |
7393 * </p> | |
7394 * | |
7395 * @param startLine first line the color is applied to, 0 based | |
7396 * @param lineCount number of lines the color applies to. | |
7397 * @param background line background color | |
7398 * @exception SWTException <ul> | |
7399 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7400 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7401 * </ul> | |
7402 * @exception IllegalArgumentException <ul> | |
7403 * <li>ERROR_INVALID_ARGUMENT when the specified line range is invalid</li> | |
7404 * </ul> | |
7405 */ | |
7406 public void setLineBackground(int startLine, int lineCount, Color background) { | |
7407 checkWidget(); | |
7408 if (isListening(LineGetBackground)) return; | |
7409 if (startLine < 0 || startLine + lineCount > content.getLineCount()) { | |
7410 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
7411 } | |
7412 if (background !is null) { | |
7413 renderer.setLineBackground(startLine, lineCount, background); | |
7414 } else { | |
7415 renderer.clearLineBackground(startLine, lineCount); | |
7416 } | |
7417 redrawLines(startLine, lineCount); | |
7418 } | |
7419 /** | |
7420 * Sets the bullet of the specified lines. | |
7421 * <p> | |
7422 * Should not be called if a LineStyleListener has been set since the listener | |
7423 * maintains the line attributes. | |
7424 * </p><p> | |
7425 * All line attributes are maintained relative to the line text, not the | |
7426 * line index that is specified in this method call. | |
7427 * During text changes, when entire lines are inserted or removed, the line | |
7428 * attributes that are associated with the lines after the change | |
7429 * will "move" with their respective text. An entire line is defined as | |
7430 * extending from the first character on a line to the last and including the | |
7431 * line delimiter. | |
7432 * </p><p> | |
7433 * When two lines are joined by deleting a line delimiter, the top line | |
7434 * attributes take precedence and the attributes of the bottom line are deleted. | |
7435 * For all other text changes line attributes will remain unchanged. | |
7436 * </p> | |
7437 * | |
7438 * @param startLine first line the bullet is applied to, 0 based | |
7439 * @param lineCount number of lines the bullet applies to. | |
7440 * @param bullet line bullet | |
7441 * | |
7442 * @exception SWTException <ul> | |
7443 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7444 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7445 * </ul> | |
7446 * @exception IllegalArgumentException <ul> | |
7447 * <li>ERROR_INVALID_ARGUMENT when the specified line range is invalid</li> | |
7448 * </ul> | |
7449 * @since 3.2 | |
7450 */ | |
7451 public void setLineBullet(int startLine, int lineCount, Bullet bullet) { | |
7452 checkWidget(); | |
7453 if (isListening(LineGetStyle)) return; | |
7454 if (startLine < 0 || startLine + lineCount > content.getLineCount()) { | |
7455 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
7456 } | |
7457 | |
7458 renderer.setLineBullet(startLine, lineCount, bullet); | |
7459 resetCache(startLine, lineCount); | |
7460 redrawLines(startLine, lineCount); | |
7461 int caretLine = getCaretLine(); | |
7462 if (startLine <= caretLine && caretLine < startLine + lineCount) { | |
7463 setCaretLocation(); | |
7464 } | |
7465 } | |
7466 void setVariableLineHeight () { | |
7467 if (!fixedLineHeight) return; | |
7468 fixedLineHeight = false; | |
7469 renderer.calculateIdle(); | |
7470 } | |
7471 /** | |
7472 * Sets the indent of the specified lines. | |
7473 * <p> | |
7474 * Should not be called if a LineStyleListener has been set since the listener | |
7475 * maintains the line attributes. | |
7476 * </p><p> | |
7477 * All line attributes are maintained relative to the line text, not the | |
7478 * line index that is specified in this method call. | |
7479 * During text changes, when entire lines are inserted or removed, the line | |
7480 * attributes that are associated with the lines after the change | |
7481 * will "move" with their respective text. An entire line is defined as | |
7482 * extending from the first character on a line to the last and including the | |
7483 * line delimiter. | |
7484 * </p><p> | |
7485 * When two lines are joined by deleting a line delimiter, the top line | |
7486 * attributes take precedence and the attributes of the bottom line are deleted. | |
7487 * For all other text changes line attributes will remain unchanged. | |
7488 * </p> | |
7489 * | |
7490 * @param startLine first line the indent is applied to, 0 based | |
7491 * @param lineCount number of lines the indent applies to. | |
7492 * @param indent line indent | |
7493 * | |
7494 * @exception SWTException <ul> | |
7495 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7496 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7497 * </ul> | |
7498 * @exception IllegalArgumentException <ul> | |
7499 * <li>ERROR_INVALID_ARGUMENT when the specified line range is invalid</li> | |
7500 * </ul> | |
7501 * @see #setIndent(int) | |
7502 * @since 3.2 | |
7503 */ | |
7504 public void setLineIndent(int startLine, int lineCount, int indent) { | |
7505 checkWidget(); | |
7506 if (isListening(LineGetStyle)) return; | |
7507 if (startLine < 0 || startLine + lineCount > content.getLineCount()) { | |
7508 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
7509 } | |
7510 | |
7511 renderer.setLineIndent(startLine, lineCount, indent); | |
7512 resetCache(startLine, lineCount); | |
7513 redrawLines(startLine, lineCount); | |
7514 int caretLine = getCaretLine(); | |
7515 if (startLine <= caretLine && caretLine < startLine + lineCount) { | |
7516 setCaretLocation(); | |
7517 } | |
7518 } | |
7519 /** | |
7520 * Sets the justify of the specified lines. | |
7521 * <p> | |
7522 * Should not be called if a LineStyleListener has been set since the listener | |
7523 * maintains the line attributes. | |
7524 * </p><p> | |
7525 * All line attributes are maintained relative to the line text, not the | |
7526 * line index that is specified in this method call. | |
7527 * During text changes, when entire lines are inserted or removed, the line | |
7528 * attributes that are associated with the lines after the change | |
7529 * will "move" with their respective text. An entire line is defined as | |
7530 * extending from the first character on a line to the last and including the | |
7531 * line delimiter. | |
7532 * </p><p> | |
7533 * When two lines are joined by deleting a line delimiter, the top line | |
7534 * attributes take precedence and the attributes of the bottom line are deleted. | |
7535 * For all other text changes line attributes will remain unchanged. | |
7536 * </p> | |
7537 * | |
7538 * @param startLine first line the justify is applied to, 0 based | |
7539 * @param lineCount number of lines the justify applies to. | |
7540 * @param justify true if lines should be justified | |
7541 * | |
7542 * @exception SWTException <ul> | |
7543 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7544 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7545 * </ul> | |
7546 * @exception IllegalArgumentException <ul> | |
7547 * <li>ERROR_INVALID_ARGUMENT when the specified line range is invalid</li> | |
7548 * </ul> | |
7549 * @see #setJustify(bool) | |
7550 * @since 3.2 | |
7551 */ | |
7552 public void setLineJustify(int startLine, int lineCount, bool justify) { | |
7553 checkWidget(); | |
7554 if (isListening(LineGetStyle)) return; | |
7555 if (startLine < 0 || startLine + lineCount > content.getLineCount()) { | |
7556 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
7557 } | |
7558 | |
7559 renderer.setLineJustify(startLine, lineCount, justify); | |
7560 resetCache(startLine, lineCount); | |
7561 redrawLines(startLine, lineCount); | |
7562 int caretLine = getCaretLine(); | |
7563 if (startLine <= caretLine && caretLine < startLine + lineCount) { | |
7564 setCaretLocation(); | |
7565 } | |
7566 } | |
7567 /** | |
7568 * Sets the line spacing of the widget. The line spacing applies for all lines. | |
7569 * | |
7570 * @param lineSpacing the line spacing | |
7571 * @exception SWTException <ul> | |
7572 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7573 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7574 * </ul> | |
7575 * @since 3.2 | |
7576 */ | |
7577 public void setLineSpacing(int lineSpacing) { | |
7578 checkWidget(); | |
7579 if (this.lineSpacing is lineSpacing || lineSpacing < 0) return; | |
7580 this.lineSpacing = lineSpacing; | |
7581 setVariableLineHeight(); | |
7582 resetCache(0, content.getLineCount()); | |
7583 setCaretLocation(); | |
7584 super.redraw(); | |
7585 } | |
7586 void setMargins (int leftMargin, int topMargin, int rightMargin, int bottomMargin) { | |
7587 checkWidget(); | |
7588 this.leftMargin = leftMargin; | |
7589 this.topMargin = topMargin; | |
7590 this.rightMargin = rightMargin; | |
7591 this.bottomMargin = bottomMargin; | |
7592 setCaretLocation(); | |
7593 } | |
7594 /** | |
7595 * Flips selection anchor based on word selection direction. | |
7596 */ | |
7597 void setMouseWordSelectionAnchor() { | |
7598 if (clickCount > 1) { | |
7599 if (caretOffset < doubleClickSelection.x) { | |
7600 selectionAnchor = doubleClickSelection.y; | |
7601 } else if (caretOffset > doubleClickSelection.y) { | |
7602 selectionAnchor = doubleClickSelection.x; | |
7603 } | |
7604 } | |
7605 } | |
7606 /** | |
7607 * Sets the orientation of the receiver, which must be one | |
7608 * of the constants <code>SWT.LEFT_TO_RIGHT</code> or <code>SWT.RIGHT_TO_LEFT</code>. | |
7609 * | |
7610 * @param orientation new orientation style | |
7611 * | |
7612 * @exception SWTException <ul> | |
7613 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7614 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7615 * </ul> | |
7616 * | |
7617 * @since 2.1.2 | |
7618 */ | |
7619 public void setOrientation(int orientation) { | |
7620 if ((orientation & (SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT)) is 0) { | |
7621 return; | |
7622 } | |
7623 if ((orientation & SWT.RIGHT_TO_LEFT) !is 0 && (orientation & SWT.LEFT_TO_RIGHT) !is 0) { | |
7624 return; | |
7625 } | |
7626 if ((orientation & SWT.RIGHT_TO_LEFT) !is 0 && isMirrored()) { | |
7627 return; | |
7628 } | |
7629 if ((orientation & SWT.LEFT_TO_RIGHT) !is 0 && !isMirrored()) { | |
7630 return; | |
7631 } | |
7632 if (!BidiUtil.setOrientation(this, orientation)) { | |
7633 return; | |
7634 } | |
7635 isMirrored_ = (orientation & SWT.RIGHT_TO_LEFT) !is 0; | |
7636 caretDirection = SWT.NULL; | |
7637 resetCache(0, content.getLineCount()); | |
7638 setCaretLocation(); | |
7639 keyActionMap = null; | |
7640 createKeyBindings(); | |
7641 super.redraw(); | |
7642 } | |
7643 /** | |
7644 * Adjusts the maximum and the page size of the scroll bars to | |
7645 * reflect content width/length changes. | |
7646 * | |
7647 * @param vertical indicates if the vertical scrollbar also needs to be set | |
7648 */ | |
7649 void setScrollBars(bool vertical) { | |
7650 int inactive = 1; | |
7651 if (vertical || !isFixedLineHeight()) { | |
7652 ScrollBar verticalBar = getVerticalBar(); | |
7653 if (verticalBar !is null) { | |
7654 int maximum = renderer.getHeight(); | |
7655 // only set the real values if the scroll bar can be used | |
7656 // (ie. because the thumb size is less than the scroll maximum) | |
7657 // avoids flashing on Motif, fixes 1G7RE1J and 1G5SE92 | |
7658 if (clientAreaHeight < maximum) { | |
7659 verticalBar.setMaximum(maximum); | |
7660 verticalBar.setThumb(clientAreaHeight); | |
7661 verticalBar.setPageIncrement(clientAreaHeight); | |
7662 } else if (verticalBar.getThumb() !is inactive || verticalBar.getMaximum() !is inactive) { | |
7663 verticalBar.setValues( | |
7664 verticalBar.getSelection(), | |
7665 verticalBar.getMinimum(), | |
7666 inactive, | |
7667 inactive, | |
7668 verticalBar.getIncrement(), | |
7669 inactive); | |
7670 } | |
7671 } | |
7672 } | |
7673 ScrollBar horizontalBar = getHorizontalBar(); | |
7674 if (horizontalBar !is null && horizontalBar.getVisible()) { | |
7675 int maximum = renderer.getWidth(); | |
7676 // only set the real values if the scroll bar can be used | |
7677 // (ie. because the thumb size is less than the scroll maximum) | |
7678 // avoids flashing on Motif, fixes 1G7RE1J and 1G5SE92 | |
7679 if (clientAreaWidth < maximum) { | |
7680 horizontalBar.setMaximum(maximum); | |
7681 horizontalBar.setThumb(clientAreaWidth - leftMargin - rightMargin); | |
7682 horizontalBar.setPageIncrement(clientAreaWidth - leftMargin - rightMargin); | |
7683 } else if (horizontalBar.getThumb() !is inactive || horizontalBar.getMaximum() !is inactive) { | |
7684 horizontalBar.setValues( | |
7685 horizontalBar.getSelection(), | |
7686 horizontalBar.getMinimum(), | |
7687 inactive, | |
7688 inactive, | |
7689 horizontalBar.getIncrement(), | |
7690 inactive); | |
7691 } | |
7692 } | |
7693 } | |
7694 /** | |
7695 * Sets the selection to the given position and scrolls it into view. Equivalent to setSelection(start,start). | |
7696 * | |
7697 * @param start new caret position | |
7698 * @see #setSelection(int,int) | |
7699 * @exception SWTException <ul> | |
7700 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7701 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7702 * </ul> | |
7703 * @exception IllegalArgumentException <ul> | |
7704 * <li>ERROR_INVALID_ARGUMENT when either the start or the end of the selection range is inside a | |
7705 * multi byte line delimiter (and thus neither clearly in front of or after the line delimiter) | |
7706 * </ul> | |
7707 */ | |
7708 public void setSelection(int start) { | |
7709 // checkWidget test done in setSelectionRange | |
7710 setSelection(start, start); | |
7711 } | |
7712 /** | |
7713 * Sets the selection and scrolls it into view. | |
7714 * <p> | |
7715 * Indexing is zero based. Text selections are specified in terms of | |
7716 * caret positions. In a text widget that contains N characters, there are | |
7717 * N+1 caret positions, ranging from 0..N | |
7718 * </p> | |
7719 * | |
7720 * @param point x=selection start offset, y=selection end offset | |
7721 * The caret will be placed at the selection start when x > y. | |
7722 * @see #setSelection(int,int) | |
7723 * @exception SWTException <ul> | |
7724 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7725 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7726 * </ul> | |
7727 * @exception IllegalArgumentException <ul> | |
7728 * <li>ERROR_NULL_ARGUMENT when point is null</li> | |
7729 * <li>ERROR_INVALID_ARGUMENT when either the start or the end of the selection range is inside a | |
7730 * multi byte line delimiter (and thus neither clearly in front of or after the line delimiter) | |
7731 * </ul> | |
7732 */ | |
7733 public void setSelection(Point point) { | |
7734 checkWidget(); | |
7735 if (point is null) SWT.error (SWT.ERROR_NULL_ARGUMENT); | |
7736 setSelection(point.x, point.y); | |
7737 } | |
7738 /** | |
7739 * Sets the receiver's selection background color to the color specified | |
7740 * by the argument, or to the default system color for the control | |
7741 * if the argument is null. | |
7742 * | |
7743 * @param color the new color (or null) | |
7744 * | |
7745 * @exception IllegalArgumentException <ul> | |
7746 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> | |
7747 * </ul> | |
7748 * @exception SWTException <ul> | |
7749 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7750 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7751 * </ul> | |
7752 * @since 2.1 | |
7753 */ | |
7754 public void setSelectionBackground (Color color) { | |
7755 checkWidget (); | |
7756 if (color !is null) { | |
7757 if (color.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
7758 } | |
7759 selectionBackground = color; | |
7760 super.redraw(); | |
7761 } | |
7762 /** | |
7763 * Sets the receiver's selection foreground color to the color specified | |
7764 * by the argument, or to the default system color for the control | |
7765 * if the argument is null. | |
7766 * <p> | |
7767 * Note that this is a <em>HINT</em>. Some platforms do not allow the application | |
7768 * to change the selection foreground color. | |
7769 * </p> | |
7770 * @param color the new color (or null) | |
7771 * | |
7772 * @exception IllegalArgumentException <ul> | |
7773 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> | |
7774 * </ul> | |
7775 * @exception SWTException <ul> | |
7776 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7777 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7778 * </ul> | |
7779 * @since 2.1 | |
7780 */ | |
7781 public void setSelectionForeground (Color color) { | |
7782 checkWidget (); | |
7783 if (color !is null) { | |
7784 if (color.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
7785 } | |
7786 selectionForeground = color; | |
7787 super.redraw(); | |
7788 } | |
7789 /** | |
7790 * Sets the selection and scrolls it into view. | |
7791 * <p> | |
7792 * Indexing is zero based. Text selections are specified in terms of | |
7793 * caret positions. In a text widget that contains N characters, there are | |
7794 * N+1 caret positions, ranging from 0..N | |
7795 * </p> | |
7796 * | |
7797 * @param start selection start offset. The caret will be placed at the | |
7798 * selection start when start > end. | |
7799 * @param end selection end offset | |
7800 * @see #setSelectionRange(int,int) | |
7801 * @exception SWTException <ul> | |
7802 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7803 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7804 * </ul> | |
7805 * @exception IllegalArgumentException <ul> | |
7806 * <li>ERROR_INVALID_ARGUMENT when either the start or the end of the selection range is inside a | |
7807 * multi byte line delimiter (and thus neither clearly in front of or after the line delimiter) | |
7808 * </ul> | |
7809 */ | |
7810 public void setSelection(int start, int end) { | |
7811 setSelectionRange(start, end - start); | |
7812 showSelection(); | |
7813 } | |
7814 /** | |
7815 * Sets the selection. | |
7816 * <p> | |
7817 * The new selection may not be visible. Call showSelection to scroll | |
7818 * the selection into view. | |
7819 * </p> | |
7820 * | |
7821 * @param start offset of the first selected character, start >= 0 must be true. | |
7822 * @param length number of characters to select, 0 <= start + length | |
7823 * <= getCharCount() must be true. | |
7824 * A negative length places the caret at the selection start. | |
7825 * @param sendEvent a Selection event is sent when set to true and when | |
7826 * the selection is reset. | |
7827 */ | |
7828 void setSelection(int start, int length, bool sendEvent) { | |
7829 int end = start + length; | |
7830 start = content.utf8AdjustOffset(start); | |
7831 end = content.utf8AdjustOffset(end); | |
7832 if (start > end) { | |
7833 int temp = end; | |
7834 end = start; | |
7835 start = temp; | |
7836 } | |
7837 // is the selection range different or is the selection direction | |
7838 // different? | |
7839 if (selection.x !is start || selection.y !is end || | |
7840 (length > 0 && selectionAnchor !is selection.x) || | |
7841 (length < 0 && selectionAnchor !is selection.y)) { | |
7842 clearSelection(sendEvent); | |
7843 if (length < 0) { | |
7844 selectionAnchor = selection.y = end; | |
7845 caretOffset = selection.x = start; | |
7846 } else { | |
7847 selectionAnchor = selection.x = start; | |
7848 caretOffset = selection.y = end; | |
7849 } | |
7850 caretAlignment = PREVIOUS_OFFSET_TRAILING; | |
7851 internalRedrawRange(selection.x, selection.y - selection.x); | |
7852 } | |
7853 } | |
7854 /** | |
7855 * Sets the selection. | |
7856 * <p> | |
7857 * The new selection may not be visible. Call showSelection to scroll the selection | |
7858 * into view. A negative length places the caret at the visual start of the selection. | |
7859 * </p> | |
7860 * | |
7861 * @param start offset of the first selected character | |
7862 * @param length number of characters to select | |
7863 * | |
7864 * @exception SWTException <ul> | |
7865 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7866 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7867 * </ul> | |
7868 * @exception IllegalArgumentException <ul> | |
7869 * <li>ERROR_INVALID_ARGUMENT when either the start or the end of the selection range is inside a | |
7870 * multi byte line delimiter (and thus neither clearly in front of or after the line delimiter) | |
7871 * </ul> | |
7872 */ | |
7873 public void setSelectionRange(int start, int length) { | |
7874 checkWidget(); | |
7875 int contentLength = getCharCount(); | |
7876 start = Math.max(0, Math.min (start, contentLength)); | |
7877 int end = start + length; | |
7878 if (end < 0) { | |
7879 length = -start; | |
7880 } else { | |
7881 if (end > contentLength) length = contentLength - start; | |
7882 } | |
7883 if (isLineDelimiter(start) || isLineDelimiter(start + length)) { | |
7884 // the start offset or end offset of the selection range is inside a | |
7885 // multi byte line delimiter. This is an illegal operation and an exception | |
7886 // is thrown. Fixes 1GDKK3R | |
7887 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
7888 } | |
7889 setSelection(start, length, false); | |
7890 setCaretLocation(); | |
7891 } | |
7892 /** | |
7893 * Adds the specified style. | |
7894 * <p> | |
7895 * The new style overwrites existing styles for the specified range. | |
7896 * Existing style ranges are adjusted if they partially overlap with | |
7897 * the new style. To clear an individual style, call setStyleRange | |
7898 * with a StyleRange that has null attributes. | |
7899 * </p><p> | |
7900 * Should not be called if a LineStyleListener has been set since the | |
7901 * listener maintains the styles. | |
7902 * </p> | |
7903 * | |
7904 * @param range StyleRange object containing the style information. | |
7905 * Overwrites the old style in the given range. May be null to delete | |
7906 * all styles. | |
7907 * @exception SWTException <ul> | |
7908 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7909 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7910 * </ul> | |
7911 * @exception IllegalArgumentException <ul> | |
7912 * <li>ERROR_INVALID_RANGE when the style range is outside the valid range (> getCharCount())</li> | |
7913 * </ul> | |
7914 */ | |
7915 public void setStyleRange(StyleRange range) { | |
7916 checkWidget(); | |
7917 if (isListening(LineGetStyle)) return; | |
7918 if (range !is null) { | |
7919 if (range.isUnstyled()) { | |
7920 setStyleRanges(range.start, range.length, null, null, false); | |
7921 } else { | |
7922 setStyleRanges(range.start, 0, null, [range], false); | |
7923 } | |
7924 } else { | |
7925 setStyleRanges(0, 0, null, null, true); | |
7926 } | |
7927 } | |
7928 /** | |
7929 * Clears the styles in the range specified by <code>start</code> and | |
7930 * <code>length</code> and adds the new styles. | |
7931 * <p> | |
7932 * The ranges array contains start and length pairs. Each pair refers to | |
7933 * the corresponding style in the styles array. For example, the pair | |
7934 * that starts at ranges[n] with length ranges[n+1] uses the style | |
7935 * at styles[n/2]. The range fields within each StyleRange are ignored. | |
7936 * If ranges or styles is null, the specified range is cleared. | |
7937 * </p><p> | |
7938 * Note: It is expected that the same instance of a StyleRange will occur | |
7939 * multiple times within the styles array, reducing memory usage. | |
7940 * </p><p> | |
7941 * Should not be called if a LineStyleListener has been set since the | |
7942 * listener maintains the styles. | |
7943 * </p> | |
7944 * | |
7945 * @param start offset of first character where styles will be deleted | |
7946 * @param length length of the range to delete styles in | |
7947 * @param ranges the array of ranges. The ranges must not overlap and must be in order. | |
7948 * @param styles the array of StyleRanges. The range fields within the StyleRange are unused. | |
7949 * | |
7950 * @exception SWTException <ul> | |
7951 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7952 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7953 * </ul> | |
7954 * @exception IllegalArgumentException <ul> | |
7955 * <li>ERROR_NULL_ARGUMENT when an element in the styles array is null</li> | |
7956 * <li>ERROR_INVALID_RANGE when the number of ranges and style do not match (ranges.length * 2 is styles.length)</li> | |
7957 * <li>ERROR_INVALID_RANGE when a range is outside the valid range (> getCharCount() or less than zero)</li> | |
7958 * <li>ERROR_INVALID_RANGE when a range overlaps</li> | |
7959 * </ul> | |
7960 * | |
7961 * @since 3.2 | |
7962 */ | |
7963 public void setStyleRanges(int start, int length, int[] ranges, StyleRange[] styles) { | |
7964 checkWidget(); | |
7965 if (isListening(LineGetStyle)) return; | |
7966 if (ranges is null || styles is null) { | |
7967 setStyleRanges(start, length, null, null, false); | |
7968 } else { | |
7969 setStyleRanges(start, length, ranges, styles, false); | |
7970 } | |
7971 } | |
7972 /** | |
7973 * Sets styles to be used for rendering the widget content. | |
7974 * <p> | |
7975 * All styles in the widget will be replaced with the given set of ranges and styles. | |
7976 * The ranges array contains start and length pairs. Each pair refers to | |
7977 * the corresponding style in the styles array. For example, the pair | |
7978 * that starts at ranges[n] with length ranges[n+1] uses the style | |
7979 * at styles[n/2]. The range fields within each StyleRange are ignored. | |
7980 * If either argument is null, the styles are cleared. | |
7981 * </p><p> | |
7982 * Note: It is expected that the same instance of a StyleRange will occur | |
7983 * multiple times within the styles array, reducing memory usage. | |
7984 * </p><p> | |
7985 * Should not be called if a LineStyleListener has been set since the | |
7986 * listener maintains the styles. | |
7987 * </p> | |
7988 * | |
7989 * @param ranges the array of ranges. The ranges must not overlap and must be in order. | |
7990 * @param styles the array of StyleRanges. The range fields within the StyleRange are unused. | |
7991 * | |
7992 * @exception SWTException <ul> | |
7993 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
7994 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
7995 * </ul> | |
7996 * @exception IllegalArgumentException <ul> | |
7997 * <li>ERROR_NULL_ARGUMENT when an element in the styles array is null</li> | |
7998 * <li>ERROR_INVALID_RANGE when the number of ranges and style do not match (ranges.length * 2 is styles.length)</li> | |
7999 * <li>ERROR_INVALID_RANGE when a range is outside the valid range (> getCharCount() or less than zero)</li> | |
8000 * <li>ERROR_INVALID_RANGE when a range overlaps</li> | |
8001 * </ul> | |
8002 * | |
8003 * @since 3.2 | |
8004 */ | |
8005 public void setStyleRanges(int[] ranges, StyleRange[] styles) { | |
8006 checkWidget(); | |
8007 if (isListening(LineGetStyle)) return; | |
8008 if (ranges is null || styles is null) { | |
8009 setStyleRanges(0, 0, null, null, true); | |
8010 } else { | |
8011 setStyleRanges(0, 0, ranges, styles, true); | |
8012 } | |
8013 } | |
8014 void setStyleRanges(int start, int length, int[] ranges, StyleRange[] styles, bool reset) { | |
8015 int charCount = content.getCharCount(); | |
8016 int end = start + length; | |
8017 if (start > end || start < 0) { | |
8018 SWT.error(SWT.ERROR_INVALID_RANGE); | |
8019 } | |
8020 if (styles !is null) { | |
8021 if (end > charCount) { | |
8022 SWT.error(SWT.ERROR_INVALID_RANGE); | |
8023 } | |
8024 if (ranges !is null) { | |
8025 if (ranges.length !is styles.length << 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
8026 } | |
8027 int lastOffset = 0; | |
8028 bool variableHeight = false; | |
8029 for (int i = 0; i < styles.length; i ++) { | |
8030 if (styles[i] is null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
8031 int rangeStart, rangeLength; | |
8032 if (ranges !is null) { | |
8033 rangeStart = ranges[i << 1]; | |
8034 rangeLength = ranges[(i << 1) + 1]; | |
8035 } else { | |
8036 rangeStart = styles[i].start; | |
8037 rangeLength = styles[i].length; | |
8038 } | |
8039 if (rangeLength < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
8040 if (!(0 <= rangeStart && rangeStart + rangeLength <= charCount)) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
8041 if (lastOffset > rangeStart) SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
8042 variableHeight |= styles[i].isVariableHeight(); | |
8043 lastOffset = rangeStart + rangeLength; | |
8044 } | |
8045 if (variableHeight) setVariableLineHeight(); | |
8046 } | |
8047 int rangeStart = start, rangeEnd = end; | |
8048 if (styles !is null && styles.length > 0) { | |
8049 if (ranges !is null) { | |
8050 rangeStart = ranges[0]; | |
8051 rangeEnd = ranges[ranges.length - 2] + ranges[ranges.length - 1]; | |
8052 } else { | |
8053 rangeStart = styles[0].start; | |
8054 rangeEnd = styles[styles.length - 1].start + styles[styles.length - 1].length; | |
8055 } | |
8056 } | |
8057 int lastLineBottom = 0; | |
8058 if (!isFixedLineHeight() && !reset) { | |
8059 int lineEnd = content.getLineAtOffset(Math.max(end, rangeEnd)); | |
8060 int partialTopIndex = getPartialTopIndex(); | |
8061 int partialBottomIndex = getPartialBottomIndex(); | |
8062 if (partialTopIndex <= lineEnd && lineEnd <= partialBottomIndex) { | |
8063 lastLineBottom = getLinePixel(lineEnd + 1); | |
8064 } | |
8065 } | |
8066 if (reset) { | |
8067 renderer.setStyleRanges(null, null); | |
8068 } else { | |
8069 renderer.updateRanges(start, length, length); | |
8070 } | |
8071 if (styles !is null && styles.length > 0) { | |
8072 renderer.setStyleRanges(ranges, styles); | |
8073 } | |
8074 if (reset) { | |
8075 resetCache(0, content.getLineCount()); | |
8076 super.redraw(); | |
8077 } else { | |
8078 int lineStart = content.getLineAtOffset(Math.min(start, rangeStart)); | |
8079 int lineEnd = content.getLineAtOffset(Math.max(end, rangeEnd)); | |
8080 resetCache(lineStart, lineEnd - lineStart + 1); | |
8081 int partialTopIndex = getPartialTopIndex(); | |
8082 int partialBottomIndex = getPartialBottomIndex(); | |
8083 if (!(lineStart > partialBottomIndex || lineEnd < partialTopIndex)) { | |
8084 int y = 0; | |
8085 int height = clientAreaHeight; | |
8086 if (partialTopIndex <= lineStart && lineStart <= partialBottomIndex) { | |
8087 int lineTop = Math.max(y, getLinePixel(lineStart)); | |
8088 y = lineTop; | |
8089 height -= lineTop; | |
8090 } | |
8091 if (partialTopIndex <= lineEnd && lineEnd <= partialBottomIndex) { | |
8092 int newLastLineBottom = getLinePixel(lineEnd + 1); | |
8093 if (!isFixedLineHeight()) { | |
8094 scrollText(lastLineBottom, newLastLineBottom); | |
8095 } | |
8096 height = newLastLineBottom - y; | |
8097 } | |
8098 super.redraw(0, y, clientAreaWidth, height, false); | |
8099 } | |
8100 } | |
8101 setCaretLocation(); | |
8102 } | |
8103 /** | |
8104 * Sets styles to be used for rendering the widget content. All styles | |
8105 * in the widget will be replaced with the given set of styles. | |
8106 * <p> | |
8107 * Note: Because a StyleRange includes the start and length, the | |
8108 * same instance cannot occur multiple times in the array of styles. | |
8109 * If the same style attributes, such as font and color, occur in | |
8110 * multiple StyleRanges, <code>setStyleRanges(int[], StyleRange[])</code> | |
8111 * can be used to share styles and reduce memory usage. | |
8112 * </p><p> | |
8113 * Should not be called if a LineStyleListener has been set since the | |
8114 * listener maintains the styles. | |
8115 * </p> | |
8116 * | |
8117 * @param ranges StyleRange objects containing the style information. | |
8118 * The ranges should not overlap. The style rendering is undefined if | |
8119 * the ranges do overlap. Must not be null. The styles need to be in order. | |
8120 * @exception SWTException <ul> | |
8121 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
8122 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
8123 * </ul> | |
8124 * @exception IllegalArgumentException <ul> | |
8125 * <li>ERROR_INVALID_RANGE when the last of the style ranges is outside the valid range (> getCharCount())</li> | |
8126 * </ul> | |
8127 * | |
8128 * @see #setStyleRanges(int[], StyleRange[]) | |
8129 */ | |
8130 public void setStyleRanges(StyleRange[] ranges) { | |
8131 checkWidget(); | |
8132 if (isListening(LineGetStyle)) return; | |
8133 // SWT extension: allow null for zero length string | |
8134 //if (ranges is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
8135 setStyleRanges(0, 0, null, ranges, true); | |
8136 } | |
8137 /** | |
8138 * Sets the tab width. | |
8139 * | |
8140 * @param tabs tab width measured in characters. | |
8141 * @exception SWTException <ul> | |
8142 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
8143 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
8144 * </ul> | |
8145 */ | |
8146 public void setTabs(int tabs) { | |
8147 checkWidget(); | |
8148 tabLength = tabs; | |
8149 renderer.setFont(null, tabs); | |
8150 resetCache(0, content.getLineCount()); | |
8151 setCaretLocation(); | |
8152 super.redraw(); | |
8153 } | |
8154 /** | |
8155 * Sets the widget content. | |
8156 * If the widget has the SWT.SINGLE style and "text" contains more than | |
8157 * one line, only the first line is rendered but the text is stored | |
8158 * unchanged. A subsequent call to getText will return the same text | |
8159 * that was set. | |
8160 * <p> | |
8161 * <b>Note:</b> Only a single line of text should be set when the SWT.SINGLE | |
8162 * style is used. | |
8163 * </p> | |
8164 * | |
8165 * @param text new widget content. Replaces existing content. Line styles | |
8166 * that were set using StyledText API are discarded. The | |
8167 * current selection is also discarded. | |
8168 * @exception SWTException <ul> | |
8169 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
8170 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
8171 * </ul> | |
8172 */ | |
8173 public void setText(String text) { | |
8174 checkWidget(); | |
8175 // SWT extension: allow null for zero length string | |
8176 // if (text is null) { | |
8177 // SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
8178 // } | |
8179 Event event = new Event(); | |
8180 event.start = 0; | |
8181 event.end = getCharCount(); | |
8182 event.text = text; | |
8183 event.doit = true; | |
8184 notifyListeners(SWT.Verify, event); | |
8185 if (event.doit) { | |
8186 StyledTextEvent styledTextEvent = null; | |
8187 if (isListening(ExtendedModify)) { | |
8188 styledTextEvent = new StyledTextEvent(content); | |
8189 styledTextEvent.start = event.start; | |
8190 styledTextEvent.end = event.start + event.text.length; | |
8191 styledTextEvent.text = content.getTextRange(event.start, event.end - event.start); | |
8192 } | |
8193 content.setText(event.text); | |
8194 sendModifyEvent(event); | |
8195 if (styledTextEvent !is null) { | |
8196 notifyListeners(ExtendedModify, styledTextEvent); | |
8197 } | |
8198 } | |
8199 } | |
8200 /** | |
8201 * Sets the text limit to the specified number of characters. | |
8202 * <p> | |
8203 * The text limit specifies the amount of text that | |
8204 * the user can type into the widget. | |
8205 * </p> | |
8206 * | |
8207 * @param limit the new text limit. | |
8208 * @exception SWTException <ul> | |
8209 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
8210 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
8211 * </ul> | |
8212 * @exception IllegalArgumentException <ul> | |
8213 * <li>ERROR_CANNOT_BE_ZERO when limit is 0</li> | |
8214 * </ul> | |
8215 */ | |
8216 public void setTextLimit(int limit) { | |
8217 checkWidget(); | |
8218 if (limit is 0) { | |
8219 SWT.error(SWT.ERROR_CANNOT_BE_ZERO); | |
8220 } | |
8221 textLimit = limit; | |
8222 } | |
8223 /** | |
8224 * Sets the top index. Do nothing if there is no text set. | |
8225 * <p> | |
8226 * The top index is the index of the line that is currently at the top | |
8227 * of the widget. The top index changes when the widget is scrolled. | |
8228 * Indexing starts from zero. | |
8229 * Note: The top index is reset to 0 when new text is set in the widget. | |
8230 * </p> | |
8231 * | |
8232 * @param topIndex new top index. Must be between 0 and | |
8233 * getLineCount() - fully visible lines per page. If no lines are fully | |
8234 * visible the maximum value is getLineCount() - 1. An out of range | |
8235 * index will be adjusted accordingly. | |
8236 * @exception SWTException <ul> | |
8237 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
8238 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
8239 * </ul> | |
8240 */ | |
8241 public void setTopIndex(int topIndex) { | |
8242 checkWidget(); | |
8243 if (getCharCount() is 0) { | |
8244 return; | |
8245 } | |
8246 int lineCount = content.getLineCount(), pixel; | |
8247 if (isFixedLineHeight()) { | |
8248 int pageSize = Math.max(1, Math.min(lineCount, getLineCountWhole())); | |
8249 if (topIndex < 0) { | |
8250 topIndex = 0; | |
8251 } else if (topIndex > lineCount - pageSize) { | |
8252 topIndex = lineCount - pageSize; | |
8253 } | |
8254 pixel = getLinePixel(topIndex); | |
8255 } else { | |
8256 topIndex = Math.max(0, Math.min(lineCount - 1, topIndex)); | |
8257 pixel = getLinePixel(topIndex); | |
8258 if (pixel > 0) { | |
8259 pixel = getAvailableHeightBellow(pixel); | |
8260 } else { | |
8261 pixel = getAvailableHeightAbove(pixel); | |
8262 } | |
8263 } | |
8264 scrollVertical(pixel, true); | |
8265 } | |
8266 /** | |
8267 * Sets the top pixel offset. Do nothing if there is no text set. | |
8268 * <p> | |
8269 * The top pixel offset is the vertical pixel offset of the widget. The | |
8270 * widget is scrolled so that the given pixel position is at the top. | |
8271 * The top index is adjusted to the corresponding top line. | |
8272 * Note: The top pixel is reset to 0 when new text is set in the widget. | |
8273 * </p> | |
8274 * | |
8275 * @param pixel new top pixel offset. Must be between 0 and | |
8276 * (getLineCount() - visible lines per page) / getLineHeight()). An out | |
8277 * of range offset will be adjusted accordingly. | |
8278 * @exception SWTException <ul> | |
8279 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
8280 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
8281 * </ul> | |
8282 * @since 2.0 | |
8283 */ | |
8284 public void setTopPixel(int pixel) { | |
8285 checkWidget(); | |
8286 if (getCharCount() is 0) { | |
8287 return; | |
8288 } | |
8289 if (pixel < 0) pixel = 0; | |
8290 int lineCount = content.getLineCount(); | |
8291 int height = clientAreaHeight - topMargin - bottomMargin; | |
8292 int verticalOffset = getVerticalScrollOffset(); | |
8293 if (isFixedLineHeight()) { | |
8294 int maxTopPixel = Math.max(0, lineCount * getVerticalIncrement() - height); | |
8295 if (pixel > maxTopPixel) pixel = maxTopPixel; | |
8296 pixel -= verticalOffset; | |
8297 } else { | |
8298 pixel -= verticalOffset; | |
8299 if (pixel > 0) { | |
8300 pixel = getAvailableHeightBellow(pixel); | |
8301 } | |
8302 } | |
8303 scrollVertical(pixel, true); | |
8304 } | |
8305 /** | |
8306 * Sets whether the widget wraps lines. | |
8307 * <p> | |
8308 * This overrides the creation style bit SWT.WRAP. | |
8309 * </p> | |
8310 * | |
8311 * @param wrap true=widget wraps lines, false=widget does not wrap lines | |
8312 * @since 2.0 | |
8313 */ | |
8314 public void setWordWrap(bool wrap) { | |
8315 checkWidget(); | |
8316 if ((getStyle() & SWT.SINGLE) !is 0) return; | |
8317 if (wordWrap is wrap) return; | |
8318 wordWrap = wrap; | |
8319 setVariableLineHeight(); | |
8320 resetCache(0, content.getLineCount()); | |
8321 horizontalScrollOffset = 0; | |
8322 ScrollBar horizontalBar = getHorizontalBar(); | |
8323 if (horizontalBar !is null) { | |
8324 horizontalBar.setVisible(!wordWrap); | |
8325 } | |
8326 setScrollBars(true); | |
8327 setCaretLocation(); | |
8328 super.redraw(); | |
8329 } | |
8330 // SWT: If necessary, scroll to show the location | |
8331 bool showLocation(Rectangle rect, bool scrollPage) { | |
8332 int clientAreaWidth = this.clientAreaWidth - leftMargin - rightMargin; | |
8333 int clientAreaHeight = this.clientAreaHeight - topMargin - bottomMargin; | |
8334 bool scrolled = false; | |
8335 if (rect.y <= topMargin) { | |
8336 scrolled = scrollVertical(rect.y - topMargin, true); | |
8337 } else if (rect.y + rect.height > clientAreaHeight) { | |
8338 if (clientAreaHeight is 0) { | |
8339 scrolled = scrollVertical(rect.y, true); | |
8340 } else { | |
8341 scrolled = scrollVertical(rect.y + rect.height - clientAreaHeight, true); | |
8342 } | |
8343 } | |
8344 if (clientAreaWidth > 0) { | |
8345 int minScroll = scrollPage ? clientAreaWidth / 4 : 0; | |
8346 if (rect.x < leftMargin) { | |
8347 int scrollWidth = Math.max(leftMargin - rect.x, minScroll); | |
8348 int maxScroll = horizontalScrollOffset; | |
8349 scrolled = scrollHorizontal(-Math.min(maxScroll, scrollWidth), true); | |
8350 } else if (rect.x + rect.width > clientAreaWidth) { | |
8351 int scrollWidth = Math.max(rect.x + rect.width - clientAreaWidth, minScroll); | |
8352 int maxScroll = renderer.getWidth() - horizontalScrollOffset - this.clientAreaWidth; | |
8353 scrolled = scrollHorizontal(Math.min(maxScroll, scrollWidth), true); | |
8354 } | |
8355 } | |
8356 return scrolled; | |
8357 } | |
8358 /** | |
8359 * Sets the caret location and scrolls the caret offset into view. | |
8360 */ | |
8361 void showCaret() { | |
8362 Rectangle bounds = getBoundsAtOffset(caretOffset); | |
8363 if (!showLocation(bounds, true)) { | |
8364 setCaretLocation(); | |
8365 } | |
8366 } | |
8367 /** | |
8368 * Scrolls the selection into view. | |
8369 * <p> | |
8370 * The end of the selection will be scrolled into view. | |
8371 * Note that if a right-to-left selection exists, the end of the selection is | |
8372 * the visual beginning of the selection (i.e., where the caret is located). | |
8373 * </p> | |
8374 * | |
8375 * @exception SWTException <ul> | |
8376 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
8377 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
8378 * </ul> | |
8379 */ | |
8380 public void showSelection() { | |
8381 checkWidget(); | |
8382 // is selection from right-to-left? | |
8383 bool rightToLeft = caretOffset is selection.x; | |
8384 int startOffset, endOffset; | |
8385 if (rightToLeft) { | |
8386 startOffset = selection.y; | |
8387 endOffset = selection.x; | |
8388 } else { | |
8389 startOffset = selection.x; | |
8390 endOffset = selection.y; | |
8391 } | |
8392 | |
8393 Rectangle startBounds = getBoundsAtOffset(startOffset); | |
8394 Rectangle endBounds = getBoundsAtOffset(endOffset); | |
8395 | |
8396 // can the selection be fully displayed within the widget's visible width? | |
8397 int w = clientAreaWidth - leftMargin - rightMargin; | |
8398 bool selectionFits = rightToLeft ? startBounds.x - endBounds.x <= w : endBounds.x - startBounds.x <= w; | |
8399 if (selectionFits) { | |
8400 // show as much of the selection as possible by first showing | |
8401 // the start of the selection | |
8402 if (showLocation(startBounds, false)) { | |
8403 // endX value could change if showing startX caused a scroll to occur | |
8404 endBounds = getBoundsAtOffset(endOffset); | |
8405 } | |
8406 // the character at endOffset is not part of the selection | |
8407 endBounds.width = 0; | |
8408 showLocation(endBounds, false); | |
8409 } else { | |
8410 // just show the end of the selection since the selection start | |
8411 // will not be visible | |
8412 showLocation(endBounds, true); | |
8413 } | |
8414 } | |
8415 /** | |
8416 * Updates the selection and caret position depending on the text change. | |
8417 * <p> | |
8418 * If the selection intersects with the replaced text, the selection is | |
8419 * reset and the caret moved to the end of the new text. | |
8420 * If the selection is behind the replaced text it is moved so that the | |
8421 * same text remains selected. If the selection is before the replaced text | |
8422 * it is left unchanged. | |
8423 * </p> | |
8424 * | |
8425 * @param startOffset offset of the text change | |
8426 * @param replacedLength length of text being replaced | |
8427 * @param newLength length of new text | |
8428 */ | |
8429 void updateSelection(int startOffset, int replacedLength, int newLength) { | |
8430 if (selection.y <= startOffset) { | |
8431 // selection ends before text change | |
8432 if (wordWrap) setCaretLocation(); | |
8433 return; | |
8434 } | |
8435 if (selection.x < startOffset) { | |
8436 // clear selection fragment before text change | |
8437 internalRedrawRange(selection.x, startOffset - selection.x); | |
8438 } | |
8439 if (selection.y > startOffset + replacedLength && selection.x < startOffset + replacedLength) { | |
8440 // clear selection fragment after text change. | |
8441 // do this only when the selection is actually affected by the | |
8442 // change. Selection is only affected if it intersects the change (1GDY217). | |
8443 int netNewLength = newLength - replacedLength; | |
8444 int redrawStart = startOffset + newLength; | |
8445 internalRedrawRange(redrawStart, selection.y + netNewLength - redrawStart); | |
8446 } | |
8447 if (selection.y > startOffset && selection.x < startOffset + replacedLength) { | |
8448 // selection intersects replaced text. set caret behind text change | |
8449 setSelection(startOffset + newLength, 0, true); | |
8450 } else { | |
8451 // move selection to keep same text selected | |
8452 setSelection(selection.x + newLength - replacedLength, selection.y - selection.x, true); | |
8453 } | |
8454 setCaretLocation(); | |
8455 } | |
8456 } |