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