DragDrop

I'm trying to create a control where an arbitrary object (for example a file dragged from windows explorer) can be dragged over certain GOObjects in the diagram and dropped onto the GoObject at which time I can do some arbitrary thing with the dropped object.<?:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

But I am not sure how to get or handle drag&drop events. i.e. I need to somehow get the DragEnter/DragOver/DragDrop type events when the external object is dragged over different objects in the diagram.

The GoView itself has the normal DragEnter/DragOver/DragDrop/etc. events but these are only for the view as a whole and not for individual objects in the view. I’m assuming I will need to do something with the GoToolDragging but I’m not sure how to tackle this. Most topics on this in the forum seem to be related to dragging GoObjects onto other GoObjects which is not quite what I want to achieve.

Any help will be appreciated.

OK, here’s a simple example, handling drag-and-drops from a TreeView. Some of the functionality requires overriding methods, so I have put everything into a class that inherits from GoView.
The first method shows one simple way of deciding whether the drop should be allowed, by setting DragEventArgs.Effect. Of course your decision making will probably need to be more complicated, perhaps calling a predicate on the potential target object and perhaps considering the nature of the IDataObject being dragged.
The second method actually handles the drop by creating a GoObject and adding it to the document. Presumably you know what you want to create based on the IDataObject for the drag. Note how the real drop point, in document coordinates, is calculated.
Optionally, the third method controls what is shown while dragging, before the drop. In this example it just creates the object that would be created on a drop. That object gets added to the view and is discarded when the mouse leaves the view or when the drop actually occurs or is cancelled.
public class SpecialView : GoView {
public SpecialView() {}
// Disable an external drop onto a GoGeneralNode
protected override void DoExternalDrag(DragEventArgs evt) {
base.DoExternalDrag(evt); // also do standard behavior
GoObject over = PickObject(true, false, this.LastInput.DocPoint, true);
if (over is GoGeneralNode)
evt.Effect = DragDropEffects.None;
}
// Handle a drop from a TreeView by creating a GoText object
// at the document’s point of the drop, within a transaction,
// and by making that object the view’s new selection.
protected override IGoCollection DoExternalDrop(DragEventArgs evt) {
IDataObject data = evt.Data;
Object treenodeobj = data.GetData(typeof(TreeNode));
if (treenodeobj is TreeNode) {
TreeNode treenode = (TreeNode)treenodeobj;
Point screenPnt = new Point(evt.X, evt.Y);
Point viewPnt = PointToClient(screenPnt);
PointF docPnt = ConvertViewToDoc(viewPnt);
StartTransaction();
GoText t = new GoText();
t.Text = treenode.Text;
t.Position = docPnt;
this.Document.Add(t);
this.Selection.Clear();
this.Selection.Add(t);
FinishTransaction(“dropped from tree”);
RaiseExternalObjectsDropped(this.LastInput);
this.Selection.HotSpot = new SizeF(0, 0);
return this.Selection;
} else {
return base.DoExternalDrop(evt);
}
}
// This override controls what is shown (if anything) during
// an external drag into this view.
// The default behavior supports showing an image of the selection
// if the drag started in another GoView.
protected override GoObject GetExternalDragImage(DragEventArgs evt) {
IDataObject data = evt.Data;
Object treenodeobj = data.GetData(typeof(TreeNode));
if (treenodeobj is TreeNode) {
TreeNode treenode = (TreeNode)treenodeobj;
GoText t = new GoText();
t.Text = treenode.Text;
return t;
} else {
return base.GetExternalDragImage(evt);
}
}
}

Thanks, that’s what I needed.