Exemple #1
0
        internal static void ApplyStyle(String enterStyle, String exitStyle, String cssStyle)
        {
            MSHTMLHostUtil.CreateControl();

            String bodyInnerHTML = "<div id=__divOuter nowrap style='width:1px; height:10px'>" +
                                   enterStyle +
                                   "<div id=__divInner" + cssStyle + "></div>" +
                                   exitStyle +
                                   "</div>";

            // MessageBox.Show("Body HTML for empty content: " + bodyInnerHTML);
            _htmlBody.SetInnerHTML(bodyInnerHTML);

            NativeMethods.IHTMLDocument3 htmlDoc3 = (NativeMethods.IHTMLDocument3)_tridentControl.GetDocument();
            Debug.Assert(null != htmlDoc3);

            _htmlDivInner = htmlDoc3.GetElementById("__divInner");
            _htmlDivOuter = htmlDoc3.GetElementById("__divOuter");
            Debug.Assert(null != _htmlDivOuter && null != _htmlDivInner);
        }
        public override void Render(HtmlMobileTextWriter writer)
        {
            Alignment alignment = (Alignment)Style[Style.AlignmentKey, true];
            Wrapping  wrapping  = (Wrapping)Style[Style.WrappingKey, true];
            bool      wrap      = (wrapping == Wrapping.Wrap || wrapping == Wrapping.NotSet);
            String    width     = DesignerAdapterUtil.GetWidth(Control);

            ((DesignerTextWriter)writer).EnterZeroFontSizeTag();
            writer.WriteBeginTag("div");
            if (!wrap)
            {
                byte templateStatus;
                int  maxWidth = DesignerAdapterUtil.GetMaxWidthToFit(Control, out templateStatus);
                if (templateStatus == DesignerAdapterUtil.CONTROL_IN_TEMPLATE_EDIT)
                {
                    width = maxWidth.ToString(CultureInfo.InvariantCulture) + "px";
                }
                writer.WriteAttribute("style", "overflow-x:hidden;width:" + width);
            }
            else
            {
                writer.WriteAttribute("style", "word-wrap:break-word;width:" + width);
            }

            if (alignment != Alignment.NotSet)
            {
                writer.WriteAttribute("align", Enum.GetName(typeof(Alignment), alignment));
            }
            writer.Write(">");

            MSHTMLHostUtil.ApplyStyle(null, null, null);
            String filteredText     = FilterTags(Control.Text.Trim());
            int    uniqueLineHeight = MSHTMLHostUtil.GetHtmlFragmentHeight("a");
            int    requiredHeight   = MSHTMLHostUtil.GetHtmlFragmentHeight(filteredText);
            int    requiredWidth    = MSHTMLHostUtil.GetHtmlFragmentWidth(filteredText);

            ((DesignerTextWriter)writer).WriteCssStyleText(Style, null, (requiredHeight > uniqueLineHeight || requiredWidth > 1) ? filteredText : "&nbsp;", false);
            writer.WriteEndTag("div");
            ((DesignerTextWriter)writer).ExitZeroFontSizeTag();
        }
