| 1 |
/******************************************************************************* |
|---|
| 2 |
* Copyright (c) 2007, 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 dwt.dnd.TableDropTargetEffect; |
|---|
| 14 |
|
|---|
| 15 |
import dwt.graphics.Point; |
|---|
| 16 |
import dwt.internal.win32.OS; |
|---|
| 17 |
import dwt.widgets.Table; |
|---|
| 18 |
import dwt.widgets.TableItem; |
|---|
| 19 |
|
|---|
| 20 |
import dwt.dnd.DropTargetEffect; |
|---|
| 21 |
import dwt.dnd.DropTargetEvent; |
|---|
| 22 |
import dwt.dnd.DND; |
|---|
| 23 |
|
|---|
| 24 |
import dwt.dwthelper.utils; |
|---|
| 25 |
|
|---|
| 26 |
/** |
|---|
| 27 |
* This class provides a default drag under effect (eg. select, insert and scroll) |
|---|
| 28 |
* when a drag occurs over a <code>Table</code>. |
|---|
| 29 |
* |
|---|
| 30 |
* <p>Classes that wish to provide their own drag under effect for a <code>Table</code> |
|---|
| 31 |
* can extend the <code>TableDropTargetEffect</code> and override any applicable methods |
|---|
| 32 |
* in <code>TableDropTargetEffect</code> to display their own drag under effect.</p> |
|---|
| 33 |
* |
|---|
| 34 |
* Subclasses that override any methods of this class must call the corresponding |
|---|
| 35 |
* <code>super</code> method to get the default drag under effect implementation. |
|---|
| 36 |
* |
|---|
| 37 |
* <p>The feedback value is either one of the FEEDBACK constants defined in |
|---|
| 38 |
* class <code>DND</code> which is applicable to instances of this class, |
|---|
| 39 |
* or it must be built by <em>bitwise OR</em>'ing together |
|---|
| 40 |
* (that is, using the <code>int</code> "|" operator) two or more |
|---|
| 41 |
* of those <code>DND</code> effect constants. |
|---|
| 42 |
* </p> |
|---|
| 43 |
* <p> |
|---|
| 44 |
* <dl> |
|---|
| 45 |
* <dt><b>Feedback:</b></dt> |
|---|
| 46 |
* <dd>FEEDBACK_SELECT, FEEDBACK_SCROLL</dd> |
|---|
| 47 |
* </dl> |
|---|
| 48 |
* </p> |
|---|
| 49 |
* |
|---|
| 50 |
* @see DropTargetAdapter |
|---|
| 51 |
* @see DropTargetEvent |
|---|
| 52 |
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> |
|---|
| 53 |
* |
|---|
| 54 |
* @since 3.3 |
|---|
| 55 |
*/ |
|---|
| 56 |
public class TableDropTargetEffect : DropTargetEffect { |
|---|
| 57 |
static final int SCROLL_HYSTERESIS = 200; // milli seconds |
|---|
| 58 |
|
|---|
| 59 |
int scrollIndex = -1; |
|---|
| 60 |
long scrollBeginTime; |
|---|
| 61 |
TableItem dropHighlight; |
|---|
| 62 |
|
|---|
| 63 |
/** |
|---|
| 64 |
* Creates a new <code>TableDropTargetEffect</code> to handle the drag under effect on the specified |
|---|
| 65 |
* <code>Table</code>. |
|---|
| 66 |
* |
|---|
| 67 |
* @param table the <code>Table</code> over which the user positions the cursor to drop the data |
|---|
| 68 |
*/ |
|---|
| 69 |
public this(Table table) { |
|---|
| 70 |
super(table); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
int checkEffect(int effect) { |
|---|
| 74 |
// Some effects are mutually exclusive. Make sure that only one of the mutually exclusive effects has been specified. |
|---|
| 75 |
if ((effect & DND.FEEDBACK_SELECT) !is 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER & ~DND.FEEDBACK_INSERT_BEFORE; |
|---|
| 76 |
if ((effect & DND.FEEDBACK_INSERT_BEFORE) !is 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER; |
|---|
| 77 |
return effect; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
/** |
|---|
| 81 |
* This implementation of <code>dragEnter</code> provides a default drag under effect |
|---|
| 82 |
* for the feedback specified in <code>event.feedback</code>. |
|---|
| 83 |
* |
|---|
| 84 |
* For additional information see <code>DropTargetAdapter.dragEnter</code>. |
|---|
| 85 |
* |
|---|
| 86 |
* Subclasses that override this method should call <code>super.dragEnter(event)</code> |
|---|
| 87 |
* to get the default drag under effect implementation. |
|---|
| 88 |
* |
|---|
| 89 |
* @param event the information associated with the drag enter event |
|---|
| 90 |
* |
|---|
| 91 |
* @see DropTargetAdapter |
|---|
| 92 |
* @see DropTargetEvent |
|---|
| 93 |
*/ |
|---|
| 94 |
public void dragEnter(DropTargetEvent event) { |
|---|
| 95 |
scrollBeginTime = 0; |
|---|
| 96 |
scrollIndex = -1; |
|---|
| 97 |
dropHighlight = null; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
/** |
|---|
| 101 |
* This implementation of <code>dragLeave</code> provides a default drag under effect |
|---|
| 102 |
* for the feedback specified in <code>event.feedback</code>. |
|---|
| 103 |
* |
|---|
| 104 |
* For additional information see <code>DropTargetAdapter.dragLeave</code>. |
|---|
| 105 |
* |
|---|
| 106 |
* Subclasses that override this method should call <code>super.dragLeave(event)</code> |
|---|
| 107 |
* to get the default drag under effect implementation. |
|---|
| 108 |
* |
|---|
| 109 |
* @param event the information associated with the drag leave event |
|---|
| 110 |
* |
|---|
| 111 |
* @see DropTargetAdapter |
|---|
| 112 |
* @see DropTargetEvent |
|---|
| 113 |
*/ |
|---|
| 114 |
public void dragLeave(DropTargetEvent event) { |
|---|
| 115 |
Table table = cast(Table) control; |
|---|
| 116 |
auto handle = table.handle; |
|---|
| 117 |
if (dropHighlight !is null) { |
|---|
| 118 |
LVITEM lvItem; |
|---|
| 119 |
lvItem.stateMask = OS.LVIS_DROPHILITED; |
|---|
| 120 |
OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, &lvItem); |
|---|
| 121 |
dropHighlight = null; |
|---|
| 122 |
} |
|---|
| 123 |
scrollBeginTime = 0; |
|---|
| 124 |
scrollIndex = -1; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
/** |
|---|
| 128 |
* This implementation of <code>dragOver</code> provides a default drag under effect |
|---|
| 129 |
* for the feedback specified in <code>event.feedback</code>. The class description |
|---|
| 130 |
* lists the FEEDBACK constants that are applicable to the class. |
|---|
| 131 |
* |
|---|
| 132 |
* For additional information see <code>DropTargetAdapter.dragOver</code>. |
|---|
| 133 |
* |
|---|
| 134 |
* Subclasses that override this method should call <code>super.dragOver(event)</code> |
|---|
| 135 |
* to get the default drag under effect implementation. |
|---|
| 136 |
* |
|---|
| 137 |
* @param event the information associated with the drag over event |
|---|
| 138 |
* |
|---|
| 139 |
* @see DropTargetAdapter |
|---|
| 140 |
* @see DropTargetEvent |
|---|
| 141 |
* @see DND#FEEDBACK_SELECT |
|---|
| 142 |
* @see DND#FEEDBACK_SCROLL |
|---|
| 143 |
*/ |
|---|
| 144 |
public void dragOver(DropTargetEvent event) { |
|---|
| 145 |
Table table = cast(Table) getControl(); |
|---|
| 146 |
int effect = checkEffect(event.feedback); |
|---|
| 147 |
auto handle = table.handle; |
|---|
| 148 |
Point coordinates = new Point(event.x, event.y); |
|---|
| 149 |
coordinates = table.toControl(coordinates); |
|---|
| 150 |
LVHITTESTINFO pinfo; |
|---|
| 151 |
pinfo.pt.x = coordinates.x; |
|---|
| 152 |
pinfo.pt.y = coordinates.y; |
|---|
| 153 |
OS.SendMessage(handle, OS.LVM_HITTEST, 0, &pinfo); |
|---|
| 154 |
if ((effect & DND.FEEDBACK_SCROLL) is 0) { |
|---|
| 155 |
scrollBeginTime = 0; |
|---|
| 156 |
scrollIndex = -1; |
|---|
| 157 |
} else { |
|---|
| 158 |
if (pinfo.iItem !is -1 && scrollIndex is pinfo.iItem && scrollBeginTime !is 0) { |
|---|
| 159 |
if (System.currentTimeMillis() >= scrollBeginTime) { |
|---|
| 160 |
int top = Math.max (0, OS.SendMessage (handle, OS.LVM_GETTOPINDEX, 0, 0)); |
|---|
| 161 |
int count = OS.SendMessage (handle, OS.LVM_GETITEMCOUNT, 0, 0); |
|---|
| 162 |
int index = (scrollIndex - 1 < top) ? Math.max(0, scrollIndex - 1) : Math.min(count - 1, scrollIndex + 1); |
|---|
| 163 |
bool scroll = true; |
|---|
| 164 |
if (pinfo.iItem is top) { |
|---|
| 165 |
scroll = pinfo.iItem !is index; |
|---|
| 166 |
} else { |
|---|
| 167 |
RECT itemRect; |
|---|
| 168 |
itemRect.left = OS.LVIR_BOUNDS; |
|---|
| 169 |
if (OS.SendMessage (handle, OS.LVM_GETITEMRECT, pinfo.iItem, &itemRect) !is 0) { |
|---|
| 170 |
RECT rect; |
|---|
| 171 |
OS.GetClientRect (handle, &rect); |
|---|
| 172 |
POINT pt; |
|---|
| 173 |
pt.x = itemRect.left; |
|---|
| 174 |
pt.y = itemRect.top; |
|---|
| 175 |
if (OS.PtInRect (&rect, pt)) { |
|---|
| 176 |
pt.y = itemRect.bottom; |
|---|
| 177 |
if (OS.PtInRect (&rect, pt)) scroll = false; |
|---|
| 178 |
} |
|---|
| 179 |
} |
|---|
| 180 |
} |
|---|
| 181 |
if (scroll) { |
|---|
| 182 |
OS.SendMessage (handle, OS.LVM_ENSUREVISIBLE, index, 0); |
|---|
| 183 |
table.redraw(); |
|---|
| 184 |
} |
|---|
| 185 |
scrollBeginTime = 0; |
|---|
| 186 |
scrollIndex = -1; |
|---|
| 187 |
} |
|---|
| 188 |
} else { |
|---|
| 189 |
scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS; |
|---|
| 190 |
scrollIndex = pinfo.iItem; |
|---|
| 191 |
} |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
if (pinfo.iItem !is -1 && (effect & DND.FEEDBACK_SELECT) !is 0) { |
|---|
| 195 |
TableItem item = table.getItem(pinfo.iItem); |
|---|
| 196 |
if (dropHighlight !is item) { |
|---|
| 197 |
LVITEM lvItem; |
|---|
| 198 |
lvItem.stateMask = OS.LVIS_DROPHILITED; |
|---|
| 199 |
OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, &lvItem); |
|---|
| 200 |
lvItem.state = OS.LVIS_DROPHILITED; |
|---|
| 201 |
OS.SendMessage(handle, OS.LVM_SETITEMSTATE, pinfo.iItem, &lvItem); |
|---|
| 202 |
dropHighlight = item; |
|---|
| 203 |
} |
|---|
| 204 |
} else { |
|---|
| 205 |
if (dropHighlight !is null) { |
|---|
| 206 |
LVITEM lvItem; |
|---|
| 207 |
lvItem.stateMask = OS.LVIS_DROPHILITED; |
|---|
| 208 |
OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, &lvItem); |
|---|
| 209 |
dropHighlight = null; |
|---|
| 210 |
} |
|---|
| 211 |
} |
|---|
| 212 |
} |
|---|
| 213 |
} |
|---|