コード例 #1
0
        private static bool TryCalculatePreferredSize(IArrangedElement container, bool measureOnly, out Size preferredSize)
        {
            ArrangedElementCollection children = container.Children;

            // PreferredSize is garbage unless measureOnly is specified
            preferredSize = new Size(-7103, -7105);

            // Short circuit for items with no children
            if (!measureOnly && children.Count == 0)
            {
                return(CommonProperties.GetAutoSize(container));
            }

            bool dock     = false;
            bool anchor   = false;
            bool autoSize = false;

            for (int i = children.Count - 1; i >= 0; i--)
            {
                IArrangedElement element = children[i];
                if (CommonProperties.GetNeedsDockAndAnchorLayout(element))
                {
                    if (!dock && CommonProperties.GetNeedsDockLayout(element))
                    {
                        dock = true;
                    }

                    if (!anchor && CommonProperties.GetNeedsAnchorLayout(element))
                    {
                        anchor = true;
                    }

                    if (!autoSize && CommonProperties.xGetAutoSizedAndAnchored(element))
                    {
                        autoSize = true;
                    }
                }
            }

            Debug.WriteLineIf(CompModSwitches.RichLayout.TraceInfo, "\tanchor : " + anchor.ToString());
            Debug.WriteLineIf(CompModSwitches.RichLayout.TraceInfo, "\tdock :   " + dock.ToString());

            Size preferredSizeForDocking   = Size.Empty;
            Size preferredSizeForAnchoring = Size.Empty;

            if (dock)
            {
                preferredSizeForDocking = LayoutDockedControls(container, measureOnly);
            }

            if (anchor && !measureOnly)
            {
                // In the case of anchor, where we currently are defines the preferred size,
                // so dont recalculate the positions of everything.
                LayoutAnchoredControls(container);
            }

            if (autoSize)
            {
                LayoutAutoSizedControls(container);
            }

            if (!measureOnly)
            {
                // Set the anchored controls to their computed positions.
                ApplyCachedBounds(container);
            }
            else
            {
                // Finish the preferredSize computation and clear cached anchored positions.
                preferredSizeForAnchoring = GetAnchorPreferredSize(container);

                Padding containerPadding = Padding.Empty;
                if (container is Control control)
                {
                    // Calling this will respect Control.DefaultPadding.
                    containerPadding = control.Padding;
                }
                else
                {
                    // Not likely to happen but handle this gracefully.
                    containerPadding = CommonProperties.GetPadding(container, Padding.Empty);
                }

                preferredSizeForAnchoring.Width  -= containerPadding.Left;
                preferredSizeForAnchoring.Height -= containerPadding.Top;

                ClearCachedBounds(container);
                preferredSize = LayoutUtils.UnionSizes(preferredSizeForDocking, preferredSizeForAnchoring);
            }

            return(CommonProperties.GetAutoSize(container));
        }
コード例 #2
0
        private static bool xLayout(IArrangedElement container, bool measureOnly, out Size preferredSize)
        {
            ArrangedElementCollection children = container.Children;

            preferredSize = new Size(-7103, -7105);
            if (measureOnly || (children.Count != 0))
            {
                bool flag  = false;
                bool flag2 = false;
                bool flag3 = false;
                for (int i = children.Count - 1; i >= 0; i--)
                {
                    IArrangedElement element = children[i];
                    if (CommonProperties.GetNeedsDockAndAnchorLayout(element))
                    {
                        if (!flag && CommonProperties.GetNeedsDockLayout(element))
                        {
                            flag = true;
                        }
                        if (!flag2 && CommonProperties.GetNeedsAnchorLayout(element))
                        {
                            flag2 = true;
                        }
                        if (!flag3 && CommonProperties.xGetAutoSizedAndAnchored(element))
                        {
                            flag3 = true;
                        }
                    }
                }
                Size empty = Size.Empty;
                Size b     = Size.Empty;
                if (flag)
                {
                    empty = LayoutDockedControls(container, measureOnly);
                }
                if (flag2 && !measureOnly)
                {
                    LayoutAnchoredControls(container);
                }
                if (flag3)
                {
                    LayoutAutoSizedControls(container);
                }
                if (!measureOnly)
                {
                    ApplyCachedBounds(container);
                }
                else
                {
                    b = GetAnchorPreferredSize(container);
                    Padding padding = Padding.Empty;
                    Control control = container as Control;
                    if (control != null)
                    {
                        padding = control.Padding;
                    }
                    else
                    {
                        padding = CommonProperties.GetPadding(container, Padding.Empty);
                    }
                    b.Width  -= padding.Left;
                    b.Height -= padding.Top;
                    ClearCachedBounds(container);
                    preferredSize = LayoutUtils.UnionSizes(empty, b);
                }
            }
            return(CommonProperties.GetAutoSize(container));
        }