private static Dictionary <string, Style> GetAllStyles(DependencyObject control)
        {
            //find all user-controls and pages
            ControlSet allUserControls = XamlQuery.ParentsByType <UserControl>(control);
            ControlSet allPages        = XamlQuery.ParentsByType <Page>(control);

            Common.AddToLog(allUserControls.Count + " user-controls");
            Common.AddToLog(allPages.Count + " pages");

            ControlSet allContainers = new ControlSet();

            allContainers.AddRange(allUserControls);
            allContainers.AddRange(allPages);

            //get styles from all user-controls and pages and their merged-dictionaries
            Dictionary <string, Style> allStyles = new Dictionary <string, Style>();

            foreach (UserControl container in allContainers)
            {
                List <ResourceDictionary> allDictionaries = GetResourceDictionaryTree(container.Resources);
                foreach (ResourceDictionary dictionary in allDictionaries)
                {
                    foreach (object key in dictionary.Keys)
                    {
                        object resource = container.Resources[key];
                        if (key is string && resource is Style)
                        {
                            allStyles.Add((string)key, (Style)resource);
                        }
                    }
                }
            }

            //get styles defined in App and its merged-dictionaries
            if (Application.Current != null)
            {
                List <ResourceDictionary> allDictionaries = GetResourceDictionaryTree(Application.Current.Resources);
                foreach (ResourceDictionary dictionary in allDictionaries)
                {
                    foreach (object key in dictionary.Keys)
                    {
                        object resource = Application.Current.Resources[key];
                        if (key is string && resource is Style)
                        {
                            allStyles.Add((string)key, (Style)resource);
                        }
                    }
                }
            }

            Common.AddToLog(allStyles.Count + " styles");
            foreach (string key in allStyles.Keys)
            {
                Common.AddToLog("found-style: " + key + ", " + allStyles[key].TargetType);
            }

            return(allStyles);
        }
Beispiel #2
0
        public ControlSet Execute(DependencyObject sourceControl)
        {
            Common.AddToLog("executing selector: " + this.Query);

            SourceControl = sourceControl;
            ControlSet superSet        = XamlQuery.All(sourceControl);
            ControlSet matchedControls = new ControlSet();

            if (SimpleSelectors.Count == 1) //if only one simple-selector, then execute it and return the result
            {
                return(SimpleSelectors[0].Execute(superSet));
            }
            else if (SimpleSelectors.Count > 1) //if more than one simple-selectors, then apply combinators while executing
            {
                //apply each combinator to result of simple-selector that precedes it
                ControlSet currentSet = new ControlSet();
                currentSet.AddRange(superSet);
                for (int index = 0; index < SimpleSelectors.Count; index++)
                {
                    if (index > 0)
                    {
                        Common.AddToLog("**Executing Combinator: " + Combinators[index - 1].Token.Content);
                        currentSet = Combinators[index - 1].Execute(currentSet);
                        Common.AddToLog("**Combinator-Result: " + currentSet.Count + " controls");
                        if (Combinators[index - 1].Type == CombinatorType.Adjacent)
                        {
                            //for adjacent combinator
                            //   get first-set by executing simple-selector before + with result of combinator
                            //   get second-set by executing simple-selector after + with result of combinator
                            //   combine first and second sets as result of combinator and its simple-selectors
                            ControlSet firstSet  = SimpleSelectors[index - 1].Execute(currentSet);
                            ControlSet secondSet = SimpleSelectors[index].Execute(currentSet);
                            currentSet.Clear();
                            currentSet.AddRange(firstSet);
                            currentSet.AddRange(secondSet);
                            Common.AddToLog("**First-Set-Result: " + firstSet.Count + " controls. " + string.Join(",", (from c in firstSet select c.GetType().ToString()).ToArray()));
                            Common.AddToLog("**Second-Set-Result: " + secondSet.Count + " controls. " + string.Join(",", (from c in secondSet select c.GetType().ToString()).ToArray()));
                            Common.AddToLog("**Simple-Selector-Result: " + currentSet.Count + " controls");
                            continue;
                        }
                    }
                    Common.AddToLog("**Executing Simple-Selector: " + SimpleSelectors[index].Query);
                    currentSet = SimpleSelectors[index].Execute(currentSet);
                    Common.AddToLog("**Simple-Selector-Result: " + currentSet.Count + " controls");
                }
                matchedControls.AddRange(currentSet);
            }

            return(matchedControls);
        }
