Beispiel #1
0
        /// <summary>
        /// Register custom highlighting patterns for usage in the editor
        /// </summary>
        public static void RegisterCustomHighlightingPatterns(IHighlightingThemes hlThemes = null)
        {
            try
            {
                HighlightingManager.Instance.InitializeDefinitions(hlThemes);

                string path = Path.GetDirectoryName(Application.ResourceAssembly.Location);
                path = Path.Combine(path, "AvalonEdit\\Highlighting");

                if (Directory.Exists(path))
                {
                    // Some HighlightingDefinitions contain 'import' statements which means that some
                    // XSHDs have to be loaded before others (an exception is thrown otherwise)
                    // Therefore, we use filenames to indicate sequence for loading xshds
                    SortedSet <string> files = new SortedSet <string>(Directory.GetFiles(path).Where(x =>
                    {
                        var extension = Path.GetExtension(x);
                        return(extension != null && extension.Contains("xshd"));
                    }));

                    foreach (var file in files)
                    {
                        try
                        {
                            var definition = LoadXshdDefinition(file);
                            var hightlight = LoadHighlightingDefinition(file);

                            HighlightingManager.Instance.RegisterHighlighting(definition.Name, definition.Extensions.ToArray(), hightlight);
                        }
                        catch { throw; }
                    }
                }
            }
            catch { throw; }
        }
Beispiel #2
0
        /// <summary>
        /// Class Constructor
        /// </summary>
        public HighlightingManager()
        {
            sortedHighlightingsByName = new SortedList <string, IHighlightingDefinition>();
            listHighlightingsByName   = new ObservableCollection <IHighlightingDefinition>();

            highlightingsByExtension = new SortedDictionary <string, IHighlightingDefinition>(StringComparer.OrdinalIgnoreCase);
            allHighlightings         = new List <IHighlightingDefinition>();

            this.mHlThemes          = null;
            this.BackupDynResources = null;
        }
Beispiel #3
0
        /// <summary>
        /// Dirkster99
        /// Initialize the highlighting definitions to force re-load of definitions
        /// </summary>
        public void InitializeDefinitions(IHighlightingThemes hlThemes = null)
        {
            lock (lockObj)
            {
                this.sortedHighlightingsByName.Clear();

                this.listHighlightingsByName.Clear();
                this.highlightingsByExtension.Clear();
                this.allHighlightings.Clear();

                this.mHlThemes = hlThemes;
            }
        }
        /// <summary>
        /// Apply highlighting theme to highlighting pattern definition.
        /// This results in re-defined highlighting colors while keeping
        /// rules for regular expression matching.
        /// </summary>
        /// <param name="hdef">Current highlighting pattern</param>
        /// <param name="hlThemes">Collection of highlighting styles to be applied on current highlighting patterns</param>
        public static void ApplyHighlightingTheme(IHighlightingDefinition hdef,
                                                  IHighlightingThemes hlThemes)
        {
            if (hdef == null || hlThemes == null)
            {
                return;
            }

            IHighlightingTheme theme = hlThemes.FindTheme(hdef.Name);  // Is the current highlighting (eg.: HTML) themable?

            if (theme != null)
            {
                if (hdef.NamedHighlightingColors != null)
                {
                    // Go through each color definition in the highlighting and apply the theme on each match
                    foreach (HighlightingColor c in hdef.NamedHighlightingColors)
                    {
                        IWordsStyle s = theme.GetWordsStyle(c.Name);

                        if (s != null)
                        {
                            if (s.bgColor != null)
                            {
                                c.Background = new SimpleHighlightingBrush(s.bgColor);
                            }
                            else
                            {
                                c.Background = null;
                            }

                            if (s.fgColor != null)
                            {
                                c.Foreground = new SimpleHighlightingBrush(s.fgColor);
                            }
                            else
                            {
                                c.Foreground = null;
                            }

                            if (s.fontStyle != null)
                            {
                                c.FontStyle = s.fontStyle;
                            }
                            else
                            {
                                c.FontStyle = null;
                            }

                            if (s.fontWeight != null)
                            {
                                c.FontWeight = s.fontWeight;
                            }
                            else
                            {
                                c.FontStyle = null;
                            }
                        }
                        else
                        {
                            logger.WarnFormat("Named Color: '{0}'in '{1}' does not exist in '{2}'.", c.Name, hdef.Name, hlThemes.FileNamePath);
                        }
                    }
                }
            }
            else
            {
                logger.WarnFormat("highlighting definition: '{0}' does not have a style in '{1}'.", hdef.Name, hlThemes.FileNamePath);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Change WPF theme.
        ///
        /// This method can be called when the theme is to be reseted by all means
        /// (eg.: when powering application up).
        ///
        /// !!! Use the CurrentTheme property to change !!!
        /// !!! the theme when App is running           !!!
        /// </summary>
        public void ResetTheme()
        {
            // Reset customized resources (if there are any from last change) and
            // enforce reload of original values from resource dictionary
            if (HighlightingManager.Instance.BackupDynResources != null)
            {
                try
                {
                    foreach (string t in HighlightingManager.Instance.BackupDynResources)
                    {
                        Application.Current.Resources[t] = null;
                    }
                }
                catch
                {
                    // ignored
                }
                finally
                {
                    HighlightingManager.Instance.BackupDynResources = null;
                }
            }

            // Get WPF Theme definition from Themes Assembly
            IThemeBase nextThemeToSwitchTo = ApplicationThemes.SelectedTheme;

            SwitchToSelectedTheme(nextThemeToSwitchTo);

            // Backup highlighting names (if any) and restore highlighting associations after reloading highlighting definitions
            var hlNames = new List <string>();

            foreach (EdiViewModel f in Documents)
            {
                if (f != null)
                {
                    hlNames.Add(f.HighlightingDefinition?.Name);
                }
            }

            // Is the current theme configured with a highlighting theme???
            ////this.Config.FindHighlightingTheme(
            IHighlightingThemes hlThemes = nextThemeToSwitchTo.HighlightingStyles;

            // Re-load all highlighting patterns and re-apply highlightings
            HighlightingExtension.RegisterCustomHighlightingPatterns(hlThemes);

            //Re-apply highlightings after resetting highlighting manager
            List <EdiViewModel> l = Documents;

            for (int i = 0; i < l.Count; i++)
            {
                if (l[i] != null)
                {
                    if (hlNames[i] == null) // The highlighting is null if highlighting is switched off for this file(!)
                    {
                        continue;
                    }

                    IHighlightingDefinition hdef = HighlightingManager.Instance.GetDefinition(hlNames[i]);

                    if (hdef != null)
                    {
                        l[i].HighlightingDefinition = hdef;
                    }
                }
            }

            var backupDynResources = new List <string>();

            // Apply global styles to theming elements (dynamic resources in resource dictionary) of editor control
            if (HighlightingManager.Instance.HlThemes != null)
            {
                foreach (WidgetStyle w in HighlightingManager.Instance.HlThemes.GlobalStyles)
                {
                    ApplyWidgetStyle(w, backupDynResources);
                }
            }

            if (backupDynResources.Count > 0)
            {
                HighlightingManager.Instance.BackupDynResources = backupDynResources;
            }
        }