root/dwt/dnd/DropTargetEffect.d

Revision 246:fd9c62a2998e, 6.8 kB (checked in by Frank Benoit <benoit@tionex.de>, 6 months ago)

Updater SWT 3.4M7 to 3.4

Line 
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 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.Table;
21 import dwt.widgets.TableItem;
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  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
58  *
59  * @since 3.3
60  */
61 public class DropTargetEffect : DropTargetAdapter {
62     Control control;
63
64     /**
65      * Creates a new <code>DropTargetEffect</code> to handle the drag under effect on the specified
66      * <code>Control</code>.
67      *
68      * @param control the <code>Control</code> over which the user positions the cursor to drop the data
69      *
70      * @exception IllegalArgumentException <ul>
71      *    <li>ERROR_NULL_ARGUMENT - if the control is null</li>
72      * </ul>
73      */
74     public this(Control control) {
75         if (control is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
76         this.control = control;
77     }
78
79     /**
80      * Returns the Control which is registered for this DropTargetEffect.  This is the control over which the
81      * user positions the cursor to drop the data.
82      *
83      * @return the Control which is registered for this DropTargetEffect
84      */
85     public Control getControl() {
86         return control;
87     }
88
89     /**
90      * Returns the item at the given x-y coordinate in the receiver
91      * or null if no such item exists. The x-y coordinate is in the
92      * display relative coordinates.
93      *
94      * @param x the x coordinate used to locate the item
95      * @param y the y coordinate used to locate the item
96      * @return the item at the given x-y coordinate, or null if the coordinate is not in a selectable item
97      */
98     public Widget getItem(int x, int y) {
99         if ( auto table = cast(Table)control ) {
100             return getItem(table, x, y);
101         }
102         if ( auto tree = cast(Tree)control ) {
103             return getItem(tree, x, y);
104         }
105         return null;
106     }
107
108     Widget getItem(Table table, int x, int y) {
109         Point coordinates = new Point(x, y);
110         coordinates = table.toControl(coordinates);
111         TableItem item = table.getItem(coordinates);
112         if (item !is null) return item;
113         Rectangle area = table.getClientArea();
114         int tableBottom = area.y + area.height;
115         int itemCount = table.getItemCount();
116         for (int i=table.getTopIndex(); i<itemCount; i++) {
117             item = table.getItem(i);
118             Rectangle rect = item.getBounds();
119             rect.x = area.x;
120             rect.width = area.width;
121             if (rect.contains(coordinates)) return item;
122             if (rect.y > tableBottom) break;
123         }
124         return null;
125     }
126
127     Widget getItem(Tree tree, int x, int y) {
128         Point point = new Point(x, y);
129         point = tree.toControl(point);
130         TreeItem item = tree.getItem(point);
131         if (item is null) {
132             Rectangle area = tree.getClientArea();
133             if (area.contains(point)) {
134                 int treeBottom = area.y + area.height;
135                 item = tree.getTopItem();
136                 while (item !is null) {
137                     Rectangle rect = item.getBounds();
138                     int itemBottom = rect.y + rect.height;
139                     if (rect.y <= point.y && point.y < itemBottom) return item;
140                     if (itemBottom > treeBottom) break;
141                     item = nextItem(tree, item);
142                 }
143                 return null;
144             }
145         }
146         return item;
147     }
148
149     TreeItem nextItem(Tree tree, TreeItem item) {
150         if (item is null) return null;
151         if (item.getExpanded() && item.getItemCount() > 0) return item.getItem(0);
152         TreeItem childItem = item;
153         TreeItem parentItem = childItem.getParentItem();
154         int index = parentItem is null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
155         int count = parentItem is null ? tree.getItemCount() : parentItem.getItemCount();
156         while (true) {
157             if (index + 1 < count) return parentItem is null ? tree.getItem(index + 1) : parentItem.getItem(index + 1);
158             if (parentItem is null) return null;
159             childItem = parentItem;
160             parentItem = childItem.getParentItem();
161             index = parentItem is null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
162             count = parentItem is null ? tree.getItemCount() : parentItem.getItemCount();
163         }
164     }
165
166     TreeItem previousItem(Tree tree, TreeItem item) {
167         if (item is null) return null;
168         TreeItem childItem = item;
169         TreeItem parentItem = childItem.getParentItem();
170         int index = parentItem is null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
171         if (index is 0) return parentItem;
172         TreeItem nextItem = parentItem is null ? tree.getItem(index-1) : parentItem.getItem(index-1);
173         int count = nextItem.getItemCount();
174         while (count > 0 && nextItem.getExpanded()) {
175             nextItem = nextItem.getItem(count - 1);
176             count = nextItem.getItemCount();
177         }
178         return nextItem;
179     }
180 }
Note: See TracBrowser for help on using the browser.