//takes one the first word on the range, and checks it for spelling errors
        //***returns true if word is misspelled***
        private bool ProcessWord(MshtmlWordRange word, out int offset, out int length)
        {
            offset = 0;
            length = 0;

            string           otherWord = null;
            SpellCheckResult result;
            string           currentWord = word.CurrentWord;

            if (!word.IsCurrentWordUrlPart() && !WordRangeHelper.ContainsOnlySymbols(currentWord))
            {
                result = _spellingChecker.CheckWord(currentWord, out otherWord, out offset, out length);
            }
            else
            {
                result = SpellCheckResult.Correct;
            }

            if (result != SpellCheckResult.Correct)
            {
                //note: currently using this to not show any errors in smart content, since the fix isn't
                // propagated to the underlying data structure
                if (!word.FilterApplies())
                {
                    return(true);
                }
            }
            return(false);
        }
 /// <summary>
 /// Check spelling--called by the damage handler
 /// </summary>
 public void CheckSpelling(MshtmlWordRange range)
 {
     if ( !_fatalSpellingError )
     {
         _timer.Enabled = true;
         _workerQueue.Enqueue(range);
         if (_workerQueue.Count == 1)
         {
             //if the queue had been empty, process this range right away
             DoWork();
         }
     }
 }
 /// <summary>
 /// Check spelling--called by the damage handler
 /// </summary>
 public void CheckSpelling(MshtmlWordRange range)
 {
     if (!_fatalSpellingError)
     {
         _timer.Enabled = true;
         _workerQueue.Enqueue(range);
         if (_workerQueue.Count == 1)
         {
             //if the queue had been empty, process this range right away
             DoWork();
         }
     }
 }
Example #4
0
 public void DamagedRange(MarkupRange range, bool doSpellCheck)
 {
     if (doSpellCheck)
     {
         HighlightSpelling(range);
     }
     else
     {
         if (range.Text != null)
         {
             MshtmlWordRange.ExpandRangeToWordBoundaries(range);
         }
         _ignoredOnce.ClearRange(range);
     }
 }
        //iterates through a word range checking for spelling errors
        //return: whether the word range is finished (true) or not
        private bool ProcessWordRange(MshtmlWordRange wordRange)
        {
            if (wordRange.CurrentWordRange.Positioned)
            {
                //track where we will need to clear;
                MarkupPointer start = _markupServices.CreateMarkupPointer();
                start.MoveToPointer(wordRange.CurrentWordRange.End);
                ArrayList highlightwords = new ArrayList(NUMBER_OF_WORDS_TO_CHECK);

                int i = 0;
                //to do....the word range is losing its place when it stays in the queue
                while (wordRange.HasNext() && i < NUMBER_OF_WORDS_TO_CHECK)
                {
                    // advance to the next word
                    wordRange.Next();
                    // check the spelling
                    int offset, length;
                    if (ProcessWord(wordRange, out offset, out length))
                    {
                        MarkupRange highlightRange = wordRange.CurrentWordRange.Clone();
                        MarkupHelpers.AdjustMarkupRange(ref stagingTextRange, highlightRange, offset, length);

                        //note: cannot just push the current word range here, as it moves before we get to the highlighting step
                        highlightwords.Add(highlightRange);
                    }
                    i++;
                }
                MarkupPointer end = wordRange.CurrentWordRange.End;

                //got our words, clear the checked range and then add the misspellings
                ClearRange(start, end);
                foreach (MarkupRange word in highlightwords)
                {
                    HighlightWordRange(word);
                }

                return(!wordRange.HasNext());
            }
            else
            {
                return(true);
            }
        }
Example #6
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);
            }
        }
