Example #1
0
        /// <summary>
        /// Creates a new rule and adds them to the given style element.
        /// </summary>
        /// <param name="styleElement">The style element the new rule will be added to.</param>
        /// <param name="ruleName">The name of the rule, including preceding signs (. for class, # for ID).</param>
        /// <param name="rule">The rule content, e.g. "font-family: Verdana" (without quotes and braces).</param>
        public StyleRule(StyleElement styleElement, string ruleName, string rule)
        {
            int insertedRule = ((Interop.IHTMLStyleElement)styleElement.GetBaseElement()).styleSheet.AddRule(ruleName, " ", 0);

            // if succeeded, synchronize content
            Interop.IHTMLStyleSheetRulesCollection rules = ((Interop.IHTMLStyleElement)styleElement.GetBaseElement()).styleSheet.GetRules() as Interop.IHTMLStyleSheetRulesCollection;
            if (rules == null)
            {
                throw new ArgumentException("Cannot create style rule");
            }
            if (insertedRule >= 0)
            {
                styleRule = (Interop.IHTMLRuleStyle)rules.Item(insertedRule).GetStyle();
                RuleName  = ruleName;
            }
            else
            {
                for (int i = 0; i < rules.GetLength(); i++)
                {
                    Interop.IHTMLStyleSheetRule rulestyle = rules.Item(i);
                    if (ruleName.Equals(rulestyle.GetSelectorText()))
                    {
                        styleRule         = rulestyle.GetStyle();
                        styleRule.cssText = rule;
                        RuleName          = ruleName;
                        break;
                    }
                }
            }
        }
Example #2
0
        static internal IStyleRule[] AddRuleFromCollection(string selectorType, Interop.IHTMLStyleSheetRulesCollection rules)
        {
            ArrayList selector = new ArrayList();

            for (int i = 0; i < rules.GetLength(); i++)
            {
                Interop.IHTMLStyleSheetRule rule = (Interop.IHTMLStyleSheetRule)rules.Item(i);
                if (rule.GetSelectorText().Length == 0)
                {
                    continue;
                }
                IStyleRule sr = new StyleRule(rule);

                if (selectorType == null || selectorType.Equals(String.Empty))
                {
                    selector.Add(sr);
                    continue;
                }
                // first char determines type (. = class, # = id, @ = pseudo)
                if (selectorType == "." && rule.GetSelectorText()[0].Equals('.'))
                {
                    selector.Add(sr); //rule.GetSelectorText().Substring(1)
                    continue;
                }
                if (selectorType == "#" && rule.GetSelectorText()[0].Equals('#'))
                {
                    selector.Add(sr); // rule.GetSelectorText().Substring(1)
                    continue;
                }
                if (selectorType == "@" && rule.GetSelectorText()[0].Equals('@'))
                {
                    selector.Add(sr); //rule.GetSelectorText().Substring(1)
                    continue;
                }
                if (selectorType.ToLower().Equals(rule.GetSelectorText().ToLower()))
                {
                    selector.Add(sr); //rule.GetSelectorText().Substring(1)
                    continue;
                }
            }
            IStyleRule[] stylerules = new IStyleRule[selector.Count];
            Array.Copy(selector.ToArray(), stylerules, selector.Count);
            return(stylerules);
        }
Example #3
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;
        }
Example #4
0
 internal StyleRule(Interop.IHTMLStyleSheetRule rule)
 {
     nativeRule = rule;
     RuleName   = rule.GetSelectorText();
     styleRule  = rule.GetStyle();
 }