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