Example #1
0
        private static void LayoutAnchoredControls(IArrangedElement container)
        {
            Debug.WriteLineIf(CompModSwitches.RichLayout.TraceInfo, "\tAnchor Processing");
            Debug.WriteLineIf(CompModSwitches.RichLayout.TraceInfo, "\t\tdisplayRect: " + container.DisplayRectangle.ToString());

            Rectangle displayRectangle = container.DisplayRectangle;

            if (CommonProperties.GetAutoSize(container) && ((displayRectangle.Width == 0) || (displayRectangle.Height == 0)))
            {
                // we havent set oursleves to the preferred size yet. proceeding will
                // just set all the control widths to zero. let's return here
                return;
            }

            ArrangedElementCollection children = container.Children;

            for (int i = children.Count - 1; i >= 0; i--)
            {
                IArrangedElement element = children[i];
                if (CommonProperties.GetNeedsAnchorLayout(element))
                {
                    Debug.Assert(GetAnchorInfo(element) != null, "AnchorInfo should be initialized before LayoutAnchorControls().");
                    SetCachedBounds(element, GetAnchorDestination(element, displayRectangle, /*measureOnly=*/ false));
                }
            }
        }
 private static void xLayoutDockedControl(IArrangedElement element, Rectangle newElementBounds, bool measureOnly, ref Size preferredSize, ref Rectangle remainingBounds)
 {
     if (measureOnly)
     {
         Size      proposedSize = new Size(Math.Max(0, newElementBounds.Width - remainingBounds.Width), Math.Max(0, newElementBounds.Height - remainingBounds.Height));
         DockStyle dock         = GetDock(element);
         switch (dock)
         {
         case DockStyle.Top:
         case DockStyle.Bottom:
             proposedSize.Width = 0;
             break;
         }
         if ((dock == DockStyle.Left) || (dock == DockStyle.Right))
         {
             proposedSize.Height = 0;
         }
         if (dock != DockStyle.Fill)
         {
             preferredSize        += proposedSize;
             remainingBounds.Size += proposedSize;
         }
         else if ((dock == DockStyle.Fill) && CommonProperties.GetAutoSize(element))
         {
             Size size2 = element.GetPreferredSize(proposedSize);
             remainingBounds.Size += size2;
             preferredSize        += size2;
         }
     }
     else
     {
         element.SetBounds(newElementBounds, BoundsSpecified.None);
     }
 }
 private static Size xGetDockedSize(IArrangedElement element, Size remainingSize, Size constraints, bool measureOnly)
 {
     if (CommonProperties.GetAutoSize(element))
     {
         return(element.GetPreferredSize(constraints));
     }
     return(element.Bounds.Size);
 }
Example #4
0
        private static void TryCalculatePreferredSizeDockedControl(IArrangedElement element, Rectangle newElementBounds, bool measureOnly, ref Size preferredSize, ref Rectangle remainingBounds)
        {
            if (measureOnly)
            {
                Size neededSize = new Size(
                    Math.Max(0, newElementBounds.Width - remainingBounds.Width),
                    Math.Max(0, newElementBounds.Height - remainingBounds.Height));

                DockStyle dockStyle = GetDock(element);
                if ((dockStyle == DockStyle.Top) || (dockStyle == DockStyle.Bottom))
                {
                    neededSize.Width = 0;
                }

                if ((dockStyle == DockStyle.Left) || (dockStyle == DockStyle.Right))
                {
                    neededSize.Height = 0;
                }

                if (dockStyle != DockStyle.Fill)
                {
                    preferredSize        += neededSize;
                    remainingBounds.Size += neededSize;
                }
                else if (dockStyle == DockStyle.Fill && CommonProperties.GetAutoSize(element))
                {
                    Size elementPrefSize = element.GetPreferredSize(neededSize);
                    remainingBounds.Size += elementPrefSize;
                    preferredSize        += elementPrefSize;
                }
            }
            else
            {
                element.SetBounds(newElementBounds, BoundsSpecified.None);

#if DEBUG
                Control control = element as Control;
                newElementBounds.Size = control.ApplySizeConstraints(newElementBounds.Size);

                // This usually happens when a Control overrides its SetBoundsCore or sets size during OnResize
                // to enforce constraints like AutoSize. Generally you can just move this code to Control.GetAdjustedSize
                // and then PreferredSize will also pick up these constraints. See ComboBox as an example.
                if (CommonProperties.GetAutoSize(element) && !CommonProperties.GetSelfAutoSizeInDefaultLayout(element))
                {
                    Debug.Assert(
                        (newElementBounds.Width < 0 || element.Bounds.Width == newElementBounds.Width) &&
                        (newElementBounds.Height < 0 || element.Bounds.Height == newElementBounds.Height),
                        "Element modified its bounds during docking -- PreferredSize will be wrong. See comment near this assert.");
                }
#endif
            }
        }
