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.graphics.*;
17 import org.eclipse.swt.internal.*;
18 import org.eclipse.swt.internal.win32.*;
19 import org.eclipse.swt.widgets.*;
22 * This class provides a default drag under effect (eg. select, insert and scroll)
23 * when a drag occurs over a <code>Table</code>.
25 * <p>Classes that wish to provide their own drag under effect for a <code>Table</code>
26 * can extend the <code>TableDropTargetEffect</code> and override any applicable methods
27 * in <code>TableDropTargetEffect</code> to display their own drag under effect.</p>
29 * Subclasses that override any methods of this class must call the corresponding
30 * <code>super</code> method to get the default drag under effect implementation.
32 * <p>The feedback value is either one of the FEEDBACK constants defined in
33 * class <code>DND</code> which is applicable to instances of this class,
34 * or it must be built by <em>bitwise OR</em>'ing together
35 * (that is, using the <code>int</code> "|" operator) two or more
36 * of those <code>DND</code> effect constants.
39 * <dt><b>Feedback:</b></dt>
40 * <dd>FEEDBACK_SELECT, FEEDBACK_SCROLL</dd>
43 * @see DropTargetAdapter
44 * @see DropTargetEvent
45 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
49 public class TableDropTargetEffect extends DropTargetEffect {
50 static final int SCROLL_HYSTERESIS = 200; // milli seconds
54 TableItem dropHighlight;
58 * Creates a new <code>TableDropTargetEffect</code> to handle the drag under effect on the specified
61 * @param table the <code>Table</code> over which the user positions the cursor to drop the data
63 public TableDropTargetEffect(Table table) {
67 int checkEffect(int effect) {
68 // Some effects are mutually exclusive. Make sure that only one of the mutually exclusive effects has been specified.
69 if ((effect & DND.FEEDBACK_SELECT) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER & ~DND.FEEDBACK_INSERT_BEFORE;
70 if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER;
75 * This implementation of <code>dragEnter</code> provides a default drag under effect
76 * for the feedback specified in <code>event.feedback</code>.
78 * For additional information see <code>DropTargetAdapter.dragEnter</code>.
80 * Subclasses that override this method should call <code>super.dragEnter(event)</code>
81 * to get the default drag under effect implementation.
83 * @param event the information associated with the drag enter event
85 * @see DropTargetAdapter
86 * @see DropTargetEvent
89 public void dragEnter(DropTargetEvent event) {
97 * This implementation of <code>dragLeave</code> provides a default drag under effect
98 * for the feedback specified in <code>event.feedback</code>.
100 * For additional information see <code>DropTargetAdapter.dragLeave</code>.
102 * Subclasses that override this method should call <code>super.dragLeave(event)</code>
103 * to get the default drag under effect implementation.
105 * @param event the information associated with the drag leave event
107 * @see DropTargetAdapter
108 * @see DropTargetEvent
111 public void dragLeave(DropTargetEvent event) {
112 Table table = (Table) control;
113 long handle = table.handle;
114 if (dropHighlight != null) {
115 LVITEM lvItem = new LVITEM ();
116 lvItem.stateMask = OS.LVIS_DROPHILITED;
117 OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, lvItem);
118 dropHighlight = null;
120 if (iItemInsert != -1) {
121 LVINSERTMARK plvim = new LVINSERTMARK ();
122 plvim.cbSize = LVINSERTMARK.sizeof;
124 OS.SendMessage(handle, OS.LVM_SETINSERTMARK, 0, plvim);
132 * This implementation of <code>dragOver</code> provides a default drag under effect
133 * for the feedback specified in <code>event.feedback</code>. The class description
134 * lists the FEEDBACK constants that are applicable to the class.
136 * For additional information see <code>DropTargetAdapter.dragOver</code>.
138 * Subclasses that override this method should call <code>super.dragOver(event)</code>
139 * to get the default drag under effect implementation.
141 * @param event the information associated with the drag over event
143 * @see DropTargetAdapter
144 * @see DropTargetEvent
145 * @see DND#FEEDBACK_SELECT
146 * @see DND#FEEDBACK_SCROLL
149 public void dragOver(DropTargetEvent event) {
150 Table table = (Table) getControl();
151 int effect = checkEffect(event.feedback);
152 long handle = table.handle;
153 Point coordinates = new Point(event.x, event.y);
154 coordinates = DPIUtil.autoScaleUp(table.toControl(coordinates)); // To Pixels
155 LVHITTESTINFO pinfo = new LVHITTESTINFO();
156 pinfo.x = coordinates.x;
157 pinfo.y = coordinates.y;
158 OS.SendMessage(handle, OS.LVM_HITTEST, 0, pinfo);
159 if ((effect & DND.FEEDBACK_SCROLL) == 0) {
163 if (pinfo.iItem != -1 && scrollIndex == pinfo.iItem && scrollBeginTime != 0) {
164 if (System.currentTimeMillis() >= scrollBeginTime) {
165 int top = Math.max (0, (int)OS.SendMessage (handle, OS.LVM_GETTOPINDEX, 0, 0));
166 int count = (int)OS.SendMessage (handle, OS.LVM_GETITEMCOUNT, 0, 0);
167 int index = (scrollIndex - 1 < top) ? Math.max(0, scrollIndex - 1) : Math.min(count - 1, scrollIndex + 1);
168 boolean scroll = true;
169 if (pinfo.iItem == top) {
170 scroll = pinfo.iItem != index;
172 RECT itemRect = new RECT ();
173 itemRect.left = OS.LVIR_BOUNDS;
174 if (OS.SendMessage (handle, OS.LVM_GETITEMRECT, pinfo.iItem, itemRect) != 0) {
175 RECT rect = new RECT ();
176 OS.GetClientRect (handle, rect);
177 POINT pt = new POINT ();
178 pt.x = itemRect.left;
180 if (OS.PtInRect (rect, pt)) {
181 pt.y = itemRect.bottom;
182 if (OS.PtInRect (rect, pt)) scroll = false;
187 OS.SendMessage (handle, OS.LVM_ENSUREVISIBLE, index, 0);
194 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS;
195 scrollIndex = pinfo.iItem;
199 if (pinfo.iItem != -1 && (effect & DND.FEEDBACK_SELECT) != 0) {
200 TableItem item = table.getItem(pinfo.iItem);
201 if (dropHighlight != item) {
202 LVITEM lvItem = new LVITEM();
203 lvItem.stateMask = OS.LVIS_DROPHILITED;
204 OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, lvItem);
205 lvItem.state = OS.LVIS_DROPHILITED;
206 OS.SendMessage(handle, OS.LVM_SETITEMSTATE, pinfo.iItem, lvItem);
207 dropHighlight = item;
210 if (dropHighlight != null) {
211 LVITEM lvItem = new LVITEM ();
212 lvItem.stateMask = OS.LVIS_DROPHILITED;
213 OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, lvItem);
214 dropHighlight = null;
217 if (pinfo.iItem != -1 && (effect & (DND.FEEDBACK_INSERT_BEFORE | DND.FEEDBACK_INSERT_AFTER)) != 0) {
218 LVINSERTMARK plvim = new LVINSERTMARK ();
219 plvim.cbSize = LVINSERTMARK.sizeof;
220 plvim.dwFlags = (effect & DND.FEEDBACK_INSERT_AFTER) != 0 ? OS.LVIM_AFTER : 0;
221 plvim.iItem = pinfo.iItem;
222 if (OS.SendMessage(handle, OS.LVM_SETINSERTMARK, 0, plvim) != 0) {
223 iItemInsert = pinfo.iItem;
226 if (iItemInsert != -1) {
227 LVINSERTMARK plvim = new LVINSERTMARK ();
228 plvim.cbSize = LVINSERTMARK.sizeof;
230 OS.SendMessage(handle, OS.LVM_SETINSERTMARK, 0, plvim);