Пример #1
0
        /// <summary>
        /// Draw the NodeCollection instance.
        /// </summary>
        /// <param name="tc">Reference to owning TreeControl.</param>
        /// <param name="nc">Reference to NodeCollection instance.</param>
        /// <param name="g">Reference to Graphics instance to draw into.</param>
        /// <param name="clipRectangle">Clipping rectangle used when drawing.</param>
        /// <param name="preDraw">True when drawing before children and fales for drawing after.</param>
        public virtual void Draw(TreeControl tc,
                                 NodeCollection nc,
                                 Graphics g,
                                 Rectangle clipRectangle,
                                 bool preDraw)
        {
            if (preDraw)
            {
                // Only draw lines and boxes if there is at least one child node
                if (nc.VisibleCount > 0)
                {
                    // Find out if lines and boxes needed drawing
                    bool showLines      = ShowLines(tc, nc);
                    bool showBoxes      = ShowBoxes(tc, nc);
                    bool showIndicators = (IsRootCollection(tc, nc) && (tc.Indicators != Indicators.None)) ||
                                          (!IsRootCollection(tc, nc) && (tc.Indicators == Indicators.AtGroup));

                    // Is there any drawing to perform?
                    if (showLines || showBoxes || showIndicators)
                    {
                        Pen lineDashPen = tc.GetCacheLineDashPen();

                        // Cache the whole rectangle used for drawing
                        Rectangle ncBounds = nc.Cache.Bounds;

                        // Find the size of space taken up with indicators
                        Size indicatorSize = showIndicators ? tc.IndicatorSize : Size.Empty;

                        // Perform any adjustment for drawing within the bounds
                        AdjustBeforeDrawing(tc, nc, ref ncBounds);

                        // Cache the mid and right hand side of the lines column
                        int xMid   = ncBounds.Left + indicatorSize.Width + (tc.ColumnWidth / 2) - 1;
                        int xRight = ncBounds.Left + indicatorSize.Width + tc.ColumnWidth;

                        // Need to adjust drawing child connecting lines, so that wider than one
                        // pixel pens start from left of vertical for top and bottom children, but
                        // are placed to the right of middle children.
                        int xMidAdjustL = xMid - (tc.LineWidth / 2);
                        int xMidAdjustR = xMid + 1;

                        // Find the first and last visible nodes
                        int firstVisible = nc.FirstVisibleIndex;
                        int lastVisible  = nc.LastVisibleIndex;

                        // Find the first and last nodes mid points
                        int yMidFirst = nc[firstVisible].Cache.Bounds.Top + (nc[firstVisible].Cache.Bounds.Height / 2);
                        int yMidLast  = nc[lastVisible].Cache.Bounds.Top + (nc[lastVisible].Cache.Bounds.Height / 2);

                        // No point drawing line after end of clipping rectangle
                        if (yMidLast > clipRectangle.Bottom)
                        {
                            yMidLast = clipRectangle.Bottom;
                        }

                        // Do we need to draw the long vertical line?
                        if (showLines)
                        {
                            // Draw the long vertical line down the middle
                            if (IsRootCollection(tc, nc))
                            {
                                // No point drawing line before start of clipping rectangle
                                if (yMidFirst < clipRectangle.Top)
                                {
                                    yMidFirst = clipRectangle.Top;

                                    // Adjust starting point up so dot/dash lines are consistent
                                    switch (tc.LineDashStyle)
                                    {
                                    case LineDashStyle.Dot:
                                        yMidFirst -= yMidFirst % (2 * tc.LineWidth);
                                        break;

                                    case LineDashStyle.Dash:
                                        yMidFirst -= yMidFirst % (4 * tc.LineWidth);
                                        break;
                                    }
                                }

                                // We are root collection, so draw from the first node to the last
                                g.DrawLine(lineDashPen, xMid, yMidFirst, xMid, yMidLast);
                            }
                            else
                            {
                                // Not at root, so draw from top of column to the last node
                                g.DrawLine(lineDashPen, xMid, ncBounds.Top, xMid, yMidLast);
                            }
                        }

                        // Process each child node in turn, drawing horizontal line and any box
                        for (int i = nc.ChildFromY(clipRectangle.Top); i < nc.Count; i++)
                        {
                            if (DrawNode(tc, nc[i], g, clipRectangle, showLines, showBoxes,
                                         showIndicators, (i == 0) || (i == (nc.Count - 1)),
                                         lineDashPen, xMidAdjustL, xMidAdjustR, xMid, xRight,
                                         ncBounds.Left))
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }