]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/dnd/TableDropTargetEffect.java
42cb9d6b8fb4e7f427cc294fad90c92f0b1288c1
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / dnd / TableDropTargetEffect.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.dnd;
15
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.*;
20
21 /**
22  * This class provides a default drag under effect (eg. select, insert and scroll)
23  * when a drag occurs over a <code>Table</code>.
24  *
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>
28  *
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.
31  *
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.
37  * </p>
38  * <dl>
39  * <dt><b>Feedback:</b></dt>
40  * <dd>FEEDBACK_SELECT, FEEDBACK_SCROLL</dd>
41  * </dl>
42  *
43  * @see DropTargetAdapter
44  * @see DropTargetEvent
45  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
46  *
47  * @since 3.3
48  */
49 public class TableDropTargetEffect extends DropTargetEffect {
50         static final int SCROLL_HYSTERESIS = 200; // milli seconds
51
52         int scrollIndex = -1;
53         long scrollBeginTime;
54         TableItem dropHighlight;
55         int iItemInsert = -1;
56
57         /**
58          * Creates a new <code>TableDropTargetEffect</code> to handle the drag under effect on the specified
59          * <code>Table</code>.
60          *
61          * @param table the <code>Table</code> over which the user positions the cursor to drop the data
62          */
63         public TableDropTargetEffect(Table table) {
64                 super(table);
65         }
66
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;
71                 return effect;
72         }
73
74         /**
75          * This implementation of <code>dragEnter</code> provides a default drag under effect
76          * for the feedback specified in <code>event.feedback</code>.
77          *
78          * For additional information see <code>DropTargetAdapter.dragEnter</code>.
79          *
80          * Subclasses that override this method should call <code>super.dragEnter(event)</code>
81          * to get the default drag under effect implementation.
82          *
83          * @param event  the information associated with the drag enter event
84          *
85          * @see DropTargetAdapter
86          * @see DropTargetEvent
87          */
88         @Override
89         public void dragEnter(DropTargetEvent event) {
90                 scrollBeginTime = 0;
91                 scrollIndex = -1;
92                 dropHighlight = null;
93                 iItemInsert = -1;
94         }
95
96         /**
97          * This implementation of <code>dragLeave</code> provides a default drag under effect
98          * for the feedback specified in <code>event.feedback</code>.
99          *
100          * For additional information see <code>DropTargetAdapter.dragLeave</code>.
101          *
102          * Subclasses that override this method should call <code>super.dragLeave(event)</code>
103          * to get the default drag under effect implementation.
104          *
105          * @param event  the information associated with the drag leave event
106          *
107          * @see DropTargetAdapter
108          * @see DropTargetEvent
109          */
110         @Override
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;
119                 }
120                 if (iItemInsert != -1) {
121                         LVINSERTMARK plvim = new LVINSERTMARK ();
122                         plvim.cbSize = LVINSERTMARK.sizeof;
123                         plvim.iItem = -1;
124                         OS.SendMessage(handle, OS.LVM_SETINSERTMARK, 0, plvim);
125                         iItemInsert = -1;
126                 }
127                 scrollBeginTime = 0;
128                 scrollIndex = -1;
129         }
130
131         /**
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.
135          *
136          * For additional information see <code>DropTargetAdapter.dragOver</code>.
137          *
138          * Subclasses that override this method should call <code>super.dragOver(event)</code>
139          * to get the default drag under effect implementation.
140          *
141          * @param event  the information associated with the drag over event
142          *
143          * @see DropTargetAdapter
144          * @see DropTargetEvent
145          * @see DND#FEEDBACK_SELECT
146          * @see DND#FEEDBACK_SCROLL
147          */
148         @Override
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) {
160                         scrollBeginTime = 0;
161                         scrollIndex = -1;
162                 } else {
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;
171                                         } else {
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;
179                                                         pt.y = itemRect.top;
180                                                         if (OS.PtInRect (rect, pt)) {
181                                                                 pt.y = itemRect.bottom;
182                                                                 if (OS.PtInRect (rect, pt)) scroll = false;
183                                                         }
184                                                 }
185                                         }
186                                         if (scroll) {
187                                                 OS.SendMessage (handle, OS.LVM_ENSUREVISIBLE, index, 0);
188                                                 table.redraw();
189                                         }
190                                         scrollBeginTime = 0;
191                                         scrollIndex = -1;
192                                 }
193                         } else {
194                                 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS;
195                                 scrollIndex = pinfo.iItem;
196                         }
197                 }
198
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;
208                         }
209                 } else {
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;
215                         }
216                 }
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;
224                                 }
225                 } else {
226                         if (iItemInsert != -1) {
227                                 LVINSERTMARK plvim = new LVINSERTMARK ();
228                                 plvim.cbSize = LVINSERTMARK.sizeof;
229                                 plvim.iItem = -1;
230                                 OS.SendMessage(handle, OS.LVM_SETINSERTMARK, 0, plvim);
231                                 iItemInsert = -1;
232                         }
233                 }
234         }
235 }