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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.MarginPainter;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18 import dwt.DWT;
|
|
19 import dwt.custom.StyledText;
|
|
20 import dwt.events.PaintEvent;
|
|
21 import dwt.events.PaintListener;
|
|
22 import dwt.graphics.Color;
|
|
23 import dwt.graphics.GC;
|
|
24 import dwt.graphics.Rectangle;
|
|
25
|
|
26
|
|
27 /**
|
|
28 * Paints a vertical line (margin line) after a given column respecting the text
|
|
29 * viewer's font.
|
|
30 * <p>
|
|
31 * Clients usually instantiate and configure objects of this class.</p>
|
|
32 * <p>
|
|
33 * This class is not intended to be subclassed.</p>
|
|
34 *
|
|
35 * @since 2.1
|
|
36 * @noextend This class is not intended to be subclassed by clients.
|
|
37 */
|
|
38 public class MarginPainter : IPainter, PaintListener {
|
|
39
|
|
40 /** The widget of the text viewer */
|
|
41 private StyledText fTextWidget;
|
|
42
|
|
43 /** The column after which to paint the line, default value <code>80</code> */
|
|
44 private int fMarginWidth= 80;
|
|
45 /** The color in which to paint the line */
|
|
46 private Color fColor;
|
|
47 /** The line style of the line to be painted, default value <code>DWT.LINE_SOLID</code> */
|
|
48 private int fLineStyle= DWT.LINE_SOLID;
|
|
49 /** The line width of the line to be painted, default value <code>1</code> */
|
|
50 private int fLineWidth= 0; // NOTE: 0 means width is 1 but with optimized performance
|
|
51 /** The cached x-offset of the <code>fMarginWidth</code> for the current font */
|
|
52 private int fCachedWidgetX= -1;
|
|
53 /** The active state of this painter */
|
|
54 private bool fIsActive= false;
|
|
55
|
|
56 /**
|
|
57 * Creates a new painter for the given text viewer.
|
|
58 *
|
|
59 * @param textViewer the text viewer
|
|
60 */
|
|
61 public MarginPainter(ITextViewer textViewer) {
|
|
62 fTextWidget= textViewer.getTextWidget();
|
|
63 }
|
|
64
|
|
65 /**
|
|
66 * Sets the column after which to draw the margin line.
|
|
67 *
|
|
68 * @param width the column
|
|
69 */
|
|
70 public void setMarginRulerColumn(int width) {
|
|
71 fMarginWidth= width;
|
|
72 initialize();
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * Sets the line style of the margin line.
|
|
77 *
|
|
78 * @param lineStyle a <code>DWT</code> style constant describing the line style
|
|
79 */
|
|
80 public void setMarginRulerStyle(int lineStyle) {
|
|
81 fLineStyle= lineStyle;
|
|
82 }
|
|
83
|
|
84 /**
|
|
85 * Sets the line width of the margin line.
|
|
86 *
|
|
87 * @param lineWidth the line width
|
|
88 */
|
|
89 public void setMarginRulerWidth(int lineWidth) {
|
|
90 if (lineWidth is 1)
|
|
91 lineWidth= 0; // NOTE: 0 means width is 1 but with optimized performance
|
|
92 fLineWidth= lineWidth;
|
|
93 }
|
|
94
|
|
95 /**
|
|
96 * Sets the color of the margin line. Must be called before <code>paint</code> is called the first time.
|
|
97 *
|
|
98 * @param color the color
|
|
99 */
|
|
100 public void setMarginRulerColor(Color color) {
|
|
101 fColor= color;
|
|
102 }
|
|
103
|
|
104 /**
|
|
105 * Initializes this painter, by flushing and recomputing all caches and causing
|
|
106 * the widget to be redrawn. Must be called explicitly when font of text widget changes.
|
|
107 */
|
|
108 public void initialize() {
|
|
109 computeWidgetX();
|
|
110 fTextWidget.redraw();
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * Computes and remembers the x-offset of the margin column for the
|
|
115 * current widget font.
|
|
116 */
|
|
117 private void computeWidgetX() {
|
|
118 GC gc= new GC(fTextWidget);
|
|
119 int pixels= gc.getFontMetrics().getAverageCharWidth();
|
|
120 gc.dispose();
|
|
121
|
|
122 fCachedWidgetX= pixels * fMarginWidth;
|
|
123 }
|
|
124
|
|
125 /*
|
|
126 * @see IPainter#deactivate(bool)
|
|
127 */
|
|
128 public void deactivate(bool redraw) {
|
|
129 if (fIsActive) {
|
|
130 fIsActive= false;
|
|
131 fCachedWidgetX= -1;
|
|
132 fTextWidget.removePaintListener(this);
|
|
133 if (redraw)
|
|
134 fTextWidget.redraw();
|
|
135 }
|
|
136 }
|
|
137
|
|
138 /*
|
|
139 * @see IPainter#dispose()
|
|
140 */
|
|
141 public void dispose() {
|
|
142 fTextWidget= null;
|
|
143 }
|
|
144
|
|
145 /*
|
|
146 * @see IPainter#paint(int)
|
|
147 */
|
|
148 public void paint(int reason) {
|
|
149 if (!fIsActive) {
|
|
150 fIsActive= true;
|
|
151 fTextWidget.addPaintListener(this);
|
|
152 if (fCachedWidgetX is -1)
|
|
153 computeWidgetX();
|
|
154 fTextWidget.redraw();
|
|
155 } else if (CONFIGURATION is reason || INTERNAL is reason)
|
|
156 fTextWidget.redraw();
|
|
157 }
|
|
158
|
|
159 /*
|
|
160 * @see dwt.events.PaintListener#paintControl(dwt.events.PaintEvent)
|
|
161 */
|
|
162 public void paintControl(PaintEvent e) {
|
|
163 if (fTextWidget !is null) {
|
|
164 int x= fCachedWidgetX - fTextWidget.getHorizontalPixel();
|
|
165 if (x >= 0) {
|
|
166 Rectangle area= fTextWidget.getClientArea();
|
|
167 e.gc.setForeground(fColor);
|
|
168 e.gc.setLineStyle(fLineStyle);
|
|
169 e.gc.setLineWidth(fLineWidth);
|
|
170 e.gc.drawLine(x, 0, x, area.height);
|
|
171 }
|
|
172 }
|
|
173 }
|
|
174
|
|
175 /*
|
|
176 * @see dwtx.jface.text.IPainter#setPositionManager(dwtx.jface.text.IPaintPositionManager)
|
|
177 */
|
|
178 public void setPositionManager(IPaintPositionManager manager) {
|
|
179 }
|
|
180 }
|