Beispiel #3
0
        private ControlSet FilterByPropertyQuery(ControlSet superSet)
        {
            ControlSet matchedControls = new ControlSet();

            //prepare query-string
            string queryString = string.Concat((from token in this.Tokens select token.Content).ToArray());

            queryString = queryString.Replace("[", "");
            queryString = queryString.Replace("]", "");
            queryString = queryString.Replace("!=", "!");
            queryString = queryString.Replace("^=", "^");
            queryString = queryString.Replace("$=", "$");
            queryString = queryString.Replace("~=", "~");
            Common.AddToLog("clean filter-query: " + queryString);

            //find controls using property-details
            FilterType filterType = ResolveFilterType(queryString);

            string[] propertyParts = queryString.Split(Parser.PropertyValueDelimiterSymbols.ToCharArray());
            if (propertyParts.Length > 0)
            {
                string propertyNameDescriptor = propertyParts[0];
                string propertyRawValue       = propertyParts.Length > 1 ? propertyParts[1] : string.Empty;

                ControlSet subSet = FilterByDependencyProperty(superSet, filterType, propertyParts.Length, propertyNameDescriptor, propertyRawValue);
                if (subSet == null) //if not dependency property, check normal property
                {
                    subSet = FilterByNormalProperty(superSet, filterType, propertyParts.Length, propertyNameDescriptor, propertyRawValue);
                }
                matchedControls.AddRange(subSet);
            }

            return(matchedControls);
        }
