private static int GetLength(NativeMethods.IHTMLElement2 element)
        {
            NativeMethods.IHTMLRectCollection rectColl = element.GetClientRects();
            //Debug.Assert(rectColl.GetLength() == 1);
            Object obj = rectColl.GetLength() - 1;

            NativeMethods.IHTMLRect rect = (NativeMethods.IHTMLRect)rectColl.Item(ref obj);
            return(rect.GetRight() - rect.GetLeft());
        }
        internal static int GetMaxWidthToFit(MobileControl control, out byte templateStatus)
        {
            IDesigner parentDesigner  = ControlDesigner(control.Parent);
            IDesigner controlDesigner = ControlDesigner(control);
            int       defaultControlWidthInTemplate;

            NativeMethods.IHTMLElement2 htmlElement2Parent = null;

            if (controlDesigner == null)
            {
                templateStatus = CONTROL_IN_TEMPLATE_NONEDIT;
                return(0);
            }
            Debug.Assert(controlDesigner is MobileControlDesigner ||
                         controlDesigner is MobileTemplatedControlDesigner,
                         "controlDesigner is not MobileControlDesigner or MobileTemplatedControlDesigner");

            templateStatus = 0x00;
            if (parentDesigner is MobileTemplatedControlDesigner)
            {
                htmlElement2Parent =
                    (NativeMethods.IHTMLElement2)
                        ((MobileTemplatedControlDesigner)parentDesigner).DesignTimeElementInternal;
            }
            else if (parentDesigner is MobileContainerDesigner)
            {
                htmlElement2Parent =
                    (NativeMethods.IHTMLElement2)
                        ((MobileContainerDesigner)parentDesigner).DesignTimeElementInternal;
            }

            bool inTemplate;
            int  nestingLevel = DesignerAdapterUtil.NestingLevel(control, out inTemplate, out defaultControlWidthInTemplate);

            if (inTemplate)
            {
                templateStatus = CONTROL_IN_TEMPLATE_EDIT;
            }

            if (htmlElement2Parent != null)
            {
                int maxWidth;
                if (!inTemplate)
                {
                    Debug.Assert(control.Parent is MobileControl);
                    Style     parentStyle       = ((MobileControl)control.Parent).Style;
                    Alignment alignment         = (Alignment)parentStyle[Style.AlignmentKey, true];
                    int       parentChildOffset = 0;

                    // AUI 2786
                    if (alignment != Alignment.NotSet && alignment != Alignment.Left)
                    {
                        parentChildOffset = _regularParentChildOffset;
                    }
                    else
                    {
                        NativeMethods.IHTMLRectCollection rectColl = null;
                        NativeMethods.IHTMLRect           rect     = null;
                        int    index = 0;
                        Object obj   = index;

                        NativeMethods.IHTMLElement2 htmlElement2;

                        if (controlDesigner is MobileControlDesigner)
                        {
                            htmlElement2 = (NativeMethods.IHTMLElement2)((MobileControlDesigner)controlDesigner).DesignTimeElementInternal;
                        }
                        else
                        {
                            htmlElement2 = (NativeMethods.IHTMLElement2)((MobileTemplatedControlDesigner)controlDesigner).DesignTimeElementInternal;
                        }

                        if (null == htmlElement2)
                        {
                            return(0);
                        }

                        try
                        {
                            rectColl = htmlElement2.GetClientRects();
                        }
                        catch (Exception)
                        {
                            // this happens when switching from Design view to HTML view
                            return(0);
                        }

                        if (rectColl.GetLength() >= 1)
                        {
                            rect = (NativeMethods.IHTMLRect)rectColl.Item(ref obj);
                            parentChildOffset = rect.GetLeft();

                            rectColl = htmlElement2Parent.GetClientRects();
                            //Debug.Assert(rectColl.GetLength() == 1);
                            rect = (NativeMethods.IHTMLRect)rectColl.Item(ref obj);
                            parentChildOffset -= rect.GetLeft();
                        }
                    }

                    maxWidth = GetLength(htmlElement2Parent) - _marginWidth - parentChildOffset;
                    if (maxWidth > 0 && maxWidth > _defaultContainerWidth - nestingLevel * _marginPerLevel)
                    {
                        maxWidth = _defaultContainerWidth - nestingLevel * _marginPerLevel;
                    }
                }
                else
                {
                    int parentWidth = GetLength(htmlElement2Parent);
                    if (parentWidth == 0)
                    {
                        // AUI 4525
                        maxWidth = defaultControlWidthInTemplate;
                    }
                    else
                    {
                        maxWidth = parentWidth - _templateParentChildOffset;
                    }

                    if (maxWidth > 0 && maxWidth > defaultControlWidthInTemplate - nestingLevel * _marginPerLevel)
                    {
                        maxWidth = defaultControlWidthInTemplate - nestingLevel * _marginPerLevel;
                    }
                }
                return(maxWidth);
            }
            return(0);
        }