Example #7
0
        public void HighlightSpelling(MarkupRange range)
        {
            // check spelling
            MshtmlWordRange wordRange;

            if (range == null) //check the whole document.
            {
                wordRange = new MshtmlWordRange(_htmlDocument, false, _filter, _damageFunction);
            }
            else
            {
                //range is invalid for some reason--damage committed while switching views, getting it later on the timer
                if (!range.Positioned)
                {
                    return;
                }
                else if (range.Text == null || String.IsNullOrEmpty(range.Text.Trim()))
                {
                    //empty range--on a delete for instance, just clear
                    _spellingHighlighter.ClearRange(range.Start, range.End);
                    _ignoredOnce.ClearRange(range);
                    return;
                }
                else
                {
                    MarkupRange origRange = range.Clone();
                    //here are the words to check
                    wordRange = new MshtmlWordRange(_htmlDocument, range, _filter, _damageFunction);
                    //check for emptiness at start and end, clear those
                    _spellingHighlighter.ClearRange(origRange.Start, range.Start);
                    _spellingHighlighter.ClearRange(range.End, origRange.End);

                    _ignoredOnce.ClearRange(range);
                }
            }

            _spellingHighlighter.CheckSpelling(wordRange);
        }
        /// <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;
            }
        }
        //takes one the first word on the range, and checks it for spelling errors
        //***returns true if word is misspelled***
        private bool ProcessWord(MshtmlWordRange word, out int offset, out int length)
        {
            offset = 0;
            length = 0;

            string otherWord = null;
            SpellCheckResult result;
            string currentWord = word.CurrentWord;
            if (!word.IsCurrentWordUrlPart() && !WordRangeHelper.ContainsOnlySymbols(currentWord))
                result = _spellingChecker.CheckWord( currentWord, out otherWord, out offset, out length ) ;
            else
                result = SpellCheckResult.Correct;

            if (result != SpellCheckResult.Correct)
            {
                //note: currently using this to not show any errors in smart content, since the fix isn't
                // propogated to the underlying data structure
                if (!word.FilterApplies())
                {
                    return true;
                }
            }
            return false;
        }
        //iterates through a word range checking for spelling errors
        //return: whether the word range is finished (true) or not
        private bool ProcessWordRange(MshtmlWordRange wordRange)
        {
            if (wordRange.CurrentWordRange.Positioned)
            {
                //track where we will need to clear;
                MarkupPointer start = _markupServices.CreateMarkupPointer();
                start.MoveToPointer(wordRange.CurrentWordRange.End);
                ArrayList highlightwords = new ArrayList(NUMBER_OF_WORDS_TO_CHECK);

                int i = 0;
                //to do....the word range is losing its place when it stays in the queue
                while (wordRange.HasNext() && i < NUMBER_OF_WORDS_TO_CHECK )
                {
                    // advance to the next word
                    wordRange.Next() ;
                    // check the spelling
                    int offset, length;
                    if (ProcessWord(wordRange, out offset, out length))
                    {
                        MarkupRange highlightRange = wordRange.CurrentWordRange.Clone();
                        MarkupHelpers.AdjustMarkupRange(ref stagingTextRange, highlightRange, offset, length);

                        //note: cannot just push the current word range here, as it moves before we get to the highlighting step
                        highlightwords.Add(highlightRange);
                    }
                    i++;
                }
                MarkupPointer end = wordRange.CurrentWordRange.End;

                //got our words, clear the checked range and then add the misspellings
                ClearRange(start, end);
                foreach (MarkupRange word in highlightwords)
                {
                    HighlightWordRange(word);
                }

                return !wordRange.HasNext();
            }
            else
                return true;
        }
        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 void HighlightSpelling(MarkupRange range)
        {
            // check spelling
            MshtmlWordRange wordRange;
            if (range == null) //check the whole document.
            {
                wordRange = new MshtmlWordRange(_htmlDocument, false, _filter, _damageFunction);
            }
            else
            {
                //range is invalid for some reason--damage committed while switching views, getting it later on the timer
                if (!range.Positioned)
                    return;
                else if (range.Text == null || String.IsNullOrEmpty(range.Text.Trim()))
                {
                    //empty range--on a delete for instance, just clear
                    _spellingHighlighter.ClearRange(range.Start, range.End);
                    _ignoredOnce.ClearRange(range);
                    return;
                }
                else
                {
                    MarkupRange origRange = range.Clone();
                    //here are the words to check
                    wordRange = new MshtmlWordRange(_htmlDocument, range, _filter, _damageFunction);
                    //check for emptiness at start and end, clear those
                    _spellingHighlighter.ClearRange(origRange.Start, range.Start);
                    _spellingHighlighter.ClearRange(range.End, origRange.End);

                    _ignoredOnce.ClearRange(range);
                }
            }

            _spellingHighlighter.CheckSpelling(wordRange);
        }