1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.dnd;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.internal.*;
19 import org.eclipse.swt.internal.win32.*;
20 import org.eclipse.swt.widgets.*;
23 * This class provides a default drag under effect (eg. select, insert, scroll and expand)
24 * when a drag occurs over a <code>Tree</code>.
26 * <p>Classes that wish to provide their own drag under effect for a <code>Tree</code>
27 * can extend the <code>TreeDropTargetEffect</code> class and override any applicable methods
28 * in <code>TreeDropTargetEffect</code> to display their own drag under effect.</p>
30 * Subclasses that override any methods of this class must call the corresponding
31 * <code>super</code> method to get the default drag under effect implementation.
33 * <p>The feedback value is either one of the FEEDBACK constants defined in
34 * class <code>DND</code> which is applicable to instances of this class,
35 * or it must be built by <em>bitwise OR</em>'ing together
36 * (that is, using the <code>int</code> "|" operator) two or more
37 * of those <code>DND</code> effect constants.
40 * <dt><b>Feedback:</b></dt>
41 * <dd>FEEDBACK_SELECT, FEEDBACK_INSERT_BEFORE, FEEDBACK_INSERT_AFTER, FEEDBACK_EXPAND, FEEDBACK_SCROLL</dd>
44 * Note: Only one of the styles FEEDBACK_SELECT, FEEDBACK_INSERT_BEFORE or
45 * FEEDBACK_INSERT_AFTER may be specified.
48 * @see DropTargetAdapter
49 * @see DropTargetEvent
50 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
54 public class TreeDropTargetEffect extends DropTargetEffect {
55 static final int SCROLL_HYSTERESIS = 200; // milli seconds
56 static final int EXPAND_HYSTERESIS = 1000; // milli seconds
67 * Creates a new <code>TreeDropTargetEffect</code> to handle the drag under effect on the specified
70 * @param tree the <code>Tree</code> over which the user positions the cursor to drop the data
72 public TreeDropTargetEffect(Tree tree) {
76 int checkEffect(int effect) {
77 // Some effects are mutually exclusive. Make sure that only one of the mutually exclusive effects has been specified.
78 if ((effect & DND.FEEDBACK_SELECT) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER & ~DND.FEEDBACK_INSERT_BEFORE;
79 if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER;
84 * This implementation of <code>dragEnter</code> provides a default drag under effect
85 * for the feedback specified in <code>event.feedback</code>.
87 * For additional information see <code>DropTargetAdapter.dragEnter</code>.
89 * Subclasses that override this method should call <code>super.dragEnter(event)</code>
90 * to get the default drag under effect implementation.
92 * @param event the information associated with the drag enter event
94 * @see DropTargetAdapter
95 * @see DropTargetEvent
98 public void dragEnter(DropTargetEvent event) {
108 * This implementation of <code>dragLeave</code> provides a default drag under effect
109 * for the feedback specified in <code>event.feedback</code>.
111 * For additional information see <code>DropTargetAdapter.dragLeave</code>.
113 * Subclasses that override this method should call <code>super.dragLeave(event)</code>
114 * to get the default drag under effect implementation.
116 * @param event the information associated with the drag leave event
118 * @see DropTargetAdapter
119 * @see DropTargetEvent
122 public void dragLeave(DropTargetEvent event) {
123 Tree tree = (Tree) control;
124 long handle = tree.handle;
125 if (dropIndex != -1) {
126 TVITEM tvItem = new TVITEM ();
127 tvItem.hItem = dropIndex;
128 tvItem.mask = OS.TVIF_STATE;
129 tvItem.stateMask = OS.TVIS_DROPHILITED;
131 OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);
134 if (insertItem != null) {
135 tree.setInsertMark(null, false);
145 * This implementation of <code>dragOver</code> provides a default drag under effect
146 * for the feedback specified in <code>event.feedback</code>.
148 * For additional information see <code>DropTargetAdapter.dragOver</code>.
150 * Subclasses that override this method should call <code>super.dragOver(event)</code>
151 * to get the default drag under effect implementation.
153 * @param event the information associated with the drag over event
155 * @see DropTargetAdapter
156 * @see DropTargetEvent
157 * @see DND#FEEDBACK_SELECT
158 * @see DND#FEEDBACK_INSERT_BEFORE
159 * @see DND#FEEDBACK_INSERT_AFTER
160 * @see DND#FEEDBACK_SCROLL
163 public void dragOver(DropTargetEvent event) {
164 Tree tree = (Tree) getControl();
165 int effect = checkEffect(event.feedback);
166 long handle = tree.handle;
167 Point coordinates = new Point(event.x, event.y);
168 coordinates = DPIUtil.autoScaleUp(tree.toControl(coordinates)); // To Pixels
169 TVHITTESTINFO lpht = new TVHITTESTINFO ();
170 lpht.x = coordinates.x;
171 lpht.y = coordinates.y;
172 OS.SendMessage (handle, OS.TVM_HITTEST, 0, lpht);
173 long hItem = lpht.hItem;
174 if ((effect & DND.FEEDBACK_SCROLL) == 0) {
178 if (hItem != -1 && scrollIndex == hItem && scrollBeginTime != 0) {
179 if (System.currentTimeMillis() >= scrollBeginTime) {
180 long topItem = OS.SendMessage(handle, OS.TVM_GETNEXTITEM, OS.TVGN_FIRSTVISIBLE, 0);
181 long nextItem = OS.SendMessage(handle, OS.TVM_GETNEXTITEM, hItem == topItem ? OS.TVGN_PREVIOUSVISIBLE : OS.TVGN_NEXTVISIBLE, hItem);
182 boolean scroll = true;
183 if (hItem == topItem) {
184 scroll = nextItem != 0;
186 RECT itemRect = new RECT ();
187 if (OS.TreeView_GetItemRect (handle, nextItem, itemRect, true)) {
188 RECT rect = new RECT ();
189 OS.GetClientRect (handle, rect);
190 POINT pt = new POINT ();
191 pt.x = itemRect.left;
193 if (OS.PtInRect (rect, pt)) {
194 pt.y = itemRect.bottom;
195 if (OS.PtInRect (rect, pt)) scroll = false;
200 OS.SendMessage (handle, OS.TVM_ENSUREVISIBLE, 0, nextItem);
207 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS;
211 if ((effect & DND.FEEDBACK_EXPAND) == 0) {
215 if (hItem != -1 && expandIndex == hItem && expandBeginTime != 0) {
216 if (System.currentTimeMillis() >= expandBeginTime) {
217 if (OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_CHILD, hItem) != 0) {
218 TreeItem item = (TreeItem)tree.getDisplay().findWidget(tree.handle, hItem);
219 if (item != null && !item.getExpanded()) {
220 item.setExpanded(true);
222 Event expandEvent = new Event ();
223 expandEvent.item = item;
224 tree.notifyListeners(SWT.Expand, expandEvent);
231 expandBeginTime = System.currentTimeMillis() + EXPAND_HYSTERESIS;
235 if (dropIndex != -1 && (dropIndex != hItem || (effect & DND.FEEDBACK_SELECT) == 0)) {
236 TVITEM tvItem = new TVITEM ();
237 tvItem.hItem = dropIndex;
238 tvItem.mask = OS.TVIF_STATE;
239 tvItem.stateMask = OS.TVIS_DROPHILITED;
241 OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);
244 if (hItem != -1 && hItem != dropIndex && (effect & DND.FEEDBACK_SELECT) != 0) {
245 TVITEM tvItem = new TVITEM ();
246 tvItem.hItem = hItem;
247 tvItem.mask = OS.TVIF_STATE;
248 tvItem.stateMask = OS.TVIS_DROPHILITED;
249 tvItem.state = OS.TVIS_DROPHILITED;
250 OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);
253 if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0 || (effect & DND.FEEDBACK_INSERT_AFTER) != 0) {
254 boolean before = (effect & DND.FEEDBACK_INSERT_BEFORE) != 0;
256 * Bug in Windows. When TVM_SETINSERTMARK is used to set
257 * an insert mark for a tree and an item is expanded or
258 * collapsed near the insert mark, the tree does not redraw
259 * the insert mark properly. The fix is to hide and show
260 * the insert mark whenever an item is expanded or collapsed.
261 * Since the insert mark can not be queried from the tree,
262 * use the Tree API rather than calling the OS directly.
264 TreeItem item = (TreeItem)tree.getDisplay().findWidget(tree.handle, hItem);
266 if (item != insertItem || before != insertBefore) {
267 tree.setInsertMark(item, before);
270 insertBefore = before;
272 if (insertItem != null) {
273 tree.setInsertMark(null, false);
278 if (insertItem != null) {
279 tree.setInsertMark(null, false);