예제 #1
0
        private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            editor.Document.Selection.Clear();

            var search       = new DocumentTextSearch(editor.Document);
            var lastPosition = new DocumentPosition(editor.Document);

            bool endOfDocument   = false;
            bool theFirstFinding = true;

            while (!endOfDocument)
            {
                TextRange range;

                range = search.Find("#break#", theFirstFinding ? new DocumentPosition(editor.Document) : lastPosition);

                if (range != null)
                {
                    theFirstFinding = false;
                    lastPosition    = range.EndPosition;

                    editor.Document.Selection.AddSelectionStart(range.StartPosition);
                    editor.Document.Selection.AddSelectionEnd(range.EndPosition);

                    var documentEditor = new RadDocumentEditor(editor.Document);
                    documentEditor.InsertLineBreak();
                }
                else
                {
                    endOfDocument = true;
                }
            }
        }
        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
            {
            }
        }
예제 #3
0
        private void btnReplaceAll_Click(object sender, RoutedEventArgs e)
        {
            int foundCount = 0;

            this.richTextBox.SuspendUpdateLayout();
            this.Document.BeginUpdate();
            try
            {
                using (DocumentPosition startFindPosition = new DocumentPosition(this.Document.DocumentLayoutBox, true))
                {
                    var found = true;

                    while (found)
                    {
                        DocumentTextSearch textSearch = new DocumentTextSearch(this.Document);
                        TextRange          find       = textSearch.Find(this.GetSearchText(), startFindPosition);

                        found = find != null;

                        if (found)
                        {
                            startFindPosition.MoveToPosition(find.EndPosition);
                            startFindPosition.AnchorToNextFormattingSymbol();

                            //This is needed to update the current style for editing
                            this.Document.CaretPosition.MoveToPosition(find.StartPosition);
                            find.SetSelection(this.Document);
                            if (this.replaceCallback(this.tbReplaceText.Text))
                            {
                                foundCount++;
                            }
                            startFindPosition.RemoveAnchorFromNextFormattingSymbol();
                            startFindPosition.MoveToPosition(this.Document.CaretPosition);
                        }
                    }
                }
            }
            finally
            {
                this.Document.EndUpdate();
                this.richTextBox.ResumeUpdateLayout();
            }

            RadWindow.Alert(new DialogParameters()
            {
                Header  = this.Header,
                Content = string.Format(LocalizationManager.GetString("Documents_FindReplaceDialog_MadeReplacements"), foundCount),
                Owner   = this
            });
        }
예제 #4
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 HighlightOccurrencesInVisibleBoxes(IEnumerable <SpanLayoutBox> spanList)
        {
            if (spanList.Count() == 0)
            {
                return;
            }
            SpanLayoutBox firstBox = spanList.First();
            SpanLayoutBox lastBox  = spanList.Last();

            DocumentPosition searchStart = new DocumentPosition(this.Document);
            DocumentPosition searchEnd   = new DocumentPosition(this.Document);

            searchStart.MoveToInline(firstBox, 0);
            searchEnd.MoveToInline(lastBox, lastBox.PositionsCountInBox - 1);

            DocumentTextSearch textSearch = new DocumentTextSearch(this.Document);

            TextRange textRange = textSearch.Find(word, searchStart, searchEnd);
            int       count     = 0;

            while (textRange != null)
            {
                count++;
                DocumentPosition lineStart = new DocumentPosition(textRange.StartPosition);
                DocumentPosition lineEnd   = new DocumentPosition(lineStart);
                lineEnd.MoveToCurrentLineEnd();
                while (lineEnd < textRange.EndPosition)
                {
                    this.FlushBoxes(lineStart, lineEnd);
                    lineStart.MoveToCurrentLineEnd();
                    lineStart.MoveToNext();
                    lineEnd.MoveToNext();
                    lineEnd.MoveToCurrentLineEnd();
                }
                this.FlushBoxes(lineStart, textRange.EndPosition);

                searchStart.MoveToPosition(textRange.EndPosition);
                if (searchStart >= searchEnd)
                {
                    break;
                }
                textRange = textSearch.Find(word, searchStart, searchEnd);
            }
        }
        private void HighlightOccurrencesInVisibleBoxes(IEnumerable<SpanLayoutBox> spanList)
        {
            if (spanList.Count() == 0)
            {
                return;
            }
            SpanLayoutBox firstBox = spanList.First();
            SpanLayoutBox lastBox = spanList.Last();

            DocumentPosition searchStart = new DocumentPosition(this.Document);
            DocumentPosition searchEnd = new DocumentPosition(this.Document);
            searchStart.MoveToInline(firstBox, 0);
            searchEnd.MoveToInline(lastBox, lastBox.PositionsCountInBox - 1);

            DocumentTextSearch textSearch = new DocumentTextSearch(this.Document);

            TextRange textRange = textSearch.Find(word, searchStart, searchEnd);
            int count = 0;
            while (textRange != null)
            {
                count++;
                DocumentPosition lineStart = new DocumentPosition(textRange.StartPosition);
                DocumentPosition lineEnd = new DocumentPosition(lineStart);
                lineEnd.MoveToCurrentLineEnd();
                while (lineEnd < textRange.EndPosition)
                {
                    this.FlushBoxes(lineStart, lineEnd);
                    lineStart.MoveToCurrentLineEnd();
                    lineStart.MoveToNext();
                    lineEnd.MoveToNext();
                    lineEnd.MoveToCurrentLineEnd();
                }
                this.FlushBoxes(lineStart, textRange.EndPosition);

                searchStart.MoveToPosition(textRange.EndPosition);
                if (searchStart >= searchEnd)
                {
                    break;
                }
                textRange = textSearch.Find(word, searchStart, searchEnd);                
            }
        }
        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);
        }
