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