Mercurial > projects > dwt-addons
comparison dwtx/jface/text/CursorLinePainter.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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 | |
14 module dwtx.jface.text.CursorLinePainter; | |
15 | |
16 import dwt.dwthelper.utils; | |
17 | |
18 | |
19 | |
20 | |
21 import dwt.custom.LineBackgroundEvent; | |
22 import dwt.custom.LineBackgroundListener; | |
23 import dwt.custom.StyledText; | |
24 import dwt.graphics.Color; | |
25 import dwt.graphics.Point; | |
26 | |
27 | |
28 /** | |
29 * A painter the draws the background of the caret line in a configured color. | |
30 * <p> | |
31 * Clients usually instantiate and configure object 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 CursorLinePainter : IPainter, LineBackgroundListener { | |
39 | |
40 /** The viewer the painter works on */ | |
41 private final ITextViewer fViewer; | |
42 /** The cursor line back ground color */ | |
43 private Color fHighlightColor; | |
44 /** The paint position manager for managing the line coordinates */ | |
45 private IPaintPositionManager fPositionManager; | |
46 | |
47 /** Keeps track of the line to be painted */ | |
48 private Position fCurrentLine= new Position(0, 0); | |
49 /** Keeps track of the line to be cleared */ | |
50 private Position fLastLine= new Position(0, 0); | |
51 /** Keeps track of the line number of the last painted line */ | |
52 private int fLastLineNumber= -1; | |
53 /** Indicates whether this painter is active */ | |
54 private bool fIsActive; | |
55 | |
56 /** | |
57 * Creates a new painter for the given source viewer. | |
58 * | |
59 * @param textViewer the source viewer for which to create a painter | |
60 */ | |
61 public CursorLinePainter(ITextViewer textViewer) { | |
62 fViewer= textViewer; | |
63 } | |
64 | |
65 /** | |
66 * Sets the color in which to draw the background of the cursor line. | |
67 * | |
68 * @param highlightColor the color in which to draw the background of the cursor line | |
69 */ | |
70 public void setHighlightColor(Color highlightColor) { | |
71 fHighlightColor= highlightColor; | |
72 } | |
73 | |
74 /* | |
75 * @see LineBackgroundListener#lineGetBackground(LineBackgroundEvent) | |
76 */ | |
77 public void lineGetBackground(LineBackgroundEvent event) { | |
78 // don't use cached line information because of asynchronous painting | |
79 | |
80 StyledText textWidget= fViewer.getTextWidget(); | |
81 if (textWidget !is null) { | |
82 | |
83 int caret= textWidget.getCaretOffset(); | |
84 int length= event.lineText.length(); | |
85 | |
86 if (event.lineOffset <= caret && caret <= event.lineOffset + length) | |
87 event.lineBackground= fHighlightColor; | |
88 else | |
89 event.lineBackground= textWidget.getBackground(); | |
90 } | |
91 } | |
92 | |
93 /** | |
94 * Updates all the cached information about the lines to be painted and to be cleared. Returns <code>true</code> | |
95 * if the line number of the cursor line has changed. | |
96 * | |
97 * @return <code>true</code> if cursor line changed | |
98 */ | |
99 private bool updateHighlightLine() { | |
100 try { | |
101 | |
102 IDocument document= fViewer.getDocument(); | |
103 int modelCaret= getModelCaret(); | |
104 int lineNumber= document.getLineOfOffset(modelCaret); | |
105 | |
106 // redraw if the current line number is different from the last line number we painted | |
107 // initially fLastLineNumber is -1 | |
108 if (lineNumber !is fLastLineNumber || !fCurrentLine.overlapsWith(modelCaret, 0)) { | |
109 | |
110 fLastLine.offset= fCurrentLine.offset; | |
111 fLastLine.length= fCurrentLine.length; | |
112 fLastLine.isDeleted= fCurrentLine.isDeleted; | |
113 | |
114 if (fCurrentLine.isDeleted) { | |
115 fCurrentLine.isDeleted= false; | |
116 fPositionManager.managePosition(fCurrentLine); | |
117 } | |
118 | |
119 fCurrentLine.offset= document.getLineOffset(lineNumber); | |
120 if (lineNumber is document.getNumberOfLines() - 1) | |
121 fCurrentLine.length= document.getLength() - fCurrentLine.offset; | |
122 else | |
123 fCurrentLine.length= document.getLineOffset(lineNumber + 1) - fCurrentLine.offset; | |
124 | |
125 fLastLineNumber= lineNumber; | |
126 return true; | |
127 | |
128 } | |
129 | |
130 } catch (BadLocationException e) { | |
131 } | |
132 | |
133 return false; | |
134 } | |
135 | |
136 /** | |
137 * Returns the location of the caret as offset in the source viewer's | |
138 * input document. | |
139 * | |
140 * @return the caret location | |
141 */ | |
142 private int getModelCaret() { | |
143 int widgetCaret= fViewer.getTextWidget().getCaretOffset(); | |
144 if (fViewer instanceof ITextViewerExtension5) { | |
145 ITextViewerExtension5 extension= (ITextViewerExtension5) fViewer; | |
146 return extension.widgetOffset2ModelOffset(widgetCaret); | |
147 } | |
148 IRegion visible= fViewer.getVisibleRegion(); | |
149 return widgetCaret + visible.getOffset(); | |
150 } | |
151 | |
152 /** | |
153 * Assumes the given position to specify offset and length of a line to be painted. | |
154 * | |
155 * @param position the specification of the line to be painted | |
156 */ | |
157 private void drawHighlightLine(Position position) { | |
158 | |
159 // if the position that is about to be drawn was deleted then we can't | |
160 if (position.isDeleted()) | |
161 return; | |
162 | |
163 int widgetOffset= 0; | |
164 if (fViewer instanceof ITextViewerExtension5) { | |
165 | |
166 ITextViewerExtension5 extension= (ITextViewerExtension5) fViewer; | |
167 widgetOffset= extension.modelOffset2WidgetOffset(position.getOffset()); | |
168 if (widgetOffset is -1) | |
169 return; | |
170 | |
171 } else { | |
172 | |
173 IRegion visible= fViewer.getVisibleRegion(); | |
174 widgetOffset= position.getOffset() - visible.getOffset(); | |
175 if (widgetOffset < 0 || visible.getLength() < widgetOffset ) | |
176 return; | |
177 } | |
178 | |
179 StyledText textWidget= fViewer.getTextWidget(); | |
180 // check for https://bugs.eclipse.org/bugs/show_bug.cgi?id=64898 | |
181 // this is a guard against the symptoms but not the actual solution | |
182 if (0 <= widgetOffset && widgetOffset <= textWidget.getCharCount()) { | |
183 Point upperLeft= textWidget.getLocationAtOffset(widgetOffset); | |
184 int width= textWidget.getClientArea().width + textWidget.getHorizontalPixel(); | |
185 int height= textWidget.getLineHeight(widgetOffset); | |
186 textWidget.redraw(0, upperLeft.y, width, height, false); | |
187 } | |
188 } | |
189 | |
190 /* | |
191 * @see IPainter#deactivate(bool) | |
192 */ | |
193 public void deactivate(bool redraw) { | |
194 if (fIsActive) { | |
195 fIsActive= false; | |
196 | |
197 /* on turning off the feature one has to paint the currently | |
198 * highlighted line with the standard background color | |
199 */ | |
200 if (redraw) | |
201 drawHighlightLine(fCurrentLine); | |
202 | |
203 fViewer.getTextWidget().removeLineBackgroundListener(this); | |
204 | |
205 if (fPositionManager !is null) | |
206 fPositionManager.unmanagePosition(fCurrentLine); | |
207 | |
208 fLastLineNumber= -1; | |
209 fCurrentLine.offset= 0; | |
210 fCurrentLine.length= 0; | |
211 } | |
212 } | |
213 | |
214 /* | |
215 * @see IPainter#dispose() | |
216 */ | |
217 public void dispose() { | |
218 } | |
219 | |
220 /* | |
221 * @see IPainter#paint(int) | |
222 */ | |
223 public void paint(int reason) { | |
224 if (fViewer.getDocument() is null) { | |
225 deactivate(false); | |
226 return; | |
227 } | |
228 | |
229 StyledText textWidget= fViewer.getTextWidget(); | |
230 | |
231 // check selection | |
232 Point selection= textWidget.getSelection(); | |
233 int startLine= textWidget.getLineAtOffset(selection.x); | |
234 int endLine= textWidget.getLineAtOffset(selection.y); | |
235 if (startLine !is endLine) { | |
236 deactivate(true); | |
237 return; | |
238 } | |
239 | |
240 // initialization | |
241 if (!fIsActive) { | |
242 textWidget.addLineBackgroundListener(this); | |
243 fPositionManager.managePosition(fCurrentLine); | |
244 fIsActive= true; | |
245 } | |
246 | |
247 //redraw line highlight only if it hasn't been drawn yet on the respective line | |
248 if (updateHighlightLine()) { | |
249 // clear last line | |
250 drawHighlightLine(fLastLine); | |
251 // draw new line | |
252 drawHighlightLine(fCurrentLine); | |
253 } | |
254 } | |
255 | |
256 /* | |
257 * @see IPainter#setPositionManager(IPaintPositionManager) | |
258 */ | |
259 public void setPositionManager(IPaintPositionManager manager) { | |
260 fPositionManager = manager; | |
261 } | |
262 } |