Mercurial > projects > dwt-mac
comparison dwt/custom/StyledTextDropTargetEffect.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 1a8b3cb347e0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 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 *******************************************************************************/ | |
11 module dwt.custom; | |
12 | |
13 import dwt.*; | |
14 import dwt.dnd.*; | |
15 import dwt.graphics.*; | |
16 import dwt.widgets.*; | |
17 | |
18 /** | |
19 * This adapter class provides a default drag under effect (eg. select and scroll) | |
20 * when a drag occurs over a <code>StyledText</code>. | |
21 * | |
22 * <p>Classes that wish to provide their own drag under effect for a <code>StyledText</code> | |
23 * can extend this class, override the <code>StyledTextDropTargetEffect.dragOver</code> | |
24 * method and override any other applicable methods in <code>StyledTextDropTargetEffect</code> to | |
25 * display their own drag under effect.</p> | |
26 * | |
27 * Subclasses that override any methods of this class should call the corresponding | |
28 * <code>super</code> method to get the default drag under effect implementation. | |
29 * | |
30 * <p>The feedback value is either one of the FEEDBACK constants defined in | |
31 * class <code>DND</code> which is applicable to instances of this class, | |
32 * or it must be built by <em>bitwise OR</em>'ing together | |
33 * (that is, using the <code>int</code> "|" operator) two or more | |
34 * of those <code>DND</code> effect constants. | |
35 * </p> | |
36 * <p> | |
37 * <dl> | |
38 * <dt><b>Feedback:</b></dt> | |
39 * <dd>FEEDBACK_SELECT, FEEDBACK_SCROLL</dd> | |
40 * </dl> | |
41 * </p> | |
42 * | |
43 * @see DropTargetAdapter | |
44 * @see DropTargetEvent | |
45 * | |
46 * @since 3.3 | |
47 */ | |
48 public class StyledTextDropTargetEffect : DropTargetEffect { | |
49 static final int CARET_WIDTH = 2; | |
50 static final int SCROLL_HYSTERESIS = 100; // milli seconds | |
51 static final int SCROLL_TOLERANCE = 20; // pixels | |
52 | |
53 int currentOffset = -1; | |
54 long scrollBeginTime; | |
55 int scrollX = -1, scrollY = -1; | |
56 Listener paintListener; | |
57 | |
58 /** | |
59 * Creates a new <code>StyledTextDropTargetEffect</code> to handle the drag under effect on the specified | |
60 * <code>StyledText</code>. | |
61 * | |
62 * @param styledText the <code>StyledText</code> over which the user positions the cursor to drop the data | |
63 */ | |
64 public StyledTextDropTargetEffect(StyledText styledText) { | |
65 super(styledText); | |
66 paintListener = new Listener () { | |
67 public void handleEvent (Event event) { | |
68 if (currentOffset !is -1) { | |
69 StyledText text = (StyledText) getControl(); | |
70 Point position = text.getLocationAtOffset(currentOffset); | |
71 int height = text.getLineHeight(currentOffset); | |
72 event.gc.setBackground(event.display.getSystemColor (DWT.COLOR_BLACK)); | |
73 event.gc.fillRectangle(position.x, position.y, CARET_WIDTH, height); | |
74 } | |
75 } | |
76 }; | |
77 } | |
78 | |
79 /** | |
80 * This implementation of <code>dragEnter</code> provides a default drag under effect | |
81 * for the feedback specified in <code>event.feedback</code>. | |
82 * | |
83 * For additional information see <code>DropTargetAdapter.dragEnter</code>. | |
84 * | |
85 * Subclasses that override this method should call <code>super.dragEnter(event)</code> | |
86 * to get the default drag under effect implementation. | |
87 * | |
88 * @param event the information associated with the drag start event | |
89 * | |
90 * @see DropTargetAdapter | |
91 * @see DropTargetEvent | |
92 */ | |
93 public void dragEnter(DropTargetEvent event) { | |
94 currentOffset = -1; | |
95 scrollBeginTime = 0; | |
96 scrollX = -1; | |
97 scrollY = -1; | |
98 getControl().removeListener(DWT.Paint, paintListener); | |
99 getControl().addListener (DWT.Paint, paintListener); | |
100 } | |
101 | |
102 /** | |
103 * This implementation of <code>dragLeave</code> provides a default drag under effect | |
104 * for the feedback specified in <code>event.feedback</code>. | |
105 * | |
106 * For additional information see <code>DropTargetAdapter.dragLeave</code>. | |
107 * | |
108 * Subclasses that override this method should call <code>super.dragLeave(event)</code> | |
109 * to get the default drag under effect implementation. | |
110 * | |
111 * @param event the information associated with the drag leave event | |
112 * | |
113 * @see DropTargetAdapter | |
114 * @see DropTargetEvent | |
115 */ | |
116 public void dragLeave(DropTargetEvent event) { | |
117 StyledText text = (StyledText) getControl(); | |
118 if (currentOffset !is -1) { | |
119 refreshCaret(text, currentOffset, -1); | |
120 } | |
121 text.removeListener(DWT.Paint, paintListener); | |
122 scrollBeginTime = 0; | |
123 scrollX = -1; | |
124 scrollY = -1; | |
125 } | |
126 | |
127 /** | |
128 * This implementation of <code>dragOver</code> provides a default drag under effect | |
129 * for the feedback specified in <code>event.feedback</code>. | |
130 * | |
131 * For additional information see <code>DropTargetAdapter.dragOver</code>. | |
132 * | |
133 * Subclasses that override this method should call <code>super.dragOver(event)</code> | |
134 * to get the default drag under effect implementation. | |
135 * | |
136 * @param event the information associated with the drag over event | |
137 * | |
138 * @see DropTargetAdapter | |
139 * @see DropTargetEvent | |
140 * @see DND#FEEDBACK_SELECT | |
141 * @see DND#FEEDBACK_SCROLL | |
142 */ | |
143 public void dragOver(DropTargetEvent event) { | |
144 int effect = event.feedback; | |
145 StyledText text = (StyledText) getControl(); | |
146 | |
147 Point pt = text.getDisplay().map(null, text, event.x, event.y); | |
148 if ((effect & DND.FEEDBACK_SCROLL) is 0) { | |
149 scrollBeginTime = 0; | |
150 scrollX = scrollY = -1; | |
151 } else { | |
152 if (text.getCharCount() is 0) { | |
153 scrollBeginTime = 0; | |
154 scrollX = scrollY = -1; | |
155 } else { | |
156 if (scrollX !is -1 && scrollY !is -1 && scrollBeginTime !is 0 && | |
157 (pt.x >= scrollX && pt.x <= (scrollX + SCROLL_TOLERANCE) || | |
158 pt.y >= scrollY && pt.y <= (scrollY + SCROLL_TOLERANCE))) { | |
159 if (System.currentTimeMillis() >= scrollBeginTime) { | |
160 Rectangle area = text.getClientArea(); | |
161 GC gc = new GC(text); | |
162 FontMetrics fm = gc.getFontMetrics(); | |
163 gc.dispose(); | |
164 int charWidth = fm.getAverageCharWidth(); | |
165 int scrollAmount = 10*charWidth; | |
166 if (pt.x < area.x + 3*charWidth) { | |
167 int leftPixel = text.getHorizontalPixel(); | |
168 text.setHorizontalPixel(leftPixel - scrollAmount); | |
169 } | |
170 if (pt.x > area.width - 3*charWidth) { | |
171 int leftPixel = text.getHorizontalPixel(); | |
172 text.setHorizontalPixel(leftPixel + scrollAmount); | |
173 } | |
174 int lineHeight = text.getLineHeight(); | |
175 if (pt.y < area.y + lineHeight) { | |
176 int topPixel = text.getTopPixel(); | |
177 text.setTopPixel(topPixel - lineHeight); | |
178 } | |
179 if (pt.y > area.height - lineHeight) { | |
180 int topPixel = text.getTopPixel(); | |
181 text.setTopPixel(topPixel + lineHeight); | |
182 } | |
183 scrollBeginTime = 0; | |
184 scrollX = scrollY = -1; | |
185 } | |
186 } else { | |
187 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS; | |
188 scrollX = pt.x; | |
189 scrollY = pt.y; | |
190 } | |
191 } | |
192 } | |
193 | |
194 if ((effect & DND.FEEDBACK_SELECT) !is 0) { | |
195 int[] trailing = new int [1]; | |
196 int newOffset = text.getOffsetAtPoint(pt.x, pt.y, trailing, false); | |
197 newOffset += trailing [0]; | |
198 if (newOffset !is currentOffset) { | |
199 refreshCaret(text, currentOffset, newOffset); | |
200 currentOffset = newOffset; | |
201 } | |
202 } | |
203 } | |
204 | |
205 void refreshCaret(StyledText text, int oldOffset, int newOffset) { | |
206 if (oldOffset !is newOffset) { | |
207 if (oldOffset !is -1) { | |
208 Point oldPos = text.getLocationAtOffset(oldOffset); | |
209 int oldHeight = text.getLineHeight(oldOffset); | |
210 text.redraw (oldPos.x, oldPos.y, CARET_WIDTH, oldHeight, false); | |
211 } | |
212 if (newOffset !is -1) { | |
213 Point newPos = text.getLocationAtOffset(newOffset); | |
214 int newHeight = text.getLineHeight(newOffset); | |
215 text.redraw (newPos.x, newPos.y, CARET_WIDTH, newHeight, false); | |
216 } | |
217 } | |
218 } | |
219 | |
220 /** | |
221 * This implementation of <code>dropAccept</code> provides a default drag under effect | |
222 * for the feedback specified in <code>event.feedback</code>. | |
223 * | |
224 * For additional information see <code>DropTargetAdapter.dropAccept</code>. | |
225 * | |
226 * Subclasses that override this method should call <code>super.dropAccept(event)</code> | |
227 * to get the default drag under effect implementation. | |
228 * | |
229 * @param event the information associated with the drop accept event | |
230 * | |
231 * @see DropTargetAdapter | |
232 * @see DropTargetEvent | |
233 */ | |
234 public void dropAccept(DropTargetEvent event) { | |
235 if (currentOffset !is -1) { | |
236 StyledText text = (StyledText) getControl(); | |
237 text.setSelection(currentOffset); | |
238 currentOffset = -1; | |
239 } | |
240 } | |
241 } |