private static void MeasureNode(SqlParseTreeNode node, int depth, Dictionary <int, int> levelCounts, List <TreeNodeIcon> icons) { if (levelCounts.ContainsKey(depth) == false) { levelCounts.Add(depth, 0); } levelCounts[depth]++; TreeNodeIcon icon = new TreeNodeIcon(); icon.Node = node; icon.X = levelCounts[depth] - 1; icon.Y = depth; icon.Left = _leftMargin + icon.X * (_blockWidth + _spacingWidth); icon.Top = _topMargin + icon.Y * (_blockHeight + _spacingHeight); icon.Width = _blockWidth; icon.Height = _blockHeight; icons.Add(icon); foreach (SqlParseTreeNode child in node.Children) { MeasureNode(child, depth + 1, levelCounts, icons); } }
private void TreeViewImage_MouseClick(object sender, MouseEventArgs e) { string noArgumentsMessage = "[No arguments to display]"; Point mouseLocation = new Point(e.X, e.Y); NodeIcon selectedIcon = GetMatchingNodeIcon(mouseLocation); if (selectedIcon != null) { if (_currentToolTip != null) { _currentToolTip.Hide(treeViewImage); _currentToolTip.Dispose(); _currentToolTip = null; } string text = string.Empty; if (selectedIcon is TreeNodeIcon) { TreeNodeIcon treeIcon = selectedIcon as TreeNodeIcon; text = FormatText(treeIcon.Node.Arguments); if (string.IsNullOrEmpty(text)) { text = noArgumentsMessage; } text = treeIcon.Node.OperationName + Environment.NewLine + text; } else if (selectedIcon is MemoNodeIcon) { MemoNodeIcon memoIcon = selectedIcon as MemoNodeIcon; text = FormatText(memoIcon.Node.Arguments); if (string.IsNullOrEmpty(text)) { text = noArgumentsMessage; } text = memoIcon.Node.OperationName + Environment.NewLine + text; } text = text.Trim(); int numberOfLines = text.Count(c => c == '\r') + 1; _currentToolTip = new ToolTip(); _currentToolTip.IsBalloon = true; _currentToolTip.Show( text, treeViewImage, selectedIcon.Left + selectedIcon.Width - 25, selectedIcon.Top - 44 - 13 * numberOfLines); _toolTipMouseLocation = mouseLocation; _currentNodeIcon = selectedIcon; } }
private TreeNodeIcon PositionNode(SqlParseTreeNode node, int top, int left) { string nodeText = node.ToString(); SizeF textSize = _dummyGraphics.MeasureString(nodeText, _textFont); TreeNodeIcon icon = new TreeNodeIcon(); icon.X = left; icon.Y = top; icon.Left = left; icon.Top = top; icon.Width = (int)Math.Max(_iconWidth, textSize.Width); icon.Height = _iconHeight + _iconToTextSpacing + (int)textSize.Height; icon.Node = node; icon.Text = nodeText; icon.TextRectangle = new Rectangle(left + (icon.Width - (int)textSize.Width) / 2, top + _iconHeight + _iconToTextSpacing, (int)textSize.Width + 2, (int)textSize.Height + 2); icon.IconRectangle = new Rectangle(left + (icon.Width - _iconWidth) / 2, top, _iconWidth, _iconHeight); icon.Icon = TreeNodeIconMapper.GetIconForNode(node); return(icon); }
private static void PositionNodes(List <TreeNodeIcon> icons, out int width) { // Get the root node TreeNodeIcon root = icons.FirstOrDefault(i => i.Parent == null); if (root == null) { throw new ApplicationException("Cannot find root node!"); } width = _leftMargin + root.DescendantWidth + _rightMargin; root.Top = _topMargin; root.Left = width / 2 - _blockWidth / 2; int level = 0; List <TreeNodeIcon> nodes = new List <TreeNodeIcon>() { root }; while (nodes.Count > 0) { level++; foreach (TreeNodeIcon node in nodes) { node.Children.Sort((a, b) => a.X.CompareTo(b.X)); foreach (TreeNodeIcon child in node.Children) { int previousSiblingWidth = node.Children.Where(n => n.X < child.X).Sum(n => n.DescendantWidth) + node.Children.Count(n => n.X < child.X) * _interChildSpacingWidth; double relativePosition = 1.0 * (previousSiblingWidth + child.DescendantWidth / 2) / node.DescendantWidth; child.Top = _topMargin + (_blockHeight + _spacingHeight) * level; child.Left = (int)(node.Left - node.DescendantWidth / 2 + relativePosition * node.DescendantWidth); } } nodes = nodes.SelectMany(n => n.Children).ToList(); } }
private List <TreeNodeIcon> PositionSubtree(SqlParseTreeNode head, int top, int left) { List <TreeNodeIcon> icons = new List <TreeNodeIcon>(); int originalTop = top; //int adjust = _connectorVerticalSpacing * head.Children.Count / 2 - _interIconVerticalSpacing; int firstArrowTop = (2 * top + _iconHeight) / 2 - _connectorVerticalSpacing * (head.Children.Count - 1) / 2; if (top > firstArrowTop) { top += (top - firstArrowTop) + _iconHeight / 2; } TreeNodeIcon icon = PositionNode(head, top, left); icons.Add(icon); int horizontalSpacing = Math.Max(_interIconHorizontalSpacing, _connectorHorizontalSpacing * head.Children.Count); int newLeft = left + icon.Width + horizontalSpacing; //int newTop = originalTop; int altTop = (icon.IconRectangle.Top + icon.IconRectangle.Bottom) / 2 - _connectorVerticalSpacing * (icon.Children.Count - 1) / 2; int newTop = Math.Min(originalTop, altTop); foreach (SqlParseTreeNode child in head.Children) { List <TreeNodeIcon> childIcons = PositionSubtree(child, newTop, newLeft); if (childIcons.Count > 0) { childIcons[0].Parent = icon; icon.Children.Add(childIcons[0]); } icons.AddRange(childIcons); newTop += childIcons.GetHeight() + _interIconVerticalSpacing; } return(icons); }