1 /******************************************************************************* 2 * Copyright (c) 2000, 2008 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 * Port to the D programming language: 11 * Frank Benoit <benoit@tionex.de> 12 *******************************************************************************/ 13 module org.eclipse.swt.dnd.TreeDropTargetEffect; 14 15 import java.lang.all; 16 17 18 import org.eclipse.swt.graphics.Point; 19 import org.eclipse.swt.internal.gtk.OS; 20 import org.eclipse.swt.widgets.Tree; 21 import org.eclipse.swt.dnd.DropTargetEffect; 22 import org.eclipse.swt.dnd.DropTargetEvent; 23 import org.eclipse.swt.dnd.DND; 24 25 /** 26 * This class provides a default drag under effect (eg. select, insert, scroll and expand) 27 * when a drag occurs over a <code>Tree</code>. 28 * 29 * <p>Classes that wish to provide their own drag under effect for a <code>Tree</code> 30 * can extend the <code>TreeDropTargetEffect</code> class and override any applicable methods 31 * in <code>TreeDropTargetEffect</code> to display their own drag under effect.</p> 32 * 33 * Subclasses that override any methods of this class must call the corresponding 34 * <code>super</code> method to get the default drag under effect implementation. 35 * 36 * <p>The feedback value is either one of the FEEDBACK constants defined in 37 * class <code>DND</code> which is applicable to instances of this class, 38 * or it must be built by <em>bitwise OR</em>'ing together 39 * (that is, using the <code>int</code> "|" operator) two or more 40 * of those <code>DND</code> effect constants. 41 * </p> 42 * <p> 43 * <dl> 44 * <dt><b>Feedback:</b></dt> 45 * <dd>FEEDBACK_SELECT, FEEDBACK_INSERT_BEFORE, FEEDBACK_INSERT_AFTER, FEEDBACK_EXPAND, FEEDBACK_SCROLL</dd> 46 * </dl> 47 * </p><p> 48 * Note: Only one of the styles FEEDBACK_SELECT, FEEDBACK_INSERT_BEFORE or 49 * FEEDBACK_INSERT_AFTER may be specified. 50 * </p> 51 * 52 * @see DropTargetAdapter 53 * @see DropTargetEvent 54 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> 55 * 56 * @since 3.3 57 */ 58 public class TreeDropTargetEffect : DropTargetEffect { 59 static const int SCROLL_HYSTERESIS = 150; // milli seconds 60 static const int EXPAND_HYSTERESIS = 1000; // milli seconds 61 62 int scrollIndex = -1; 63 long scrollBeginTime; 64 65 int expandIndex = -1; 66 long expandBeginTime; 67 68 /** 69 * Creates a new <code>TreeDropTargetEffect</code> to handle the drag under effect on the specified 70 * <code>Tree</code>. 71 * 72 * @param tree the <code>Tree</code> over which the user positions the cursor to drop the data 73 */ 74 public this(Tree tree) { 75 super(tree); 76 } 77 78 int checkEffect(int effect) { 79 // Some effects are mutually exclusive. Make sure that only one of the mutually exclusive effects has been specified. 80 if ((effect & DND.FEEDBACK_SELECT) !is 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER & ~DND.FEEDBACK_INSERT_BEFORE; 81 if ((effect & DND.FEEDBACK_INSERT_BEFORE) !is 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER; 82 return effect; 83 } 84 85 /** 86 * This implementation of <code>dragEnter</code> provides a default drag under effect 87 * for the feedback specified in <code>event.feedback</code>. 88 * 89 * For additional information see <code>DropTargetAdapter.dragEnter</code>. 90 * 91 * Subclasses that override this method should call <code>super.dragEnter(event)</code> 92 * to get the default drag under effect implementation. 93 * 94 * @param event the information associated with the drag enter event 95 * 96 * @see DropTargetAdapter 97 * @see DropTargetEvent 98 */ 99 public override void dragEnter(DropTargetEvent event) { 100 expandBeginTime = 0; 101 expandIndex = -1; 102 scrollBeginTime = 0; 103 scrollIndex = -1; 104 } 105 106 /** 107 * This implementation of <code>dragLeave</code> provides a default drag under effect 108 * for the feedback specified in <code>event.feedback</code>. 109 * 110 * For additional information see <code>DropTargetAdapter.dragLeave</code>. 111 * 112 * Subclasses that override this method should call <code>super.dragLeave(event)</code> 113 * to get the default drag under effect implementation. 114 * 115 * @param event the information associated with the drag leave event 116 * 117 * @see DropTargetAdapter 118 * @see DropTargetEvent 119 */ 120 public override void dragLeave(DropTargetEvent event) { 121 Tree tree = cast(Tree) control; 122 auto handle = tree.handle; 123 OS.gtk_tree_view_set_drag_dest_row(handle, null, OS.GTK_TREE_VIEW_DROP_BEFORE); 124 125 scrollBeginTime = 0; 126 scrollIndex = -1; 127 expandBeginTime = 0; 128 expandIndex = -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>. 134 * 135 * For additional information see <code>DropTargetAdapter.dragOver</code>. 136 * 137 * Subclasses that override this method should call <code>super.dragOver(event)</code> 138 * to get the default drag under effect implementation. 139 * 140 * @param event the information associated with the drag over event 141 * 142 * @see DropTargetAdapter 143 * @see DropTargetEvent 144 * @see DND#FEEDBACK_SELECT 145 * @see DND#FEEDBACK_INSERT_BEFORE 146 * @see DND#FEEDBACK_INSERT_AFTER 147 * @see DND#FEEDBACK_SCROLL 148 */ 149 public override void dragOver(DropTargetEvent event) { 150 Tree tree = cast(Tree) control; 151 int effect = checkEffect(event.feedback); 152 153 auto handle = tree.handle; 154 Point coordinates = new Point(event.x, event.y); 155 coordinates = tree.toControl(coordinates); 156 void* path; 157 OS.gtk_tree_view_get_path_at_pos (handle, coordinates.x, coordinates.y, &path, null, null, null); 158 int index = -1; 159 if (path !is null) { 160 auto indices = OS.gtk_tree_path_get_indices(path); 161 if (indices !is null) { 162 ptrdiff_t depth = OS.gtk_tree_path_get_depth(path); 163 index = indices[depth - 1]; 164 } 165 } 166 if ((effect & DND.FEEDBACK_SCROLL) is 0) { 167 scrollBeginTime = 0; 168 scrollIndex = -1; 169 } else { 170 if (index !is -1 && scrollIndex is index && scrollBeginTime !is 0) { 171 if (System.currentTimeMillis() >= scrollBeginTime) { 172 GdkRectangle cellRect; 173 OS.gtk_tree_view_get_cell_area (handle, path, null, &cellRect); 174 if (cellRect.y < cellRect.height) { 175 int tx, ty; 176 OS.gtk_tree_view_widget_to_tree_coords(handle, cellRect.x, cellRect.y - cellRect.height, &tx, &ty); 177 OS.gtk_tree_view_scroll_to_point (handle, -1, ty); 178 } else { 179 //scroll down 180 OS.gtk_tree_view_get_path_at_pos (handle, coordinates.x, coordinates.y + cellRect.height, &path, null, null, null); 181 if (path !is null) { 182 OS.gtk_tree_view_scroll_to_cell(handle, path, null, false, 0, 0); 183 OS.gtk_tree_path_free(path); 184 path = null; 185 } 186 OS.gtk_tree_view_get_path_at_pos (handle, coordinates.x, coordinates.y, &path, null, null, null); 187 } 188 scrollBeginTime = 0; 189 scrollIndex = -1; 190 } 191 } else { 192 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS; 193 scrollIndex = index; 194 } 195 } 196 if ((effect & DND.FEEDBACK_EXPAND) is 0) { 197 expandBeginTime = 0; 198 expandIndex = -1; 199 } else { 200 if (index !is -1 && expandIndex is index && expandBeginTime !is 0) { 201 if (System.currentTimeMillis() >= expandBeginTime) { 202 OS.gtk_tree_view_expand_row (handle, path, false); 203 expandBeginTime = 0; 204 expandIndex = -1; 205 } 206 } else { 207 expandBeginTime = System.currentTimeMillis() + EXPAND_HYSTERESIS; 208 expandIndex = index; 209 } 210 } 211 if (path !is null) { 212 int position = -1; 213 if ((effect & DND.FEEDBACK_SELECT) !is 0) position = OS.GTK_TREE_VIEW_DROP_INTO_OR_BEFORE; 214 if ((effect & DND.FEEDBACK_INSERT_BEFORE) !is 0) position = OS.GTK_TREE_VIEW_DROP_BEFORE; 215 if ((effect & DND.FEEDBACK_INSERT_AFTER) !is 0) position = OS.GTK_TREE_VIEW_DROP_AFTER; 216 if (position !is -1) { 217 OS.gtk_tree_view_set_drag_dest_row(handle, path, position); 218 } else { 219 OS.gtk_tree_view_set_drag_dest_row(handle, null, OS.GTK_TREE_VIEW_DROP_BEFORE); 220 } 221 } else { 222 OS.gtk_tree_view_set_drag_dest_row(handle, null, OS.GTK_TREE_VIEW_DROP_BEFORE); 223 } 224 225 if (path !is null) OS.gtk_tree_path_free (path ); 226 } 227 228 }