/// <summary> /// Inserts an element into the Collection at the specified index. /// </summary> /// <param name="index">The zero-based index at which item should be inserted.</param> /// <param name="item">The object to insert. The value can be null for reference types.</param> protected override void InsertItem(int index, CTreeNode item) { if (item.Parent != null) { throw new ArgumentException("The node is currently assigned to INodeContainer.");//item.OwnerCollection.Remove(item); } else { base.InsertItem(index, item); //item.OwnerCollection = this; if (owner is CTreeNode) { CTreeNode ownerNode = (CTreeNode)owner; item.ParentNode = ownerNode; item.Level = ownerNode.Level + 1; if (!(ownerNode.IsExpanded && ownerNode.Visible)) { item.Visible = false; } } if (ParentCTreeView != null) { item.OwnerCTreeView = ParentCTreeView; ParentCTreeView.SuspendLayout(); item.TraverseNodes(node => { ParentCTreeView.Controls.Add(node.Control); }); ParentCTreeView.ResumeLayout(false); ParentCTreeView.Recalculate(); } } }
/// <summary> /// Raises the MouseDown event. /// </summary> /// <param name="e">A MouseEventArgs that contains the event data.</param> protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left && ShowPlusMinus) { if (!Focused) { Focus(); //? } CTreeNode toggleNode = null; this.Nodes.TraverseNodes(node => node.Visible && node.Nodes.Count > 0, node => { Point cursorLocation = e.Location; cursorLocation.Offset(-AutoScrollPosition.X, -AutoScrollPosition.Y); if (node.PlusMinus != null && node.PlusMinus.IsUnderMouse(cursorLocation)) { toggleNode = node; } }); ClearSelection(); if (toggleNode != null) { toggleNode.Toggle(); if (SelectionMode != CTreeViewSelectionMode.None) { toggleNode.IsSelected = true; } } } base.OnMouseDown(e); }
/// <summary> /// Replaces the element at the specified index. /// </summary> /// <param name="index">The zero-based index of the element to replace.</param> /// <param name="item">The new value for the element at the specified index. The value can be null for reference types.</param> protected override void SetItem(int index, CTreeNode item) { //CTreeView.SuspendLayout(); BeginUpdateCTreeView(); RemoveItem(index); //? InsertItem(index, item); //? //base.SetItem(index, item); EndUpdateCTreeView(); //CTreeView.ResumeLayout(false); }
private void updateDragTargetPosition(CTreeNode nodeBefore, CTreeNode nodeAfter) { if (DragTargetPosition.NodeBefore != nodeBefore || DragTargetPosition.NodeAfter != nodeAfter) { DragTargetPosition = new DragTargetPositionClass(null, nodeBefore, nodeAfter); if (nodeBefore == null) { if (DrawStyle == CTreeViewDrawStyle.VerticalDiagram) { dragDropLinePoint1 = new Point(nodeAfter.BoundsSubtree.X - 2, nodeAfter.BoundsSubtree.Y); dragDropLinePoint2 = new Point(nodeAfter.BoundsSubtree.X - 2, nodeAfter.BoundsSubtree.Bottom); } else { dragDropLinePoint1 = new Point(nodeAfter.BoundsSubtree.X, nodeAfter.BoundsSubtree.Y - 2); dragDropLinePoint2 = new Point(nodeAfter.BoundsSubtree.Right, nodeAfter.BoundsSubtree.Y - 2); } } else if (nodeAfter == null) { if (DrawStyle == CTreeViewDrawStyle.VerticalDiagram) { dragDropLinePoint1 = new Point(nodeBefore.BoundsSubtree.Right + 2, nodeBefore.BoundsSubtree.Y); dragDropLinePoint2 = new Point(nodeBefore.BoundsSubtree.Right + 2, nodeBefore.BoundsSubtree.Bottom); } else { dragDropLinePoint1 = new Point(nodeBefore.BoundsSubtree.X, nodeBefore.BoundsSubtree.Bottom + 2); dragDropLinePoint2 = new Point(nodeBefore.BoundsSubtree.Right, nodeBefore.BoundsSubtree.Bottom + 2); } } else { if (DrawStyle == CTreeViewDrawStyle.VerticalDiagram) { int y1 = nodeBefore.BoundsSubtree.Y; int y2 = Math.Max(nodeBefore.BoundsSubtree.Bottom, nodeAfter.BoundsSubtree.Bottom); int x = nodeBefore.BoundsSubtree.Right + IndentWidth / 2; dragDropLinePoint1 = new Point(x, y1); dragDropLinePoint2 = new Point(x, y2); } else { int x1 = nodeBefore.BoundsSubtree.X; int x2 = Math.Max(nodeBefore.BoundsSubtree.Right, nodeAfter.BoundsSubtree.Right); int y = nodeBefore.BoundsSubtree.Bottom + IndentWidth / 2; dragDropLinePoint1 = new Point(x1, y); dragDropLinePoint2 = new Point(x2, y); } } dragDropRectangle = Rectangle.Empty; Refresh(); } }
private void updateDragTargetPosition(CTreeNode node) { if (DragTargetPosition.NodeDirect != node) { DragTargetPosition = new DragTargetPositionClass(node, null, null); dragDropRectangle = node.Bounds; dragDropRectangle.Inflate(2, 2); dragDropLinePoint1 = Point.Empty; dragDropLinePoint2 = Point.Empty; Refresh(); } }
/// <summary> /// Retrieves the tree node that is at the specified point. /// </summary> /// <param name="pt">The Point to evaluate and retrieve the node from.</param> /// <returns>The CTreeNode at the specified point, in client coordinates, or null if there is no node at that location.</returns> public CTreeNode GetNodeAt(Point pt) { bool success = false; CTreeNode nodeAtPoint = null; Nodes.TraverseNodes(node => node.Visible && !success, node => { if (node.Bounds.Contains(pt)) { nodeAtPoint = node; success = true; } }); return(nodeAtPoint); }
internal DragTargetPositionClass(CTreeNode nodeDirect, CTreeNode nodeBefore, CTreeNode nodeAfter) { _nodeDirect = nodeDirect; _nodeBefore = nodeBefore; _nodeAfter = nodeAfter; }
/// <summary> /// Sets the drag destination nodes according to specified cursor position. /// </summary> /// <param name="dragPosition">The position of mouse cursor during drag.</param> internal void SetDragTargetPosition(Point dragPosition) { CTreeNode destinationNode = null; CTreeNodeCollection destinationCollection = Nodes; Nodes.TraverseNodes(node => node.Visible && node.BoundsSubtree.Contains(dragPosition), node => { destinationNode = node; destinationCollection = node.Nodes; }); if (destinationNode != null && destinationNode.Bounds.Contains(dragPosition)) //Drag position within node { //Find drag position within node int delta, coordinate, firstBound, secondBound; if (DrawStyle == CTreeViewDrawStyle.VerticalDiagram) { delta = destinationNode.Bounds.Width / 4; coordinate = dragPosition.X; firstBound = destinationNode.Bounds.Left; secondBound = destinationNode.Bounds.Right; } else { delta = destinationNode.Bounds.Height / 4; coordinate = dragPosition.Y; firstBound = destinationNode.Bounds.Top; secondBound = destinationNode.Bounds.Bottom; } if (coordinate >= firstBound + delta && coordinate <= secondBound - delta) { updateDragTargetPosition(destinationNode); return; } else if (coordinate < firstBound + delta) //before { updateDragTargetPosition(destinationNode.PrevNode, destinationNode); return; } else if (coordinate > secondBound - delta) //after { updateDragTargetPosition(destinationNode, destinationNode.NextNode); return; } } else //Drag position out of the nodes { //Check drag position between two nodes CTreeNode upperNode = null, lowerNode = null; bool isBetween = false; for (int count = 0; count <= destinationCollection.Count - 2; count++) { upperNode = destinationCollection[count]; lowerNode = destinationCollection[count + 1]; Point betweenLocation = Point.Empty; Size betweenSize = Size.Empty; if (DrawStyle == CTreeViewDrawStyle.VerticalDiagram) { betweenLocation = new Point(upperNode.BoundsSubtree.Right, upperNode.BoundsSubtree.Top); betweenSize = new Size(lowerNode.BoundsSubtree.Left - upperNode.BoundsSubtree.Right, Math.Max(upperNode.BoundsSubtree.Height, lowerNode.BoundsSubtree.Height)); } else { betweenLocation = new Point(upperNode.BoundsSubtree.Left, upperNode.BoundsSubtree.Bottom); betweenSize = new Size(Math.Max(upperNode.BoundsSubtree.Width, lowerNode.BoundsSubtree.Width), lowerNode.BoundsSubtree.Top - upperNode.BoundsSubtree.Bottom); } Rectangle betweenRectangle = new Rectangle(betweenLocation, betweenSize); if (betweenRectangle.Contains(dragPosition)) { isBetween = true; break; } } if (isBetween) //Drag position between two nodes { updateDragTargetPosition(upperNode, lowerNode); return; } else if (destinationNode != null) { Rectangle ownerBounds = destinationNode.Bounds; bool isAbove, isBelow; if (DrawStyle == CTreeViewDrawStyle.VerticalDiagram) { isAbove = (dragPosition.X <= ownerBounds.Left); isBelow = (dragPosition.X >= ownerBounds.Right); } else { isAbove = (dragPosition.Y <= ownerBounds.Top); isBelow = (dragPosition.Y >= ownerBounds.Bottom); } if (isAbove) //before { updateDragTargetPosition(destinationNode.PrevNode, destinationNode); return; } else if (isBelow) //after { updateDragTargetPosition(destinationNode, destinationNode.NextNode); return; } } } updateDragTargetPosition(); }
/// <summary> /// Initializes a new instance of the CTreeViewEventArgs class for the specified tree node. /// </summary> /// <param name="node">The CTreeNode that the event is responding to.</param> public CTreeViewEventArgs(CTreeNode node) { Node = node; }