Beispiel #4
0
        public ControlSet Execute(ControlSet superSet)
        {
            Common.AddToLog("executing filter-q: " + this.Query);

            if (Tokens.Count < 2)
            {
                return(new ControlSet());                    //filter-selector should have minimum two tokens, the first one being the filter-delimiter symbol
            }
            ControlSet matchedControls = new ControlSet();

            if (Type == FilterSelectorType.Style)
            {
                string styleName   = Tokens[1].Content;
                Style  styleObject = ControlUtility.GetStyleByName(styleName);
                if (styleObject != null)
                {
                    foreach (DependencyObject control in superSet) //find controls by style
                    {
                        if (control is FrameworkElement)
                        {
                            FrameworkElement element = (FrameworkElement)control;
                            if (element.Style != null)
                            {
                                //if (element.Style.Equals(styleObject))
                                if (ControlUtility.IsStyleEqualsOrBasedOn(element.Style, styleObject))
                                {
                                    Common.AddToLog("matched-by-style: " + element.Name + " based on style");
                                    matchedControls.Add(control);
                                }
                            }
                        }
                    }
                }
            }
            else if (Type == FilterSelectorType.Name)
            {
                string name = Tokens[1].Content;

                foreach (DependencyObject control in superSet) //find control by name
                {
                    if (control is FrameworkElement)
                    {
                        if (((FrameworkElement)control).Name.Equals(name))
                        {
                            Common.AddToLog("matched-by-name: " + ((FrameworkElement)control).Name);
                            matchedControls.Add(control);
                        }
                    }
                }
            }
            else if (Type == FilterSelectorType.Property)
            {
                matchedControls.AddRange(FilterByPropertyQuery(superSet));
            }

            return(matchedControls);
        }
        public ControlSet Execute(ControlSet superSet)
        {
            Common.AddToLog("executing simple-query: " + this.Query);

            if (Type == SimpleSelectorType.None)
            {
                return(new ControlSet());
            }

            //set main controls
            ControlSet mainControls = new ControlSet();

            if (Type == SimpleSelectorType.Universal)
            {
                mainControls.AddRange(superSet);
            }
            else if (Type == SimpleSelectorType.Element)
            {
                Type givenControlType = ControlUtility.ResolveControlType(MainToken.Content);
                if (givenControlType != null)
                {
                    foreach (DependencyObject control in superSet)
                    {
                        //if (givenControlType.IsAssignableFrom(control.GetType()))
                        //if (givenControlType.IsSubclassOf(control.GetType()))
                        //if (givenControlType.FullName.Equals(control.GetType().FullName))
                        if (givenControlType.IsInstanceOfType(control))
                        {
                            mainControls.Add(control);
                        }
                    }
                }
            }

            //apply filter-selectors
            if (this.FilterSelectors.Count == 0)
            {
                return(mainControls);
            }
            else
            {
                foreach (FilterSelector filterSelector in this.FilterSelectors)
                {
                    mainControls = filterSelector.Execute(mainControls);
                }
                return(mainControls);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Searches the children of a control using the specified CSS selector query.
        /// </summary>
        /// <param name="sourceControl">The control whose visual children are searched. It is important to search in a lesser scope in order to prevent including unwanted controls in search result, because Silverlight introduces many controls dynamically that are not part of the original XAML markup. For example, if you need to search inside a Canvas, pass that Canvas control instead of passing root element (LayoutRoot) as argument.</param>
        /// <param name="query">The CSS selector query.</param>
        /// <returns>The ControlSet object containing the matched controls.</returns>
        public static ControlSet Search(DependencyObject sourceControl, string query)
        {
            Common.ClearLog();
            //Common.TraceLog = true;
            //InspectQuery(query);

            ControlSet      matchedControls = new ControlSet();
            List <Selector> allSelectors    = Parser.Parse(query);

            foreach (Selector selector in allSelectors)
            {
                ControlSet selectorControls = selector.Execute(sourceControl);
                matchedControls.AddRange(selectorControls);
            }

            //Common.Show(matchedControls.Count + " matched-controls");
            //Common.Show(string.Join("\r\n", (from control in matchedControls select control.ToString()).ToArray()));
            //Common.Show(string.Join("\r\n", Common.Log.ToArray()));
            return(matchedControls);
        }
Beispiel #7
0
        public ControlSet Execute(ControlSet superSet)
        {
            Common.AddToLog("executing filter-q: " + this.Query);

            if (Tokens.Count < 2) return (new ControlSet()); //filter-selector should have minimum two tokens, the first one being the filter-delimiter symbol

            ControlSet matchedControls = new ControlSet();
            if (Type == FilterSelectorType.Style)
            {
                string styleName = Tokens[1].Content;
                Style styleObject = ControlUtility.GetStyleByName(styleName);
                if (styleObject != null)
                {
                    foreach (DependencyObject control in superSet) //find controls by style
                    {
                        if (control is FrameworkElement)
                        {
                            FrameworkElement element = (FrameworkElement)control;
                            if (element.Style != null)
                            {
                                //if (element.Style.Equals(styleObject))
                                if (ControlUtility.IsStyleEqualsOrBasedOn(element.Style, styleObject))
                                {
                                    Common.AddToLog("matched-by-style: " + element.Name + " based on style");
                                    matchedControls.Add(control);
                                }
                            }
                        }
                    }
                }
            }
            else if (Type == FilterSelectorType.Name)
            {
                string name = Tokens[1].Content;

                foreach (DependencyObject control in superSet) //find control by name
                {
                    if (control is FrameworkElement)
                    {
                        if (((FrameworkElement)control).Name.Equals(name))
                        {
                            Common.AddToLog("matched-by-name: " + ((FrameworkElement)control).Name);
                            matchedControls.Add(control);
                        }
                    }
                }
            }
            else if (Type == FilterSelectorType.Property)
            {
                matchedControls.AddRange(FilterByPropertyQuery(superSet));
            }

            return (matchedControls);
        }
Beispiel #8
0
        private ControlSet FilterByPropertyQuery(ControlSet superSet)
        {
            ControlSet matchedControls = new ControlSet();

            //prepare query-string
            string queryString = string.Concat((from token in this.Tokens select token.Content).ToArray());
            queryString = queryString.Replace("[", "");
            queryString = queryString.Replace("]", "");
            queryString = queryString.Replace("!=", "!");
            queryString = queryString.Replace("^=", "^");
            queryString = queryString.Replace("$=", "$");
            queryString = queryString.Replace("~=", "~");
            Common.AddToLog("clean filter-query: " + queryString);

            //find controls using property-details
            FilterType filterType = ResolveFilterType(queryString);
            string[] propertyParts = queryString.Split(Parser.PropertyValueDelimiterSymbols.ToCharArray());
            if (propertyParts.Length > 0)
            {
                string propertyNameDescriptor = propertyParts[0];
                string propertyRawValue = propertyParts.Length > 1 ? propertyParts[1] : string.Empty;

                ControlSet subSet = FilterByDependencyProperty(superSet, filterType, propertyParts.Length, propertyNameDescriptor, propertyRawValue);
                if (subSet == null) //if not dependency property, check normal property
                {
                    subSet = FilterByNormalProperty(superSet, filterType, propertyParts.Length, propertyNameDescriptor, propertyRawValue);
                }
                matchedControls.AddRange(subSet);
            }

            return (matchedControls);
        }
Beispiel #9
0
        private static Dictionary<string, Style> GetAllStyles(DependencyObject control)
        {
            //find all user-controls and pages
            ControlSet allUserControls = XamlQuery.ParentsByType<UserControl>(control);
            ControlSet allPages = XamlQuery.ParentsByType<Page>(control);

            Common.AddToLog(allUserControls.Count + " user-controls");
            Common.AddToLog(allPages.Count + " pages");

            ControlSet allContainers = new ControlSet();
            allContainers.AddRange(allUserControls);
            allContainers.AddRange(allPages);

            //get styles from all user-controls and pages and their merged-dictionaries
            Dictionary<string, Style> allStyles = new Dictionary<string, Style>();

            foreach (UserControl container in allContainers)
            {
                List<ResourceDictionary> allDictionaries = GetResourceDictionaryTree(container.Resources);
                foreach (ResourceDictionary dictionary in allDictionaries)
                {
                    foreach (object key in dictionary.Keys)
                    {
                        object resource = container.Resources[key];
                        if (key is string && resource is Style)
                        {
                            allStyles.Add((string)key, (Style)resource);
                        }
                    }
                }
            }

            //get styles defined in App and its merged-dictionaries
            if (Application.Current != null)
            {
                List<ResourceDictionary> allDictionaries = GetResourceDictionaryTree(Application.Current.Resources);
                foreach (ResourceDictionary dictionary in allDictionaries)
                {
                    foreach (object key in dictionary.Keys)
                    {
                        object resource = Application.Current.Resources[key];
                        if (key is string && resource is Style)
                        {
                            allStyles.Add((string)key, (Style)resource);
                        }
                    }
                }
            }

            Common.AddToLog(allStyles.Count + " styles");
            foreach (string key in allStyles.Keys)
            {
                Common.AddToLog("found-style: " + key + ", " + allStyles[key].TargetType);
            }

            return (allStyles);
        }
Beispiel #10
0
        public ControlSet Execute(ControlSet superSet)
        {
            Common.AddToLog("executing combinator: " + this.Type);

            ControlSet result = new ControlSet();

            switch (this.Type)
            {
            case CombinatorType.Descendant:
                //get descendants (children in visual-tree) of all controls in superset
                foreach (DependencyObject control in superSet)
                {
                    ControlSet controlSet = XamlQuery.All(control);
                    foreach (DependencyObject c in controlSet)
                    {
                        if (!result.Contains(c))
                        {
                            result.Add(c);
                        }
                    }
                }
                break;

            case CombinatorType.Child:
                //get children of all Panel controls in superset
                foreach (DependencyObject control in superSet)
                {
                    if (control is Panel)
                    {
                        result.AddRange(from child in ((Panel)control).Children select(DependencyObject) child);
                    }
                }
                break;

            case CombinatorType.Adjacent:
                //get children of all parent Panels of all controls in superset
                //if a parent is not a Panel, then it is ignored
                foreach (DependencyObject control in superSet)
                {
                    if (control is FrameworkElement)
                    {
                        DependencyObject parent = ((FrameworkElement)control).Parent;
                        if (parent is Panel)
                        {
                            result.AddRange(from child in ((Panel)parent).Children select(DependencyObject) child);
                        }
                    }
                }

                //include original controls also in result
                result.AddRange(superSet);
                Common.AddToLog("adj-superset-added");
                break;
            }

            //remove duplicate-controls
            ControlSet uniqueControls = new ControlSet();

            foreach (DependencyObject c in result)
            {
                if (!uniqueControls.Contains(c))
                {
                    uniqueControls.Add(c);
                }
            }

            return(uniqueControls);
        }
 public void Enable(params object[] controls)
 {
     _internalSet.AddRange(controls);
 }
 public void Disable(object[] controls)
 {
     _internalSet.AddRange(controls);
 }
Beispiel #13
0
        public ControlSet Execute(DependencyObject sourceControl)
        {
            Common.AddToLog("executing selector: " + this.Query);

            SourceControl = sourceControl;
            ControlSet superSet = XamlQuery.All(sourceControl);
            ControlSet matchedControls = new ControlSet();

            if (SimpleSelectors.Count == 1) //if only one simple-selector, then execute it and return the result
            {
                return (SimpleSelectors[0].Execute(superSet));
            }
            else if (SimpleSelectors.Count > 1) //if more than one simple-selectors, then apply combinators while executing
            {
                //apply each combinator to result of simple-selector that precedes it
                ControlSet currentSet = new ControlSet();
                currentSet.AddRange(superSet);
                for (int index = 0; index < SimpleSelectors.Count; index++)
                {
                    if (index > 0)
                    {
                        Common.AddToLog("**Executing Combinator: " + Combinators[index - 1].Token.Content);
                        currentSet = Combinators[index - 1].Execute(currentSet);
                        Common.AddToLog("**Combinator-Result: " + currentSet.Count + " controls");
                        if (Combinators[index - 1].Type == CombinatorType.Adjacent)
                        {
                            //for adjacent combinator
                            //   get first-set by executing simple-selector before + with result of combinator
                            //   get second-set by executing simple-selector after + with result of combinator
                            //   combine first and second sets as result of combinator and its simple-selectors
                            ControlSet firstSet = SimpleSelectors[index - 1].Execute(currentSet);
                            ControlSet secondSet = SimpleSelectors[index].Execute(currentSet);
                            currentSet.Clear();
                            currentSet.AddRange(firstSet);
                            currentSet.AddRange(secondSet);
                            Common.AddToLog("**First-Set-Result: " + firstSet.Count + " controls. " + string.Join(",", (from c in firstSet select c.GetType().ToString()).ToArray()));
                            Common.AddToLog("**Second-Set-Result: " + secondSet.Count + " controls. " + string.Join(",", (from c in secondSet select c.GetType().ToString()).ToArray()));
                            Common.AddToLog("**Simple-Selector-Result: " + currentSet.Count + " controls");
                            continue;
                        }
                    }
                    Common.AddToLog("**Executing Simple-Selector: " + SimpleSelectors[index].Query);
                    currentSet = SimpleSelectors[index].Execute(currentSet);
                    Common.AddToLog("**Simple-Selector-Result: " + currentSet.Count + " controls");
                }
                matchedControls.AddRange(currentSet);
            }

            return (matchedControls);
        }
Beispiel #14
0
        public ControlSet Execute(ControlSet superSet)
        {
            Common.AddToLog("executing simple-query: " + this.Query);

            if (Type == SimpleSelectorType.None) return (new ControlSet());

            //set main controls
            ControlSet mainControls = new ControlSet();
            if (Type == SimpleSelectorType.Universal)
            {
                mainControls.AddRange(superSet);
            }
            else if (Type == SimpleSelectorType.Element)
            {
                Type givenControlType = ControlUtility.ResolveControlType(MainToken.Content);
                if (givenControlType != null)
                {
                    foreach (DependencyObject control in superSet)
                    {
                        //if (givenControlType.IsAssignableFrom(control.GetType()))
                        //if (givenControlType.IsSubclassOf(control.GetType()))
                        //if (givenControlType.FullName.Equals(control.GetType().FullName))
                        if (givenControlType.IsInstanceOfType(control))
                        {
                            mainControls.Add(control);
                        }
                    }
                }
            }

            //apply filter-selectors
            if (this.FilterSelectors.Count == 0)
            {
                return (mainControls);
            }
            else
            {
                foreach (FilterSelector filterSelector in this.FilterSelectors)
                {
                    mainControls = filterSelector.Execute(mainControls);
                }
                return (mainControls);
            }
        }
Beispiel #15
0
 public void Show(params Control[] controls)
 {
     _controls.AddRange(controls);
     _allControls.AddRange(controls);
 }
Beispiel #16
0
        public ControlSet Execute(ControlSet superSet)
        {
            Common.AddToLog("executing combinator: " + this.Type);

            ControlSet result = new ControlSet();

            switch (this.Type)
            {
                case CombinatorType.Descendant:
                    //get descendants (children in visual-tree) of all controls in superset
                    foreach (DependencyObject control in superSet)
                    {
                        ControlSet controlSet = XamlQuery.All(control);
                        foreach (DependencyObject c in controlSet)
                        {
                            if (!result.Contains(c))
                            {
                                result.Add(c);
                            }
                        }
                    }
                    break;
                case CombinatorType.Child:
                    //get children of all Panel controls in superset
                    foreach (DependencyObject control in superSet)
                    {
                        if (control is Panel)
                        {
                            foreach (var child in ((Panel)control).Children)
                            {
                                result.Add((DependencyObject)child);
                            }
                        }
                    }
                    break;
                case CombinatorType.Adjacent:
                    //get children of all parent Panels of all controls in superset
                    //if a parent is not a Panel, then it is ignored
                    foreach (DependencyObject control in superSet)
                    {
                        if (control is FrameworkElement)
                        {
                            DependencyObject parent = ((FrameworkElement)control).Parent;
                            if (parent is Panel)
                            {
                                foreach (var child in ((Panel)control).Children)
                                {
                                    result.Add((DependencyObject)child);
                                }
                            }
                        }
                    }

                    //include original controls also in result
                    result.AddRange(superSet);
                    Common.AddToLog("adj-superset-added");
                    break;
            }

            //remove duplicate-controls
            ControlSet uniqueControls = new ControlSet();
            foreach (DependencyObject c in result)
            {
                if (!uniqueControls.Contains(c))
                {
                    uniqueControls.Add(c);
                }
            }

            return (uniqueControls);
        }