Mercurial > projects > dwt-win
comparison dwt/custom/StyledTextDropTargetEffect.d @ 213:36f5cb12e1a2
Update to SWT 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 17 May 2008 17:34:28 +0200 |
parents | a5afe31f5cdd |
children | fd9c62a2998e |
comparison
equal
deleted
inserted
replaced
212:ab60f3309436 | 213:36f5cb12e1a2 |
---|---|
16 import dwt.DWT; | 16 import dwt.DWT; |
17 import dwt.dnd.DND; | 17 import dwt.dnd.DND; |
18 import dwt.dnd.DropTargetAdapter; | 18 import dwt.dnd.DropTargetAdapter; |
19 import dwt.dnd.DropTargetEffect; | 19 import dwt.dnd.DropTargetEffect; |
20 import dwt.dnd.DropTargetEvent; | 20 import dwt.dnd.DropTargetEvent; |
21 import dwt.graphics.FontMetrics; | |
22 import dwt.graphics.GC; | |
21 import dwt.graphics.Point; | 23 import dwt.graphics.Point; |
22 import dwt.graphics.Rectangle; | 24 import dwt.graphics.Rectangle; |
23 import dwt.widgets.Event; | 25 import dwt.widgets.Event; |
24 import dwt.widgets.Listener; | 26 import dwt.widgets.Listener; |
25 import dwt.custom.StyledText; | 27 import dwt.custom.StyledText; |
29 import Math = tango.math.Math; | 31 import Math = tango.math.Math; |
30 import dwt.dwthelper.utils; | 32 import dwt.dwthelper.utils; |
31 | 33 |
32 /** | 34 /** |
33 * This adapter class provides a default drag under effect (eg. select and scroll) | 35 * This adapter class provides a default drag under effect (eg. select and scroll) |
34 * when a drag occurs over a <code>Table</code>. | 36 * when a drag occurs over a <code>StyledText</code>. |
35 * | 37 * |
36 * <p>Classes that wish to provide their own drag under effect for a <code>StyledText</code> | 38 * <p>Classes that wish to provide their own drag under effect for a <code>StyledText</code> |
37 * can extend this class, override the <code>StyledTextDropTargetEffect.dragOver</code> | 39 * can extend this class, override the <code>StyledTextDropTargetEffect.dragOver</code> |
38 * method and override any other applicable methods in <code>StyledTextDropTargetEffect</code> to | 40 * method and override any other applicable methods in <code>StyledTextDropTargetEffect</code> to |
39 * display their own drag under effect.</p> | 41 * display their own drag under effect.</p> |
170 if (scrollX !is -1 && scrollY !is -1 && scrollBeginTime !is 0 && | 172 if (scrollX !is -1 && scrollY !is -1 && scrollBeginTime !is 0 && |
171 (pt.x >= scrollX && pt.x <= (scrollX + SCROLL_TOLERANCE) || | 173 (pt.x >= scrollX && pt.x <= (scrollX + SCROLL_TOLERANCE) || |
172 pt.y >= scrollY && pt.y <= (scrollY + SCROLL_TOLERANCE))) { | 174 pt.y >= scrollY && pt.y <= (scrollY + SCROLL_TOLERANCE))) { |
173 if (System.currentTimeMillis() >= scrollBeginTime) { | 175 if (System.currentTimeMillis() >= scrollBeginTime) { |
174 Rectangle area = text.getClientArea(); | 176 Rectangle area = text.getClientArea(); |
175 Rectangle bounds = text.getTextBounds(0, 0); | 177 GC gc = new GC(text); |
176 int charWidth = bounds.width; | 178 FontMetrics fm = gc.getFontMetrics(); |
179 gc.dispose(); | |
180 int charWidth = fm.getAverageCharWidth(); | |
177 int scrollAmount = 10*charWidth; | 181 int scrollAmount = 10*charWidth; |
178 if (pt.x < area.x + 3*charWidth) { | 182 if (pt.x < area.x + 3*charWidth) { |
179 int leftPixel = text.getHorizontalPixel(); | 183 int leftPixel = text.getHorizontalPixel(); |
180 text.setHorizontalPixel(leftPixel - scrollAmount); | 184 text.setHorizontalPixel(leftPixel - scrollAmount); |
181 if (text.getHorizontalPixel() !is leftPixel) { | |
182 text.redraw(); | |
183 } | |
184 } | 185 } |
185 if (pt.x > area.width - 3*charWidth) { | 186 if (pt.x > area.width - 3*charWidth) { |
186 int leftPixel = text.getHorizontalPixel(); | 187 int leftPixel = text.getHorizontalPixel(); |
187 text.setHorizontalPixel(leftPixel + scrollAmount); | 188 text.setHorizontalPixel(leftPixel + scrollAmount); |
188 if (text.getHorizontalPixel() !is leftPixel) { | 189 } |
189 text.redraw(); | 190 int lineHeight = text.getLineHeight(); |
190 } | |
191 } | |
192 int lineHeight = bounds.height; | |
193 if (pt.y < area.y + lineHeight) { | 191 if (pt.y < area.y + lineHeight) { |
194 int topPixel = text.getTopPixel(); | 192 int topPixel = text.getTopPixel(); |
195 text.setTopPixel(topPixel - lineHeight); | 193 text.setTopPixel(topPixel - lineHeight); |
196 if (text.getTopPixel() !is topPixel) { | |
197 text.redraw(); | |
198 } | |
199 } | 194 } |
200 if (pt.y > area.height - lineHeight) { | 195 if (pt.y > area.height - lineHeight) { |
201 int topPixel = text.getTopPixel(); | 196 int topPixel = text.getTopPixel(); |
202 text.setTopPixel(topPixel + lineHeight); | 197 text.setTopPixel(topPixel + lineHeight); |
203 if (text.getTopPixel() !is topPixel) { | |
204 text.redraw(); | |
205 } | |
206 } | 198 } |
207 scrollBeginTime = 0; | 199 scrollBeginTime = 0; |
208 scrollX = scrollY = -1; | 200 scrollX = scrollY = -1; |
209 } | 201 } |
210 } else { | 202 } else { |
214 } | 206 } |
215 } | 207 } |
216 } | 208 } |
217 | 209 |
218 if ((effect & DND.FEEDBACK_SELECT) !is 0) { | 210 if ((effect & DND.FEEDBACK_SELECT) !is 0) { |
219 StyledTextContent content = text.getContent(); | 211 int[] trailing = new int [1]; |
220 int newOffset = -1; | 212 int newOffset = text.getOffsetAtPoint(pt.x, pt.y, trailing, false); |
221 try { | 213 newOffset += trailing [0]; |
222 newOffset = text.getOffsetAtLocation(pt); | 214 if (newOffset !is currentOffset) { |
223 } catch ( tango.core.Exception.IllegalArgumentException ex1) { | |
224 int maxOffset = content.getCharCount(); | |
225 Point maxLocation = text.getLocationAtOffset(maxOffset); | |
226 if (pt.y >= maxLocation.y) { | |
227 try { | |
228 newOffset = text.getOffsetAtLocation(new Point(pt.x, maxLocation.y)); | |
229 } catch (tango.core.Exception.IllegalArgumentException ex2) { | |
230 newOffset = maxOffset; | |
231 } | |
232 } else { | |
233 try { | |
234 int startOffset = text.getOffsetAtLocation(new Point(0, pt.y)); | |
235 int endOffset = maxOffset; | |
236 int line = content.getLineAtOffset(startOffset); | |
237 int lineCount = content.getLineCount(); | |
238 if (line + 1 < lineCount) { | |
239 endOffset = content.getOffsetAtLine(line + 1) - 1; | |
240 } | |
241 int lineHeight = text.getLineHeight(startOffset); | |
242 for (int i = endOffset; i >= startOffset; i--) { | |
243 Point p = text.getLocationAtOffset(i); | |
244 if (p.x < pt.x && p.y < pt.y && p.y + lineHeight > pt.y) { | |
245 newOffset = i; | |
246 break; | |
247 } | |
248 } | |
249 } catch (tango.core.Exception.IllegalArgumentException ex2) { | |
250 newOffset = -1; | |
251 } | |
252 } | |
253 } | |
254 if (newOffset !is -1 && newOffset !is currentOffset) { | |
255 // check if offset is line delimiter | |
256 // see StyledText.isLineDelimiter() | |
257 int line = content.getLineAtOffset(newOffset); | |
258 int lineOffset = content.getOffsetAtLine(line); | |
259 int offsetInLine = newOffset - lineOffset; | |
260 // offsetInLine will be greater than line length if the line | |
261 // delimiter is longer than one character and the offset is set | |
262 // in between parts of the line delimiter. | |
263 if (offsetInLine > content.getLine(line).length) { | |
264 newOffset = Math.max(0, newOffset - 1); | |
265 } | |
266 refreshCaret(text, currentOffset, newOffset); | 215 refreshCaret(text, currentOffset, newOffset); |
267 currentOffset = newOffset; | 216 currentOffset = newOffset; |
268 } | 217 } |
269 } | 218 } |
270 } | 219 } |