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
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18
|
|
19 import dwt.custom.StyledText;
|
|
20 import dwt.graphics.GC;
|
|
21 import dwt.graphics.Point;
|
|
22 import dwt.graphics.Rectangle;
|
|
23 import dwt.widgets.Control;
|
|
24 import dwtx.jface.text.source.ILineRange;
|
|
25 import dwtx.jface.text.source.LineRange;
|
|
26
|
|
27 /**
|
|
28 * A collection of JFace Text functions.
|
|
29 * <p>
|
|
30 * This class is neither intended to be instantiated nor subclassed.
|
|
31 * </p>
|
|
32 *
|
|
33 * @since 3.3
|
|
34 * @noinstantiate This class is not intended to be instantiated by clients.
|
|
35 */
|
|
36 public final class JFaceTextUtil {
|
|
37
|
|
38 private JFaceTextUtil() {
|
|
39 // Do not instantiate
|
|
40 }
|
|
41
|
|
42 /**
|
|
43 * Computes the line height for the given line range.
|
|
44 *
|
|
45 * @param textWidget the <code>StyledText</code> widget
|
|
46 * @param startLine the start line
|
|
47 * @param endLine the end line (exclusive)
|
|
48 * @param lineCount the line count used by the old API
|
|
49 * @return the height of all lines starting with <code>startLine</code> and ending above <code>endLime</code>
|
|
50 */
|
|
51 public static int computeLineHeight(StyledText textWidget, int startLine, int endLine, int lineCount) {
|
|
52 return getLinePixel(textWidget, endLine) - getLinePixel(textWidget, startLine);
|
|
53 }
|
|
54
|
|
55 /**
|
|
56 * Returns the last fully visible line of the widget. The exact semantics of "last fully visible
|
|
57 * line" are:
|
|
58 * <ul>
|
|
59 * <li>the last line of which the last pixel is visible, if any
|
|
60 * <li>otherwise, the only line that is partially visible
|
|
61 * </ul>
|
|
62 *
|
|
63 * @param widget the widget
|
|
64 * @return the last fully visible line
|
|
65 */
|
|
66 public static int getBottomIndex(StyledText widget) {
|
|
67 int lastPixel= computeLastVisiblePixel(widget);
|
|
68
|
|
69 // bottom is in [0 .. lineCount - 1]
|
|
70 int bottom= widget.getLineIndex(lastPixel);
|
|
71
|
|
72 // bottom is the first line - no more checking
|
|
73 if (bottom is 0)
|
|
74 return bottom;
|
|
75
|
|
76 int pixel= widget.getLinePixel(bottom);
|
|
77 // bottom starts on or before the client area start - bottom is the only visible line
|
|
78 if (pixel <= 0)
|
|
79 return bottom;
|
|
80
|
|
81 int offset= widget.getOffsetAtLine(bottom);
|
|
82 int height= widget.getLineHeight(offset);
|
|
83
|
|
84 // bottom is not showing entirely - use the previous line
|
|
85 if (pixel + height - 1 > lastPixel)
|
|
86 return bottom - 1;
|
|
87
|
|
88 // bottom is fully visible and its last line is exactly the last pixel
|
|
89 return bottom;
|
|
90 }
|
|
91
|
|
92 /**
|
|
93 * Returns the index of the first (possibly only partially) visible line of the widget
|
|
94 *
|
|
95 * @param widget the widget
|
|
96 * @return the index of the first line of which a pixel is visible
|
|
97 */
|
|
98 public static int getPartialTopIndex(StyledText widget) {
|
|
99 // see StyledText#getPartialTopIndex()
|
|
100 int top= widget.getTopIndex();
|
|
101 int pixels= widget.getLinePixel(top);
|
|
102
|
|
103 // FIXME remove when https://bugs.eclipse.org/bugs/show_bug.cgi?id=123770 is fixed
|
|
104 if (pixels is -widget.getLineHeight(widget.getOffsetAtLine(top))) {
|
|
105 top++;
|
|
106 pixels= 0;
|
|
107 }
|
|
108
|
|
109 if (pixels > 0)
|
|
110 top--;
|
|
111
|
|
112 return top;
|
|
113 }
|
|
114
|
|
115 /**
|
|
116 * Returns the index of the last (possibly only partially) visible line of the widget
|
|
117 *
|
|
118 * @param widget the text widget
|
|
119 * @return the index of the last line of which a pixel is visible
|
|
120 */
|
|
121 public static int getPartialBottomIndex(StyledText widget) {
|
|
122 // @see StyledText#getPartialBottomIndex()
|
|
123 int lastPixel= computeLastVisiblePixel(widget);
|
|
124 int bottom= widget.getLineIndex(lastPixel);
|
|
125 return bottom;
|
|
126 }
|
|
127
|
|
128 /**
|
|
129 * Returns the last visible pixel in the widget's client area.
|
|
130 *
|
|
131 * @param widget the widget
|
|
132 * @return the last visible pixel in the widget's client area
|
|
133 */
|
|
134 private static int computeLastVisiblePixel(StyledText widget) {
|
|
135 int caHeight= widget.getClientArea().height;
|
|
136 int lastPixel= caHeight - 1;
|
|
137 // XXX what if there is a margin? can't take trim as this includes the scrollbars which are not part of the client area
|
|
138 // if ((textWidget.getStyle() & DWT.BORDER) !is 0)
|
|
139 // lastPixel -= 4;
|
|
140 return lastPixel;
|
|
141 }
|
|
142
|
|
143 /**
|
|
144 * Returns the line index of the first visible model line in the viewer. The line may be only
|
|
145 * partially visible.
|
|
146 *
|
|
147 * @param viewer the text viewer
|
|
148 * @return the first line of which a pixel is visible, or -1 for no line
|
|
149 */
|
|
150 public static int getPartialTopIndex(ITextViewer viewer) {
|
|
151 StyledText widget= viewer.getTextWidget();
|
|
152 int widgetTop= getPartialTopIndex(widget);
|
|
153 return widgetLine2ModelLine(viewer, widgetTop);
|
|
154 }
|
|
155
|
|
156 /**
|
|
157 * Returns the last, possibly partially, visible line in the view port.
|
|
158 *
|
|
159 * @param viewer the text viewer
|
|
160 * @return the last, possibly partially, visible line in the view port
|
|
161 */
|
|
162 public static int getPartialBottomIndex(ITextViewer viewer) {
|
|
163 StyledText textWidget= viewer.getTextWidget();
|
|
164 int widgetBottom= getPartialBottomIndex(textWidget);
|
|
165 return widgetLine2ModelLine(viewer, widgetBottom);
|
|
166 }
|
|
167
|
|
168 /**
|
|
169 * Returns the range of lines that is visible in the viewer, including any partially visible
|
|
170 * lines.
|
|
171 *
|
|
172 * @param viewer the viewer
|
|
173 * @return the range of lines that is visible in the viewer, <code>null</code> if no lines are
|
|
174 * visible
|
|
175 */
|
|
176 public static ILineRange getVisibleModelLines(ITextViewer viewer) {
|
|
177 int top= getPartialTopIndex(viewer);
|
|
178 int bottom= getPartialBottomIndex(viewer);
|
|
179 if (top is -1 || bottom is -1)
|
|
180 return null;
|
|
181 return new LineRange(top, bottom - top + 1);
|
|
182 }
|
|
183
|
|
184 /**
|
|
185 * Converts a widget line into a model (i.e. {@link IDocument}) line using the
|
|
186 * {@link ITextViewerExtension5} if available, otherwise by adapting the widget line to the
|
|
187 * viewer's {@link ITextViewer#getVisibleRegion() visible region}.
|
|
188 *
|
|
189 * @param viewer the viewer
|
|
190 * @param widgetLine the widget line to convert.
|
|
191 * @return the model line corresponding to <code>widgetLine</code> or -1 to signal that there
|
|
192 * is no corresponding model line
|
|
193 */
|
|
194 public static int widgetLine2ModelLine(ITextViewer viewer, int widgetLine) {
|
|
195 int modelLine;
|
|
196 if (viewer instanceof ITextViewerExtension5) {
|
|
197 ITextViewerExtension5 extension= (ITextViewerExtension5) viewer;
|
|
198 modelLine= extension.widgetLine2ModelLine(widgetLine);
|
|
199 } else {
|
|
200 try {
|
|
201 IRegion r= viewer.getVisibleRegion();
|
|
202 IDocument d= viewer.getDocument();
|
|
203 modelLine= widgetLine + d.getLineOfOffset(r.getOffset());
|
|
204 } catch (BadLocationException x) {
|
|
205 modelLine= widgetLine;
|
|
206 }
|
|
207 }
|
|
208 return modelLine;
|
|
209 }
|
|
210
|
|
211 /**
|
|
212 * Converts a model (i.e. {@link IDocument}) line into a widget line using the
|
|
213 * {@link ITextViewerExtension5} if available, otherwise by adapting the model line to the
|
|
214 * viewer's {@link ITextViewer#getVisibleRegion() visible region}.
|
|
215 *
|
|
216 * @param viewer the viewer
|
|
217 * @param modelLine the model line to convert.
|
|
218 * @return the widget line corresponding to <code>modelLine</code> or -1 to signal that there
|
|
219 * is no corresponding widget line
|
|
220 */
|
|
221 public static int modelLineToWidgetLine(ITextViewer viewer, final int modelLine) {
|
|
222 int widgetLine;
|
|
223 if (viewer instanceof ITextViewerExtension5) {
|
|
224 ITextViewerExtension5 extension= (ITextViewerExtension5) viewer;
|
|
225 widgetLine= extension.modelLine2WidgetLine(modelLine);
|
|
226 } else {
|
|
227 IRegion region= viewer.getVisibleRegion();
|
|
228 IDocument document= viewer.getDocument();
|
|
229 try {
|
|
230 int visibleStartLine= document.getLineOfOffset(region.getOffset());
|
|
231 int visibleEndLine= document.getLineOfOffset(region.getOffset() + region.getLength());
|
|
232 if (modelLine < visibleStartLine || modelLine > visibleEndLine)
|
|
233 widgetLine= -1;
|
|
234 else
|
|
235 widgetLine= modelLine - visibleStartLine;
|
|
236 } catch (BadLocationException x) {
|
|
237 // ignore and return -1
|
|
238 widgetLine= -1;
|
|
239 }
|
|
240 }
|
|
241 return widgetLine;
|
|
242 }
|
|
243
|
|
244
|
|
245 /**
|
|
246 * Returns the number of hidden pixels of the first partially visible line. If there is no
|
|
247 * partially visible line, zero is returned.
|
|
248 *
|
|
249 * @param textWidget the widget
|
|
250 * @return the number of hidden pixels of the first partial line, always >= 0
|
|
251 */
|
|
252 public static int getHiddenTopLinePixels(StyledText textWidget) {
|
|
253 int top= getPartialTopIndex(textWidget);
|
|
254 return -textWidget.getLinePixel(top);
|
|
255 }
|
|
256
|
|
257 /*
|
|
258 * @see StyledText#getLinePixel(int)
|
|
259 */
|
|
260 public static int getLinePixel(StyledText textWidget, int line) {
|
|
261 return textWidget.getLinePixel(line);
|
|
262 }
|
|
263
|
|
264 /*
|
|
265 * @see StyledText#getLineIndex(int)
|
|
266 */
|
|
267 public static int getLineIndex(StyledText textWidget, int y) {
|
|
268 int lineIndex= textWidget.getLineIndex(y);
|
|
269 return lineIndex;
|
|
270 }
|
|
271
|
|
272 /**
|
|
273 * Returns <code>true</code> if the widget displays the entire contents, i.e. it cannot
|
|
274 * be vertically scrolled.
|
|
275 *
|
|
276 * @param widget the widget
|
|
277 * @return <code>true</code> if the widget displays the entire contents, i.e. it cannot
|
|
278 * be vertically scrolled, <code>false</code> otherwise
|
|
279 */
|
|
280 public static bool isShowingEntireContents(StyledText widget) {
|
|
281 if (widget.getTopPixel() !is 0) // more efficient shortcut
|
|
282 return false;
|
|
283
|
|
284 int lastVisiblePixel= computeLastVisiblePixel(widget);
|
|
285 int lastPossiblePixel= widget.getLinePixel(widget.getLineCount());
|
|
286 return lastPossiblePixel <= lastVisiblePixel;
|
|
287 }
|
|
288
|
|
289 /**
|
|
290 * Determines the graphical area covered by the given text region in
|
|
291 * the given viewer.
|
|
292 *
|
|
293 * @param region the region whose graphical extend must be computed
|
|
294 * @param textViewer the text viewer containing the region
|
|
295 * @return the graphical extend of the given region in the given viewer
|
|
296 *
|
|
297 * @since 3.4
|
|
298 */
|
|
299 public static Rectangle computeArea(IRegion region, ITextViewer textViewer) {
|
|
300 int start= 0;
|
|
301 int end= 0;
|
|
302 IRegion widgetRegion= modelRange2WidgetRange(region, textViewer);
|
|
303 if (widgetRegion !is null) {
|
|
304 start= widgetRegion.getOffset();
|
|
305 end= start + widgetRegion.getLength();
|
|
306 }
|
|
307
|
|
308 StyledText styledText= textViewer.getTextWidget();
|
|
309 Rectangle bounds;
|
|
310 if (end > 0 && start < end)
|
|
311 bounds= styledText.getTextBounds(start, end - 1);
|
|
312 else {
|
|
313 Point loc= styledText.getLocationAtOffset(start);
|
|
314 bounds= new Rectangle(loc.x, loc.y, getAverageCharWidth(textViewer.getTextWidget()), styledText.getLineHeight(start));
|
|
315 }
|
|
316
|
|
317 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
|
|
318 }
|
|
319
|
|
320 /**
|
|
321 * Translates a given region of the text viewer's document into
|
|
322 * the corresponding region of the viewer's widget.
|
|
323 *
|
|
324 * @param region the document region
|
|
325 * @param textViewer the viewer containing the region
|
|
326 * @return the corresponding widget region
|
|
327 *
|
|
328 * @since 3.4
|
|
329 */
|
|
330 private static IRegion modelRange2WidgetRange(IRegion region, ITextViewer textViewer) {
|
|
331 if (textViewer instanceof ITextViewerExtension5) {
|
|
332 ITextViewerExtension5 extension= (ITextViewerExtension5) textViewer;
|
|
333 return extension.modelRange2WidgetRange(region);
|
|
334 }
|
|
335
|
|
336 IRegion visibleRegion= textViewer.getVisibleRegion();
|
|
337 int start= region.getOffset() - visibleRegion.getOffset();
|
|
338 int end= start + region.getLength();
|
|
339 if (end > visibleRegion.getLength())
|
|
340 end= visibleRegion.getLength();
|
|
341
|
|
342 return new Region(start, end - start);
|
|
343 }
|
|
344
|
|
345 /**
|
|
346 * Returns the average character width of the given control's font.
|
|
347 *
|
|
348 * @param control the control to calculate the average char width for
|
|
349 * @return the average character width of the controls font
|
|
350 *
|
|
351 * @since 3.4
|
|
352 */
|
|
353 public static int getAverageCharWidth(Control control) {
|
|
354 GC gc= new GC(control);
|
|
355 gc.setFont(control.getFont());
|
|
356 int increment= gc.getFontMetrics().getAverageCharWidth();
|
|
357 gc.dispose();
|
|
358 return increment;
|
|
359 }
|
|
360
|
|
361 }
|