1 11 package org.eclipse.swt.dnd; 12 13 import org.eclipse.swt.graphics.*; 14 import org.eclipse.swt.internal.win32.*; 15 import org.eclipse.swt.widgets.*; 16 17 49 public class TreeDropTargetEffect extends DropTargetEffect { 50 static final int SCROLL_HYSTERESIS = 200; static final int EXPAND_HYSTERESIS = 1000; 53 int dropIndex; 54 int scrollIndex; 55 long scrollBeginTime; 56 int expandIndex; 57 long expandBeginTime; 58 TreeItem insertItem; 59 boolean insertBefore; 60 61 67 public TreeDropTargetEffect(Tree tree) { 68 super(tree); 69 } 70 71 int checkEffect(int effect) { 72 if ((effect & DND.FEEDBACK_SELECT) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER & ~DND.FEEDBACK_INSERT_BEFORE; 74 if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER; 75 return effect; 76 } 77 78 92 public void dragEnter(DropTargetEvent event) { 93 dropIndex = -1; 94 insertItem = null; 95 expandBeginTime = 0; 96 expandIndex = -1; 97 scrollBeginTime = 0; 98 scrollIndex = -1; 99 } 100 101 115 public void dragLeave(DropTargetEvent event) { 116 Tree tree = (Tree) control; 117 int handle = tree.handle; 118 if (dropIndex != -1) { 119 TVITEM tvItem = new TVITEM (); 120 tvItem.hItem = dropIndex; 121 tvItem.mask = OS.TVIF_STATE; 122 tvItem.stateMask = OS.TVIS_DROPHILITED; 123 tvItem.state = 0; 124 OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem); 125 dropIndex = -1; 126 } 127 if (insertItem != null) { 128 tree.setInsertMark(null, false); 129 insertItem = null; 130 } 131 expandBeginTime = 0; 132 expandIndex = -1; 133 scrollBeginTime = 0; 134 scrollIndex = -1; 135 } 136 137 155 public void dragOver(DropTargetEvent event) { 156 Tree tree = (Tree) getControl(); 157 int effect = checkEffect(event.feedback); 158 int handle = tree.handle; 159 Point coordinates = new Point(event.x, event.y); 160 coordinates = tree.toControl(coordinates); 161 TVHITTESTINFO lpht = new TVHITTESTINFO (); 162 lpht.x = coordinates.x; 163 lpht.y = coordinates.y; 164 OS.SendMessage (handle, OS.TVM_HITTEST, 0, lpht); 165 int hItem = lpht.hItem; 166 if ((effect & DND.FEEDBACK_SCROLL) == 0) { 167 scrollBeginTime = 0; 168 scrollIndex = -1; 169 } else { 170 if (hItem != -1 && scrollIndex == hItem && scrollBeginTime != 0) { 171 if (System.currentTimeMillis() >= scrollBeginTime) { 172 int topItem = OS.SendMessage(handle, OS.TVM_GETNEXTITEM, OS.TVGN_FIRSTVISIBLE, 0); 173 int nextItem = OS.SendMessage(handle, OS.TVM_GETNEXTITEM, hItem == topItem ? OS.TVGN_PREVIOUSVISIBLE : OS.TVGN_NEXTVISIBLE, hItem); 174 boolean scroll = true; 175 if (hItem == topItem) { 176 scroll = nextItem != 0; 177 } else { 178 RECT itemRect = new RECT (); 179 itemRect.left = nextItem; 180 if (OS.SendMessage (handle, OS.TVM_GETITEMRECT, 1, itemRect) != 0) { 181 RECT rect = new RECT (); 182 OS.GetClientRect (handle, rect); 183 POINT pt = new POINT (); 184 pt.x = itemRect.left; 185 pt.y = itemRect.top; 186 if (OS.PtInRect (rect, pt)) { 187 pt.y = itemRect.bottom; 188 if (OS.PtInRect (rect, pt)) scroll = false; 189 } 190 } 191 } 192 if (scroll) { 193 OS.SendMessage (handle, OS.TVM_ENSUREVISIBLE, 0, nextItem); 194 tree.redraw(); 195 } 196 scrollBeginTime = 0; 197 scrollIndex = -1; 198 } 199 } else { 200 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS; 201 scrollIndex = hItem; 202 } 203 } 204 if ((effect & DND.FEEDBACK_EXPAND) == 0) { 205 expandBeginTime = 0; 206 expandIndex = -1; 207 } else { 208 if (hItem != -1 && expandIndex == hItem && expandBeginTime != 0) { 209 if (System.currentTimeMillis() >= expandBeginTime) { 210 if (OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_CHILD, hItem) != 0) { 211 TVITEM tvItem = new TVITEM (); 212 tvItem.hItem = hItem; 213 tvItem.mask = OS.TVIF_HANDLE | OS.TVIF_STATE; 214 OS.SendMessage (handle, OS.TVM_GETITEM, 0, tvItem); 215 if ((tvItem.state & OS.TVIS_EXPANDED) == 0) { 216 OS.SendMessage (handle, OS.TVM_EXPAND, OS.TVE_EXPAND, hItem); 217 tree.redraw(); 218 } 219 } 220 expandBeginTime = 0; 221 expandIndex = -1; 222 } 223 } else { 224 expandBeginTime = System.currentTimeMillis() + EXPAND_HYSTERESIS; 225 expandIndex = hItem; 226 } 227 } 228 if (dropIndex != -1 && (dropIndex != hItem || (effect & DND.FEEDBACK_SELECT) == 0)) { 229 TVITEM tvItem = new TVITEM (); 230 tvItem.hItem = dropIndex; 231 tvItem.mask = OS.TVIF_STATE; 232 tvItem.stateMask = OS.TVIS_DROPHILITED; 233 tvItem.state = 0; 234 OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem); 235 dropIndex = -1; 236 } 237 if (hItem != -1 && hItem != dropIndex && (effect & DND.FEEDBACK_SELECT) != 0) { 238 TVITEM tvItem = new TVITEM (); 239 tvItem.hItem = hItem; 240 tvItem.mask = OS.TVIF_STATE; 241 tvItem.stateMask = OS.TVIS_DROPHILITED; 242 tvItem.state = OS.TVIS_DROPHILITED; 243 OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem); 244 dropIndex = hItem; 245 } 246 if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0 || (effect & DND.FEEDBACK_INSERT_AFTER) != 0) { 247 boolean before = (effect & DND.FEEDBACK_INSERT_BEFORE) != 0; 248 257 TreeItem item = (TreeItem)tree.getDisplay().findWidget(tree.handle, hItem); 258 if (item != null) { 259 if (item != insertItem || before != insertBefore) { 260 tree.setInsertMark(item, before); 261 } 262 insertItem = item; 263 insertBefore = before; 264 } else { 265 if (insertItem != null) { 266 tree.setInsertMark(null, false); 267 } 268 insertItem = null; 269 } 270 } else { 271 if (insertItem != null) { 272 tree.setInsertMark(null, false); 273 } 274 insertItem = null; 275 } 276 } 277 } 278 | Popular Tags |