Esempio n. 1
0
        private void openSpellingForm_Execute(object sender, EventArgs e)
        {
            _mshtmlControl.MarkupServices.BeginUndoUnit(Guid.NewGuid().ToString());

            bool    supportsIgnoreOnce = false;
            Command cmdIgnoreOnce      = CommandManager.Get(CommandId.IgnoreOnce);

            if (cmdIgnoreOnce != null && cmdIgnoreOnce.On)
            {
                supportsIgnoreOnce = true;
            }

            // must first force the control to lose focus so that it doesn't "lose"
            // the selection when the dialog opens
            IntPtr hPrevious = User32.SetFocus(IntPtr.Zero);

            using (SpellCheckerForm spellCheckerForm = new SpellCheckerForm(SpellingChecker, _mshtmlControl.FindForm(), supportsIgnoreOnce))
            {
                //  center the spell-checking form over the document body
                spellCheckerForm.StartPosition = FormStartPosition.CenterParent;

                // WinLive 263320: We want to check from the current word to the end of the document.
                // TODO: Although this works fine, it would be better to only spellcheck inside editable regions.
                MarkupRange rangeToSpellCheck = _currentWordInfo.WordRange.Clone();
                rangeToSpellCheck.End.MoveAdjacentToElement(_htmlDocument.body, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeEnd);

                // get the word range to check
                MshtmlWordRange wordRange = new MshtmlWordRange(_htmlDocument, rangeToSpellCheck, _filter, _damageFunction);

                spellCheckerForm.WordIgnored += (sender2, args) => IgnoreOnce(wordRange.CurrentWordRange);

                // check spelling
                spellCheckerForm.CheckSpelling(wordRange);
                _mshtmlControl.MarkupServices.EndUndoUnit();

                // restore focus to the control that had it before we spell-checked
                User32.SetFocus(hPrevious);
            }
        }
        /// <summary>
        /// Check the spelling of the document, returning true if the user completed the spelling check
        /// </summary>
        /// <returns>false if they cancelled the spelling check or if we're already in the middle of executing a spell check</returns>
        public bool CheckSpelling()
        {
            if (!Editable || _isSpellChecking)
                return false;

            try
            {
                _isSpellChecking = true;

                // create an undo unit for the spell-check
                IUndoUnit undoUnit = CreateUndoUnit();

                // save the current selection because it will be lost during spell checking
                MarkupRange previousMarkupRange = null;
                bool previousMarkupRangeCollapsed = true;

                if (SelectedMarkupRange != null)
                {
                    previousMarkupRange = SelectedMarkupRange.Clone();
                    previousMarkupRange.Start.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Left;
                    previousMarkupRange.End.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Right;

                    // if the current selection is collapsed we'll make sure it stays collapsed.
                    // the selection can grow if its inside a misspelled word that gets corrected.
                    previousMarkupRangeCollapsed = previousMarkupRange.IsEmpty();
                }

                // must first force the control to lose focus so that it doesn't "lose"
                // the selection when the dialog opens
                IntPtr hPrevious = User32.SetFocus(IntPtr.Zero);

                bool ignoreOnceSupported = CommandManager.Get(CommandId.IgnoreOnce).On;

                // check spelling
                bool fCompleted = false;

                using (SpellCheckerForm spellCheckerForm = new SpellCheckerForm(SpellingChecker, EditorControl.FindForm(), ignoreOnceSupported))
                {
                    //  center the spell-checking form over the document body
                    spellCheckerForm.StartPosition = FormStartPosition.CenterParent;

                    // determine whether we are checking a selection or the whole document
                    // get selection
                    IHTMLSelectionObject selection = HTMLDocument.selection;
                    bool checkSelection = (selection != null) && (selection.type.ToLower(CultureInfo.InvariantCulture) == "text");

                    // get the word range to check
                    MshtmlWordRange wordRange = new MshtmlWordRange(HTMLDocument, checkSelection, IgnoreRangeForSpellChecking, new DamageFunction(_damageServices.AddDamage));

                    spellCheckerForm.WordIgnored += (sender, args) => OnSpellCheckWordIgnored(wordRange.CurrentWordRange);

                    // check spelling
                    using (undoUnit)
                    {
                        spellCheckerForm.CheckSpelling(wordRange);
                        undoUnit.Commit();
                    }

                    // reselect what was selected previous to spell-checking
                    if (previousMarkupRange != null)
                    {
                        if (previousMarkupRangeCollapsed)
                            previousMarkupRange.Collapse(true);

                        previousMarkupRange.ToTextRange().select();
                    }

                    // return completed status
                    fCompleted = spellCheckerForm.Completed;
                }

                if (fCompleted && _mainFrameWindow != null && _mainFrameWindow is IWordRangeProvider)
                {
                    // Spell check the subject, it doesn't support the "ignore once" feature
                    using (SpellCheckerForm spellCheckerForm = new SpellCheckerForm(SpellingChecker, EditorControl.FindForm(), false))
                    {
                        //  center the spell-checking form over the document body
                        spellCheckerForm.StartPosition = FormStartPosition.CenterParent;

                        IWordRangeProvider wordRangeProvider = (IWordRangeProvider)_mainFrameWindow;
                        IWordRange wordRangeSubject = wordRangeProvider.GetSubjectSpellcheckWordRange();

                        spellCheckerForm.CheckSpelling(wordRangeSubject);

                        wordRangeProvider.CloseSubjectSpellcheckWordRange();
                        fCompleted = spellCheckerForm.Completed;
                    }
                }

                // restore focus to the control that had it before we spell-checked
                User32.SetFocus(hPrevious);

                return fCompleted;

            }
            finally
            {
                _isSpellChecking = false;
            }
        }
