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