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