예제 #8
0
        private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            editor.Document.Selection.Clear();

            var search = new DocumentTextSearch(editor.Document);
            var lastPosition = new DocumentPosition(editor.Document);

            bool endOfDocument = false;
            bool theFirstFinding = true;

            while (!endOfDocument)
            {
                TextRange range;

                range = search.Find("#break#", theFirstFinding ? new DocumentPosition(editor.Document) : lastPosition);

                if (range != null)
                {
                    theFirstFinding = false;
                    lastPosition = range.EndPosition;

                    editor.Document.Selection.AddSelectionStart(range.StartPosition);
                    editor.Document.Selection.AddSelectionEnd(range.EndPosition);

                    var documentEditor = new RadDocumentEditor(editor.Document);
                    documentEditor.InsertLineBreak();
                }
                else
                {
                    endOfDocument = true;
                }
            }
        }
예제 #9
0
        public void FindNext(DocumentPosition fromPosition)
        {
            this.tbFindText.Focus();
            this.tbFindText.SelectAll();

            if (this.selectReplaceStateService.IsLastFoundSuccessful)
            {
                this.selectReplaceStateService.UnsubscribeFromDocumentEvents();
                this.selectReplaceStateService.SubscribeForDocumentEvents();
            }

            if (this.initialFindPosition == null)
            {
                this.initialFindPosition = new DocumentPosition(fromPosition);
            }

            DocumentTextSearch textSearch = new DocumentTextSearch(this.Document);
            TextRange          find       = textSearch.Find(this.GetSearchText(), fromPosition);

            if (find != null)
            {
                if (find.StartPosition >= this.initialFindPosition && !this.passedThroughEnd ||
                    find.StartPosition < this.initialFindPosition && this.passedThroughEnd)
                {
                    this.selectReplaceStateService.SelectFoundRange(find);
                    this.richTextBox.ActiveEditorPresenter.UpdateScrollOffsetFromDocumentPosition(find.StartPosition);
                    this.RepositionDialog(find.StartPosition);
                }
                else
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        RadWindow.Alert(new DialogParameters()
                        {
                            Header  = this.Header,
                            Content = LocalizationManager.GetString("Documents_FindReplaceDialog_FinishedSearching")
                        });
                    }));
                    this.ResetFindDialog();
                    this.passedThroughEnd = false;
                    this.Document.Selection.Clear();
                }
            }
            else
            {
                if (!passedThroughEnd)
                {
                    passedThroughEnd = true;
                    this.FindNext(new DocumentPosition(this.Document));
                }
                else
                {
                    string content;
                    if (this.selectReplaceStateService.IsLastFoundSuccessful)
                    {
                        content = LocalizationManager.GetString("Documents_FindReplaceDialog_FinishedSearching");
                    }
                    else
                    {
                        content = string.Format(LocalizationManager.GetString("Documents_FindReplaceDialog_SearchedTextNotFound"), this.tbFindText.Text);
                    }
                    Dispatcher.BeginInvoke(new Action(() => RadWindow.Alert(new DialogParameters()
                    {
                        Header  = this.Header,
                        Content = content
                    })));
                    this.Document.Selection.Clear();
                    this.ResetFindDialog();
                }
            }
        }