Example #5
0
        private static void ApplyCachedBounds(IArrangedElement container)
        {
            if (CommonProperties.GetAutoSize(container))
            {
                // Avoiding calling DisplayRectangle before checking AutoSize for Everett compat
                Rectangle displayRectangle = container.DisplayRectangle;
                if ((displayRectangle.Width == 0) || (displayRectangle.Height == 0))
                {
                    ClearCachedBounds(container);
                    return;
                }
            }

            IDictionary dictionary = (IDictionary)container.Properties.GetObject(s_cachedBoundsProperty);

            if (dictionary != null)
            {
#if DEBUG
                // In debug builds, we need to modify the collection, so we add a break and an
                // outer loop to prevent attempting to IEnumerator.MoveNext() on a modified
                // collection.
                while (dictionary.Count > 0)
                {
#endif
                foreach (DictionaryEntry entry in dictionary)
                {
                    IArrangedElement element = (IArrangedElement)entry.Key;

                    Debug.Assert(element.Container == container, "We have non-children in our containers cached bounds store.");
#if DEBUG
                    // We are about to set the bounds to the cached value. We clear the cached value
                    // before SetBounds because some controls fiddle with the bounds on SetBounds
                    // and will callback InitLayout with a different bounds and BoundsSpecified.
                    dictionary.Remove(entry.Key);
#endif
                    Rectangle bounds = (Rectangle)entry.Value;
                    element.SetBounds(bounds, BoundsSpecified.None);
#if DEBUG
                    break;
                }
#endif
                }

                ClearCachedBounds(container);
            }
        }
        private static void LayoutAnchoredControls(IArrangedElement container)
        {
            Rectangle displayRectangle = container.DisplayRectangle;

            if (!CommonProperties.GetAutoSize(container) || ((displayRectangle.Width != 0) && (displayRectangle.Height != 0)))
            {
                ArrangedElementCollection children = container.Children;
                for (int i = children.Count - 1; i >= 0; i--)
                {
                    IArrangedElement element = children[i];
                    if (CommonProperties.GetNeedsAnchorLayout(element))
                    {
                        SetCachedBounds(element, GetAnchorDestination(element, displayRectangle, false));
                    }
                }
            }
        }
Example #7
0
        private static Size xGetDockedSize(IArrangedElement element, Size remainingSize, Size constraints, bool measureOnly)
        {
            Size desiredSize;

            if (CommonProperties.GetAutoSize(element))
            {
                // Ask control for its desired size using the provided constraints.
                // (e.g., a control docked to top will constrain width to remaining width
                // and minimize height.)
                desiredSize = element.GetPreferredSize(constraints);
            }
            else
            {
                desiredSize = element.Bounds.Size;
            }

            Debug.Assert((desiredSize.Width >= 0 && desiredSize.Height >= 0), "Error detected in xGetDockSize: Element size was negative.");
            return(desiredSize);
        }
Example #8
0
        // Entry point from LayoutEngine
        internal override bool LayoutCore(IArrangedElement container, LayoutEventArgs args)
        {
#if DEBUG
            if (CompModSwitches.FlowLayout.TraceInfo)
            {
                Debug.WriteLine("FlowLayout::Layout("
                                + "container=" + container.ToString() + ", "
                                + "displayRect=" + container.DisplayRectangle.ToString() + ", "
                                + "args=" + args.ToString() + ")");
            }
            Debug.Indent();
#endif

            // ScrollableControl will first try to get the layoutbounds from the derived control when
            // trying to figure out if ScrollBars should be added.
            CommonProperties.SetLayoutBounds(container, xLayout(container, container.DisplayRectangle, /* measureOnly = */ false));
#if DEBUG
            Debug.Unindent();
#endif
            return(CommonProperties.GetAutoSize(container));
        }
        private static void ApplyCachedBounds(IArrangedElement container)
        {
            if (CommonProperties.GetAutoSize(container))
            {
                Rectangle displayRectangle = container.DisplayRectangle;
                if ((displayRectangle.Width == 0) || (displayRectangle.Height == 0))
                {
                    ClearCachedBounds(container);
                    return;
                }
            }
            IDictionary dictionary = (IDictionary)container.Properties.GetObject(_cachedBoundsProperty);

            if (dictionary != null)
            {
                // Blazor supports keys properly
#if BLAZOR
                foreach (DictionaryEntry entry in dictionary)
                {
                    IArrangedElement key    = (IArrangedElement)entry.Key;
                    Rectangle        bounds = (Rectangle)entry.Value;
                    key.SetBounds(bounds, BoundsSpecified.None);
                }
#elif BRIDGE
                foreach (dynamic entry in dictionary)
                {
                    IArrangedElement key    = (IArrangedElement)entry.key;
                    Rectangle        bounds = (Rectangle)entry.value;
                    if (key != null)
                    {
                        key.SetBounds(bounds, BoundsSpecified.None);
                    }
                }
#endif

                ClearCachedBounds(container);
            }
        }
Example #10
0
        private static void ApplyCachedBounds(IArrangedElement container)
        {
            if (CommonProperties.GetAutoSize(container))
            {
                Rectangle displayRectangle = container.DisplayRectangle;
                if ((displayRectangle.Width == 0) || (displayRectangle.Height == 0))
                {
                    ClearCachedBounds(container);
                    return;
                }
            }
            IDict dictionary = (IDict)container.Properties.GetObject(_cachedBoundsProperty);

            if (dictionary != null)
            {
                foreach (dynamic entry in dictionary)
                {
                    IArrangedElement key    = (IArrangedElement)entry.Key;
                    Rectangle        bounds = (Rectangle)entry.Value;
                    key.SetBounds(bounds, BoundsSpecified.None);
                }
                ClearCachedBounds(container);
            }
        }
Example #11
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));
        }
Example #12
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));
        }
 internal override bool LayoutCore(IArrangedElement container, LayoutEventArgs args)
 {
     CommonProperties.SetLayoutBounds(container, this.xLayout(container, container.DisplayRectangle, false));
     return(CommonProperties.GetAutoSize(container));
 }