private void MultiSelectTreeviewElementsGroupsDragFinishingHandler(object sender, DragFinishingEventArgs e)
{
// we want to finish off the drag ourselves, and not have the treeview control move the nodes around.
// (In fact, in a lame attempt at 'data binding', we're going to completely redraw the tree after
// making all the required changes from this drag-drop.) So set the flag to indicate that.
e.FinishDrag = false;
// first determine the node that they will be moved to. This will depend on if we are dragging onto a node
// directly, or above/below one to reorder.
ElementNode newParentNode = null; // the ElementNode that the selected items will move to
TreeNode expandNode = null; // if we need to expand a node once we've moved everything
int index = -1;
if (e.DragBetweenNodes == DragBetweenNodes.DragOnTargetNode) {
newParentNode = e.TargetNode.Tag as ElementNode;
expandNode = e.TargetNode;
} else if (e.DragBetweenNodes == DragBetweenNodes.DragBelowTargetNode && e.TargetNode.IsExpanded) {
newParentNode = e.TargetNode.Tag as ElementNode;
expandNode = e.TargetNode;
index = 0;
} else {
if (e.TargetNode.Parent == null) {
newParentNode = null; // needs to go at the root level
} else {
newParentNode = e.TargetNode.Parent.Tag as ElementNode;
}
if (e.DragBetweenNodes == DragBetweenNodes.DragAboveTargetNode) {
index = e.TargetNode.Index;
} else {
index = e.TargetNode.Index + 1;
}
}
// Check to see if the new parent node would be 'losing' the Element (ie. becoming a
// group instead of a leaf node with a element/patches). Prompt the user first.
if (CheckIfNodeWillLosePatches(newParentNode))
return;
// If moving element nodes, we need to iterate through all selected treenodes, and remove them from
// the parent in which they are selected (which we can determine from the treenode parent), and add them
// to the target node. If copying, we need to just add them to the new parent node.
foreach (TreeNode treeNode in e.SourceNodes) {
ElementNode sourceNode = treeNode.Tag as ElementNode;
ElementNode oldParentNode = (treeNode.Parent != null) ? treeNode.Parent.Tag as ElementNode : null;
int currentIndex = treeNode.Index;
if (e.DragMode == DragDropEffects.Move) {
if (index >= 0) {
VixenSystem.Nodes.MoveNode(sourceNode, newParentNode, oldParentNode, index);
// if we're moving nodes within the same group, but earlier in the group, then increment the target position each time.
// This is because when the target is AFTER the current position, the shuffling offsets the nodes so that the target
// index can stay the same. This isn't the case for the reverse case.
if (newParentNode == oldParentNode && index < currentIndex)
index++;
} else {
VixenSystem.Nodes.MoveNode(sourceNode, newParentNode, oldParentNode);
}
} else if (e.DragMode == DragDropEffects.Copy) {
if (index >= 0) {
// increment the index after every move, so the items are inserted in the correct order (if not, they would be reversed)
VixenSystem.Nodes.AddChildToParent(sourceNode, newParentNode, index++);
} else {
VixenSystem.Nodes.AddChildToParent(sourceNode, newParentNode);
}
} else {
VixenSystem.Logging.Warning("ConfigElements: Trying to deal with a drag that is an unknown type!");
}
}
if (expandNode != null)
expandNode.Expand();
PopulateNodeTree();
PopulateFormWithNode((multiSelectTreeviewElementsGroups.SelectedNode == null) ? null : (multiSelectTreeviewElementsGroups.SelectedNode.Tag as ElementNode), true);
}