129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.JFaceTextUtil;
|
|
14
|
131
|
15 import dwtx.jface.text.IDocumentPartitioningListener; // packageimport
|
|
16 import dwtx.jface.text.DefaultTextHover; // packageimport
|
|
17 import dwtx.jface.text.AbstractInformationControl; // packageimport
|
|
18 import dwtx.jface.text.TextUtilities; // packageimport
|
|
19 import dwtx.jface.text.IInformationControlCreatorExtension; // packageimport
|
|
20 import dwtx.jface.text.AbstractInformationControlManager; // packageimport
|
|
21 import dwtx.jface.text.ITextViewerExtension2; // packageimport
|
|
22 import dwtx.jface.text.IDocumentPartitioner; // packageimport
|
|
23 import dwtx.jface.text.DefaultIndentLineAutoEditStrategy; // packageimport
|
|
24 import dwtx.jface.text.ITextSelection; // packageimport
|
|
25 import dwtx.jface.text.Document; // packageimport
|
|
26 import dwtx.jface.text.FindReplaceDocumentAdapterContentProposalProvider; // packageimport
|
|
27 import dwtx.jface.text.ITextListener; // packageimport
|
|
28 import dwtx.jface.text.BadPartitioningException; // packageimport
|
|
29 import dwtx.jface.text.ITextViewerExtension5; // packageimport
|
|
30 import dwtx.jface.text.IDocumentPartitionerExtension3; // packageimport
|
|
31 import dwtx.jface.text.IUndoManager; // packageimport
|
|
32 import dwtx.jface.text.ITextHoverExtension2; // packageimport
|
|
33 import dwtx.jface.text.IRepairableDocument; // packageimport
|
|
34 import dwtx.jface.text.IRewriteTarget; // packageimport
|
|
35 import dwtx.jface.text.DefaultPositionUpdater; // packageimport
|
|
36 import dwtx.jface.text.RewriteSessionEditProcessor; // packageimport
|
|
37 import dwtx.jface.text.TextViewerHoverManager; // packageimport
|
|
38 import dwtx.jface.text.DocumentRewriteSession; // packageimport
|
|
39 import dwtx.jface.text.TextViewer; // packageimport
|
|
40 import dwtx.jface.text.ITextViewerExtension8; // packageimport
|
|
41 import dwtx.jface.text.RegExMessages; // packageimport
|
|
42 import dwtx.jface.text.IDelayedInputChangeProvider; // packageimport
|
|
43 import dwtx.jface.text.ITextOperationTargetExtension; // packageimport
|
|
44 import dwtx.jface.text.IWidgetTokenOwner; // packageimport
|
|
45 import dwtx.jface.text.IViewportListener; // packageimport
|
|
46 import dwtx.jface.text.GapTextStore; // packageimport
|
|
47 import dwtx.jface.text.MarkSelection; // packageimport
|
|
48 import dwtx.jface.text.IDocumentPartitioningListenerExtension; // packageimport
|
|
49 import dwtx.jface.text.IDocumentAdapterExtension; // packageimport
|
|
50 import dwtx.jface.text.IInformationControlExtension; // packageimport
|
|
51 import dwtx.jface.text.IDocumentPartitioningListenerExtension2; // packageimport
|
|
52 import dwtx.jface.text.DefaultDocumentAdapter; // packageimport
|
|
53 import dwtx.jface.text.ITextViewerExtension3; // packageimport
|
|
54 import dwtx.jface.text.IInformationControlCreator; // packageimport
|
|
55 import dwtx.jface.text.TypedRegion; // packageimport
|
|
56 import dwtx.jface.text.ISynchronizable; // packageimport
|
|
57 import dwtx.jface.text.IMarkRegionTarget; // packageimport
|
|
58 import dwtx.jface.text.TextViewerUndoManager; // packageimport
|
|
59 import dwtx.jface.text.IRegion; // packageimport
|
|
60 import dwtx.jface.text.IInformationControlExtension2; // packageimport
|
|
61 import dwtx.jface.text.IDocumentExtension4; // packageimport
|
|
62 import dwtx.jface.text.IDocumentExtension2; // packageimport
|
|
63 import dwtx.jface.text.IDocumentPartitionerExtension2; // packageimport
|
|
64 import dwtx.jface.text.Assert; // packageimport
|
|
65 import dwtx.jface.text.DefaultInformationControl; // packageimport
|
|
66 import dwtx.jface.text.IWidgetTokenOwnerExtension; // packageimport
|
|
67 import dwtx.jface.text.DocumentClone; // packageimport
|
|
68 import dwtx.jface.text.DefaultUndoManager; // packageimport
|
|
69 import dwtx.jface.text.IFindReplaceTarget; // packageimport
|
|
70 import dwtx.jface.text.IAutoEditStrategy; // packageimport
|
|
71 import dwtx.jface.text.ILineTrackerExtension; // packageimport
|
|
72 import dwtx.jface.text.IUndoManagerExtension; // packageimport
|
|
73 import dwtx.jface.text.TextSelection; // packageimport
|
|
74 import dwtx.jface.text.DefaultAutoIndentStrategy; // packageimport
|
|
75 import dwtx.jface.text.IAutoIndentStrategy; // packageimport
|
|
76 import dwtx.jface.text.IPainter; // packageimport
|
|
77 import dwtx.jface.text.IInformationControl; // packageimport
|
|
78 import dwtx.jface.text.IInformationControlExtension3; // packageimport
|
|
79 import dwtx.jface.text.ITextViewerExtension6; // packageimport
|
|
80 import dwtx.jface.text.IInformationControlExtension4; // packageimport
|
|
81 import dwtx.jface.text.DefaultLineTracker; // packageimport
|
|
82 import dwtx.jface.text.IDocumentInformationMappingExtension; // packageimport
|
|
83 import dwtx.jface.text.IRepairableDocumentExtension; // packageimport
|
|
84 import dwtx.jface.text.ITextHover; // packageimport
|
|
85 import dwtx.jface.text.FindReplaceDocumentAdapter; // packageimport
|
|
86 import dwtx.jface.text.ILineTracker; // packageimport
|
|
87 import dwtx.jface.text.Line; // packageimport
|
|
88 import dwtx.jface.text.ITextViewerExtension; // packageimport
|
|
89 import dwtx.jface.text.IDocumentAdapter; // packageimport
|
|
90 import dwtx.jface.text.TextEvent; // packageimport
|
|
91 import dwtx.jface.text.BadLocationException; // packageimport
|
|
92 import dwtx.jface.text.AbstractDocument; // packageimport
|
|
93 import dwtx.jface.text.AbstractLineTracker; // packageimport
|
|
94 import dwtx.jface.text.TreeLineTracker; // packageimport
|
|
95 import dwtx.jface.text.ITextPresentationListener; // packageimport
|
|
96 import dwtx.jface.text.Region; // packageimport
|
|
97 import dwtx.jface.text.ITextViewer; // packageimport
|
|
98 import dwtx.jface.text.IDocumentInformationMapping; // packageimport
|
|
99 import dwtx.jface.text.MarginPainter; // packageimport
|
|
100 import dwtx.jface.text.IPaintPositionManager; // packageimport
|
|
101 import dwtx.jface.text.TextPresentation; // packageimport
|
|
102 import dwtx.jface.text.IFindReplaceTargetExtension; // packageimport
|
|
103 import dwtx.jface.text.ISlaveDocumentManagerExtension; // packageimport
|
|
104 import dwtx.jface.text.ISelectionValidator; // packageimport
|
|
105 import dwtx.jface.text.IDocumentExtension; // packageimport
|
|
106 import dwtx.jface.text.PropagatingFontFieldEditor; // packageimport
|
|
107 import dwtx.jface.text.ConfigurableLineTracker; // packageimport
|
|
108 import dwtx.jface.text.SlaveDocumentEvent; // packageimport
|
|
109 import dwtx.jface.text.IDocumentListener; // packageimport
|
|
110 import dwtx.jface.text.PaintManager; // packageimport
|
|
111 import dwtx.jface.text.IFindReplaceTargetExtension3; // packageimport
|
|
112 import dwtx.jface.text.ITextDoubleClickStrategy; // packageimport
|
|
113 import dwtx.jface.text.IDocumentExtension3; // packageimport
|
|
114 import dwtx.jface.text.Position; // packageimport
|
|
115 import dwtx.jface.text.TextMessages; // packageimport
|
|
116 import dwtx.jface.text.CopyOnWriteTextStore; // packageimport
|
|
117 import dwtx.jface.text.WhitespaceCharacterPainter; // packageimport
|
|
118 import dwtx.jface.text.IPositionUpdater; // packageimport
|
|
119 import dwtx.jface.text.DefaultTextDoubleClickStrategy; // packageimport
|
|
120 import dwtx.jface.text.ListLineTracker; // packageimport
|
|
121 import dwtx.jface.text.ITextInputListener; // packageimport
|
|
122 import dwtx.jface.text.BadPositionCategoryException; // packageimport
|
|
123 import dwtx.jface.text.IWidgetTokenKeeperExtension; // packageimport
|
|
124 import dwtx.jface.text.IInputChangedListener; // packageimport
|
|
125 import dwtx.jface.text.ITextOperationTarget; // packageimport
|
|
126 import dwtx.jface.text.IDocumentInformationMappingExtension2; // packageimport
|
|
127 import dwtx.jface.text.ITextViewerExtension7; // packageimport
|
|
128 import dwtx.jface.text.IInformationControlExtension5; // packageimport
|
|
129 import dwtx.jface.text.IDocumentRewriteSessionListener; // packageimport
|
|
130 import dwtx.jface.text.AbstractReusableInformationControlCreator; // packageimport
|
|
131 import dwtx.jface.text.TabsToSpacesConverter; // packageimport
|
|
132 import dwtx.jface.text.CursorLinePainter; // packageimport
|
|
133 import dwtx.jface.text.ITextHoverExtension; // packageimport
|
|
134 import dwtx.jface.text.IEventConsumer; // packageimport
|
|
135 import dwtx.jface.text.IDocument; // packageimport
|
|
136 import dwtx.jface.text.IWidgetTokenKeeper; // packageimport
|
|
137 import dwtx.jface.text.DocumentCommand; // packageimport
|
|
138 import dwtx.jface.text.TypedPosition; // packageimport
|
|
139 import dwtx.jface.text.IEditingSupportRegistry; // packageimport
|
|
140 import dwtx.jface.text.IDocumentPartitionerExtension; // packageimport
|
|
141 import dwtx.jface.text.AbstractHoverInformationControlManager; // packageimport
|
|
142 import dwtx.jface.text.IEditingSupport; // packageimport
|
|
143 import dwtx.jface.text.IMarkSelection; // packageimport
|
|
144 import dwtx.jface.text.ISlaveDocumentManager; // packageimport
|
|
145 import dwtx.jface.text.DocumentEvent; // packageimport
|
|
146 import dwtx.jface.text.DocumentPartitioningChangedEvent; // packageimport
|
|
147 import dwtx.jface.text.ITextStore; // packageimport
|
|
148 import dwtx.jface.text.JFaceTextMessages; // packageimport
|
|
149 import dwtx.jface.text.DocumentRewriteSessionEvent; // packageimport
|
|
150 import dwtx.jface.text.SequentialRewriteTextStore; // packageimport
|
|
151 import dwtx.jface.text.DocumentRewriteSessionType; // packageimport
|
|
152 import dwtx.jface.text.TextAttribute; // packageimport
|
|
153 import dwtx.jface.text.ITextViewerExtension4; // packageimport
|
|
154 import dwtx.jface.text.ITypedRegion; // packageimport
|
|
155
|
|
156
|
129
|
157 import dwt.dwthelper.utils;
|
|
158
|
|
159
|
|
160
|
|
161 import dwt.custom.StyledText;
|
|
162 import dwt.graphics.GC;
|
|
163 import dwt.graphics.Point;
|
|
164 import dwt.graphics.Rectangle;
|
|
165 import dwt.widgets.Control;
|
|
166 import dwtx.jface.text.source.ILineRange;
|
|
167 import dwtx.jface.text.source.LineRange;
|
|
168
|
|
169 /**
|
|
170 * A collection of JFace Text functions.
|
|
171 * <p>
|
|
172 * This class is neither intended to be instantiated nor subclassed.
|
|
173 * </p>
|
|
174 *
|
|
175 * @since 3.3
|
|
176 * @noinstantiate This class is not intended to be instantiated by clients.
|
|
177 */
|
|
178 public final class JFaceTextUtil {
|
|
179
|
|
180 private JFaceTextUtil() {
|
|
181 // Do not instantiate
|
|
182 }
|
|
183
|
|
184 /**
|
|
185 * Computes the line height for the given line range.
|
|
186 *
|
|
187 * @param textWidget the <code>StyledText</code> widget
|
|
188 * @param startLine the start line
|
|
189 * @param endLine the end line (exclusive)
|
|
190 * @param lineCount the line count used by the old API
|
|
191 * @return the height of all lines starting with <code>startLine</code> and ending above <code>endLime</code>
|
|
192 */
|
|
193 public static int computeLineHeight(StyledText textWidget, int startLine, int endLine, int lineCount) {
|
|
194 return getLinePixel(textWidget, endLine) - getLinePixel(textWidget, startLine);
|
|
195 }
|
|
196
|
|
197 /**
|
|
198 * Returns the last fully visible line of the widget. The exact semantics of "last fully visible
|
|
199 * line" are:
|
|
200 * <ul>
|
|
201 * <li>the last line of which the last pixel is visible, if any
|
|
202 * <li>otherwise, the only line that is partially visible
|
|
203 * </ul>
|
|
204 *
|
|
205 * @param widget the widget
|
|
206 * @return the last fully visible line
|
|
207 */
|
|
208 public static int getBottomIndex(StyledText widget) {
|
|
209 int lastPixel= computeLastVisiblePixel(widget);
|
|
210
|
|
211 // bottom is in [0 .. lineCount - 1]
|
|
212 int bottom= widget.getLineIndex(lastPixel);
|
|
213
|
|
214 // bottom is the first line - no more checking
|
|
215 if (bottom is 0)
|
|
216 return bottom;
|
|
217
|
|
218 int pixel= widget.getLinePixel(bottom);
|
|
219 // bottom starts on or before the client area start - bottom is the only visible line
|
|
220 if (pixel <= 0)
|
|
221 return bottom;
|
|
222
|
|
223 int offset= widget.getOffsetAtLine(bottom);
|
|
224 int height= widget.getLineHeight(offset);
|
|
225
|
|
226 // bottom is not showing entirely - use the previous line
|
|
227 if (pixel + height - 1 > lastPixel)
|
|
228 return bottom - 1;
|
|
229
|
|
230 // bottom is fully visible and its last line is exactly the last pixel
|
|
231 return bottom;
|
|
232 }
|
|
233
|
|
234 /**
|
|
235 * Returns the index of the first (possibly only partially) visible line of the widget
|
|
236 *
|
|
237 * @param widget the widget
|
|
238 * @return the index of the first line of which a pixel is visible
|
|
239 */
|
|
240 public static int getPartialTopIndex(StyledText widget) {
|
|
241 // see StyledText#getPartialTopIndex()
|
|
242 int top= widget.getTopIndex();
|
|
243 int pixels= widget.getLinePixel(top);
|
|
244
|
|
245 // FIXME remove when https://bugs.eclipse.org/bugs/show_bug.cgi?id=123770 is fixed
|
|
246 if (pixels is -widget.getLineHeight(widget.getOffsetAtLine(top))) {
|
|
247 top++;
|
|
248 pixels= 0;
|
|
249 }
|
|
250
|
|
251 if (pixels > 0)
|
|
252 top--;
|
|
253
|
|
254 return top;
|
|
255 }
|
|
256
|
|
257 /**
|
|
258 * Returns the index of the last (possibly only partially) visible line of the widget
|
|
259 *
|
|
260 * @param widget the text widget
|
|
261 * @return the index of the last line of which a pixel is visible
|
|
262 */
|
|
263 public static int getPartialBottomIndex(StyledText widget) {
|
|
264 // @see StyledText#getPartialBottomIndex()
|
|
265 int lastPixel= computeLastVisiblePixel(widget);
|
|
266 int bottom= widget.getLineIndex(lastPixel);
|
|
267 return bottom;
|
|
268 }
|
|
269
|
|
270 /**
|
|
271 * Returns the last visible pixel in the widget's client area.
|
|
272 *
|
|
273 * @param widget the widget
|
|
274 * @return the last visible pixel in the widget's client area
|
|
275 */
|
|
276 private static int computeLastVisiblePixel(StyledText widget) {
|
|
277 int caHeight= widget.getClientArea().height;
|
|
278 int lastPixel= caHeight - 1;
|
|
279 // XXX what if there is a margin? can't take trim as this includes the scrollbars which are not part of the client area
|
|
280 // if ((textWidget.getStyle() & DWT.BORDER) !is 0)
|
|
281 // lastPixel -= 4;
|
|
282 return lastPixel;
|
|
283 }
|
|
284
|
|
285 /**
|
|
286 * Returns the line index of the first visible model line in the viewer. The line may be only
|
|
287 * partially visible.
|
|
288 *
|
|
289 * @param viewer the text viewer
|
|
290 * @return the first line of which a pixel is visible, or -1 for no line
|
|
291 */
|
|
292 public static int getPartialTopIndex(ITextViewer viewer) {
|
|
293 StyledText widget= viewer.getTextWidget();
|
|
294 int widgetTop= getPartialTopIndex(widget);
|
|
295 return widgetLine2ModelLine(viewer, widgetTop);
|
|
296 }
|
|
297
|
|
298 /**
|
|
299 * Returns the last, possibly partially, visible line in the view port.
|
|
300 *
|
|
301 * @param viewer the text viewer
|
|
302 * @return the last, possibly partially, visible line in the view port
|
|
303 */
|
|
304 public static int getPartialBottomIndex(ITextViewer viewer) {
|
|
305 StyledText textWidget= viewer.getTextWidget();
|
|
306 int widgetBottom= getPartialBottomIndex(textWidget);
|
|
307 return widgetLine2ModelLine(viewer, widgetBottom);
|
|
308 }
|
|
309
|
|
310 /**
|
|
311 * Returns the range of lines that is visible in the viewer, including any partially visible
|
|
312 * lines.
|
|
313 *
|
|
314 * @param viewer the viewer
|
|
315 * @return the range of lines that is visible in the viewer, <code>null</code> if no lines are
|
|
316 * visible
|
|
317 */
|
|
318 public static ILineRange getVisibleModelLines(ITextViewer viewer) {
|
|
319 int top= getPartialTopIndex(viewer);
|
|
320 int bottom= getPartialBottomIndex(viewer);
|
|
321 if (top is -1 || bottom is -1)
|
|
322 return null;
|
|
323 return new LineRange(top, bottom - top + 1);
|
|
324 }
|
|
325
|
|
326 /**
|
|
327 * Converts a widget line into a model (i.e. {@link IDocument}) line using the
|
|
328 * {@link ITextViewerExtension5} if available, otherwise by adapting the widget line to the
|
|
329 * viewer's {@link ITextViewer#getVisibleRegion() visible region}.
|
|
330 *
|
|
331 * @param viewer the viewer
|
|
332 * @param widgetLine the widget line to convert.
|
|
333 * @return the model line corresponding to <code>widgetLine</code> or -1 to signal that there
|
|
334 * is no corresponding model line
|
|
335 */
|
|
336 public static int widgetLine2ModelLine(ITextViewer viewer, int widgetLine) {
|
|
337 int modelLine;
|
|
338 if (viewer instanceof ITextViewerExtension5) {
|
|
339 ITextViewerExtension5 extension= (ITextViewerExtension5) viewer;
|
|
340 modelLine= extension.widgetLine2ModelLine(widgetLine);
|
|
341 } else {
|
|
342 try {
|
|
343 IRegion r= viewer.getVisibleRegion();
|
|
344 IDocument d= viewer.getDocument();
|
|
345 modelLine= widgetLine + d.getLineOfOffset(r.getOffset());
|
|
346 } catch (BadLocationException x) {
|
|
347 modelLine= widgetLine;
|
|
348 }
|
|
349 }
|
|
350 return modelLine;
|
|
351 }
|
|
352
|
|
353 /**
|
|
354 * Converts a model (i.e. {@link IDocument}) line into a widget line using the
|
|
355 * {@link ITextViewerExtension5} if available, otherwise by adapting the model line to the
|
|
356 * viewer's {@link ITextViewer#getVisibleRegion() visible region}.
|
|
357 *
|
|
358 * @param viewer the viewer
|
|
359 * @param modelLine the model line to convert.
|
|
360 * @return the widget line corresponding to <code>modelLine</code> or -1 to signal that there
|
|
361 * is no corresponding widget line
|
|
362 */
|
|
363 public static int modelLineToWidgetLine(ITextViewer viewer, final int modelLine) {
|
|
364 int widgetLine;
|
|
365 if (viewer instanceof ITextViewerExtension5) {
|
|
366 ITextViewerExtension5 extension= (ITextViewerExtension5) viewer;
|
|
367 widgetLine= extension.modelLine2WidgetLine(modelLine);
|
|
368 } else {
|
|
369 IRegion region= viewer.getVisibleRegion();
|
|
370 IDocument document= viewer.getDocument();
|
|
371 try {
|
|
372 int visibleStartLine= document.getLineOfOffset(region.getOffset());
|
|
373 int visibleEndLine= document.getLineOfOffset(region.getOffset() + region.getLength());
|
|
374 if (modelLine < visibleStartLine || modelLine > visibleEndLine)
|
|
375 widgetLine= -1;
|
|
376 else
|
|
377 widgetLine= modelLine - visibleStartLine;
|
|
378 } catch (BadLocationException x) {
|
|
379 // ignore and return -1
|
|
380 widgetLine= -1;
|
|
381 }
|
|
382 }
|
|
383 return widgetLine;
|
|
384 }
|
|
385
|
|
386
|
|
387 /**
|
|
388 * Returns the number of hidden pixels of the first partially visible line. If there is no
|
|
389 * partially visible line, zero is returned.
|
|
390 *
|
|
391 * @param textWidget the widget
|
|
392 * @return the number of hidden pixels of the first partial line, always >= 0
|
|
393 */
|
|
394 public static int getHiddenTopLinePixels(StyledText textWidget) {
|
|
395 int top= getPartialTopIndex(textWidget);
|
|
396 return -textWidget.getLinePixel(top);
|
|
397 }
|
|
398
|
|
399 /*
|
|
400 * @see StyledText#getLinePixel(int)
|
|
401 */
|
|
402 public static int getLinePixel(StyledText textWidget, int line) {
|
|
403 return textWidget.getLinePixel(line);
|
|
404 }
|
|
405
|
|
406 /*
|
|
407 * @see StyledText#getLineIndex(int)
|
|
408 */
|
|
409 public static int getLineIndex(StyledText textWidget, int y) {
|
|
410 int lineIndex= textWidget.getLineIndex(y);
|
|
411 return lineIndex;
|
|
412 }
|
|
413
|
|
414 /**
|
|
415 * Returns <code>true</code> if the widget displays the entire contents, i.e. it cannot
|
|
416 * be vertically scrolled.
|
|
417 *
|
|
418 * @param widget the widget
|
|
419 * @return <code>true</code> if the widget displays the entire contents, i.e. it cannot
|
|
420 * be vertically scrolled, <code>false</code> otherwise
|
|
421 */
|
|
422 public static bool isShowingEntireContents(StyledText widget) {
|
|
423 if (widget.getTopPixel() !is 0) // more efficient shortcut
|
|
424 return false;
|
|
425
|
|
426 int lastVisiblePixel= computeLastVisiblePixel(widget);
|
|
427 int lastPossiblePixel= widget.getLinePixel(widget.getLineCount());
|
|
428 return lastPossiblePixel <= lastVisiblePixel;
|
|
429 }
|
|
430
|
|
431 /**
|
|
432 * Determines the graphical area covered by the given text region in
|
|
433 * the given viewer.
|
|
434 *
|
|
435 * @param region the region whose graphical extend must be computed
|
|
436 * @param textViewer the text viewer containing the region
|
|
437 * @return the graphical extend of the given region in the given viewer
|
|
438 *
|
|
439 * @since 3.4
|
|
440 */
|
|
441 public static Rectangle computeArea(IRegion region, ITextViewer textViewer) {
|
|
442 int start= 0;
|
|
443 int end= 0;
|
|
444 IRegion widgetRegion= modelRange2WidgetRange(region, textViewer);
|
|
445 if (widgetRegion !is null) {
|
|
446 start= widgetRegion.getOffset();
|
|
447 end= start + widgetRegion.getLength();
|
|
448 }
|
|
449
|
|
450 StyledText styledText= textViewer.getTextWidget();
|
|
451 Rectangle bounds;
|
|
452 if (end > 0 && start < end)
|
|
453 bounds= styledText.getTextBounds(start, end - 1);
|
|
454 else {
|
|
455 Point loc= styledText.getLocationAtOffset(start);
|
|
456 bounds= new Rectangle(loc.x, loc.y, getAverageCharWidth(textViewer.getTextWidget()), styledText.getLineHeight(start));
|
|
457 }
|
|
458
|
|
459 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
|
|
460 }
|
|
461
|
|
462 /**
|
|
463 * Translates a given region of the text viewer's document into
|
|
464 * the corresponding region of the viewer's widget.
|
|
465 *
|
|
466 * @param region the document region
|
|
467 * @param textViewer the viewer containing the region
|
|
468 * @return the corresponding widget region
|
|
469 *
|
|
470 * @since 3.4
|
|
471 */
|
|
472 private static IRegion modelRange2WidgetRange(IRegion region, ITextViewer textViewer) {
|
|
473 if (textViewer instanceof ITextViewerExtension5) {
|
|
474 ITextViewerExtension5 extension= (ITextViewerExtension5) textViewer;
|
|
475 return extension.modelRange2WidgetRange(region);
|
|
476 }
|
|
477
|
|
478 IRegion visibleRegion= textViewer.getVisibleRegion();
|
|
479 int start= region.getOffset() - visibleRegion.getOffset();
|
|
480 int end= start + region.getLength();
|
|
481 if (end > visibleRegion.getLength())
|
|
482 end= visibleRegion.getLength();
|
|
483
|
|
484 return new Region(start, end - start);
|
|
485 }
|
|
486
|
|
487 /**
|
|
488 * Returns the average character width of the given control's font.
|
|
489 *
|
|
490 * @param control the control to calculate the average char width for
|
|
491 * @return the average character width of the controls font
|
|
492 *
|
|
493 * @since 3.4
|
|
494 */
|
|
495 public static int getAverageCharWidth(Control control) {
|
|
496 GC gc= new GC(control);
|
|
497 gc.setFont(control.getFont());
|
|
498 int increment= gc.getFontMetrics().getAverageCharWidth();
|
|
499 gc.dispose();
|
|
500 return increment;
|
|
501 }
|
|
502
|
|
503 }
|