]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/dnd/TreeDropTargetEffect.java
Work around SWT 4.13 - 4.18 Win32 DnD bug 567422
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / dnd / TreeDropTargetEffect.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.*;
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.*;
21
22 /**
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>.
25  *
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>
29  *
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.
32  *
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.
38  * </p>
39  * <dl>
40  * <dt><b>Feedback:</b></dt>
41  * <dd>FEEDBACK_SELECT, FEEDBACK_INSERT_BEFORE, FEEDBACK_INSERT_AFTER, FEEDBACK_EXPAND, FEEDBACK_SCROLL</dd>
42  * </dl>
43  * <p>
44  * Note: Only one of the styles FEEDBACK_SELECT, FEEDBACK_INSERT_BEFORE or
45  * FEEDBACK_INSERT_AFTER may be specified.
46  * </p>
47  *
48  * @see DropTargetAdapter
49  * @see DropTargetEvent
50  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
51  *
52  * @since 3.3
53  */
54 public class TreeDropTargetEffect extends DropTargetEffect {
55         static final int SCROLL_HYSTERESIS = 200; // milli seconds
56         static final int EXPAND_HYSTERESIS = 1000; // milli seconds
57
58         long dropIndex;
59         long scrollIndex;
60         long scrollBeginTime;
61         long expandIndex;
62         long expandBeginTime;
63         TreeItem insertItem;
64         boolean insertBefore;
65
66         /**
67          * Creates a new <code>TreeDropTargetEffect</code> to handle the drag under effect on the specified
68          * <code>Tree</code>.
69          *
70          * @param tree the <code>Tree</code> over which the user positions the cursor to drop the data
71          */
72         public TreeDropTargetEffect(Tree tree) {
73                 super(tree);
74         }
75
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;
80                 return effect;
81         }
82
83         /**
84          * This implementation of <code>dragEnter</code> provides a default drag under effect
85          * for the feedback specified in <code>event.feedback</code>.
86          *
87          * For additional information see <code>DropTargetAdapter.dragEnter</code>.
88          *
89          * Subclasses that override this method should call <code>super.dragEnter(event)</code>
90          * to get the default drag under effect implementation.
91          *
92          * @param event  the information associated with the drag enter event
93          *
94          * @see DropTargetAdapter
95          * @see DropTargetEvent
96          */
97         @Override
98         public void dragEnter(DropTargetEvent event) {
99                 dropIndex = -1;
100                 insertItem = null;
101                 expandBeginTime = 0;
102                 expandIndex = -1;
103                 scrollBeginTime = 0;
104                 scrollIndex = -1;
105         }
106
107         /**
108          * This implementation of <code>dragLeave</code> provides a default drag under effect
109          * for the feedback specified in <code>event.feedback</code>.
110          *
111          * For additional information see <code>DropTargetAdapter.dragLeave</code>.
112          *
113          * Subclasses that override this method should call <code>super.dragLeave(event)</code>
114          * to get the default drag under effect implementation.
115          *
116          * @param event  the information associated with the drag leave event
117          *
118          * @see DropTargetAdapter
119          * @see DropTargetEvent
120          */
121         @Override
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;
130                         tvItem.state = 0;
131                         OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);
132                         dropIndex = -1;
133                 }
134                 if (insertItem != null) {
135                         tree.setInsertMark(null, false);
136                         insertItem = null;
137                 }
138                 expandBeginTime = 0;
139                 expandIndex = -1;
140                 scrollBeginTime = 0;
141                 scrollIndex = -1;
142         }
143
144         /**
145          * This implementation of <code>dragOver</code> provides a default drag under effect
146          * for the feedback specified in <code>event.feedback</code>.
147          *
148          * For additional information see <code>DropTargetAdapter.dragOver</code>.
149          *
150          * Subclasses that override this method should call <code>super.dragOver(event)</code>
151          * to get the default drag under effect implementation.
152          *
153          * @param event  the information associated with the drag over event
154          *
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
161          */
162         @Override
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) {
175                         scrollBeginTime = 0;
176                         scrollIndex = -1;
177                 } else {
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;
185                                         } else {
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;
192                                                         pt.y = itemRect.top;
193                                                         if (OS.PtInRect (rect, pt)) {
194                                                                 pt.y = itemRect.bottom;
195                                                                 if (OS.PtInRect (rect, pt)) scroll = false;
196                                                         }
197                                                 }
198                                         }
199                                         if (scroll) {
200                                                 OS.SendMessage (handle, OS.TVM_ENSUREVISIBLE, 0, nextItem);
201                                                 tree.redraw();
202                                         }
203                                         scrollBeginTime = 0;
204                                         scrollIndex = -1;
205                                 }
206                         } else {
207                                 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS;
208                                 scrollIndex = hItem;
209                         }
210                 }
211                 if ((effect & DND.FEEDBACK_EXPAND) == 0) {
212                         expandBeginTime = 0;
213                         expandIndex = -1;
214                 } else {
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);
221                                                         tree.redraw();
222                                                         Event expandEvent = new Event ();
223                                                         expandEvent.item = item;
224                                                         tree.notifyListeners(SWT.Expand, expandEvent);
225                                                 }
226                                         }
227                                         expandBeginTime = 0;
228                                         expandIndex = -1;
229                                 }
230                         } else {
231                                 expandBeginTime = System.currentTimeMillis() + EXPAND_HYSTERESIS;
232                                 expandIndex = hItem;
233                         }
234                 }
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;
240                         tvItem.state = 0;
241                         OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);
242                         dropIndex = -1;
243                 }
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);
251                         dropIndex = hItem;
252                 }
253                 if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0 || (effect & DND.FEEDBACK_INSERT_AFTER) != 0) {
254                         boolean before = (effect & DND.FEEDBACK_INSERT_BEFORE) != 0;
255                         /*
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.
263                         */
264                         TreeItem item = (TreeItem)tree.getDisplay().findWidget(tree.handle, hItem);
265                         if (item != null) {
266                                 if (item != insertItem || before != insertBefore) {
267                                         tree.setInsertMark(item, before);
268                                 }
269                                 insertItem = item;
270                                 insertBefore = before;
271                         } else {
272                                 if (insertItem != null) {
273                                         tree.setInsertMark(null, false);
274                                 }
275                                 insertItem = null;
276                         }
277                 } else {
278                         if (insertItem != null) {
279                                 tree.setInsertMark(null, false);
280                         }
281                         insertItem = null;
282                 }
283         }
284 }