public CustomPaintTextBox(TextBoxBase clientTextBox, SpellCheckHelper speller) { //Set up the CustomPaintTextBox this.clientTextBox = clientTextBox; this.mySpeller = speller; //Create a bitmap with the same dimensions as the textbox myBitmap = new Bitmap(clientTextBox.Width, clientTextBox.Height); //Create the graphics object from this bitmpa...this is where we will draw the lines to start with bufferGraphics = Graphics.FromImage(this.myBitmap); bufferGraphics.Clip = new Region(clientTextBox.ClientRectangle); //Get the graphics object for the textbox. We use this to draw the bufferGraphics textBoxGraphics = Graphics.FromHwnd(clientTextBox.Handle); //Assign a handle for this class and set it to the handle for the textbox this.AssignHandle(clientTextBox.Handle); //We also need to make sure we update the handle if the handle for the textbox changes //This occurs if wordWrap is turned off for a RichTextBox clientTextBox.HandleCreated += TextBoxBase_HandleCreated; //We need to add a handler to change the clip rectangle if the textBox is resized clientTextBox.ClientSizeChanged += TextBoxBase_ClientSizeChanged; //this.disposedValue = false; }
/// <summary> /// Replaces hard hyphens at end of line with soft hyphens. /// </summary> /// <param name="input"></param> /// <returns></returns> public static string ReplaceHyphensWithSoftHyphens(string input) { SpellCheckHelper spellCheck = new SpellCheckHelper(null, GUI.GetCurrentLocaleId()); if (!spellCheck.InitializeSpellCheck()) { return(input); } Regex regex = new Regex("(\\b\\p{L}+)(-|\u2010|\u2011|\u2012|\u2013|\u2014|\u2015)\n(\\p{L}+\\b)"); return(regex.Replace(input, new MatchEvaluator(delegate(Match match) { return ReplaceHyphens(match, spellCheck); }))); }
// IDisposable protected virtual void Dispose(bool disposing) { if (!this.disposedValue & disposing) { if (disposing) { // TODO: dispose managed state (managed objects). if (clientTextBox != null) { this.ReleaseHandle(); clientTextBox.Invalidate(); clientTextBox.HandleCreated -= TextBoxBase_HandleCreated; clientTextBox.ClientSizeChanged -= TextBoxBase_ClientSizeChanged; clientTextBox.Dispose(); clientTextBox = null; } if (myBitmap != null) { myBitmap.Dispose(); myBitmap = null; } if (textBoxGraphics != null) { textBoxGraphics.Dispose(); textBoxGraphics = null; } if (bufferGraphics != null) { bufferGraphics.Dispose(); bufferGraphics = null; } if (mySpeller != null) { mySpeller = null; } } // TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. // TODO: set large fields to null. } this.disposedValue = true; }
static string ReplaceHyphens(Match m, SpellCheckHelper spellCheck) { string before = m.Groups[0].Value; string after = m.Groups[3].Value; char last = before[before.Length - 1]; char first = after[0]; if (Char.IsUpper(first) && Char.IsUpper(last) || Char.IsLower(first) && Char.IsLower(last)) { string word = before + after; if (!spellCheck.IsMispelled(word)) { return(before + SOFT_HYPHEN + "\n" + after); } } // Return the matched string. return(m.Value); }
public RedUnderlineAdorner(TextBox textbox, SpellCheckHelper mySpeller) : base(textbox) { this.textbox = textbox; this.mySpeller = mySpeller; scrollChangedEventHandler = new ScrollChangedEventHandler( delegate { SignalInvalidate(); }); textChangedEventHandler = new TextChangedEventHandler( delegate { mySpeller.SpellCheck(); SignalInvalidate(); }); textbox.AddHandler(ScrollViewer.ScrollChangedEvent, scrollChangedEventHandler); textbox.TextChanged += textChangedEventHandler; }
/// <summary> /// Fires when UseCustomDictionaries changes /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <param name="a">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void UseCustomDictionariesChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs a) { var textBoxBase = dependencyObject as TextBoxBase; if (textBoxBase == null) { return; } var isEnabled = (bool)a.NewValue; if (isEnabled) { var handler = new EventHandler((s, e) => { SpellCheck.SetIsEnabled(textBoxBase, false); SpellCheck.SetIsEnabled(textBoxBase, true); }); ToggleSpellCheck += handler; textBoxBase.Unloaded += (s, e) => ToggleSpellCheck -= handler; SpellCheck.SetIsEnabled(textBoxBase, true); lock (textBoxBase) { try { if (_customDictionaries == null) //load custom dictionaries { _customDictionaries = new List <string>(); var customDictionaryPath = SpellCheckHelper.GetCustomDictionaryPath(); if (!Directory.Exists(customDictionaryPath)) { Directory.CreateDirectory(customDictionaryPath); } _customDictionaryFile = SpellCheckHelper.GetCustomDictionaryFile(customDictionaryPath); _ignoreAllDictionaryFile = SpellCheckHelper.GetIgnoreAllDictionaryFile(customDictionaryPath); var fs = File.Create(_ignoreAllDictionaryFile); fs.Close(); if (!File.Exists(_customDictionaryFile)) { fs = File.Create(_customDictionaryFile); fs.Close(); } var dir = new DirectoryInfo(customDictionaryPath); foreach (var fileInfo in dir.GetFiles("*.lex")) { _customDictionaries.Add(fileInfo.FullName); } } var dictionaries = SpellCheck.GetCustomDictionaries(textBoxBase); dictionaries.Clear(); foreach (var fileName in _customDictionaries) { dictionaries.Add(new Uri(fileName)); } } catch (Exception ex) { LoggingMediator.Log(ex); } } if (textBoxBase.ContextMenu != null) { return; } textBoxBase.ContextMenu = new ContextMenu(); textBoxBase.ContextMenuOpening += textBoxEx_ContextMenuOpening; } }
protected override void toolStripButtonSpellCheck_Click(object sender, EventArgs e) { string localeId = null; if (LookupISO_3_1_Codes.ContainsKey(curLangCode)) { localeId = LookupISO_3_1_Codes[curLangCode]; } else if (LookupISO_3_1_Codes.ContainsKey(curLangCode.Substring(0, 3))) { localeId = LookupISO_3_1_Codes[curLangCode.Substring(0, 3)]; } if (localeId == null) { MessageBox.Show("Need to add an entry in Data/ISO639-1.xml file."); return; } speller = new SpellCheckHelper(this.textBox1, localeId); if (this.toolStripButtonSpellCheck.Checked) { speller.EnableSpellCheck(); } else { speller.DisableSpellCheck(); } this.textBox1.Refresh(); }