private void SelectionTimer_Tick(object sender, EventArgs e)
        {
            //کل متن را انتخاب میکند
            (sender as DispatcherTimer).Stop();
            try
            {
                var document = richTextBox.Document;
                DocumentTextSearch search = new DocumentTextSearch(document);
                List <Telerik.Windows.Documents.TextSearch.TextRange> rangesTrackingDocumentChanges = new List <Telerik.Windows.Documents.TextSearch.TextRange>();
                foreach (var textRange in search.FindAll(selectionText))
                {
                    Telerik.Windows.Documents.TextSearch.TextRange newRange = new Telerik.Windows.Documents.TextSearch.TextRange(new DocumentPosition(textRange.StartPosition, true), new DocumentPosition(textRange.EndPosition, true));
                    rangesTrackingDocumentChanges.Add(newRange);
                }

                foreach (var textRange in rangesTrackingDocumentChanges)
                {
                    richTextBox.Document.Selection.SetSelectionStart(textRange.StartPosition);
                    richTextBox.Document.Selection.AddSelectionEnd(textRange.EndPosition);
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 2
0
        public void IgnoreWord(string word)
        {
            this.controlSpellChecker.IgnoredWords.AddWord(word);
            DocumentTextSearch search        = new DocumentTextSearch(this.Document);
            DocumentPosition   savedPosition = this.Document.CaretPosition;

            foreach (TextRange text in search.FindAll(word))
            {
                this.Document.Selection.AddSelectionStart(text.StartPosition);
                this.Document.Selection.AddSelectionEnd(text.EndPosition);
                this.ChangeUnderlineDecoration(Telerik.WinControls.RichTextBox.UI.UnderlineType.None);
            }

            this.Document.Selection.AddSelectionStart(savedPosition);
            this.Document.Selection.AddSelectionEnd(savedPosition);
        }
        private void radRichTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                double originalOffset = radRichTextBox.VerticalOffset;
                double originalY      = this.radRichTextBox.Document.CaretPosition.Location.Y;
                using (DocumentPosition originalCaretPosition =
                           new DocumentPosition(this.radRichTextBox.Document.CaretPosition, true))
                {
                    Paragraph currentParagraph = radRichTextBox.Document.CaretPosition.GetCurrentParagraph();

                    // remove any extant links, so that we can reparse them
                    IEnumerable <HyperlinkRangeStart> links = this.radRichTextBox.Document.CaretPosition
                                                              .GetCurrentParagraph().EnumerateChildrenOfType <HyperlinkRangeStart>();
                    foreach (HyperlinkRangeStart link in links)
                    {
                        RadDocumentEditor documentEditor = new RadDocumentEditor(radRichTextBox.Document);
                        documentEditor.DeleteAnnotationRange(link);
                    }

                    foreach (Inline inline in currentParagraph.Inlines)
                    {
                        string currentBook    = string.Empty;
                        uint   currentChapter = 0;
                        uint   currentVerse   = 0;


                        DocumentTextSearch search = new DocumentTextSearch(radRichTextBox.Document);
                        radRichTextBox.Document.CaretPosition.MoveToFirstPositionInParagraph();
                        DocumentPosition startDocPos = radRichTextBox.Document.CaretPosition;
                        radRichTextBox.Document.CaretPosition.MoveToLastPositionInParagraph();
                        DocumentPosition endDocPos = radRichTextBox.Document.CaretPosition;
                        radRichTextBox.Document.CaretPosition.MoveToFirstPositionInParagraph();

                        foreach (TextRange textRange in search.FindAll("(\\b((1|2|3|I|II|III|i|ii|iii) )?[\\w']+\\b\\s\\d+:\\d*[\\d \\-:]{0,300}\\d)|(, ?\\d+:\\d+)[\\-]*\\d*[\\d \\-:]{0,300}\\d|(, ?\\d+)[\\-]*\\d*|(v\\d{1,3})", startDocPos, originalCaretPosition))
                        {
                            // textRange will represent a string in one of the following formats:
                            // 1: book chapter:verse (with the possible suffix of "-{chapter:}verse
                            // 2: , chapter:verse (with the possible suffix of "-{chapter:}verse
                            // 3: , verse  (with the possible suffix of "-{chapter:}verse
                            // 4: v[verse]



                            RadDocumentEditor documentEditor = new RadDocumentEditor(radRichTextBox.Document);
                            radRichTextBox.Document.Selection.Ranges.Clear();
                            radRichTextBox.Document.Selection.AddSelectionStart(textRange.StartPosition);
                            radRichTextBox.Document.Selection.AddSelectionEnd(textRange.EndPosition);
                            string currentWord = radRichTextBox.Document.Selection.GetSelectedText();

                            Debug.WriteLine($"trying to parse {currentWord}");

                            ParsedReference result = ProcessReference(currentWord, currentBook, currentChapter, currentVerse);

                            if (result.WasParsedSuccessfully)
                            {
                                HyperlinkInfo info = new HyperlinkInfo()
                                {
                                    NavigateUri = $"kgb://book={result.Book}&chap={result.Chapter}&verse={result.Verse}",
                                    Target      = HyperlinkTargets.Blank,
                                    IsAnchor    = false
                                };


                                documentEditor.InsertHyperlink(info);
                                currentBook    = result.Book;
                                currentChapter = result.Chapter;
                                currentVerse   = result.Verse;
                            }

                            radRichTextBox.Document.Selection.Ranges.Clear();
                        }
                    }

                    radRichTextBox.Document.CaretPosition.MoveToPosition(originalCaretPosition);
                    radRichTextBox.Document.Selection.Ranges.Clear();
                    radRichTextBox.ActiveEditorPresenter.ScrollToVerticalOffset(originalOffset - originalY + this.radRichTextBox.Document.CaretPosition.Location.Y);
                }
            }
            radRichTextBox.UpdateEditorLayout(false);
        }