comparison dwt/dnd/TableDropTargetEffect.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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 *******************************************************************************/
11 module dwt.dnd;
12
13 import dwt.DWT;
14 import dwt.graphics.*;
15 import dwt.internal.Callback;
16 import dwt.internal.carbon.DataBrowserCallbacks;
17 import dwt.internal.carbon.OS;
18 import dwt.widgets.*;
19
20 /**
21 * This class provides a default drag under effect (eg. select, insert and scroll)
22 * when a drag occurs over a <code>Table</code>.
23 *
24 * <p>Classes that wish to provide their own drag under effect for a <code>Table</code>
25 * can extend the <code>TableDropTargetEffect</code> and override any applicable methods
26 * in <code>TableDropTargetEffect</code> to display their own drag under effect.</p>
27 *
28 * Subclasses that override any methods of this class must call the corresponding
29 * <code>super</code> method to get the default drag under effect implementation.
30 *
31 * <p>The feedback value is either one of the FEEDBACK constants defined in
32 * class <code>DND</code> which is applicable to instances of this class,
33 * or it must be built by <em>bitwise OR</em>'ing together
34 * (that is, using the <code>int</code> "|" operator) two or more
35 * of those <code>DND</code> effect constants.
36 * </p>
37 * <p>
38 * <dl>
39 * <dt><b>Feedback:</b></dt>
40 * <dd>FEEDBACK_SELECT, FEEDBACK_SCROLL</dd>
41 * </dl>
42 * </p>
43 *
44 * @see DropTargetAdapter
45 * @see DropTargetEvent
46 *
47 * @since 3.3
48 */
49 public class TableDropTargetEffect : DropTargetEffect {
50 static final int SCROLL_HYSTERESIS = 150; // milli seconds
51
52 TableItem scrollItem;
53 long scrollBeginTime;
54 DataBrowserCallbacks callbacks = null;
55
56 static Callback AcceptDragProc;
57 static {
58 AcceptDragProc = new Callback(TableDropTargetEffect.class, "AcceptDragProc", 5); //$NON-NLS-1$
59 int acceptDragProc = AcceptDragProc.getAddress();
60 if (acceptDragProc is 0) DWT.error(DWT.ERROR_NO_MORE_CALLBACKS);
61 }
62
63 static int AcceptDragProc(int theControl, int itemID, int property, int theRect, int theDrag) {
64 DropTarget target = FindDropTarget(theControl, theDrag);
65 if (target is null) return 0;
66 return (target.feedback & DND.FEEDBACK_SELECT) !is 0 ? 1 : 0;
67 }
68
69 static DropTarget FindDropTarget(int theControl, int theDrag) {
70 if (theControl is 0) return null;
71 Display display = Display.findDisplay(Thread.currentThread());
72 if (display is null || display.isDisposed()) return null;
73 Widget widget = display.findWidget(theControl);
74 if (widget is null) return null;
75 return (DropTarget)widget.getData(DND.DROP_TARGET_KEY);
76 }
77
78 /**
79 * Creates a new <code>TableDropTargetEffect</code> to handle the drag under effect on the specified
80 * <code>Table</code>.
81 *
82 * @param table the <code>Table</code> over which the user positions the cursor to drop the data
83 */
84 public TableDropTargetEffect(Table table) {
85 super(table);
86 }
87
88 int checkEffect(int effect) {
89 // Some effects are mutually exclusive. Make sure that only one of the mutually exclusive effects has been specified.
90 if ((effect & DND.FEEDBACK_SELECT) !is 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER & ~DND.FEEDBACK_INSERT_BEFORE;
91 if ((effect & DND.FEEDBACK_INSERT_BEFORE) !is 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER;
92 return effect;
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 enter event
105 *
106 * @see DropTargetAdapter
107 * @see DropTargetEvent
108 */
109 public void dragEnter(DropTargetEvent event) {
110 if (callbacks is null) {
111 Table table = (Table) control;
112 DataBrowserCallbacks callbacks = new DataBrowserCallbacks ();
113 OS.GetDataBrowserCallbacks (table.handle, callbacks);
114 callbacks.v1_acceptDragCallback = AcceptDragProc.getAddress();
115 OS.SetDataBrowserCallbacks(table.handle, callbacks);
116 }
117 scrollBeginTime = 0;
118 scrollItem = null;
119 }
120
121 /**
122 * This implementation of <code>dragLeave</code> provides a default drag under effect
123 * for the feedback specified in <code>event.feedback</code>.
124 *
125 * For additional information see <code>DropTargetAdapter.dragLeave</code>.
126 *
127 * Subclasses that override this method should call <code>super.dragLeave(event)</code>
128 * to get the default drag under effect implementation.
129 *
130 * @param event the information associated with the drag leave event
131 *
132 * @see DropTargetAdapter
133 * @see DropTargetEvent
134 */
135 public void dragLeave(DropTargetEvent event) {
136 scrollBeginTime = 0;
137 scrollItem = null;
138 }
139
140 /**
141 * This implementation of <code>dragOver</code> provides a default drag under effect
142 * for the feedback specified in <code>event.feedback</code>. The class description
143 * lists the FEEDBACK constants that are applicable to the class.
144 *
145 * For additional information see <code>DropTargetAdapter.dragOver</code>.
146 *
147 * Subclasses that override this method should call <code>super.dragOver(event)</code>
148 * to get the default drag under effect implementation.
149 *
150 * @param event the information associated with the drag over event
151 *
152 * @see DropTargetAdapter
153 * @see DropTargetEvent
154 * @see DND#FEEDBACK_SELECT
155 * @see DND#FEEDBACK_SCROLL
156 */
157 public void dragOver(DropTargetEvent event) {
158 Table table = (Table) control;
159 int effect = checkEffect(event.feedback);
160
161 TableItem item = (TableItem)getItem(table, event.x, event.y);
162
163 if ((effect & DND.FEEDBACK_SCROLL) is 0) {
164 scrollBeginTime = 0;
165 scrollItem = null;
166 } else {
167 if (item !is null && item.opEquals(scrollItem) && scrollBeginTime !is 0) {
168 if (System.currentTimeMillis() >= scrollBeginTime) {
169 Rectangle area = table.getClientArea();
170 int headerHeight = table.getHeaderHeight();
171 int itemHeight= table.getItemHeight();
172 Point pt = new Point(event.x, event.y);
173 pt = table.getDisplay().map(null, table, pt);
174 TableItem nextItem = null;
175 if (pt.y < area.y + headerHeight + 2 * itemHeight) {
176 int index = Math.max(0, table.indexOf(item)-1);
177 nextItem = table.getItem(index);
178 }
179 if (pt.y > area.y + area.height - 2 * itemHeight) {
180 int index = Math.min(table.getItemCount()-1, table.indexOf(item)+1);
181 nextItem = table.getItem(index);
182 }
183 if (nextItem !is null) table.showItem(nextItem);
184 scrollBeginTime = 0;
185 scrollItem = null;
186 }
187 } else {
188 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS;
189 scrollItem = item;
190 }
191 }
192
193 // store current effect for selection feedback
194 ((DropTarget)event.widget).feedback = effect;
195 }
196 }