Exemple #3
0
        public override void Render(HtmlMobileTextWriter writer)
        {
            // Invalid text writers are not supported in this Adapter.
            if (!(writer is DesignerTextWriter))
            {
                return;
            }

            Alignment alignment = (Alignment)Style[Style.AlignmentKey, true];
            byte      templateStatus;
            int       maxWidth = DesignerAdapterUtil.GetMaxWidthToFit(Control, out templateStatus);
            String    width    = DesignerAdapterUtil.GetWidth(Control);

            if (Control.ImageUrl.Length == 0)
            {
                if (Control.Format == CommandFormat.Button)
                {
                    if (maxWidth == 0 && templateStatus == DesignerAdapterUtil.CONTROL_IN_TEMPLATE_NONEDIT)
                    {
                        maxWidth = DesignerAdapterUtil.CONTROL_MAX_WIDTH_IN_TEMPLATE;
                    }

                    if (maxWidth == 0 && DesignerAdapterUtil.InMobileUserControl(Control))
                    {
                        maxWidth = Constants.ControlMaxsizeAtToplevel;
                    }

                    if (maxWidth == 0)
                    {
                        // Render will be called a second time for which maxWidth != 0
                        return;
                    }

                    String additionalStyle = null;
                    String controlText     = Control.Text;
                    String commandCaption;
                    int    requiredWidth = 0;

                    DesignerTextWriter twTmp;
                    twTmp = new DesignerTextWriter();
                    twTmp.WriteBeginTag("input");
                    twTmp.WriteStyleAttribute(Style, null);
                    twTmp.WriteAttribute("type", "submit");
                    twTmp.Write(" value=\"");
                    twTmp.WriteText(controlText, true);
                    twTmp.Write("\"/>");
                    String htmlFragment = twTmp.ToString();

                    MSHTMLHostUtil.ApplyStyle(String.Empty, String.Empty, null);
                    requiredWidth = MSHTMLHostUtil.GetHtmlFragmentWidth(htmlFragment);

                    ((DesignerTextWriter)writer).EnterZeroFontSizeTag();
                    writer.WriteBeginTag("div");
                    if (requiredWidth + SAFETY_MARGIN > maxWidth)
                    {
                        if (templateStatus == DesignerAdapterUtil.CONTROL_IN_TEMPLATE_EDIT)
                        {
                            int tmpRequiredWidth, allowedLength;
                            int captionLength = controlText.Length;
                            twTmp = new DesignerTextWriter();
                            twTmp.WriteBeginTag("input");
                            twTmp.WriteStyleAttribute(Style, null);
                            twTmp.WriteAttribute("type", "submit");
                            twTmp.WriteAttribute("value", "{0}");
                            twTmp.Write("/>");
                            htmlFragment = twTmp.ToString();
                            // At least 10 characters can fit into the caption of the command
                            for (allowedLength = (captionLength < 10 ? captionLength : 10); allowedLength <= captionLength; allowedLength++)
                            {
                                tmpRequiredWidth = MSHTMLHostUtil.GetHtmlFragmentWidth(String.Format(CultureInfo.CurrentCulture, htmlFragment, HttpUtility.HtmlEncode(controlText.Substring(0, allowedLength))));
                                if (tmpRequiredWidth + SAFETY_MARGIN > maxWidth)
                                {
                                    break;
                                }
                            }
                            commandCaption = controlText.Substring(0, allowedLength - 1);
                        }
                        else
                        {
                            commandCaption = controlText;
                        }
                    }
                    else
                    {
                        writer.WriteAttribute("style", "width:" + width);
                        commandCaption = controlText;
                    }

                    if (alignment != Alignment.NotSet)
                    {
                        writer.WriteAttribute("align", Enum.GetName(typeof(Alignment), alignment));
                    }
                    writer.Write(">");

                    writer.EnterLayout(Style);

                    writer.WriteBeginTag("input");
                    if (requiredWidth + SAFETY_MARGIN > maxWidth)
                    {
                        additionalStyle = String.Format(CultureInfo.CurrentCulture, "width:{0};", width);
                    }
                    ((DesignerTextWriter)writer).WriteStyleAttribute(Style, additionalStyle);
                    writer.WriteAttribute("type", "submit");

                    writer.Write(" value=\"");
                    writer.WriteText(commandCaption, true);
                    writer.Write("\"/>");

                    writer.ExitLayout(Style);
                }
                else
                {
                    Wrapping wrapping = (Wrapping)Style[Style.WrappingKey, true];
                    bool     wrap     = (wrapping == Wrapping.Wrap || wrapping == Wrapping.NotSet);

                    ((DesignerTextWriter)writer).EnterZeroFontSizeTag();
                    writer.WriteBeginTag("div");

                    if (!wrap)
                    {
                        if (templateStatus == DesignerAdapterUtil.CONTROL_IN_TEMPLATE_EDIT)
                        {
                            width = maxWidth.ToString(CultureInfo.InvariantCulture) + "px";
                        }
                        writer.WriteAttribute("style", "overflow-x:hidden;width:" + width);
                    }
                    else
                    {
                        writer.WriteAttribute("style", "word-wrap:break-word;width:" + width);
                    }

                    if (alignment != Alignment.NotSet)
                    {
                        writer.WriteAttribute("align", Enum.GetName(typeof(Alignment), alignment));
                    }
                    writer.Write(">");

                    writer.WriteBeginTag("a");
                    writer.WriteAttribute("href", "NavigationUrl");
                    writer.Write(">");
                    ((DesignerTextWriter)writer).WriteCssStyleText(Style, null, Control.Text, true);
                    writer.WriteEndTag("a");
                }
                writer.WriteEndTag("div");
                ((DesignerTextWriter)writer).ExitZeroFontSizeTag();
            }
            else
            {
                if (templateStatus == DesignerAdapterUtil.CONTROL_IN_TEMPLATE_EDIT)
                {
                    width = maxWidth.ToString(CultureInfo.InvariantCulture) + "px";
                }

                writer.WriteBeginTag("div");
                if (alignment == Alignment.Center)
                {
                    writer.WriteAttribute("align", "center");
                }
                writer.WriteAttribute("style", "overflow-x:hidden;width:" + width);
                writer.Write(">");

                writer.WriteBeginTag("img");
                ((DesignerTextWriter)writer).WriteStyleAttribute(Style);
                writer.WriteAttribute("src", Control.ImageUrl, true);

                // center alignment not part of HTML for images.
                if (alignment == Alignment.Right ||
                    alignment == Alignment.Left)
                {
                    writer.WriteAttribute("align", Enum.GetName(typeof(Alignment), alignment));
                }

                writer.WriteAttribute("border", "0");
                writer.Write(">");
                writer.WriteEndTag("div");
            }
        }
        public override void Render(HtmlMobileTextWriter writer)
        {
            // Invalid text writers are not supported in this Adapter.
            if (!(writer is DesignerTextWriter))
            {
                return;
            }

            byte templateStatus;
            bool pwd  = Control.Password;
            int  size = Control.Size;
            int  fittingSize;

            int maxWidth = DesignerAdapterUtil.GetMaxWidthToFit(Control, out templateStatus);

            if (maxWidth == 0)
            {
                if (templateStatus == DesignerAdapterUtil.CONTROL_IN_TEMPLATE_NONEDIT)
                {
                    maxWidth = DesignerAdapterUtil.CONTROL_MAX_WIDTH_IN_TEMPLATE;
                }
                else if (DesignerAdapterUtil.InMobileUserControl(Control))
                {
                    maxWidth = Constants.ControlMaxsizeAtToplevel;
                }
            }

            if (maxWidth == 0)
            {
                return;
            }

            bool restoreEmptyFontName = false;

            if ((String)Style[Style.FontNameKey, true] == String.Empty)
            {
                // MSHTMLHostUtil is using another font by default.
                // Setting the font name to the one that is actually
                // used by default for the desig-time rendering
                // assures that the requiredWidth returned by
                // MSHTMLHostUtil.GetHtmlFragmentWidth is accurate.
                Style[Style.FontNameKey] = "Arial";
                restoreEmptyFontName     = true;
            }

            int requiredWidth = 0;
            DesignerTextWriter tw;

            tw = new DesignerTextWriter(false);
            tw.EnterLayout(Style);
            String enterLayout = tw.ToString();

            tw = new DesignerTextWriter(false);
            tw.ExitLayout(Style);
            String exitLayout = tw.ToString();

            tw = new DesignerTextWriter(false);
            tw.WriteBeginTag("input");
            tw.WriteStyleAttribute(Style, null);
            if (size > 0)
            {
                tw.WriteAttribute("size", "{0}");
            }
            tw.Write("/>");
            String htmlFragment = tw.ToString();

            MSHTMLHostUtil.ApplyStyle(enterLayout, exitLayout, null);

            if (size < LARGESIZE_THRESHOLD)
            {
                requiredWidth = MSHTMLHostUtil.GetHtmlFragmentWidth(size > 0 ? String.Format(htmlFragment, size.ToString()) : htmlFragment);
            }

            if (requiredWidth + SAFETY_MARGIN > maxWidth || size >= LARGESIZE_THRESHOLD)
            {
                if (size == 0)
                {
                    tw = new DesignerTextWriter(false);
                    tw.WriteBeginTag("input");
                    tw.WriteStyleAttribute(Style, null);
                    tw.WriteAttribute("size", "{0}");
                    tw.Write("/>");
                    htmlFragment = tw.ToString();
                }
                fittingSize = 0;
                do
                {
                    fittingSize++;
                    requiredWidth = MSHTMLHostUtil.GetHtmlFragmentWidth(String.Format(htmlFragment, fittingSize.ToString()));
                }while (requiredWidth + SAFETY_MARGIN <= maxWidth);

                if (fittingSize > 1)
                {
                    fittingSize--;
                }
            }
            else
            {
                fittingSize = size;
            }

            if (restoreEmptyFontName)
            {
                Style[Style.FontNameKey] = String.Empty;
            }

            Alignment alignment = (Alignment)Style[Style.AlignmentKey, true];
            String    width     = DesignerAdapterUtil.GetWidth(Control);

            writer.Write("<div style='width:" + width);
            if (alignment != Alignment.NotSet)
            {
                writer.Write(";text-align:" + Enum.GetName(typeof(Alignment), alignment));
            }
            writer.Write("'>");

            ((DesignerTextWriter)writer).EnterZeroFontSizeTag();
            writer.EnterLayout(Style);

            writer.WriteBeginTag("input");
            ((DesignerTextWriter)writer).WriteStyleAttribute(Style, null);
            if (Control.Text != String.Empty)
            {
                writer.Write(" value=\"");
                writer.WriteText(Control.Text, true);
                writer.Write("\" ");
            }
            if (fittingSize > 0)
            {
                writer.WriteAttribute("size", fittingSize.ToString());
            }
            if (pwd)
            {
                writer.WriteAttribute("type", "password");
            }
            writer.Write("/>");

            writer.ExitLayout(Style);
            ((DesignerTextWriter)writer).ExitZeroFontSizeTag();
            writer.Write("</div>");
        }