Mercurial > projects > dwt-addons
annotate dwtx/jface/text/source/MatchingCharacterPainter.d @ 162:1a5b8f8129df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 00:51:37 +0200 |
parents | b6bad70d540a |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2005 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.source.MatchingCharacterPainter; | |
14 | |
131 | 15 import dwtx.jface.text.source.ISharedTextColors; // packageimport |
16 import dwtx.jface.text.source.ILineRange; // packageimport | |
17 import dwtx.jface.text.source.IAnnotationPresentation; // packageimport | |
18 import dwtx.jface.text.source.IVerticalRulerInfoExtension; // packageimport | |
19 import dwtx.jface.text.source.ICharacterPairMatcher; // packageimport | |
20 import dwtx.jface.text.source.TextInvocationContext; // packageimport | |
21 import dwtx.jface.text.source.LineChangeHover; // packageimport | |
22 import dwtx.jface.text.source.IChangeRulerColumn; // packageimport | |
23 import dwtx.jface.text.source.IAnnotationMap; // packageimport | |
24 import dwtx.jface.text.source.IAnnotationModelListenerExtension; // packageimport | |
25 import dwtx.jface.text.source.ISourceViewerExtension2; // packageimport | |
26 import dwtx.jface.text.source.IAnnotationHover; // packageimport | |
27 import dwtx.jface.text.source.ContentAssistantFacade; // packageimport | |
28 import dwtx.jface.text.source.IAnnotationAccess; // packageimport | |
29 import dwtx.jface.text.source.IVerticalRulerExtension; // packageimport | |
30 import dwtx.jface.text.source.IVerticalRulerColumn; // packageimport | |
31 import dwtx.jface.text.source.LineNumberRulerColumn; // packageimport | |
32 import dwtx.jface.text.source.IAnnotationModelExtension; // packageimport | |
33 import dwtx.jface.text.source.ILineDifferExtension; // packageimport | |
34 import dwtx.jface.text.source.DefaultCharacterPairMatcher; // packageimport | |
35 import dwtx.jface.text.source.LineNumberChangeRulerColumn; // packageimport | |
36 import dwtx.jface.text.source.IAnnotationAccessExtension; // packageimport | |
37 import dwtx.jface.text.source.ISourceViewer; // packageimport | |
38 import dwtx.jface.text.source.AnnotationModel; // packageimport | |
39 import dwtx.jface.text.source.ILineDifferExtension2; // packageimport | |
40 import dwtx.jface.text.source.IAnnotationModelListener; // packageimport | |
41 import dwtx.jface.text.source.IVerticalRuler; // packageimport | |
42 import dwtx.jface.text.source.DefaultAnnotationHover; // packageimport | |
43 import dwtx.jface.text.source.SourceViewer; // packageimport | |
44 import dwtx.jface.text.source.SourceViewerConfiguration; // packageimport | |
45 import dwtx.jface.text.source.AnnotationBarHoverManager; // packageimport | |
46 import dwtx.jface.text.source.CompositeRuler; // packageimport | |
47 import dwtx.jface.text.source.ImageUtilities; // packageimport | |
48 import dwtx.jface.text.source.VisualAnnotationModel; // packageimport | |
49 import dwtx.jface.text.source.IAnnotationModel; // packageimport | |
50 import dwtx.jface.text.source.ISourceViewerExtension3; // packageimport | |
51 import dwtx.jface.text.source.ILineDiffInfo; // packageimport | |
52 import dwtx.jface.text.source.VerticalRulerEvent; // packageimport | |
53 import dwtx.jface.text.source.ChangeRulerColumn; // packageimport | |
54 import dwtx.jface.text.source.ILineDiffer; // packageimport | |
55 import dwtx.jface.text.source.AnnotationModelEvent; // packageimport | |
56 import dwtx.jface.text.source.AnnotationColumn; // packageimport | |
57 import dwtx.jface.text.source.AnnotationRulerColumn; // packageimport | |
58 import dwtx.jface.text.source.IAnnotationHoverExtension; // packageimport | |
59 import dwtx.jface.text.source.AbstractRulerColumn; // packageimport | |
60 import dwtx.jface.text.source.ISourceViewerExtension; // packageimport | |
61 import dwtx.jface.text.source.AnnotationMap; // packageimport | |
62 import dwtx.jface.text.source.IVerticalRulerInfo; // packageimport | |
63 import dwtx.jface.text.source.IAnnotationModelExtension2; // packageimport | |
64 import dwtx.jface.text.source.LineRange; // packageimport | |
65 import dwtx.jface.text.source.IAnnotationAccessExtension2; // packageimport | |
66 import dwtx.jface.text.source.VerticalRuler; // packageimport | |
67 import dwtx.jface.text.source.JFaceTextMessages; // packageimport | |
68 import dwtx.jface.text.source.IOverviewRuler; // packageimport | |
69 import dwtx.jface.text.source.Annotation; // packageimport | |
70 import dwtx.jface.text.source.IVerticalRulerListener; // packageimport | |
71 import dwtx.jface.text.source.ISourceViewerExtension4; // packageimport | |
72 import dwtx.jface.text.source.AnnotationPainter; // packageimport | |
73 import dwtx.jface.text.source.IAnnotationHoverExtension2; // packageimport | |
74 import dwtx.jface.text.source.OverviewRuler; // packageimport | |
75 import dwtx.jface.text.source.OverviewRulerHoverManager; // packageimport | |
76 | |
77 | |
129 | 78 import dwt.dwthelper.utils; |
79 | |
80 | |
81 | |
82 | |
83 import dwt.custom.StyledText; | |
84 import dwt.events.PaintEvent; | |
85 import dwt.events.PaintListener; | |
86 import dwt.graphics.Color; | |
87 import dwt.graphics.GC; | |
88 import dwt.graphics.Point; | |
89 import dwt.graphics.Rectangle; | |
90 import dwtx.jface.text.BadLocationException; | |
91 import dwtx.jface.text.IDocument; | |
92 import dwtx.jface.text.IPaintPositionManager; | |
93 import dwtx.jface.text.IPainter; | |
94 import dwtx.jface.text.IRegion; | |
95 import dwtx.jface.text.ITextViewerExtension5; | |
96 import dwtx.jface.text.Position; | |
97 import dwtx.jface.text.Region; | |
98 | |
99 /** | |
100 * Highlights the peer character matching the character near the caret position. | |
101 * This painter can be configured with an | |
102 * {@link dwtx.jface.text.source.ICharacterPairMatcher}. | |
103 * <p> | |
104 * Clients instantiate and configure object of this class.</p> | |
105 * | |
106 * @since 2.1 | |
107 */ | |
108 public final class MatchingCharacterPainter : IPainter, PaintListener { | |
109 | |
110 /** Indicates whether this painter is active */ | |
111 private bool fIsActive= false; | |
112 /** The source viewer this painter is associated with */ | |
113 private ISourceViewer fSourceViewer; | |
114 /** The viewer's widget */ | |
115 private StyledText fTextWidget; | |
116 /** The color in which to highlight the peer character */ | |
117 private Color fColor; | |
118 /** The paint position manager */ | |
119 private IPaintPositionManager fPaintPositionManager; | |
120 /** The strategy for finding matching characters */ | |
121 private ICharacterPairMatcher fMatcher; | |
122 /** The position tracking the matching characters */ | |
162 | 123 private Position fPairPosition; |
129 | 124 /** The anchor indicating whether the character is left or right of the caret */ |
125 private int fAnchor; | |
126 | |
127 | |
128 /** | |
129 * Creates a new MatchingCharacterPainter for the given source viewer using | |
130 * the given character pair matcher. The character matcher is not adopted by | |
131 * this painter. Thus, it is not disposed. However, this painter requires | |
132 * exclusive access to the given pair matcher. | |
133 * | |
134 * @param sourceViewer | |
135 * @param matcher | |
136 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
137 public this(ISourceViewer sourceViewer, ICharacterPairMatcher matcher) { |
162 | 138 fPairPosition= new Position(0, 0); |
129 | 139 fSourceViewer= sourceViewer; |
140 fMatcher= matcher; | |
141 fTextWidget= sourceViewer.getTextWidget(); | |
142 } | |
143 | |
144 /** | |
145 * Sets the color in which to highlight the match character. | |
146 * | |
147 * @param color the color | |
148 */ | |
149 public void setColor(Color color) { | |
150 fColor= color; | |
151 } | |
152 | |
153 /* | |
154 * @see dwtx.jface.text.IPainter#dispose() | |
155 */ | |
156 public void dispose() { | |
157 if (fMatcher !is null) { | |
158 fMatcher.clear(); | |
159 fMatcher= null; | |
160 } | |
161 | |
162 fColor= null; | |
163 fTextWidget= null; | |
164 } | |
165 | |
166 /* | |
167 * @see dwtx.jface.text.IPainter#deactivate(bool) | |
168 */ | |
169 public void deactivate(bool redraw) { | |
170 if (fIsActive) { | |
171 fIsActive= false; | |
172 fTextWidget.removePaintListener(this); | |
173 if (fPaintPositionManager !is null) | |
174 fPaintPositionManager.unmanagePosition(fPairPosition); | |
175 if (redraw) | |
176 handleDrawRequest(null); | |
177 } | |
178 } | |
179 | |
180 /* | |
181 * @see dwt.events.PaintListener#paintControl(dwt.events.PaintEvent) | |
182 */ | |
183 public void paintControl(PaintEvent event) { | |
184 if (fTextWidget !is null) | |
185 handleDrawRequest(event.gc); | |
186 } | |
187 | |
188 /** | |
189 * Handles a redraw request. | |
190 * | |
191 * @param gc the GC to draw into. | |
192 */ | |
193 private void handleDrawRequest(GC gc) { | |
194 | |
195 if (fPairPosition.isDeleted) | |
196 return; | |
197 | |
198 int offset= fPairPosition.getOffset(); | |
199 int length= fPairPosition.getLength(); | |
200 if (length < 1) | |
201 return; | |
202 | |
138 | 203 if ( cast(ITextViewerExtension5)fSourceViewer ) { |
134 | 204 ITextViewerExtension5 extension= cast(ITextViewerExtension5) fSourceViewer; |
129 | 205 IRegion widgetRange= extension.modelRange2WidgetRange(new Region(offset, length)); |
206 if (widgetRange is null) | |
207 return; | |
208 | |
209 try { | |
210 // don't draw if the pair position is really hidden and widgetRange just | |
211 // marks the coverage around it. | |
212 IDocument doc= fSourceViewer.getDocument(); | |
213 int startLine= doc.getLineOfOffset(offset); | |
214 int endLine= doc.getLineOfOffset(offset + length); | |
215 if (extension.modelLine2WidgetLine(startLine) is -1 || extension.modelLine2WidgetLine(endLine) is -1) | |
216 return; | |
217 } catch (BadLocationException e) { | |
218 return; | |
219 } | |
220 | |
221 offset= widgetRange.getOffset(); | |
222 length= widgetRange.getLength(); | |
223 | |
224 } else { | |
225 IRegion region= fSourceViewer.getVisibleRegion(); | |
226 if (region.getOffset() > offset || region.getOffset() + region.getLength() < offset + length) | |
227 return; | |
228 offset -= region.getOffset(); | |
229 } | |
230 | |
231 if (ICharacterPairMatcher.RIGHT is fAnchor) | |
232 draw(gc, offset, 1); | |
233 else | |
234 draw(gc, offset + length -1, 1); | |
235 } | |
236 | |
237 /** | |
238 * Highlights the given widget region. | |
239 * | |
240 * @param gc the GC to draw into | |
241 * @param offset the offset of the widget region | |
242 * @param length the length of the widget region | |
243 */ | |
244 private void draw(GC gc, int offset, int length) { | |
245 if (gc !is null) { | |
246 | |
247 gc.setForeground(fColor); | |
162 | 248 |
129 | 249 Rectangle bounds; |
250 if (length > 0) | |
251 bounds= fTextWidget.getTextBounds(offset, offset + length - 1); | |
252 else { | |
253 Point loc= fTextWidget.getLocationAtOffset(offset); | |
254 bounds= new Rectangle(loc.x, loc.y, 1, fTextWidget.getLineHeight(offset)); | |
255 } | |
162 | 256 |
129 | 257 // draw box around line segment |
258 gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1); | |
259 | |
260 // draw box around character area | |
261 // int widgetBaseline= fTextWidget.getBaseline(); | |
262 // FontMetrics fm= gc.getFontMetrics(); | |
263 // int fontBaseline= fm.getAscent() + fm.getLeading(); | |
264 // int fontBias= widgetBaseline - fontBaseline; | |
265 | |
266 // gc.drawRectangle(left.x, left.y + fontBias, right.x - left.x - 1, fm.getHeight() - 1); | |
267 | |
268 } else { | |
269 fTextWidget.redrawRange(offset, length, true); | |
270 } | |
271 } | |
272 | |
273 /* | |
274 * @see dwtx.jface.text.IPainter#paint(int) | |
275 */ | |
276 public void paint(int reason) { | |
277 | |
278 IDocument document= fSourceViewer.getDocument(); | |
279 if (document is null) { | |
280 deactivate(false); | |
281 return; | |
282 } | |
283 | |
284 Point selection= fSourceViewer.getSelectedRange(); | |
285 if (selection.y > 0) { | |
286 deactivate(true); | |
287 return; | |
288 } | |
289 | |
290 IRegion pair= fMatcher.match(document, selection.x); | |
291 if (pair is null) { | |
292 deactivate(true); | |
293 return; | |
294 } | |
295 | |
296 if (fIsActive) { | |
297 | |
298 if (IPainter.CONFIGURATION is reason) { | |
299 | |
300 // redraw current highlighting | |
301 handleDrawRequest(null); | |
302 | |
303 } else if (pair.getOffset() !is fPairPosition.getOffset() || | |
304 pair.getLength() !is fPairPosition.getLength() || | |
305 fMatcher.getAnchor() !is fAnchor) { | |
306 | |
307 // otherwise only do something if position is different | |
308 | |
309 // remove old highlighting | |
310 handleDrawRequest(null); | |
311 // update position | |
162 | 312 fPairPosition.isDeleted_= false; |
129 | 313 fPairPosition.offset= pair.getOffset(); |
314 fPairPosition.length= pair.getLength(); | |
315 fAnchor= fMatcher.getAnchor(); | |
316 // apply new highlighting | |
317 handleDrawRequest(null); | |
318 | |
319 } | |
320 } else { | |
321 | |
322 fIsActive= true; | |
323 | |
162 | 324 fPairPosition.isDeleted_= false; |
129 | 325 fPairPosition.offset= pair.getOffset(); |
326 fPairPosition.length= pair.getLength(); | |
327 fAnchor= fMatcher.getAnchor(); | |
328 | |
329 fTextWidget.addPaintListener(this); | |
330 fPaintPositionManager.managePosition(fPairPosition); | |
331 handleDrawRequest(null); | |
332 } | |
333 } | |
334 | |
335 /* | |
336 * @see dwtx.jface.text.IPainter#setPositionManager(dwtx.jface.text.IPaintPositionManager) | |
337 */ | |
338 public void setPositionManager(IPaintPositionManager manager) { | |
339 fPaintPositionManager= manager; | |
340 } | |
341 } |