Esempio n. 3
0
        private void openSpellingForm_Execute(object sender, EventArgs e)
        {
            _mshtmlControl.MarkupServices.BeginUndoUnit(Guid.NewGuid().ToString());

            bool supportsIgnoreOnce = false;
            Command cmdIgnoreOnce = CommandManager.Get(CommandId.IgnoreOnce);
            if (cmdIgnoreOnce != null && cmdIgnoreOnce.On)
                supportsIgnoreOnce = true;

            // must first force the control to lose focus so that it doesn't "lose"
            // the selection when the dialog opens
            IntPtr hPrevious = User32.SetFocus(IntPtr.Zero);

            using (SpellCheckerForm spellCheckerForm = new SpellCheckerForm(SpellingChecker, _mshtmlControl.FindForm(), supportsIgnoreOnce))
            {
                //  center the spell-checking form over the document body
                spellCheckerForm.StartPosition = FormStartPosition.CenterParent;

                // WinLive 263320: We want to check from the current word to the end of the document.
                // TODO: Although this works fine, it would be better to only spellcheck inside editable regions.
                MarkupRange rangeToSpellCheck = _currentWordInfo.WordRange.Clone();
                rangeToSpellCheck.End.MoveAdjacentToElement(_htmlDocument.body, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeEnd);

                // get the word range to check
                MshtmlWordRange wordRange = new MshtmlWordRange(_htmlDocument, rangeToSpellCheck, _filter, _damageFunction);

                spellCheckerForm.WordIgnored += (sender2, args) => IgnoreOnce(wordRange.CurrentWordRange);

                // check spelling
                spellCheckerForm.CheckSpelling(wordRange);
                _mshtmlControl.MarkupServices.EndUndoUnit();

                // restore focus to the control that had it before we spell-checked
                User32.SetFocus(hPrevious);
            }
        }
        public bool CheckSpelling()
        {
            // check spelling
            using (SpellCheckerForm spellCheckerForm = new SpellCheckerForm(SpellingChecker, EditorControl.FindForm(), false))
            {
                //  center the spell-checking form over the document body
                spellCheckerForm.StartPosition = FormStartPosition.CenterParent;

                // create word range
                // TODO: smarter word range for html
                //TextBoxWordRange wordRange = new TextBoxWordRange(_textBox, _textBox.SelectionLength > 0);
                HtmlTextBoxWordRange wordRange = new HtmlTextBoxWordRange(_textBox);

                // check spelling
                spellCheckerForm.CheckSpelling(wordRange);

                // return completed status
                return spellCheckerForm.Completed;
            }
        }