Mercurial > projects > dwt-win
comparison dwt/dnd/DropTargetEffect.d @ 135:242e33c0e383
Added dnd source, ByteArrayTransfer,Clipboard completed
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 13 Feb 2008 04:51:22 +0100 |
parents | |
children | 36f5cb12e1a2 |
comparison
equal
deleted
inserted
replaced
134:fa7d7d66b9ed | 135:242e33c0e383 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 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.dnd.DropTargetEffect; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.graphics.Point; | |
18 import dwt.graphics.Rectangle; | |
19 import dwt.widgets.Control; | |
20 import dwt.widgets.Item; | |
21 import dwt.widgets.Table; | |
22 import dwt.widgets.Tree; | |
23 import dwt.widgets.TreeItem; | |
24 import dwt.widgets.Widget; | |
25 import dwt.dnd.DropTargetAdapter; | |
26 | |
27 | |
28 /** | |
29 * This class provides a default drag under effect during a drag and drop. | |
30 * The current implementation does not provide any visual feedback. | |
31 * | |
32 * <p>The drop target effect has the same API as the | |
33 * <code>DropTargetAdapter</code> so that it can provide custom visual | |
34 * feedback when a <code>DropTargetEvent</code> occurs. | |
35 * </p> | |
36 * | |
37 * <p>Classes that wish to provide their own drag under effect | |
38 * can extend the <code>DropTargetEffect</code> and override any applicable methods | |
39 * in <code>DropTargetAdapter</code> to display their own drag under effect.</p> | |
40 * | |
41 * <p>The feedback value is either one of the FEEDBACK constants defined in | |
42 * class <code>DND</code> which is applicable to instances of this class, | |
43 * or it must be built by <em>bitwise OR</em>'ing together | |
44 * (that is, using the <code>int</code> "|" operator) two or more | |
45 * of those <code>DND</code> effect constants. | |
46 * </p> | |
47 * <p> | |
48 * <dl> | |
49 * <dt><b>Feedback:</b></dt> | |
50 * <dd>FEEDBACK_EXPAND, FEEDBACK_INSERT_AFTER, FEEDBACK_INSERT_BEFORE, | |
51 * FEEDBACK_NONE, FEEDBACK_SELECT, FEEDBACK_SCROLL</dd> | |
52 * </dl> | |
53 * </p> | |
54 * | |
55 * @see DropTargetAdapter | |
56 * @see DropTargetEvent | |
57 * | |
58 * @since 3.3 | |
59 */ | |
60 public class DropTargetEffect : DropTargetAdapter { | |
61 Control control; | |
62 | |
63 /** | |
64 * Creates a new <code>DropTargetEffect</code> to handle the drag under effect on the specified | |
65 * <code>Control</code>. | |
66 * | |
67 * @param control the <code>Control</code> over which the user positions the cursor to drop the data | |
68 * | |
69 * @exception IllegalArgumentException <ul> | |
70 * <li>ERROR_NULL_ARGUMENT - if the control is null</li> | |
71 * </ul> | |
72 */ | |
73 public this(Control control) { | |
74 if (control is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
75 this.control = control; | |
76 } | |
77 | |
78 /** | |
79 * Returns the Control which is registered for this DropTargetEffect. This is the control over which the | |
80 * user positions the cursor to drop the data. | |
81 * | |
82 * @return the Control which is registered for this DropTargetEffect | |
83 */ | |
84 public Control getControl() { | |
85 return control; | |
86 } | |
87 | |
88 /** | |
89 * Returns the item at the given x-y coordinate in the receiver | |
90 * or null if no such item exists. The x-y coordinate is in the | |
91 * display relative coordinates. | |
92 * | |
93 * @param x the x coordinate used to locate the item | |
94 * @param y the y coordinate used to locate the item | |
95 * @return the item at the given x-y coordinate, or null if the coordinate is not in a selectable item | |
96 */ | |
97 public Widget getItem(int x, int y) { | |
98 if ( auto table = cast(Table)control ) { | |
99 return getItem(table, x, y); | |
100 } | |
101 if ( auto tree = cast(Tree)control ) { | |
102 return getItem(tree, x, y); | |
103 } | |
104 return null; | |
105 } | |
106 | |
107 Widget getItem(Table table, int x, int y) { | |
108 Point coordinates = new Point(x, y); | |
109 coordinates = table.toControl(coordinates); | |
110 Item item = table.getItem(coordinates); | |
111 if (item is null) { | |
112 Rectangle area = table.getClientArea(); | |
113 if (area.contains(coordinates)) { | |
114 // Scan across the width of the table. | |
115 for (int x1 = area.x; x1 < area.x + area.width; x1++) { | |
116 Point pt = new Point(x1, coordinates.y); | |
117 item = table.getItem(pt); | |
118 if (item !is null) { | |
119 break; | |
120 } | |
121 } | |
122 } | |
123 } | |
124 return item; | |
125 } | |
126 | |
127 Widget getItem(Tree tree, int x, int y) { | |
128 Point coordinates = new Point(x, y); | |
129 coordinates = tree.toControl(coordinates); | |
130 Item item = tree.getItem(coordinates); | |
131 if (item is null) { | |
132 Rectangle area = tree.getClientArea(); | |
133 if (area.contains(coordinates)) { | |
134 // Scan across the width of the tree. | |
135 for (int x1 = area.x; x1 < area.x + area.width; x1++) { | |
136 Point pt = new Point(x1, coordinates.y); | |
137 item = tree.getItem(pt); | |
138 if (item !is null) { | |
139 break; | |
140 } | |
141 } | |
142 } | |
143 } | |
144 return item; | |
145 } | |
146 | |
147 TreeItem nextItem(Tree tree, TreeItem item) { | |
148 if (item is null) return null; | |
149 if (item.getExpanded()) return item.getItem(0); | |
150 TreeItem childItem = item; | |
151 TreeItem parentItem = childItem.getParentItem(); | |
152 int index = parentItem is null ? tree.indexOf(childItem) : parentItem.indexOf(childItem); | |
153 int count = parentItem is null ? tree.getItemCount() : parentItem.getItemCount(); | |
154 while (true) { | |
155 if (index + 1 < count) return parentItem is null ? tree.getItem(index + 1) : parentItem.getItem(index + 1); | |
156 if (parentItem is null) return null; | |
157 childItem = parentItem; | |
158 parentItem = childItem.getParentItem(); | |
159 index = parentItem is null ? tree.indexOf(childItem) : parentItem.indexOf(childItem); | |
160 count = parentItem is null ? tree.getItemCount() : parentItem.getItemCount(); | |
161 } | |
162 } | |
163 | |
164 TreeItem previousItem(Tree tree, TreeItem item) { | |
165 if (item is null) return null; | |
166 TreeItem childItem = item; | |
167 TreeItem parentItem = childItem.getParentItem(); | |
168 int index = parentItem is null ? tree.indexOf(childItem) : parentItem.indexOf(childItem); | |
169 if (index is 0) return parentItem; | |
170 TreeItem nextItem = parentItem is null ? tree.getItem(index-1) : parentItem.getItem(index-1); | |
171 int count = nextItem.getItemCount(); | |
172 while (count > 0 && nextItem.getExpanded()) { | |
173 nextItem = nextItem.getItem(count - 1); | |
174 count = nextItem.getItemCount(); | |
175 } | |
176 return nextItem; | |
177 } | |
178 } |