Пример #1
0
 private static void LoopNodes(Interop.IHTMLDOMNode node, Interop.IHTMLDOMNode targetNode, Interop.IHTMLDocument2 targetDoc)
 {
     while (node != null)
     {
         if (node.nodeName == "#text")
         {
             ((Interop.IHTMLElement)targetNode).InsertAdjacentHTML("BeforeEnd", node.nodeValue.ToString());
         }
         else
         {
             string elementName;
             if (((Interop.IHTMLElement2)node).GetScopeName() == "HTML")
             {
                 elementName = ((Interop.IHTMLElement)node).GetTagName();
             }
             else
             {
                 elementName = String.Format("{0}:{1}", ((Interop.IHTMLElement2)node).GetScopeName(), ((Interop.IHTMLElement)node).GetTagName());
             }
             Interop.IHTMLElement clone = targetDoc.CreateElement(elementName);
             ((Interop.IHTMLElement3)clone).mergeAttributes(((Interop.IHTMLElement)node), 0);
             if (node.hasChildNodes())
             {
                 Interop.IHTMLDOMChildrenCollection children =
                     node.childNodes as Interop.IHTMLDOMChildrenCollection;
                 if (children != null)
                 {
                     for (int i = 0; i < children.length; i++)
                     {
                         Interop.IHTMLDOMNode child = (Interop.IHTMLDOMNode)children.item(i);
                         LoopNodes(child, (Interop.IHTMLDOMNode)clone, targetDoc);
                     }
                 }
             }
         }
         node = node.nextSibling;
     }
 }
Пример #2
0
        /// <summary>
        /// Creates the viewlink document.
        /// </summary>
        private void CreateControlView()
        {
            Interop.IHTMLDocument2 originDocument = (Interop.IHTMLDocument2)Element.GetDocument();
            Interop.IHTMLElement   htmlElement    = originDocument.CreateElement("HTML");
            Interop.IHTMLElement   headElement    = originDocument.CreateElement("HEAD");
            Interop.IHTMLElement   bodyElement    = originDocument.CreateElement("BODY");
            ((Interop.IHTMLElement2)htmlElement).InsertAdjacentElement("beforeBegin", headElement);
            ((Interop.IHTMLElement2)htmlElement).InsertAdjacentElement("afterBegin", bodyElement);
            _viewElement = bodyElement;
            _viewElement.SetAttribute("tabIndex", 1000, 0);
            baseDocument = (Interop.IHTMLDocument)_viewElement.GetDocument();
            try
            {
                elementDefaults = ((Interop.IElementBehaviorSiteOM2)_behaviorSite).GetDefaults();
            }
            catch
            {
                throw new ApplicationException("Wrong usage - acces to not properly attached master element");
            }
            XmlElementDesigner elementDesigner = Designer as XmlElementDesigner;

            if (_control == null || elementDesigner == null)
            {
                _viewElement.SetInnerHTML(String.Format(@"<div style=""border:dotted 1px red;background:Silver;width:220px;height:40px""><b>Error</b>:&nbsp;Element does not have a Designer attached (Element {0}:{1})</div>",
                                                        ((Interop.IHTMLElement2)_element).GetScopeName(),
                                                        _element.GetTagName()));
                ((Interop.IHTMLDocument2)baseDocument).SetDesignMode("Off");
                elementDefaults.SetViewLink(baseDocument);
                return;
            }
            else
            {
                _viewElement.SetInnerHTML(elementDesigner.GetDesignTimeHtml());
                elementDefaults.SetViewLink(baseDocument);
            }
            //done, set reference
            _control.ViewElementDefaults = elementDefaults;
            // end commands do basic document
            if (elementDesigner.DisableEditFocus)
            {
                //System.Threading.ThreadPool.QueueUserWorkItem(ExecuteCommand, (int)Interop.IDM.DISABLE_EDITFOCUS_UI);
            }

            // get viewlink specific properties from control element
            elementDefaults.SetFrozen(elementDesigner.FrozenEvents);    // true = event handler returns only master; false = event returns designtime html
            ((Interop.IHTMLElement3)_viewElement).contentEditable = InheritedDesignModeEnabled(_control.ContentEditable) ? "true" : "false";
            elementDefaults.SetViewMasterTab(elementDesigner.ViewMasterTab);
            elementDefaults.SetTabStop(elementDesigner.TabStop);
            elementDefaults.SetCanHaveHTML(elementDesigner.CanHaveHtml);
            elementDefaults.SetIsMultiLine(true);

            Interop.IHTMLStyle style        = ((Interop.IHTMLElement2)_viewElement).GetRuntimeStyle();
            ElementStyle       runtimeStyle = new ElementStyle(style);

            elementDesigner.SetRuntimeStyle(runtimeStyle);
            // assure that we don't see any unexpected scrollbars during resize operations
            ((Interop.IHTMLElement2)_element).GetRuntimeStyle().SetOverflow("hidden");
            object w = ((Interop.IHTMLElement)_element).GetStyle().GetWidth();

            elementDesigner.OnSetComponentDefaults();
            try
            {
                Interop.IHTMLDocument2 baseDocument2 = (Interop.IHTMLDocument2)baseDocument;
                int numSheets = 0;
                Interop.IHTMLStyleSheetsCollection baseDocumentStylesheets = originDocument.GetStyleSheets();
                if (baseDocumentStylesheets != null)
                {
                    numSheets = baseDocumentStylesheets.Length;
                }
                for (int j = 0; j < numSheets; j++)
                {
                    object local = j;
                    Interop.IHTMLStyleSheet sheetItem = (Interop.IHTMLStyleSheet)baseDocumentStylesheets.Item(local);
                    if (sheetItem != null)
                    {
                        int k = 0;
                        Interop.IHTMLStyleSheetRulesCollection rules = sheetItem.GetRules();
                        if (rules != null)
                        {
                            k = rules.GetLength();
                        }
                        if (k != 0)
                        {
                            Interop.IHTMLStyleSheet newSheet = baseDocument2.CreateStyleSheet(String.Empty, 0);
                            for (int i2 = 0; i2 < k; i2++)
                            {
                                Interop.IHTMLStyleSheetRule newRule = rules.Item(i2);
                                if (newRule != null)
                                {
                                    string selector = newRule.GetSelectorText();
                                    string content  = newRule.GetStyle().cssText;
                                    newSheet.AddRule(selector, content, i2);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            // once we have the viewlink created, establish a backlink
            elementDesigner.AssociatedViewLink = this;
        }