开发者

WPF .NET 4.0 as-you-type spell check control?

开发者 https://www.devze.com 2023-03-13 14:03 出处:网络
I am looking for a WPF spell checking control (similar to the in-built functionality but with support for more dictionaries).I need it to have the as-you-type functionality (red underlining).It should

I am looking for a WPF spell checking control (similar to the in-built functionality but with support for more dictionaries). I need it to have the as-you-type functionality (red underlining). It should also support more than 4 languages than the in-built .NET 4.0 spell check (e.g. English, Spanish, German, Italian and Russian language support would be great).

I prefer the control have a MIT or BSD license that can be used within a commercial Windows application. Source code would be great as I would like to integrate the spel开发者_运维知识库ling suggestions into my custom right click context menu.


I combined AvalonEdit with NHunspell by adding my own SpellCheckerBehavior. An example project of this can be found at github. It is implemented like this:

<avalonEdit:TextEditor
    xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
    Name="editor">
    <avalonEdit:TextEditor.ContextMenu>
        <ContextMenu>
            <MenuItem Command="Undo" />
            <MenuItem Command="Redo" />
            <Separator/>
            <MenuItem Command="Cut" />
            <MenuItem Command="Copy" />
            <MenuItem Command="Paste" />
        </ContextMenu>
    </avalonEdit:TextEditor.ContextMenu>
    <i:Interaction.Behaviors>
        <local:SpellCheckerBehavior />
    </i:Interaction.Behaviors>
</avalonEdit:TextEditor>

...modifying my MainWindow's constructor like this:

public MainWindow()
{
    SpellChecker.Default.HunspellInstance = new Hunspell("German.aff", "German.dic");
    editor.TextArea.TextView.LineTransformers.Add(new SpellCheckerColorizer());
}

...and adding the following classes to my project to integrate them:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Rendering;
using NHunspell;

namespace Martin
{
    public class SpellChecker
    {
        static Lazy<SpellChecker> defaultInstance = new Lazy<SpellChecker>(() => new SpellChecker());
        public static SpellChecker Default { get { return defaultInstance.Value; } }

        public Hunspell HunspellInstance { get; set; }

        public class Word
        {
            public int Index { get; set; }
            public string Value { get; set; }
        }

        static IEnumerable<Word> FindWords(string text)
        {
            foreach (Match m in new Regex(@"\w+").Matches(text))
            {
                yield return new Word() { Index = m.Index, Value = m.Value };
            }
        }

        public IEnumerable<Word> FindSpellingErrors(string text)
        {
            foreach (var word in FindWords(text))
            {
                if (!Spell(word.Value))
                {
                    yield return word;
                }
            }
        }

        public bool Spell(string word)
        {
            return HunspellInstance.Spell(word);
        }

        public List<string> Suggest(string word)
        {
            return HunspellInstance.Suggest(word);
        }
    }

    public class SpellCheckerBehavior : Behavior<TextEditor>
    {
        TextEditor textEditor;
        List<Control> originalItems;

        protected override void OnAttached()
        {
            textEditor = AssociatedObject;
            if (textEditor != null)
            {
                textEditor.ContextMenuOpening += new ContextMenuEventHandler(TextEditorContextMenuOpening);
                textEditor.TextArea.MouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(TextAreaMouseRightButtonDown);
                originalItems = textEditor.ContextMenu.Items.OfType<Control>().ToList();
            }
            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            if (textEditor != null)
            {
                textEditor.ContextMenuOpening -= new ContextMenuEventHandler(TextEditorContextMenuOpening);
                textEditor.TextArea.MouseRightButtonDown -= new System.Windows.Input.MouseButtonEventHandler(TextAreaMouseRightButtonDown);
                originalItems = null;
                textEditor = null;
            }
            base.OnDetaching();
        }

        void TextAreaMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var position = textEditor.GetPositionFromPoint(e.GetPosition(textEditor));
            if (position.HasValue)
            {
                textEditor.TextArea.Caret.Position = position.Value;
            }
        }

        void TextEditorContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            foreach (Control item in textEditor.ContextMenu.Items.OfType<Control>().ToList())
            {
                if (originalItems.Contains(item)) { continue; }
                textEditor.ContextMenu.Items.Remove(item);
            }
            var position = textEditor.TextArea.Caret.Position;
            Match word = null;
            Regex r = new Regex(@"\w+");
            var line = textEditor.Document.GetText(textEditor.Document.GetLineByNumber(position.Line));
            foreach (Match m in r.Matches(line))
            {
                if (m.Index >= position.VisualColumn) { break; }
                word = m;
            }
            if (null == word ||
                position.Column > word.Index + word.Value.Length ||
                SpellChecker.Default.Spell(word.Value))
            {
                return;
            }
            textEditor.ContextMenu.Items.Insert(0, new Separator());
            var suggestions = SpellChecker.Default.Suggest(word.Value);
            if (0 == suggestions.Count)
            {
                textEditor.ContextMenu.Items.Insert(0,
                    new MenuItem() { Header = "<No suggestions found>", IsEnabled = false });
                return;
            }
            foreach (string suggestion in suggestions)
            {
                var item = new MenuItem { Header = suggestion, FontWeight = FontWeights.Bold };
                item.Tag =
                    new Tuple<int, int>(
                        textEditor.Document.GetOffset(position.Line, word.Index + 1),
                        word.Value.Length);
                item.Click += ItemClick;
                textEditor.ContextMenu.Items.Insert(0, item);
            }
        }

        private void ItemClick(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            if (null == item) { return; }
            var segment = item.Tag as Tuple<int, int>;
            if (null == segment) { return; }
            textEditor.Document.Replace(segment.Item1, segment.Item2, item.Header.ToString());
        }
    }

    public class SpellCheckerColorizer : DocumentColorizingTransformer
    {
        private readonly TextDecorationCollection textDecorationCollection;

        public SpellCheckerColorizer()
        {
            textDecorationCollection = new TextDecorationCollection();
            textDecorationCollection.Add(new TextDecoration()
            {
                Pen = new Pen { Thickness = 1, DashStyle = DashStyles.Dot, Brush = new SolidColorBrush(Colors.Red) },
                PenThicknessUnit = TextDecorationUnit.FontRecommended
            });
        }

        protected override void ColorizeLine(DocumentLine line)
        {
            var lineText = CurrentContext.Document.Text
                .Substring(line.Offset, line.Length);
            foreach (var error in SpellChecker.Default.FindSpellingErrors(lineText))
            {
                base.ChangeLinePart(line.Offset + error.Index, line.Offset + error.Index + error.Value.Length,
                            (VisualLineElement element) => element.TextRunProperties.SetTextDecorations(textDecorationCollection));
            }
        }
    }
}


AvalonEdit ( http://www.codeproject.com/KB/edit/AvalonEdit.aspx ) was written from the ground up in WPF for SharpDevelop. Oh the wonders of a quick Google.

And if you're fine with one language at a time (but toggable), you can use the default WPF functionality ( http://joshsmithonwpf.wordpress.com/2007/02/17/spellchecking-and-suggested-spellings-in-a-textbox/ )

0

精彩评论

暂无评论...
验证码 换一张
取 消