129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Nikolay Botev <bono8106@hotmail.com> - [rulers] Shift clicking in line number column doesn't select range - https://bugs.eclipse.org/bugs/show_bug.cgi?id=32166
|
|
11 * Nikolay Botev <bono8106@hotmail.com> - [rulers] Clicking in line number ruler should not trigger annotation ruler - https://bugs.eclipse.org/bugs/show_bug.cgi?id=40889
|
|
12 * Port to the D programming language:
|
|
13 * Frank Benoit <benoit@tionex.de>
|
|
14 *******************************************************************************/
|
|
15 module dwtx.jface.text.source.LineNumberRulerColumn;
|
|
16
|
131
|
17 import dwtx.jface.text.source.ISharedTextColors; // packageimport
|
|
18 import dwtx.jface.text.source.ILineRange; // packageimport
|
|
19 import dwtx.jface.text.source.IAnnotationPresentation; // packageimport
|
|
20 import dwtx.jface.text.source.IVerticalRulerInfoExtension; // packageimport
|
|
21 import dwtx.jface.text.source.ICharacterPairMatcher; // packageimport
|
|
22 import dwtx.jface.text.source.TextInvocationContext; // packageimport
|
|
23 import dwtx.jface.text.source.LineChangeHover; // packageimport
|
|
24 import dwtx.jface.text.source.IChangeRulerColumn; // packageimport
|
|
25 import dwtx.jface.text.source.IAnnotationMap; // packageimport
|
|
26 import dwtx.jface.text.source.IAnnotationModelListenerExtension; // packageimport
|
|
27 import dwtx.jface.text.source.ISourceViewerExtension2; // packageimport
|
|
28 import dwtx.jface.text.source.IAnnotationHover; // packageimport
|
|
29 import dwtx.jface.text.source.ContentAssistantFacade; // packageimport
|
|
30 import dwtx.jface.text.source.IAnnotationAccess; // packageimport
|
|
31 import dwtx.jface.text.source.IVerticalRulerExtension; // packageimport
|
|
32 import dwtx.jface.text.source.IVerticalRulerColumn; // packageimport
|
|
33 import dwtx.jface.text.source.MatchingCharacterPainter; // packageimport
|
|
34 import dwtx.jface.text.source.IAnnotationModelExtension; // packageimport
|
|
35 import dwtx.jface.text.source.ILineDifferExtension; // packageimport
|
|
36 import dwtx.jface.text.source.DefaultCharacterPairMatcher; // packageimport
|
|
37 import dwtx.jface.text.source.LineNumberChangeRulerColumn; // packageimport
|
|
38 import dwtx.jface.text.source.IAnnotationAccessExtension; // packageimport
|
|
39 import dwtx.jface.text.source.ISourceViewer; // packageimport
|
|
40 import dwtx.jface.text.source.AnnotationModel; // packageimport
|
|
41 import dwtx.jface.text.source.ILineDifferExtension2; // packageimport
|
|
42 import dwtx.jface.text.source.IAnnotationModelListener; // packageimport
|
|
43 import dwtx.jface.text.source.IVerticalRuler; // packageimport
|
|
44 import dwtx.jface.text.source.DefaultAnnotationHover; // packageimport
|
|
45 import dwtx.jface.text.source.SourceViewer; // packageimport
|
|
46 import dwtx.jface.text.source.SourceViewerConfiguration; // packageimport
|
|
47 import dwtx.jface.text.source.AnnotationBarHoverManager; // packageimport
|
|
48 import dwtx.jface.text.source.CompositeRuler; // packageimport
|
|
49 import dwtx.jface.text.source.ImageUtilities; // packageimport
|
|
50 import dwtx.jface.text.source.VisualAnnotationModel; // packageimport
|
|
51 import dwtx.jface.text.source.IAnnotationModel; // packageimport
|
|
52 import dwtx.jface.text.source.ISourceViewerExtension3; // packageimport
|
|
53 import dwtx.jface.text.source.ILineDiffInfo; // packageimport
|
|
54 import dwtx.jface.text.source.VerticalRulerEvent; // packageimport
|
|
55 import dwtx.jface.text.source.ChangeRulerColumn; // packageimport
|
|
56 import dwtx.jface.text.source.ILineDiffer; // packageimport
|
|
57 import dwtx.jface.text.source.AnnotationModelEvent; // packageimport
|
|
58 import dwtx.jface.text.source.AnnotationColumn; // packageimport
|
|
59 import dwtx.jface.text.source.AnnotationRulerColumn; // packageimport
|
|
60 import dwtx.jface.text.source.IAnnotationHoverExtension; // packageimport
|
|
61 import dwtx.jface.text.source.AbstractRulerColumn; // packageimport
|
|
62 import dwtx.jface.text.source.ISourceViewerExtension; // packageimport
|
|
63 import dwtx.jface.text.source.AnnotationMap; // packageimport
|
|
64 import dwtx.jface.text.source.IVerticalRulerInfo; // packageimport
|
|
65 import dwtx.jface.text.source.IAnnotationModelExtension2; // packageimport
|
|
66 import dwtx.jface.text.source.LineRange; // packageimport
|
|
67 import dwtx.jface.text.source.IAnnotationAccessExtension2; // packageimport
|
|
68 import dwtx.jface.text.source.VerticalRuler; // packageimport
|
|
69 import dwtx.jface.text.source.JFaceTextMessages; // packageimport
|
|
70 import dwtx.jface.text.source.IOverviewRuler; // packageimport
|
|
71 import dwtx.jface.text.source.Annotation; // packageimport
|
|
72 import dwtx.jface.text.source.IVerticalRulerListener; // packageimport
|
|
73 import dwtx.jface.text.source.ISourceViewerExtension4; // packageimport
|
|
74 import dwtx.jface.text.source.AnnotationPainter; // packageimport
|
|
75 import dwtx.jface.text.source.IAnnotationHoverExtension2; // packageimport
|
|
76 import dwtx.jface.text.source.OverviewRuler; // packageimport
|
|
77 import dwtx.jface.text.source.OverviewRulerHoverManager; // packageimport
|
|
78
|
|
79
|
129
|
80 import dwt.dwthelper.utils;
|
|
81
|
|
82 import java.util.Arrays;
|
|
83
|
|
84 import dwt.DWT;
|
|
85 import dwt.custom.StyledText;
|
|
86 import dwt.events.DisposeEvent;
|
|
87 import dwt.events.DisposeListener;
|
|
88 import dwt.events.MouseEvent;
|
|
89 import dwt.events.MouseListener;
|
|
90 import dwt.events.MouseMoveListener;
|
|
91 import dwt.events.PaintEvent;
|
|
92 import dwt.events.PaintListener;
|
|
93 import dwt.graphics.Color;
|
|
94 import dwt.graphics.Font;
|
|
95 import dwt.graphics.FontMetrics;
|
|
96 import dwt.graphics.GC;
|
|
97 import dwt.graphics.Image;
|
|
98 import dwt.graphics.Point;
|
|
99 import dwt.graphics.Rectangle;
|
|
100 import dwt.widgets.Canvas;
|
|
101 import dwt.widgets.Composite;
|
|
102 import dwt.widgets.Control;
|
|
103 import dwt.widgets.Display;
|
|
104 import dwt.widgets.TypedListener;
|
|
105 import dwtx.jface.text.BadLocationException;
|
|
106 import dwtx.jface.text.IDocument;
|
|
107 import dwtx.jface.text.IRegion;
|
|
108 import dwtx.jface.text.ITextListener;
|
|
109 import dwtx.jface.text.ITextViewer;
|
|
110 import dwtx.jface.text.ITextViewerExtension;
|
|
111 import dwtx.jface.text.ITextViewerExtension5;
|
|
112 import dwtx.jface.text.IViewportListener;
|
|
113 import dwtx.jface.text.JFaceTextUtil;
|
|
114 import dwtx.jface.text.TextEvent;
|
|
115
|
|
116
|
|
117 /**
|
|
118 * A vertical ruler column displaying line numbers.
|
|
119 * Clients usually instantiate and configure object of this class.
|
|
120 *
|
|
121 * @since 2.0
|
|
122 */
|
|
123 public class LineNumberRulerColumn : IVerticalRulerColumn {
|
|
124
|
|
125 /**
|
|
126 * Internal listener class.
|
|
127 */
|
|
128 class InternalListener : IViewportListener, ITextListener {
|
|
129
|
|
130 /**
|
|
131 * @since 3.1
|
|
132 */
|
|
133 private bool fCachedRedrawState= true;
|
|
134
|
|
135 /*
|
|
136 * @see IViewportListener#viewportChanged(int)
|
|
137 */
|
|
138 public void viewportChanged(int verticalPosition) {
|
|
139 if (fCachedRedrawState && verticalPosition !is fScrollPos)
|
|
140 redraw();
|
|
141 }
|
|
142
|
|
143 /*
|
|
144 * @see ITextListener#textChanged(TextEvent)
|
|
145 */
|
|
146 public void textChanged(TextEvent event) {
|
|
147
|
|
148 fCachedRedrawState= event.getViewerRedrawState();
|
|
149 if (!fCachedRedrawState)
|
|
150 return;
|
|
151
|
|
152 if (updateNumberOfDigits()) {
|
|
153 computeIndentations();
|
|
154 layout(event.getViewerRedrawState());
|
|
155 return;
|
|
156 }
|
|
157
|
|
158 bool viewerCompletelyShown= isViewerCompletelyShown();
|
|
159 if (viewerCompletelyShown || fSensitiveToTextChanges || event.getDocumentEvent() is null)
|
|
160 postRedraw();
|
|
161 fSensitiveToTextChanges= viewerCompletelyShown;
|
|
162 }
|
|
163 }
|
|
164
|
|
165 /**
|
|
166 * Handles all the mouse interaction in this line number ruler column.
|
|
167 */
|
|
168 class MouseHandler : MouseListener, MouseMoveListener {
|
|
169
|
|
170 /** The cached view port size. */
|
|
171 private int fCachedViewportSize;
|
|
172 /** The area of the line at which line selection started. */
|
|
173 private int fStartLineOffset;
|
|
174 /** The number of the line at which line selection started. */
|
|
175 private int fStartLineNumber;
|
|
176 /** The auto scroll direction. */
|
|
177 private int fAutoScrollDirection;
|
|
178 /* @since 3.2 */
|
|
179 private bool fIsListeningForMove= false;
|
|
180
|
|
181 /*
|
|
182 * @see dwt.events.MouseListener#mouseUp(dwt.events.MouseEvent)
|
|
183 */
|
|
184 public void mouseUp(MouseEvent event) {
|
|
185 // see bug 45700
|
|
186 if (event.button is 1) {
|
|
187 stopSelecting();
|
|
188 stopAutoScroll();
|
|
189 }
|
|
190 }
|
|
191
|
|
192 /*
|
|
193 * @see dwt.events.MouseListener#mouseDown(dwt.events.MouseEvent)
|
|
194 */
|
|
195 public void mouseDown(MouseEvent event) {
|
|
196 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y);
|
|
197 // see bug 45700
|
|
198 if (event.button is 1) {
|
|
199 startSelecting((event.stateMask & DWT.SHIFT) !is 0);
|
|
200 }
|
|
201 }
|
|
202
|
|
203 /*
|
|
204 * @see dwt.events.MouseListener#mouseDoubleClick(dwt.events.MouseEvent)
|
|
205 */
|
|
206 public void mouseDoubleClick(MouseEvent event) {
|
|
207 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y);
|
|
208 stopSelecting();
|
|
209 stopAutoScroll();
|
|
210 }
|
|
211
|
|
212 /*
|
|
213 * @see dwt.events.MouseMoveListener#mouseMove(dwt.events.MouseEvent)
|
|
214 */
|
|
215 public void mouseMove(MouseEvent event) {
|
|
216 if (fIsListeningForMove && !autoScroll(event)) {
|
|
217 int newLine= fParentRuler.toDocumentLineNumber(event.y);
|
|
218 expandSelection(newLine);
|
|
219 }
|
|
220 fParentRuler.setLocationOfLastMouseButtonActivity(event.x, event.y);
|
|
221 }
|
|
222
|
|
223 /**
|
|
224 * Called when line drag selection started. Adds mouse move and track
|
|
225 * listeners to this column's control.
|
|
226 *
|
|
227 * @param expandExistingSelection if <code>true</code> the existing selection will be expanded,
|
|
228 * otherwise a new selection is started
|
|
229 */
|
|
230 private void startSelecting(bool expandExistingSelection) {
|
|
231 try {
|
|
232
|
|
233 // select line
|
|
234 IDocument document= fCachedTextViewer.getDocument();
|
|
235 int lineNumber= fParentRuler.getLineOfLastMouseButtonActivity();
|
|
236 if (expandExistingSelection && fCachedTextViewer instanceof ITextViewerExtension5
|
|
237 && fCachedTextViewer.getTextWidget() !is null) {
|
|
238 ITextViewerExtension5 extension5= ((ITextViewerExtension5)fCachedTextViewer);
|
|
239 // Find model curosr position
|
|
240 int widgetCaret= fCachedTextViewer.getTextWidget().getCaretOffset();
|
|
241 int modelCaret= extension5.widgetOffset2ModelOffset(widgetCaret);
|
|
242 // Find model selection range
|
|
243 Point selection= fCachedTextViewer.getSelectedRange();
|
|
244 // Start from tail of selection range (opposite of cursor position)
|
|
245 int startOffset= modelCaret is selection.x ? selection.x + selection.y : selection.x;
|
|
246
|
|
247 fStartLineNumber= document.getLineOfOffset(startOffset);
|
|
248 fStartLineOffset= startOffset;
|
|
249
|
|
250 expandSelection(lineNumber);
|
|
251 } else {
|
|
252 fStartLineNumber= lineNumber;
|
|
253 fStartLineOffset= document.getLineInformation(fStartLineNumber).getOffset();
|
|
254 fCachedTextViewer.setSelectedRange(fStartLineOffset, 0);
|
|
255 }
|
|
256 fCachedViewportSize= getVisibleLinesInViewport();
|
|
257
|
|
258 // prepare for drag selection
|
|
259 fIsListeningForMove= true;
|
|
260
|
|
261 } catch (BadLocationException x) {
|
|
262 }
|
|
263 }
|
|
264
|
|
265 /**
|
|
266 * Called when line drag selection stopped. Removes all previously
|
|
267 * installed listeners from this column's control.
|
|
268 */
|
|
269 private void stopSelecting() {
|
|
270 // drag selection stopped
|
|
271 fIsListeningForMove= false;
|
|
272 }
|
|
273
|
|
274 /**
|
|
275 * Expands the line selection from the remembered start line to the
|
|
276 * given line.
|
|
277 *
|
|
278 * @param lineNumber the line to which to expand the selection
|
|
279 */
|
|
280 private void expandSelection(int lineNumber) {
|
|
281 try {
|
|
282
|
|
283 IDocument document= fCachedTextViewer.getDocument();
|
|
284 IRegion lineInfo= document.getLineInformation(lineNumber);
|
|
285
|
|
286 Display display= fCachedTextWidget.getDisplay();
|
|
287 Point absolutePosition= display.getCursorLocation();
|
|
288 Point relativePosition= fCachedTextWidget.toControl(absolutePosition);
|
|
289
|
|
290 int offset;
|
|
291
|
|
292 if (relativePosition.x < 0)
|
|
293 offset= lineInfo.getOffset();
|
|
294 else {
|
|
295 try {
|
|
296 int widgetOffset= fCachedTextWidget.getOffsetAtLocation(relativePosition);
|
|
297 Point p= fCachedTextWidget.getLocationAtOffset(widgetOffset);
|
|
298 if (p.x > relativePosition.x)
|
|
299 widgetOffset--;
|
|
300
|
|
301 // Convert to model offset
|
|
302 if (fCachedTextViewer instanceof ITextViewerExtension5) {
|
|
303 ITextViewerExtension5 extension= (ITextViewerExtension5)fCachedTextViewer;
|
|
304 offset= extension.widgetOffset2ModelOffset(widgetOffset);
|
|
305 } else
|
|
306 offset= widgetOffset + fCachedTextViewer.getVisibleRegion().getOffset();
|
|
307
|
|
308 } catch (IllegalArgumentException ex) {
|
|
309 int lineEndOffset= lineInfo.getOffset() + lineInfo.getLength();
|
|
310
|
|
311 // Convert to widget offset
|
|
312 int lineEndWidgetOffset;
|
|
313 if (fCachedTextViewer instanceof ITextViewerExtension5) {
|
|
314 ITextViewerExtension5 extension= (ITextViewerExtension5)fCachedTextViewer;
|
|
315 lineEndWidgetOffset= extension.modelOffset2WidgetOffset(lineEndOffset);
|
|
316 } else
|
|
317 lineEndWidgetOffset= lineEndOffset - fCachedTextViewer.getVisibleRegion().getOffset();
|
|
318
|
|
319 Point p= fCachedTextWidget.getLocationAtOffset(lineEndWidgetOffset);
|
|
320 if (p.x < relativePosition.x)
|
|
321 offset= lineEndOffset;
|
|
322 else
|
|
323 offset= lineInfo.getOffset();
|
|
324 }
|
|
325 }
|
|
326
|
|
327 int start= Math.min(fStartLineOffset, offset);
|
|
328 int end= Math.max(fStartLineOffset, offset);
|
|
329
|
|
330 if (lineNumber < fStartLineNumber)
|
|
331 fCachedTextViewer.setSelectedRange(end, start - end);
|
|
332 else
|
|
333 fCachedTextViewer.setSelectedRange(start, end - start);
|
|
334
|
|
335 } catch (BadLocationException x) {
|
|
336 }
|
|
337 }
|
|
338
|
|
339 /**
|
|
340 * Called when auto scrolling stopped. Clears the auto scroll direction.
|
|
341 */
|
|
342 private void stopAutoScroll() {
|
|
343 fAutoScrollDirection= DWT.NULL;
|
|
344 }
|
|
345
|
|
346 /**
|
|
347 * Called on drag selection.
|
|
348 *
|
|
349 * @param event the mouse event caught by the mouse move listener
|
|
350 * @return <code>true</code> if scrolling happened, <code>false</code> otherwise
|
|
351 */
|
|
352 private bool autoScroll(MouseEvent event) {
|
|
353 Rectangle area= fCanvas.getClientArea();
|
|
354
|
|
355 if (event.y > area.height) {
|
|
356 autoScroll(DWT.DOWN);
|
|
357 return true;
|
|
358 }
|
|
359
|
|
360 if (event.y < 0) {
|
|
361 autoScroll(DWT.UP);
|
|
362 return true;
|
|
363 }
|
|
364
|
|
365 stopAutoScroll();
|
|
366 return false;
|
|
367 }
|
|
368
|
|
369 /**
|
|
370 * Scrolls the viewer into the given direction.
|
|
371 *
|
|
372 * @param direction the scroll direction
|
|
373 */
|
|
374 private void autoScroll(int direction) {
|
|
375
|
|
376 if (fAutoScrollDirection is direction)
|
|
377 return;
|
|
378
|
|
379 final int TIMER_INTERVAL= 5;
|
|
380 final Display display= fCanvas.getDisplay();
|
|
381 Runnable timer= null;
|
|
382 switch (direction) {
|
|
383 case DWT.UP:
|
|
384 timer= new Runnable() {
|
|
385 public void run() {
|
|
386 if (fAutoScrollDirection is DWT.UP) {
|
|
387 int top= getInclusiveTopIndex();
|
|
388 if (top > 0) {
|
|
389 fCachedTextViewer.setTopIndex(top -1);
|
|
390 expandSelection(top -1);
|
|
391 display.timerExec(TIMER_INTERVAL, this);
|
|
392 }
|
|
393 }
|
|
394 }
|
|
395 };
|
|
396 break;
|
|
397 case DWT.DOWN:
|
|
398 timer= new Runnable() {
|
|
399 public void run() {
|
|
400 if (fAutoScrollDirection is DWT.DOWN) {
|
|
401 int top= getInclusiveTopIndex();
|
|
402 fCachedTextViewer.setTopIndex(top +1);
|
|
403 expandSelection(top +1 + fCachedViewportSize);
|
|
404 display.timerExec(TIMER_INTERVAL, this);
|
|
405 }
|
|
406 }
|
|
407 };
|
|
408 break;
|
|
409 }
|
|
410
|
|
411 if (timer !is null) {
|
|
412 fAutoScrollDirection= direction;
|
|
413 display.timerExec(TIMER_INTERVAL, timer);
|
|
414 }
|
|
415 }
|
|
416
|
|
417 /**
|
|
418 * Returns the viewer's first visible line, even if only partially visible.
|
|
419 *
|
|
420 * @return the viewer's first visible line
|
|
421 */
|
|
422 private int getInclusiveTopIndex() {
|
|
423 if (fCachedTextWidget !is null && !fCachedTextWidget.isDisposed()) {
|
|
424 return JFaceTextUtil.getPartialTopIndex(fCachedTextViewer);
|
|
425 }
|
|
426 return -1;
|
|
427 }
|
|
428 }
|
|
429
|
|
430 /** This column's parent ruler */
|
|
431 private CompositeRuler fParentRuler;
|
|
432 /** Cached text viewer */
|
|
433 private ITextViewer fCachedTextViewer;
|
|
434 /** Cached text widget */
|
|
435 private StyledText fCachedTextWidget;
|
|
436 /** The columns canvas */
|
|
437 private Canvas fCanvas;
|
|
438 /** Cache for the actual scroll position in pixels */
|
|
439 private int fScrollPos;
|
|
440 /** The drawable for double buffering */
|
|
441 private Image fBuffer;
|
|
442 /** The internal listener */
|
|
443 private InternalListener fInternalListener= new InternalListener();
|
|
444 /** The font of this column */
|
|
445 private Font fFont;
|
|
446 /** The indentation cache */
|
|
447 private int[] fIndentation;
|
|
448 /** Indicates whether this column reacts on text change events */
|
|
449 private bool fSensitiveToTextChanges= false;
|
|
450 /** The foreground color */
|
|
451 private Color fForeground;
|
|
452 /** The background color */
|
|
453 private Color fBackground;
|
|
454 /** Cached number of displayed digits */
|
|
455 private int fCachedNumberOfDigits= -1;
|
|
456 /** Flag indicating whether a relayout is required */
|
|
457 private bool fRelayoutRequired= false;
|
|
458 /**
|
|
459 * Redraw runnable lock
|
|
460 * @since 3.0
|
|
461 */
|
|
462 private Object fRunnableLock= new Object();
|
|
463 /**
|
|
464 * Redraw runnable state
|
|
465 * @since 3.0
|
|
466 */
|
|
467 private bool fIsRunnablePosted= false;
|
|
468 /**
|
|
469 * Redraw runnable
|
|
470 * @since 3.0
|
|
471 */
|
|
472 private Runnable fRunnable= new Runnable() {
|
|
473 public void run() {
|
|
474 synchronized (fRunnableLock) {
|
|
475 fIsRunnablePosted= false;
|
|
476 }
|
|
477 redraw();
|
|
478 }
|
|
479 };
|
|
480 /* @since 3.2 */
|
|
481 private MouseHandler fMouseHandler;
|
|
482
|
|
483
|
|
484 /**
|
|
485 * Constructs a new vertical ruler column.
|
|
486 */
|
|
487 public LineNumberRulerColumn() {
|
|
488 }
|
|
489
|
|
490 /**
|
|
491 * Sets the foreground color of this column.
|
|
492 *
|
|
493 * @param foreground the foreground color
|
|
494 */
|
|
495 public void setForeground(Color foreground) {
|
|
496 fForeground= foreground;
|
|
497 }
|
|
498
|
|
499 /**
|
|
500 * Returns the foreground color being used to print the line numbers.
|
|
501 *
|
|
502 * @return the configured foreground color
|
|
503 * @since 3.0
|
|
504 */
|
|
505 protected Color getForeground() {
|
|
506 return fForeground;
|
|
507 }
|
|
508
|
|
509 /**
|
|
510 * Sets the background color of this column.
|
|
511 *
|
|
512 * @param background the background color
|
|
513 */
|
|
514 public void setBackground(Color background) {
|
|
515 fBackground= background;
|
|
516 if (fCanvas !is null && !fCanvas.isDisposed())
|
|
517 fCanvas.setBackground(getBackground(fCanvas.getDisplay()));
|
|
518 }
|
|
519
|
|
520 /**
|
|
521 * Returns the System background color for list widgets.
|
|
522 *
|
|
523 * @param display the display
|
|
524 * @return the System background color for list widgets
|
|
525 */
|
|
526 protected Color getBackground(Display display) {
|
|
527 if (fBackground is null)
|
|
528 return display.getSystemColor(DWT.COLOR_LIST_BACKGROUND);
|
|
529 return fBackground;
|
|
530 }
|
|
531
|
|
532 /*
|
|
533 * @see IVerticalRulerColumn#getControl()
|
|
534 */
|
|
535 public Control getControl() {
|
|
536 return fCanvas;
|
|
537 }
|
|
538
|
|
539 /*
|
|
540 * @see IVerticalRuleColumnr#getWidth
|
|
541 */
|
|
542 public int getWidth() {
|
|
543 return fIndentation[0];
|
|
544 }
|
|
545
|
|
546 /**
|
|
547 * Computes the number of digits to be displayed. Returns
|
|
548 * <code>true</code> if the number of digits changed compared
|
|
549 * to the previous call of this method. If the method is called
|
|
550 * for the first time, the return value is also <code>true</code>.
|
|
551 *
|
|
552 * @return whether the number of digits has been changed
|
|
553 * @since 3.0
|
|
554 */
|
|
555 protected bool updateNumberOfDigits() {
|
|
556 if (fCachedTextViewer is null)
|
|
557 return false;
|
|
558
|
|
559 int digits= computeNumberOfDigits();
|
|
560
|
|
561 if (fCachedNumberOfDigits !is digits) {
|
|
562 fCachedNumberOfDigits= digits;
|
|
563 return true;
|
|
564 }
|
|
565
|
|
566 return false;
|
|
567 }
|
|
568
|
|
569 /**
|
|
570 * Does the real computation of the number of digits. Subclasses may override this method if
|
|
571 * they need extra space on the line number ruler.
|
|
572 *
|
|
573 * @return the number of digits to be displayed on the line number ruler.
|
|
574 */
|
|
575 protected int computeNumberOfDigits() {
|
|
576 IDocument document= fCachedTextViewer.getDocument();
|
|
577 int lines= document is null ? 0 : document.getNumberOfLines();
|
|
578
|
|
579 int digits= 2;
|
|
580 while (lines > Math.pow(10, digits) -1) {
|
|
581 ++digits;
|
|
582 }
|
|
583 return digits;
|
|
584 }
|
|
585
|
|
586 /**
|
|
587 * Layouts the enclosing viewer to adapt the layout to changes of the
|
|
588 * size of the individual components.
|
|
589 *
|
|
590 * @param redraw <code>true</code> if this column can be redrawn
|
|
591 */
|
|
592 protected void layout(bool redraw) {
|
|
593 if (!redraw) {
|
|
594 fRelayoutRequired= true;
|
|
595 return;
|
|
596 }
|
|
597
|
|
598 fRelayoutRequired= false;
|
|
599 if (fCachedTextViewer instanceof ITextViewerExtension) {
|
|
600 ITextViewerExtension extension= (ITextViewerExtension) fCachedTextViewer;
|
|
601 Control control= extension.getControl();
|
|
602 if (control instanceof Composite && !control.isDisposed()) {
|
|
603 Composite composite= (Composite) control;
|
|
604 composite.layout(true);
|
|
605 }
|
|
606 }
|
|
607 }
|
|
608
|
|
609 /**
|
|
610 * Computes the indentations for the given font and stores them in
|
|
611 * <code>fIndentation</code>.
|
|
612 */
|
|
613 protected void computeIndentations() {
|
|
614 if (fCanvas is null || fCanvas.isDisposed())
|
|
615 return;
|
|
616
|
|
617 GC gc= new GC(fCanvas);
|
|
618 try {
|
|
619
|
|
620 gc.setFont(fCanvas.getFont());
|
|
621
|
|
622 fIndentation= new int[fCachedNumberOfDigits + 1];
|
|
623
|
|
624 char[] nines= new char[fCachedNumberOfDigits];
|
|
625 Arrays.fill(nines, '9');
|
|
626 String nineString= new String(nines);
|
|
627 Point p= gc.stringExtent(nineString);
|
|
628 fIndentation[0]= p.x;
|
|
629
|
|
630 for (int i= 1; i <= fCachedNumberOfDigits; i++) {
|
|
631 p= gc.stringExtent(nineString.substring(0, i));
|
|
632 fIndentation[i]= fIndentation[0] - p.x;
|
|
633 }
|
|
634
|
|
635 } finally {
|
|
636 gc.dispose();
|
|
637 }
|
|
638 }
|
|
639
|
|
640 /*
|
|
641 * @see IVerticalRulerColumn#createControl(CompositeRuler, Composite)
|
|
642 */
|
|
643 public Control createControl(CompositeRuler parentRuler, Composite parentControl) {
|
|
644
|
|
645 fParentRuler= parentRuler;
|
|
646 fCachedTextViewer= parentRuler.getTextViewer();
|
|
647 fCachedTextWidget= fCachedTextViewer.getTextWidget();
|
|
648
|
|
649 fCanvas= new Canvas(parentControl, DWT.NO_FOCUS ) {
|
|
650 /*
|
|
651 * @see dwt.widgets.Control#addMouseListener(dwt.events.MouseListener)
|
|
652 * @since 3.4
|
|
653 */
|
|
654 public void addMouseListener(MouseListener listener) {
|
|
655 // see bug 40889, bug 230073 and AnnotationRulerColumn#isPropagatingMouseListener()
|
|
656 if (listener is fMouseHandler)
|
|
657 super.addMouseListener(listener);
|
|
658 else {
|
|
659 TypedListener typedListener= null;
|
|
660 if (listener !is null)
|
|
661 typedListener= new TypedListener(listener);
|
|
662 addListener(DWT.MouseDoubleClick, typedListener);
|
|
663 }
|
|
664 }
|
|
665 };
|
|
666 fCanvas.setBackground(getBackground(fCanvas.getDisplay()));
|
|
667 fCanvas.setForeground(fForeground);
|
|
668
|
|
669 fCanvas.addPaintListener(new PaintListener() {
|
|
670 public void paintControl(PaintEvent event) {
|
|
671 if (fCachedTextViewer !is null)
|
|
672 doubleBufferPaint(event.gc);
|
|
673 }
|
|
674 });
|
|
675
|
|
676 fCanvas.addDisposeListener(new DisposeListener() {
|
|
677 public void widgetDisposed(DisposeEvent e) {
|
|
678 handleDispose();
|
|
679 fCachedTextViewer= null;
|
|
680 fCachedTextWidget= null;
|
|
681 }
|
|
682 });
|
|
683
|
|
684 fMouseHandler= new MouseHandler();
|
|
685 fCanvas.addMouseListener(fMouseHandler);
|
|
686 fCanvas.addMouseMoveListener(fMouseHandler);
|
|
687
|
|
688 if (fCachedTextViewer !is null) {
|
|
689
|
|
690 fCachedTextViewer.addViewportListener(fInternalListener);
|
|
691 fCachedTextViewer.addTextListener(fInternalListener);
|
|
692
|
|
693 if (fFont is null) {
|
|
694 if (fCachedTextWidget !is null && !fCachedTextWidget.isDisposed())
|
|
695 fFont= fCachedTextWidget.getFont();
|
|
696 }
|
|
697 }
|
|
698
|
|
699 if (fFont !is null)
|
|
700 fCanvas.setFont(fFont);
|
|
701
|
|
702 updateNumberOfDigits();
|
|
703 computeIndentations();
|
|
704 return fCanvas;
|
|
705 }
|
|
706
|
|
707 /**
|
|
708 * Disposes the column's resources.
|
|
709 */
|
|
710 protected void handleDispose() {
|
|
711
|
|
712 if (fCachedTextViewer !is null) {
|
|
713 fCachedTextViewer.removeViewportListener(fInternalListener);
|
|
714 fCachedTextViewer.removeTextListener(fInternalListener);
|
|
715 }
|
|
716
|
|
717 if (fBuffer !is null) {
|
|
718 fBuffer.dispose();
|
|
719 fBuffer= null;
|
|
720 }
|
|
721 }
|
|
722
|
|
723 /**
|
|
724 * Double buffer drawing.
|
|
725 *
|
|
726 * @param dest the GC to draw into
|
|
727 */
|
|
728 private void doubleBufferPaint(GC dest) {
|
|
729
|
|
730 Point size= fCanvas.getSize();
|
|
731
|
|
732 if (size.x <= 0 || size.y <= 0)
|
|
733 return;
|
|
734
|
|
735 if (fBuffer !is null) {
|
|
736 Rectangle r= fBuffer.getBounds();
|
|
737 if (r.width !is size.x || r.height !is size.y) {
|
|
738 fBuffer.dispose();
|
|
739 fBuffer= null;
|
|
740 }
|
|
741 }
|
|
742 if (fBuffer is null)
|
|
743 fBuffer= new Image(fCanvas.getDisplay(), size.x, size.y);
|
|
744
|
|
745 GC gc= new GC(fBuffer);
|
|
746 gc.setFont(fCanvas.getFont());
|
|
747 if (fForeground !is null)
|
|
748 gc.setForeground(fForeground);
|
|
749
|
|
750 try {
|
|
751 gc.setBackground(getBackground(fCanvas.getDisplay()));
|
|
752 gc.fillRectangle(0, 0, size.x, size.y);
|
|
753
|
|
754 ILineRange visibleLines= JFaceTextUtil.getVisibleModelLines(fCachedTextViewer);
|
|
755 if (visibleLines is null)
|
|
756 return;
|
|
757 fScrollPos= fCachedTextWidget.getTopPixel();
|
|
758 doPaint(gc, visibleLines);
|
|
759 } finally {
|
|
760 gc.dispose();
|
|
761 }
|
|
762
|
|
763 dest.drawImage(fBuffer, 0, 0);
|
|
764 }
|
|
765
|
|
766 /**
|
|
767 * Returns the view port height in lines.
|
|
768 *
|
|
769 * @return the view port height in lines
|
|
770 * @deprecated as of 3.2 the number of lines in the viewport cannot be computed because
|
|
771 * StyledText supports variable line heights
|
|
772 */
|
|
773 protected int getVisibleLinesInViewport() {
|
|
774 return getVisibleLinesInViewport(fCachedTextWidget);
|
|
775 }
|
|
776
|
|
777
|
|
778 /**
|
|
779 * Returns <code>true</code> if the viewport displays the entire viewer contents, i.e. the
|
|
780 * viewer is not vertically scrollable.
|
|
781 *
|
|
782 * @return <code>true</code> if the viewport displays the entire contents, <code>false</code> otherwise
|
|
783 * @since 3.2
|
|
784 */
|
|
785 protected final bool isViewerCompletelyShown() {
|
|
786 return JFaceTextUtil.isShowingEntireContents(fCachedTextWidget);
|
|
787 }
|
|
788
|
|
789 /**
|
|
790 * Draws the ruler column.
|
|
791 *
|
|
792 * @param gc the GC to draw into
|
|
793 * @param visibleLines the visible model lines
|
|
794 * @since 3.2
|
|
795 */
|
|
796 void doPaint(GC gc, ILineRange visibleLines) {
|
|
797 Display display= fCachedTextWidget.getDisplay();
|
|
798
|
|
799 // draw diff info
|
|
800 int y= -JFaceTextUtil.getHiddenTopLinePixels(fCachedTextWidget);
|
|
801
|
|
802 int lastLine= end(visibleLines);
|
|
803 for (int line= visibleLines.getStartLine(); line < lastLine; line++) {
|
|
804 int widgetLine= JFaceTextUtil.modelLineToWidgetLine(fCachedTextViewer, line);
|
|
805 if (widgetLine is -1)
|
|
806 continue;
|
|
807
|
|
808 int lineHeight= fCachedTextWidget.getLineHeight(fCachedTextWidget.getOffsetAtLine(widgetLine));
|
|
809 paintLine(line, y, lineHeight, gc, display);
|
|
810 y += lineHeight;
|
|
811 }
|
|
812 }
|
|
813
|
|
814 /* @since 3.2 */
|
|
815 private static int end(ILineRange range) {
|
|
816 return range.getStartLine() + range.getNumberOfLines();
|
|
817 }
|
|
818
|
|
819 /**
|
|
820 * Computes the string to be printed for <code>line</code>. The default implementation returns
|
|
821 * <code>Integer.toString(line + 1)</code>.
|
|
822 *
|
|
823 * @param line the line number for which the line number string is generated
|
|
824 * @return the string to be printed on the line number bar for <code>line</code>
|
|
825 * @since 3.0
|
|
826 */
|
|
827 protected String createDisplayString(int line) {
|
|
828 return Integer.toString(line + 1);
|
|
829 }
|
|
830
|
|
831 /**
|
|
832 * Returns the difference between the baseline of the widget and the
|
|
833 * baseline as specified by the font for <code>gc</code>. When drawing
|
|
834 * line numbers, the returned bias should be added to obtain text lined up
|
|
835 * on the correct base line of the text widget.
|
|
836 *
|
|
837 * @param gc the <code>GC</code> to get the font metrics from
|
|
838 * @param widgetLine the widget line
|
|
839 * @return the baseline bias to use when drawing text that is lined up with
|
|
840 * <code>fCachedTextWidget</code>
|
|
841 * @since 3.2
|
|
842 */
|
|
843 private int getBaselineBias(GC gc, int widgetLine) {
|
|
844 /*
|
|
845 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=62951
|
|
846 * widget line height may be more than the font height used for the
|
|
847 * line numbers, since font styles (bold, italics...) can have larger
|
|
848 * font metrics than the simple font used for the numbers.
|
|
849 */
|
|
850 int offset= fCachedTextWidget.getOffsetAtLine(widgetLine);
|
|
851 int widgetBaseline= fCachedTextWidget.getBaseline(offset);
|
|
852
|
|
853 FontMetrics fm= gc.getFontMetrics();
|
|
854 int fontBaseline= fm.getAscent() + fm.getLeading();
|
|
855 int baselineBias= widgetBaseline - fontBaseline;
|
|
856 return Math.max(0, baselineBias);
|
|
857 }
|
|
858
|
|
859 /**
|
|
860 * Paints the line. After this method is called the line numbers are painted on top
|
|
861 * of the result of this method.
|
|
862 *
|
|
863 * @param line the line of the document which the ruler is painted for
|
|
864 * @param y the y-coordinate of the box being painted for <code>line</code>, relative to <code>gc</code>
|
|
865 * @param lineheight the height of one line (and therefore of the box being painted)
|
|
866 * @param gc the drawing context the client may choose to draw on.
|
|
867 * @param display the display the drawing occurs on
|
|
868 * @since 3.0
|
|
869 */
|
|
870 protected void paintLine(int line, int y, int lineheight, GC gc, Display display) {
|
|
871 int widgetLine= JFaceTextUtil.modelLineToWidgetLine(fCachedTextViewer, line);
|
|
872
|
|
873 String s= createDisplayString(line);
|
|
874 int indentation= fIndentation[s.length()];
|
|
875 int baselineBias= getBaselineBias(gc, widgetLine);
|
|
876 gc.drawString(s, indentation, y + baselineBias, true);
|
|
877 }
|
|
878
|
|
879 /**
|
|
880 * Triggers a redraw in the display thread.
|
|
881 *
|
|
882 * @since 3.0
|
|
883 */
|
|
884 protected final void postRedraw() {
|
|
885 if (fCanvas !is null && !fCanvas.isDisposed()) {
|
|
886 Display d= fCanvas.getDisplay();
|
|
887 if (d !is null) {
|
|
888 synchronized (fRunnableLock) {
|
|
889 if (fIsRunnablePosted)
|
|
890 return;
|
|
891 fIsRunnablePosted= true;
|
|
892 }
|
|
893 d.asyncExec(fRunnable);
|
|
894 }
|
|
895 }
|
|
896 }
|
|
897
|
|
898 /*
|
|
899 * @see IVerticalRulerColumn#redraw()
|
|
900 */
|
|
901 public void redraw() {
|
|
902
|
|
903 if (fRelayoutRequired) {
|
|
904 layout(true);
|
|
905 return;
|
|
906 }
|
|
907
|
|
908 if (fCachedTextViewer !is null && fCanvas !is null && !fCanvas.isDisposed()) {
|
|
909 GC gc= new GC(fCanvas);
|
|
910 doubleBufferPaint(gc);
|
|
911 gc.dispose();
|
|
912 }
|
|
913 }
|
|
914
|
|
915 /*
|
|
916 * @see IVerticalRulerColumn#setModel(IAnnotationModel)
|
|
917 */
|
|
918 public void setModel(IAnnotationModel model) {
|
|
919 }
|
|
920
|
|
921 /*
|
|
922 * @see IVerticalRulerColumn#setFont(Font)
|
|
923 */
|
|
924 public void setFont(Font font) {
|
|
925 fFont= font;
|
|
926 if (fCanvas !is null && !fCanvas.isDisposed()) {
|
|
927 fCanvas.setFont(fFont);
|
|
928 updateNumberOfDigits();
|
|
929 computeIndentations();
|
|
930 }
|
|
931 }
|
|
932
|
|
933 /**
|
|
934 * Returns the parent (composite) ruler of this ruler column.
|
|
935 *
|
|
936 * @return the parent ruler
|
|
937 * @since 3.0
|
|
938 */
|
|
939 protected CompositeRuler getParentRuler() {
|
|
940 return fParentRuler;
|
|
941 }
|
|
942
|
|
943
|
|
944 /**
|
|
945 * Returns the number of lines in the view port.
|
|
946 *
|
|
947 * @param textWidget
|
|
948 * @return the number of lines visible in the view port <code>-1</code> if there's no client area
|
|
949 * @deprecated this method should not be used - it relies on the widget using a uniform line height
|
|
950 */
|
|
951 static int getVisibleLinesInViewport(StyledText textWidget) {
|
|
952 if (textWidget !is null) {
|
|
953 Rectangle clArea= textWidget.getClientArea();
|
|
954 if (!clArea.isEmpty()) {
|
|
955 int firstPixel= 0;
|
|
956 int lastPixel= clArea.height - 1; // XXX what about margins? don't take trims as they include scrollbars
|
|
957 int first= JFaceTextUtil.getLineIndex(textWidget, firstPixel);
|
|
958 int last= JFaceTextUtil.getLineIndex(textWidget, lastPixel);
|
|
959 return last - first;
|
|
960 }
|
|
961 }
|
|
962 return -1;
|
|
963 }
|
|
964
|
|
965 }
|