void FixLanguageSelectionAttributes() { // BUG in WPF: When entering text from a different language (or direction) than the current selection, // we lose all selection formatting. // By setting the language to match the input language, we don't lose selection formatting when entering text // This has a concequence where the spellcheck language will always match the input language, not the language // set for the operating system. Fortunately, this is probably a good thing. // only track changes to language when we have focus. Control.GotKeyboardFocus += (sender, e) => { if (_languageChangedListener == null) { _languageChangedListener = new LanguageChangedListener(this, swi.InputLanguageManager.Current); } }; Control.LostKeyboardFocus += (sender, e) => { _languageChangedListener?.Dispose(); _languageChangedListener = null; }; // Bug in WPF: When using composition (IME), it doesn't use the selection attributes when inserting the text // so, we apply selection attributes to the composition range while composing. swi.TextCompositionManager.AddPreviewTextInputStartHandler(Control, OnPreviewTextInputStart); swi.TextCompositionManager.AddPreviewTextInputUpdateHandler(Control, OnPreviewTextInputUpdate); swi.TextCompositionManager.AddPreviewTextInputHandler(Control, OnPreviewTextInput); }
protected override void Dispose(bool disposing) { if (disposing) { _languageChangedListener?.Dispose(); _languageChangedListener = null; } base.Dispose(disposing); }