Esempio n. 1
0
        /// <summary>
        /// A default implementation of MeasureOverride to be used by all WPF widgets
        /// </summary>
        /// <param name="constraint">Size constraints</param>
        /// <param name="wpfMeasure">Size returned by the base MeasureOverride</param>
        /// <returns></returns>
        public System.Windows.Size MeasureOverride(System.Windows.Size constraint, System.Windows.Size wpfMeasure)
        {
            // Calculate the natural size, if that's what is being measured

            if (gettingNaturalSize)
            {
                var defNaturalSize = eventSink.GetDefaultNaturalSize();

                // -2 means use the WPF default, -1 use the XWT default, any other other value is used as custom natural size
                var nw = DefaultNaturalWidth;
                if (nw == -1)
                {
                    nw = defNaturalSize.Width;
                    if (nw == 0)
                    {
                        nw = wpfMeasure.Width;
                    }
                    wpfMeasure.Width = nw;
                }
                else if (nw != -2)
                {
                    wpfMeasure.Width = nw;
                }

                var nh = DefaultNaturalHeight;
                if (nh == -1)
                {
                    nh = defNaturalSize.Height;
                    if (nh == 0)
                    {
                        nh = wpfMeasure.Height;
                    }
                    wpfMeasure.Height = nh;
                }
                else if (nh != -2)
                {
                    wpfMeasure.Height = nh;
                }
            }

            // If we are calculating the default preferred size of the widget we end here.
            // See note above about when GetPreferred* methods are called.
            if (calculatingPreferredSize)
            {
                return(wpfMeasure);
            }

            Context.InvokeUserCode(delegate
            {
                if (eventSink.GetSizeRequestMode() == SizeRequestMode.HeightForWidth)
                {
                    // Calculate the preferred width through the frontend if there is an overriden OnGetPreferredWidth, but only do it
                    // if we are not given a constraint. If there is a width constraint, we'll use that constraint value for calculating the height
                    if ((enabledEvents & WidgetEvent.PreferredWidthCheck) != 0 && constraint.Width == Double.PositiveInfinity)
                    {
                        var ws           = eventSink.OnGetPreferredWidth();
                        wpfMeasure.Width = constraint.Width = gettingNaturalSize ? ws.NaturalSize : ws.MinSize;
                    }

                    // Now calculate the preferred height for that width, also using the override if available
                    if ((enabledEvents & WidgetEvent.PreferredHeightForWidthCheck) != 0)
                    {
                        var ws            = eventSink.OnGetPreferredHeightForWidth(constraint.Width);
                        wpfMeasure.Height = gettingNaturalSize ? ws.NaturalSize : ws.MinSize;
                    }
                }
                else
                {
                    // Calculate the preferred height through the frontend, if there is an overriden OnGetPreferredHeight
                    if ((enabledEvents & WidgetEvent.PreferredHeightCheck) != 0 && constraint.Height == Double.PositiveInfinity)
                    {
                        var ws            = eventSink.OnGetPreferredHeight();
                        wpfMeasure.Height = constraint.Height = gettingNaturalSize ? ws.NaturalSize : ws.MinSize;
                    }

                    // Now calculate the preferred width for that height, also using the override if available
                    if ((enabledEvents & WidgetEvent.PreferredWidthForHeightCheck) != 0)
                    {
                        var ws           = eventSink.OnGetPreferredWidthForHeight(constraint.Height);
                        wpfMeasure.Width = gettingNaturalSize ? ws.NaturalSize : ws.MinSize;
                    }
                }
            });
            return(wpfMeasure);
        }