Mercurial > projects > dwt-addons
annotate dwtx/jface/text/source/LineNumberRulerColumn.d @ 192:c3583c6ec027
Added missing default cases for switch statements
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 03 Nov 2008 22:52:26 +0100 |
parents | 1a5b8f8129df |
children |
rev | line source |
---|---|
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 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
143
diff
changeset
|
82 import dwtx.dwtxhelper.Collection; |
129 | 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. | |
159 | 226 * |
129 | 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(); | |
159 | 236 if (expandExistingSelection && cast(ITextViewerExtension5)fCachedTextViewer |
129 | 237 && fCachedTextViewer.getTextWidget() !is null) { |
134 | 238 ITextViewerExtension5 extension5= (cast(ITextViewerExtension5)fCachedTextViewer); |
129 | 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 } | |
159 | 264 |
129 | 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); | |
159 | 285 |
129 | 286 Display display= fCachedTextWidget.getDisplay(); |
287 Point absolutePosition= display.getCursorLocation(); | |
288 Point relativePosition= fCachedTextWidget.toControl(absolutePosition); | |
289 | |
290 int offset; | |
159 | 291 |
129 | 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 | |
138 | 302 if ( cast(ITextViewerExtension5)fCachedTextViewer ) { |
134 | 303 ITextViewerExtension5 extension= cast(ITextViewerExtension5)fCachedTextViewer; |
129 | 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; | |
138 | 313 if ( cast(ITextViewerExtension5)fCachedTextViewer ) { |
134 | 314 ITextViewerExtension5 extension= cast(ITextViewerExtension5)fCachedTextViewer; |
129 | 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 } | |
159 | 364 |
129 | 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: | |
135 | 384 timer= new class() Runnable { |
129 | 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: | |
135 | 398 timer= new class() Runnable { |
129 | 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; | |
192
c3583c6ec027
Added missing default cases for switch statements
Frank Benoit <benoit@tionex.de>
parents:
162
diff
changeset
|
409 default: |
129 | 410 } |
411 | |
412 if (timer !is null) { | |
413 fAutoScrollDirection= direction; | |
414 display.timerExec(TIMER_INTERVAL, timer); | |
415 } | |
416 } | |
417 | |
418 /** | |
419 * Returns the viewer's first visible line, even if only partially visible. | |
420 * | |
421 * @return the viewer's first visible line | |
422 */ | |
423 private int getInclusiveTopIndex() { | |
424 if (fCachedTextWidget !is null && !fCachedTextWidget.isDisposed()) { | |
425 return JFaceTextUtil.getPartialTopIndex(fCachedTextViewer); | |
426 } | |
427 return -1; | |
428 } | |
429 } | |
430 | |
431 /** This column's parent ruler */ | |
432 private CompositeRuler fParentRuler; | |
433 /** Cached text viewer */ | |
434 private ITextViewer fCachedTextViewer; | |
435 /** Cached text widget */ | |
436 private StyledText fCachedTextWidget; | |
437 /** The columns canvas */ | |
438 private Canvas fCanvas; | |
439 /** Cache for the actual scroll position in pixels */ | |
440 private int fScrollPos; | |
441 /** The drawable for double buffering */ | |
442 private Image fBuffer; | |
443 /** The internal listener */ | |
159 | 444 private InternalListener fInternalListener; |
129 | 445 /** The font of this column */ |
446 private Font fFont; | |
447 /** The indentation cache */ | |
448 private int[] fIndentation; | |
449 /** Indicates whether this column reacts on text change events */ | |
450 private bool fSensitiveToTextChanges= false; | |
451 /** The foreground color */ | |
452 private Color fForeground; | |
453 /** The background color */ | |
454 private Color fBackground; | |
455 /** Cached number of displayed digits */ | |
456 private int fCachedNumberOfDigits= -1; | |
457 /** Flag indicating whether a relayout is required */ | |
458 private bool fRelayoutRequired= false; | |
459 /** | |
460 * Redraw runnable lock | |
461 * @since 3.0 | |
462 */ | |
159 | 463 private Object fRunnableLock; |
129 | 464 /** |
465 * Redraw runnable state | |
466 * @since 3.0 | |
467 */ | |
468 private bool fIsRunnablePosted= false; | |
469 /** | |
470 * Redraw runnable | |
471 * @since 3.0 | |
472 */ | |
159 | 473 private Runnable fRunnable; |
474 private void fRunnable_init() { | |
475 fRunnable = new class() Runnable { | |
476 public void run() { | |
477 synchronized (fRunnableLock) { | |
478 fIsRunnablePosted= false; | |
479 } | |
480 redraw(); | |
129 | 481 } |
159 | 482 }; |
483 } | |
129 | 484 /* @since 3.2 */ |
485 private MouseHandler fMouseHandler; | |
486 | |
487 | |
488 /** | |
489 * Constructs a new vertical ruler column. | |
490 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
491 public this() { |
159 | 492 fInternalListener= new InternalListener(); |
493 fRunnableLock= new Object(); | |
494 fRunnable_init(); | |
129 | 495 } |
496 | |
497 /** | |
498 * Sets the foreground color of this column. | |
499 * | |
500 * @param foreground the foreground color | |
501 */ | |
502 public void setForeground(Color foreground) { | |
503 fForeground= foreground; | |
504 } | |
505 | |
506 /** | |
507 * Returns the foreground color being used to print the line numbers. | |
508 * | |
509 * @return the configured foreground color | |
510 * @since 3.0 | |
511 */ | |
512 protected Color getForeground() { | |
513 return fForeground; | |
514 } | |
515 | |
516 /** | |
517 * Sets the background color of this column. | |
518 * | |
519 * @param background the background color | |
520 */ | |
521 public void setBackground(Color background) { | |
522 fBackground= background; | |
523 if (fCanvas !is null && !fCanvas.isDisposed()) | |
524 fCanvas.setBackground(getBackground(fCanvas.getDisplay())); | |
525 } | |
526 | |
527 /** | |
528 * Returns the System background color for list widgets. | |
529 * | |
530 * @param display the display | |
531 * @return the System background color for list widgets | |
532 */ | |
533 protected Color getBackground(Display display) { | |
534 if (fBackground is null) | |
535 return display.getSystemColor(DWT.COLOR_LIST_BACKGROUND); | |
536 return fBackground; | |
537 } | |
538 | |
539 /* | |
540 * @see IVerticalRulerColumn#getControl() | |
541 */ | |
542 public Control getControl() { | |
543 return fCanvas; | |
544 } | |
545 | |
546 /* | |
547 * @see IVerticalRuleColumnr#getWidth | |
548 */ | |
549 public int getWidth() { | |
550 return fIndentation[0]; | |
551 } | |
552 | |
553 /** | |
554 * Computes the number of digits to be displayed. Returns | |
555 * <code>true</code> if the number of digits changed compared | |
556 * to the previous call of this method. If the method is called | |
557 * for the first time, the return value is also <code>true</code>. | |
558 * | |
559 * @return whether the number of digits has been changed | |
560 * @since 3.0 | |
561 */ | |
562 protected bool updateNumberOfDigits() { | |
563 if (fCachedTextViewer is null) | |
564 return false; | |
565 | |
566 int digits= computeNumberOfDigits(); | |
567 | |
568 if (fCachedNumberOfDigits !is digits) { | |
569 fCachedNumberOfDigits= digits; | |
570 return true; | |
571 } | |
572 | |
573 return false; | |
574 } | |
575 | |
576 /** | |
577 * Does the real computation of the number of digits. Subclasses may override this method if | |
578 * they need extra space on the line number ruler. | |
579 * | |
580 * @return the number of digits to be displayed on the line number ruler. | |
581 */ | |
582 protected int computeNumberOfDigits() { | |
583 IDocument document= fCachedTextViewer.getDocument(); | |
584 int lines= document is null ? 0 : document.getNumberOfLines(); | |
585 | |
586 int digits= 2; | |
162 | 587 while (lines > Math.pow(cast(real)10.0, cast(uint)digits) -1) { |
129 | 588 ++digits; |
589 } | |
590 return digits; | |
591 } | |
592 | |
593 /** | |
594 * Layouts the enclosing viewer to adapt the layout to changes of the | |
595 * size of the individual components. | |
596 * | |
597 * @param redraw <code>true</code> if this column can be redrawn | |
598 */ | |
599 protected void layout(bool redraw) { | |
600 if (!redraw) { | |
601 fRelayoutRequired= true; | |
602 return; | |
603 } | |
604 | |
605 fRelayoutRequired= false; | |
138 | 606 if ( cast(ITextViewerExtension)fCachedTextViewer ) { |
134 | 607 ITextViewerExtension extension= cast(ITextViewerExtension) fCachedTextViewer; |
129 | 608 Control control= extension.getControl(); |
142 | 609 if ( cast(Composite)control && !control.isDisposed()) { |
134 | 610 Composite composite= cast(Composite) control; |
129 | 611 composite.layout(true); |
612 } | |
613 } | |
614 } | |
615 | |
616 /** | |
617 * Computes the indentations for the given font and stores them in | |
618 * <code>fIndentation</code>. | |
619 */ | |
620 protected void computeIndentations() { | |
621 if (fCanvas is null || fCanvas.isDisposed()) | |
622 return; | |
623 | |
624 GC gc= new GC(fCanvas); | |
625 try { | |
626 | |
627 gc.setFont(fCanvas.getFont()); | |
628 | |
629 fIndentation= new int[fCachedNumberOfDigits + 1]; | |
630 | |
631 char[] nines= new char[fCachedNumberOfDigits]; | |
632 Arrays.fill(nines, '9'); | |
162 | 633 String nineString= new_String(nines); |
129 | 634 Point p= gc.stringExtent(nineString); |
635 fIndentation[0]= p.x; | |
636 | |
637 for (int i= 1; i <= fCachedNumberOfDigits; i++) { | |
638 p= gc.stringExtent(nineString.substring(0, i)); | |
639 fIndentation[i]= fIndentation[0] - p.x; | |
640 } | |
641 | |
642 } finally { | |
643 gc.dispose(); | |
644 } | |
645 } | |
646 | |
647 /* | |
648 * @see IVerticalRulerColumn#createControl(CompositeRuler, Composite) | |
649 */ | |
650 public Control createControl(CompositeRuler parentRuler, Composite parentControl) { | |
651 | |
652 fParentRuler= parentRuler; | |
653 fCachedTextViewer= parentRuler.getTextViewer(); | |
654 fCachedTextWidget= fCachedTextViewer.getTextWidget(); | |
655 | |
135 | 656 fCanvas= new class(parentControl, DWT.NO_FOCUS ) Canvas { |
162 | 657 this(Composite c, int s ){ |
658 super(c,s); | |
659 } | |
129 | 660 /* |
661 * @see dwt.widgets.Control#addMouseListener(dwt.events.MouseListener) | |
662 * @since 3.4 | |
663 */ | |
664 public void addMouseListener(MouseListener listener) { | |
665 // see bug 40889, bug 230073 and AnnotationRulerColumn#isPropagatingMouseListener() | |
666 if (listener is fMouseHandler) | |
667 super.addMouseListener(listener); | |
668 else { | |
669 TypedListener typedListener= null; | |
670 if (listener !is null) | |
671 typedListener= new TypedListener(listener); | |
672 addListener(DWT.MouseDoubleClick, typedListener); | |
673 } | |
674 } | |
675 }; | |
676 fCanvas.setBackground(getBackground(fCanvas.getDisplay())); | |
677 fCanvas.setForeground(fForeground); | |
678 | |
135 | 679 fCanvas.addPaintListener(new class() PaintListener { |
129 | 680 public void paintControl(PaintEvent event) { |
681 if (fCachedTextViewer !is null) | |
682 doubleBufferPaint(event.gc); | |
683 } | |
684 }); | |
685 | |
135 | 686 fCanvas.addDisposeListener(new class() DisposeListener { |
129 | 687 public void widgetDisposed(DisposeEvent e) { |
688 handleDispose(); | |
689 fCachedTextViewer= null; | |
690 fCachedTextWidget= null; | |
691 } | |
692 }); | |
693 | |
694 fMouseHandler= new MouseHandler(); | |
695 fCanvas.addMouseListener(fMouseHandler); | |
696 fCanvas.addMouseMoveListener(fMouseHandler); | |
697 | |
698 if (fCachedTextViewer !is null) { | |
699 | |
700 fCachedTextViewer.addViewportListener(fInternalListener); | |
701 fCachedTextViewer.addTextListener(fInternalListener); | |
702 | |
703 if (fFont is null) { | |
704 if (fCachedTextWidget !is null && !fCachedTextWidget.isDisposed()) | |
705 fFont= fCachedTextWidget.getFont(); | |
706 } | |
707 } | |
708 | |
709 if (fFont !is null) | |
710 fCanvas.setFont(fFont); | |
711 | |
712 updateNumberOfDigits(); | |
713 computeIndentations(); | |
714 return fCanvas; | |
715 } | |
716 | |
717 /** | |
718 * Disposes the column's resources. | |
719 */ | |
720 protected void handleDispose() { | |
721 | |
722 if (fCachedTextViewer !is null) { | |
723 fCachedTextViewer.removeViewportListener(fInternalListener); | |
724 fCachedTextViewer.removeTextListener(fInternalListener); | |
725 } | |
726 | |
727 if (fBuffer !is null) { | |
728 fBuffer.dispose(); | |
729 fBuffer= null; | |
730 } | |
731 } | |
732 | |
733 /** | |
734 * Double buffer drawing. | |
735 * | |
736 * @param dest the GC to draw into | |
737 */ | |
738 private void doubleBufferPaint(GC dest) { | |
739 | |
740 Point size= fCanvas.getSize(); | |
741 | |
742 if (size.x <= 0 || size.y <= 0) | |
743 return; | |
744 | |
745 if (fBuffer !is null) { | |
746 Rectangle r= fBuffer.getBounds(); | |
747 if (r.width !is size.x || r.height !is size.y) { | |
748 fBuffer.dispose(); | |
749 fBuffer= null; | |
750 } | |
751 } | |
752 if (fBuffer is null) | |
753 fBuffer= new Image(fCanvas.getDisplay(), size.x, size.y); | |
754 | |
755 GC gc= new GC(fBuffer); | |
756 gc.setFont(fCanvas.getFont()); | |
757 if (fForeground !is null) | |
758 gc.setForeground(fForeground); | |
759 | |
760 try { | |
761 gc.setBackground(getBackground(fCanvas.getDisplay())); | |
762 gc.fillRectangle(0, 0, size.x, size.y); | |
763 | |
764 ILineRange visibleLines= JFaceTextUtil.getVisibleModelLines(fCachedTextViewer); | |
765 if (visibleLines is null) | |
766 return; | |
767 fScrollPos= fCachedTextWidget.getTopPixel(); | |
768 doPaint(gc, visibleLines); | |
769 } finally { | |
770 gc.dispose(); | |
771 } | |
772 | |
773 dest.drawImage(fBuffer, 0, 0); | |
774 } | |
775 | |
776 /** | |
777 * Returns the view port height in lines. | |
159 | 778 * |
129 | 779 * @return the view port height in lines |
780 * @deprecated as of 3.2 the number of lines in the viewport cannot be computed because | |
781 * StyledText supports variable line heights | |
782 */ | |
783 protected int getVisibleLinesInViewport() { | |
784 return getVisibleLinesInViewport(fCachedTextWidget); | |
785 } | |
159 | 786 |
129 | 787 |
788 /** | |
789 * Returns <code>true</code> if the viewport displays the entire viewer contents, i.e. the | |
790 * viewer is not vertically scrollable. | |
159 | 791 * |
129 | 792 * @return <code>true</code> if the viewport displays the entire contents, <code>false</code> otherwise |
793 * @since 3.2 | |
794 */ | |
795 protected final bool isViewerCompletelyShown() { | |
796 return JFaceTextUtil.isShowingEntireContents(fCachedTextWidget); | |
797 } | |
798 | |
799 /** | |
800 * Draws the ruler column. | |
801 * | |
802 * @param gc the GC to draw into | |
803 * @param visibleLines the visible model lines | |
804 * @since 3.2 | |
805 */ | |
806 void doPaint(GC gc, ILineRange visibleLines) { | |
807 Display display= fCachedTextWidget.getDisplay(); | |
159 | 808 |
129 | 809 // draw diff info |
810 int y= -JFaceTextUtil.getHiddenTopLinePixels(fCachedTextWidget); | |
159 | 811 |
129 | 812 int lastLine= end(visibleLines); |
813 for (int line= visibleLines.getStartLine(); line < lastLine; line++) { | |
814 int widgetLine= JFaceTextUtil.modelLineToWidgetLine(fCachedTextViewer, line); | |
815 if (widgetLine is -1) | |
816 continue; | |
817 | |
818 int lineHeight= fCachedTextWidget.getLineHeight(fCachedTextWidget.getOffsetAtLine(widgetLine)); | |
819 paintLine(line, y, lineHeight, gc, display); | |
820 y += lineHeight; | |
821 } | |
822 } | |
823 | |
824 /* @since 3.2 */ | |
825 private static int end(ILineRange range) { | |
826 return range.getStartLine() + range.getNumberOfLines(); | |
827 } | |
159 | 828 |
129 | 829 /** |
830 * Computes the string to be printed for <code>line</code>. The default implementation returns | |
831 * <code>Integer.toString(line + 1)</code>. | |
832 * | |
833 * @param line the line number for which the line number string is generated | |
834 * @return the string to be printed on the line number bar for <code>line</code> | |
835 * @since 3.0 | |
836 */ | |
837 protected String createDisplayString(int line) { | |
838 return Integer.toString(line + 1); | |
839 } | |
840 | |
841 /** | |
842 * Returns the difference between the baseline of the widget and the | |
843 * baseline as specified by the font for <code>gc</code>. When drawing | |
844 * line numbers, the returned bias should be added to obtain text lined up | |
845 * on the correct base line of the text widget. | |
846 * | |
847 * @param gc the <code>GC</code> to get the font metrics from | |
848 * @param widgetLine the widget line | |
849 * @return the baseline bias to use when drawing text that is lined up with | |
850 * <code>fCachedTextWidget</code> | |
851 * @since 3.2 | |
852 */ | |
853 private int getBaselineBias(GC gc, int widgetLine) { | |
854 /* | |
855 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=62951 | |
856 * widget line height may be more than the font height used for the | |
857 * line numbers, since font styles (bold, italics...) can have larger | |
858 * font metrics than the simple font used for the numbers. | |
859 */ | |
860 int offset= fCachedTextWidget.getOffsetAtLine(widgetLine); | |
861 int widgetBaseline= fCachedTextWidget.getBaseline(offset); | |
159 | 862 |
129 | 863 FontMetrics fm= gc.getFontMetrics(); |
864 int fontBaseline= fm.getAscent() + fm.getLeading(); | |
865 int baselineBias= widgetBaseline - fontBaseline; | |
866 return Math.max(0, baselineBias); | |
867 } | |
868 | |
869 /** | |
870 * Paints the line. After this method is called the line numbers are painted on top | |
871 * of the result of this method. | |
872 * | |
873 * @param line the line of the document which the ruler is painted for | |
874 * @param y the y-coordinate of the box being painted for <code>line</code>, relative to <code>gc</code> | |
875 * @param lineheight the height of one line (and therefore of the box being painted) | |
876 * @param gc the drawing context the client may choose to draw on. | |
877 * @param display the display the drawing occurs on | |
878 * @since 3.0 | |
879 */ | |
880 protected void paintLine(int line, int y, int lineheight, GC gc, Display display) { | |
881 int widgetLine= JFaceTextUtil.modelLineToWidgetLine(fCachedTextViewer, line); | |
882 | |
883 String s= createDisplayString(line); | |
162 | 884 int indentation= fIndentation[s.length]; |
129 | 885 int baselineBias= getBaselineBias(gc, widgetLine); |
886 gc.drawString(s, indentation, y + baselineBias, true); | |
887 } | |
888 | |
889 /** | |
890 * Triggers a redraw in the display thread. | |
891 * | |
892 * @since 3.0 | |
893 */ | |
894 protected final void postRedraw() { | |
895 if (fCanvas !is null && !fCanvas.isDisposed()) { | |
896 Display d= fCanvas.getDisplay(); | |
897 if (d !is null) { | |
898 synchronized (fRunnableLock) { | |
899 if (fIsRunnablePosted) | |
900 return; | |
901 fIsRunnablePosted= true; | |
902 } | |
903 d.asyncExec(fRunnable); | |
904 } | |
905 } | |
906 } | |
907 | |
908 /* | |
909 * @see IVerticalRulerColumn#redraw() | |
910 */ | |
911 public void redraw() { | |
912 | |
913 if (fRelayoutRequired) { | |
914 layout(true); | |
915 return; | |
916 } | |
917 | |
918 if (fCachedTextViewer !is null && fCanvas !is null && !fCanvas.isDisposed()) { | |
919 GC gc= new GC(fCanvas); | |
920 doubleBufferPaint(gc); | |
921 gc.dispose(); | |
922 } | |
923 } | |
924 | |
925 /* | |
926 * @see IVerticalRulerColumn#setModel(IAnnotationModel) | |
927 */ | |
928 public void setModel(IAnnotationModel model) { | |
929 } | |
930 | |
931 /* | |
932 * @see IVerticalRulerColumn#setFont(Font) | |
933 */ | |
934 public void setFont(Font font) { | |
935 fFont= font; | |
936 if (fCanvas !is null && !fCanvas.isDisposed()) { | |
937 fCanvas.setFont(fFont); | |
938 updateNumberOfDigits(); | |
939 computeIndentations(); | |
940 } | |
941 } | |
942 | |
943 /** | |
944 * Returns the parent (composite) ruler of this ruler column. | |
945 * | |
946 * @return the parent ruler | |
947 * @since 3.0 | |
948 */ | |
949 protected CompositeRuler getParentRuler() { | |
950 return fParentRuler; | |
951 } | |
159 | 952 |
953 | |
129 | 954 /** |
955 * Returns the number of lines in the view port. | |
159 | 956 * |
129 | 957 * @param textWidget |
958 * @return the number of lines visible in the view port <code>-1</code> if there's no client area | |
959 * @deprecated this method should not be used - it relies on the widget using a uniform line height | |
960 */ | |
961 static int getVisibleLinesInViewport(StyledText textWidget) { | |
962 if (textWidget !is null) { | |
963 Rectangle clArea= textWidget.getClientArea(); | |
964 if (!clArea.isEmpty()) { | |
965 int firstPixel= 0; | |
966 int lastPixel= clArea.height - 1; // XXX what about margins? don't take trims as they include scrollbars | |
967 int first= JFaceTextUtil.getLineIndex(textWidget, firstPixel); | |
968 int last= JFaceTextUtil.getLineIndex(textWidget, lastPixel); | |
969 return last - first; | |
970 } | |
971 } | |
972 return -1; | |
973 } | |
159 | 974 |
129 | 975 } |