Mercurial > projects > dwt-mac
comparison dwt/custom/StyledTextDropTargetEffect.d @ 41:6337764516f1
Sync dwt/custom with dwt-linux (took copy of complete folder)
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 07 Oct 2008 16:29:55 +0200 |
parents | e831403a80a9 |
children |
comparison
equal
deleted
inserted
replaced
40:fbe68c33eeee | 41:6337764516f1 |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
10 *******************************************************************************/ | 12 *******************************************************************************/ |
11 module dwt.custom; | 13 module dwt.custom.StyledTextDropTargetEffect; |
12 | 14 |
13 import dwt.*; | 15 |
14 import dwt.dnd.*; | 16 import dwt.DWT; |
15 import dwt.graphics.*; | 17 import dwt.dnd.DND; |
16 import dwt.widgets.*; | 18 import dwt.dnd.DropTargetAdapter; |
19 import dwt.dnd.DropTargetEffect; | |
20 import dwt.dnd.DropTargetEvent; | |
21 import dwt.graphics.FontMetrics; | |
22 import dwt.graphics.GC; | |
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 dwt.dwthelper.utils; | |
17 | 32 |
18 /** | 33 /** |
19 * This adapter class provides a default drag under effect (eg. select and scroll) | 34 * This adapter class provides a default drag under effect (eg. select and scroll) |
20 * when a drag occurs over a <code>StyledText</code>. | 35 * when a drag occurs over a <code>StyledText</code>. |
21 * | 36 * |
22 * <p>Classes that wish to provide their own drag under effect for a <code>StyledText</code> | 37 * <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> | 38 * can extend this class, override the <code>StyledTextDropTargetEffect.dragOver</code> |
24 * method and override any other applicable methods in <code>StyledTextDropTargetEffect</code> to | 39 * method and override any other applicable methods in <code>StyledTextDropTargetEffect</code> to |
25 * display their own drag under effect.</p> | 40 * display their own drag under effect.</p> |
26 * | 41 * |
27 * Subclasses that override any methods of this class should call the corresponding | 42 * 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. | 43 * <code>super</code> method to get the default drag under effect implementation. |
29 * | 44 * |
30 * <p>The feedback value is either one of the FEEDBACK constants defined in | 45 * <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, | 46 * 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 | 47 * or it must be built by <em>bitwise OR</em>'ing together |
33 * (that is, using the <code>int</code> "|" operator) two or more | 48 * (that is, using the <code>int</code> "|" operator) two or more |
34 * of those <code>DND</code> effect constants. | 49 * of those <code>DND</code> effect constants. |
35 * </p> | 50 * </p> |
36 * <p> | 51 * <p> |
37 * <dl> | 52 * <dl> |
38 * <dt><b>Feedback:</b></dt> | 53 * <dt><b>Feedback:</b></dt> |
39 * <dd>FEEDBACK_SELECT, FEEDBACK_SCROLL</dd> | 54 * <dd>FEEDBACK_SELECT, FEEDBACK_SCROLL</dd> |
40 * </dl> | 55 * </dl> |
41 * </p> | 56 * </p> |
42 * | 57 * |
43 * @see DropTargetAdapter | 58 * @see DropTargetAdapter |
44 * @see DropTargetEvent | 59 * @see DropTargetEvent |
45 * | 60 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> |
61 * | |
46 * @since 3.3 | 62 * @since 3.3 |
47 */ | 63 */ |
48 public class StyledTextDropTargetEffect : DropTargetEffect { | 64 public class StyledTextDropTargetEffect : DropTargetEffect { |
49 static final int CARET_WIDTH = 2; | 65 static final int CARET_WIDTH = 2; |
50 static final int SCROLL_HYSTERESIS = 100; // milli seconds | 66 static final int SCROLL_HYSTERESIS = 100; // milli seconds |
51 static final int SCROLL_TOLERANCE = 20; // pixels | 67 static final int SCROLL_TOLERANCE = 20; // pixels |
52 | 68 |
53 int currentOffset = -1; | 69 int currentOffset = -1; |
54 long scrollBeginTime; | 70 long scrollBeginTime; |
55 int scrollX = -1, scrollY = -1; | 71 int scrollX = -1, scrollY = -1; |
56 Listener paintListener; | 72 Listener paintListener; |
57 | 73 |
58 /** | 74 /** |
59 * Creates a new <code>StyledTextDropTargetEffect</code> to handle the drag under effect on the specified | 75 * Creates a new <code>StyledTextDropTargetEffect</code> to handle the drag under effect on the specified |
60 * <code>StyledText</code>. | 76 * <code>StyledText</code>. |
61 * | 77 * |
62 * @param styledText the <code>StyledText</code> over which the user positions the cursor to drop the data | 78 * @param styledText the <code>StyledText</code> over which the user positions the cursor to drop the data |
63 */ | 79 */ |
64 public this(StyledText styledText) { | 80 public this(StyledText styledText) { |
65 super(styledText); | 81 super(styledText); |
66 paintListener = new Listener () { | 82 paintListener = new class() Listener { |
67 public void handleEvent (Event event) { | 83 public void handleEvent (Event event) { |
68 if (currentOffset !is -1) { | 84 if (currentOffset !is -1) { |
69 StyledText text = cast(StyledText) getControl(); | 85 StyledText text = cast(StyledText) getControl(); |
70 Point position = text.getLocationAtOffset(currentOffset); | 86 Point position = text.getLocationAtOffset(currentOffset); |
71 int height = text.getLineHeight(currentOffset); | 87 int height = text.getLineHeight(currentOffset); |
73 event.gc.fillRectangle(position.x, position.y, CARET_WIDTH, height); | 89 event.gc.fillRectangle(position.x, position.y, CARET_WIDTH, height); |
74 } | 90 } |
75 } | 91 } |
76 }; | 92 }; |
77 } | 93 } |
78 | 94 |
79 /** | 95 /** |
80 * This implementation of <code>dragEnter</code> provides a default drag under effect | 96 * This implementation of <code>dragEnter</code> provides a default drag under effect |
81 * for the feedback specified in <code>event.feedback</code>. | 97 * for the feedback specified in <code>event.feedback</code>. |
82 * | 98 * |
83 * For additional information see <code>DropTargetAdapter.dragEnter</code>. | 99 * For additional information see <code>DropTargetAdapter.dragEnter</code>. |
84 * | 100 * |
85 * Subclasses that override this method should call <code>super.dragEnter(event)</code> | 101 * Subclasses that override this method should call <code>super.dragEnter(event)</code> |
86 * to get the default drag under effect implementation. | 102 * to get the default drag under effect implementation. |
87 * | 103 * |
88 * @param event the information associated with the drag start event | 104 * @param event the information associated with the drag start event |
89 * | 105 * |
90 * @see DropTargetAdapter | 106 * @see DropTargetAdapter |
91 * @see DropTargetEvent | 107 * @see DropTargetEvent |
92 */ | 108 */ |
93 public void dragEnter(DropTargetEvent event) { | 109 public override void dragEnter(DropTargetEvent event) { |
94 currentOffset = -1; | 110 currentOffset = -1; |
95 scrollBeginTime = 0; | 111 scrollBeginTime = 0; |
96 scrollX = -1; | 112 scrollX = -1; |
97 scrollY = -1; | 113 scrollY = -1; |
98 getControl().removeListener(DWT.Paint, paintListener); | 114 getControl().removeListener(DWT.Paint, paintListener); |
99 getControl().addListener (DWT.Paint, paintListener); | 115 getControl().addListener (DWT.Paint, paintListener); |
100 } | 116 } |
101 | 117 |
102 /** | 118 /** |
103 * This implementation of <code>dragLeave</code> provides a default drag under effect | 119 * This implementation of <code>dragLeave</code> provides a default drag under effect |
104 * for the feedback specified in <code>event.feedback</code>. | 120 * for the feedback specified in <code>event.feedback</code>. |
105 * | 121 * |
106 * For additional information see <code>DropTargetAdapter.dragLeave</code>. | 122 * For additional information see <code>DropTargetAdapter.dragLeave</code>. |
107 * | 123 * |
108 * Subclasses that override this method should call <code>super.dragLeave(event)</code> | 124 * Subclasses that override this method should call <code>super.dragLeave(event)</code> |
109 * to get the default drag under effect implementation. | 125 * to get the default drag under effect implementation. |
110 * | 126 * |
111 * @param event the information associated with the drag leave event | 127 * @param event the information associated with the drag leave event |
112 * | 128 * |
113 * @see DropTargetAdapter | 129 * @see DropTargetAdapter |
114 * @see DropTargetEvent | 130 * @see DropTargetEvent |
115 */ | 131 */ |
116 public void dragLeave(DropTargetEvent event) { | 132 public override void dragLeave(DropTargetEvent event) { |
117 StyledText text = cast(StyledText) getControl(); | 133 StyledText text = cast(StyledText) getControl(); |
118 if (currentOffset !is -1) { | 134 if (currentOffset !is -1) { |
119 refreshCaret(text, currentOffset, -1); | 135 refreshCaret(text, currentOffset, -1); |
120 } | 136 } |
121 text.removeListener(DWT.Paint, paintListener); | 137 text.removeListener(DWT.Paint, paintListener); |
125 } | 141 } |
126 | 142 |
127 /** | 143 /** |
128 * This implementation of <code>dragOver</code> provides a default drag under effect | 144 * This implementation of <code>dragOver</code> provides a default drag under effect |
129 * for the feedback specified in <code>event.feedback</code>. | 145 * for the feedback specified in <code>event.feedback</code>. |
130 * | 146 * |
131 * For additional information see <code>DropTargetAdapter.dragOver</code>. | 147 * For additional information see <code>DropTargetAdapter.dragOver</code>. |
132 * | 148 * |
133 * Subclasses that override this method should call <code>super.dragOver(event)</code> | 149 * Subclasses that override this method should call <code>super.dragOver(event)</code> |
134 * to get the default drag under effect implementation. | 150 * to get the default drag under effect implementation. |
135 * | 151 * |
136 * @param event the information associated with the drag over event | 152 * @param event the information associated with the drag over event |
137 * | 153 * |
138 * @see DropTargetAdapter | 154 * @see DropTargetAdapter |
139 * @see DropTargetEvent | 155 * @see DropTargetEvent |
140 * @see DND#FEEDBACK_SELECT | 156 * @see DND#FEEDBACK_SELECT |
141 * @see DND#FEEDBACK_SCROLL | 157 * @see DND#FEEDBACK_SCROLL |
142 */ | 158 */ |
143 public void dragOver(DropTargetEvent event) { | 159 public override void dragOver(DropTargetEvent event) { |
144 int effect = event.feedback; | 160 int effect = event.feedback; |
145 StyledText text = cast(StyledText) getControl(); | 161 StyledText text = cast(StyledText) getControl(); |
146 | 162 |
147 Point pt = text.getDisplay().map(null, text, event.x, event.y); | 163 Point pt = text.getDisplay().map(null, text, event.x, event.y); |
148 if ((effect & DND.FEEDBACK_SCROLL) is 0) { | 164 if ((effect & DND.FEEDBACK_SCROLL) is 0) { |
149 scrollBeginTime = 0; | 165 scrollBeginTime = 0; |
150 scrollX = scrollY = -1; | 166 scrollX = scrollY = -1; |
151 } else { | 167 } else { |
188 scrollX = pt.x; | 204 scrollX = pt.x; |
189 scrollY = pt.y; | 205 scrollY = pt.y; |
190 } | 206 } |
191 } | 207 } |
192 } | 208 } |
193 | 209 |
194 if ((effect & DND.FEEDBACK_SELECT) !is 0) { | 210 if ((effect & DND.FEEDBACK_SELECT) !is 0) { |
195 int[] trailing = new int [1]; | 211 int[] trailing = new int [1]; |
196 int newOffset = text.getOffsetAtPoint(pt.x, pt.y, trailing, false); | 212 int newOffset = text.getOffsetAtPoint(pt.x, pt.y, trailing, false); |
197 newOffset += trailing [0]; | 213 newOffset += trailing [0]; |
198 if (newOffset !is currentOffset) { | 214 if (newOffset !is currentOffset) { |
218 } | 234 } |
219 | 235 |
220 /** | 236 /** |
221 * This implementation of <code>dropAccept</code> provides a default drag under effect | 237 * This implementation of <code>dropAccept</code> provides a default drag under effect |
222 * for the feedback specified in <code>event.feedback</code>. | 238 * for the feedback specified in <code>event.feedback</code>. |
223 * | 239 * |
224 * For additional information see <code>DropTargetAdapter.dropAccept</code>. | 240 * For additional information see <code>DropTargetAdapter.dropAccept</code>. |
225 * | 241 * |
226 * Subclasses that override this method should call <code>super.dropAccept(event)</code> | 242 * Subclasses that override this method should call <code>super.dropAccept(event)</code> |
227 * to get the default drag under effect implementation. | 243 * to get the default drag under effect implementation. |
228 * | 244 * |
229 * @param event the information associated with the drop accept event | 245 * @param event the information associated with the drop accept event |
230 * | 246 * |
231 * @see DropTargetAdapter | 247 * @see DropTargetAdapter |
232 * @see DropTargetEvent | 248 * @see DropTargetEvent |
233 */ | 249 */ |
234 public void dropAccept(DropTargetEvent event) { | 250 public override void dropAccept(DropTargetEvent event) { |
235 if (currentOffset !is -1) { | 251 if (currentOffset !is -1) { |
236 StyledText text = cast(StyledText) getControl(); | 252 StyledText text = cast(StyledText) getControl(); |
237 text.setSelection(currentOffset); | 253 text.setSelection(currentOffset); |
238 currentOffset = -1; | 254 currentOffset = -1; |
239 } | 255 } |