예제 #10
0
        public void FindNext(DocumentPosition fromPosition)
        {
            this.tbFindText.Focus();
            this.tbFindText.SelectAll();

            if (this.selectReplaceStateService.IsLastFoundSuccessful)
            {
                this.selectReplaceStateService.UnsubscribeFromDocumentEvents();
                this.selectReplaceStateService.SubscribeForDocumentEvents();
            }

            if (this.initialFindPosition == null)
            {
                this.initialFindPosition = new DocumentPosition(fromPosition);
            }

            DocumentTextSearch textSearch = new DocumentTextSearch(this.Document);
            TextRange find = textSearch.Find(this.GetSearchText(), fromPosition);

            if (find != null)
            {
                if (find.StartPosition >= this.initialFindPosition && !this.passedThroughEnd
                    || find.StartPosition < this.initialFindPosition && this.passedThroughEnd)
                {
                    this.selectReplaceStateService.SelectFoundRange(find);
                    this.richTextBox.ActiveEditorPresenter.UpdateScrollOffsetFromDocumentPosition(find.StartPosition);
                    this.RepositionDialog(find.StartPosition);
                }
                else
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        RadWindow.Alert(new DialogParameters()
                        {
                            Header = this.Header,
                            Content = LocalizationManager.GetString("Documents_FindReplaceDialog_FinishedSearching")
                        });
                    }));
                    this.ResetFindDialog();
                    this.passedThroughEnd = false;
                    this.Document.Selection.Clear();
                }
            }
            else
            {
                if (!passedThroughEnd)
                {
                    passedThroughEnd = true;
                    this.FindNext(new DocumentPosition(this.Document));
                }
                else
                {
                    string content;
                    if (this.selectReplaceStateService.IsLastFoundSuccessful)
                    {
                        content = LocalizationManager.GetString("Documents_FindReplaceDialog_FinishedSearching");
                    }
                    else
                    {
                        content = string.Format(LocalizationManager.GetString("Documents_FindReplaceDialog_SearchedTextNotFound"), this.tbFindText.Text);
                    }
                    Dispatcher.BeginInvoke(new Action(() => RadWindow.Alert(new DialogParameters()
                    {
                        Header = this.Header,
                        Content = content
                    })));
                    this.Document.Selection.Clear();
                    this.ResetFindDialog();
                }
            }
        }
예제 #11
0
        private void btnReplaceAll_Click(object sender, RoutedEventArgs e)
        {
            int foundCount = 0;

            this.richTextBox.SuspendUpdateLayout();
            this.Document.BeginUpdate();
            try
            {
                using (DocumentPosition startFindPosition = new DocumentPosition(this.Document.DocumentLayoutBox, true))
                {
                    var found = true;

                    while (found)
                    {
                        DocumentTextSearch textSearch = new DocumentTextSearch(this.Document);
                        TextRange find = textSearch.Find(this.GetSearchText(), startFindPosition);

                        found = find != null;

                        if (found)
                        {
                            startFindPosition.MoveToPosition(find.EndPosition);
                            startFindPosition.AnchorToNextFormattingSymbol();

                            //This is needed to update the current style for editing
                            this.Document.CaretPosition.MoveToPosition(find.StartPosition);
                            find.SetSelection(this.Document);
                            if (this.replaceCallback(this.tbReplaceText.Text))
                            {
                                foundCount++;
                            }
                            startFindPosition.RemoveAnchorFromNextFormattingSymbol();
                        }
                    }
                }
            }
            finally
            {
                this.Document.EndUpdate();
                this.richTextBox.ResumeUpdateLayout();
            }

            RadWindow.Alert(new DialogParameters()
            {
                Header = this.Header,
                Content = string.Format(LocalizationManager.GetString("Documents_FindReplaceDialog_MadeReplacements"), foundCount)
            });
        }