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