Esempio n. 1
0
        public void Find(string _find)
        {
            TextRange documentRange = new TextRange(this.Document.ContentStart, this.Document.ContentEnd);
            documentRange.ClearAllProperties();
            _trs.Clear();
            TextPointer navigator = this.Document.ContentStart;
            while (navigator.CompareTo(this.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    Run r = ((Run)navigator.Parent);
                    string text = r.Text;

                    Match m = Regex.Match(text, _find);
                    if (m.Success)
                    {
                        TextPointer tp = r.ElementStart;
                        TextRange range = new TextRange(tp.GetPositionAtOffset(m.Index + 1), tp.GetPositionAtOffset(m.Index + m.Length + 1));
                        _trs.Add(range);
                    }
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
            foreach (TextRange range in _trs)
                range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
            FindNext();

        }
        private void Update()
        {
            var control = this.LogBox;
            if (control == null) return;

            var currentStart = control.Selection.Start;
            var currentEnd = control.Selection.End;
            control.SelectAll();
            var allRange = new TextRange(control.Selection.Start,control.Selection.End);
            var allText = control.Selection.Text;
            allRange.ClearAllProperties();

            foreach(var regexPair in ViewModel.RegexTextFormats)
            {
                var regex = new Regex(regexPair.Key);
                var resultList = regex.Matches(allText);

                foreach(Match result in resultList)
                {
                    var matchStartPoint = allRange.Start.GetPositionAtOffset(result.Index);
                    var matchEndPoint = matchStartPoint.GetPositionAtOffset(result.Length);
                    control.Selection.Select(matchStartPoint, matchEndPoint);
                    foreach (var props in regexPair.Value)
                    {
                        control.Selection.ApplyPropertyValue(props.textElementProperty, props.value);
                    }
                }
            }

            control.Selection.Select(currentStart, currentEnd);
        }
        public void SetText(string text)
        {
            TextRange range = new TextRange(sourceCodeRichTextBox.Document.ContentStart, sourceCodeRichTextBox.Document.ContentEnd);
            range.Text = text;

            if (sourceCodeRichTextBox.Document == null)
                return;

            TextRange documentRange = new TextRange(sourceCodeRichTextBox.Document.ContentStart, sourceCodeRichTextBox.Document.ContentEnd);
            documentRange.ClearAllProperties();

            TextPointer navigator = sourceCodeRichTextBox.Document.ContentStart;
            while (navigator.CompareTo(sourceCodeRichTextBox.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    SyntaxHighlight v = new SyntaxHighlight();

                    v.ApplyColors((Run)navigator.Parent);

                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
        }
        private void ApplyFormatClick(object sender, RoutedEventArgs e)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            FlowDocument doc = Rtb.Document;
            TextRange range = new TextRange(doc.ContentStart, doc.ContentEnd);
            range.ClearAllProperties();
            int i = 0;
            while (true)
            {
                TextPointer p1 = range.Start.GetPositionAtOffset(i);
                i++;
                TextPointer p2 = range.Start.GetPositionAtOffset(i);
                if (p2 == null)
                    break;
                TextRange tempRange = new TextRange(p1, p2);
                if (tempRange != null)
                {

                    tempRange.ApplyPropertyValue(TextElement.ForegroundProperty, _blueBrush);
                    tempRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                }
                i++;

            }
            Time.Text = "Formatting took: " + stopwatch.ElapsedMilliseconds + " ms, number of characters: " + range.Text.Length;
        }
Esempio n. 5
0
 internal static bool PasteContentData(InputBox inputBox, IDataObject iDataObject)
 {
     TextData data = TryGetText(iDataObject);
     if (!data.ContainsData)
     {
         if (iDataObject.GetDataPresent(DataFormats.Bitmap, true))
         {
             inputBox.Paste(iDataObject);
             return true;
         }
         return false;
     }
     inputBox.TempFlowDocument.Blocks.Clear();
     TextRange range = null;
     if (data.Format == BamaDataFormat)
     {
         object obj2 = XamlReader.Parse(data.Data);
         if (obj2 is Block)
         {
             inputBox.TempFlowDocument.Blocks.Add(obj2 as Block);
         }
         else if (obj2 is Inline)
         {
             Span span = new Span(inputBox.TempFlowDocument.ContentStart, inputBox.TempFlowDocument.ContentEnd)
             {
                 Inlines = { obj2 as Span }
             };
         }
         range = new TextRange(inputBox.TempFlowDocument.ContentStart, inputBox.TempFlowDocument.ContentEnd);
         range.ClearAllProperties();
         inputBox.Selection.Text = "";
         Span newspan = new Span(inputBox.Selection.Start, inputBox.Selection.End);
         ReplaceControls.AddBlocksToSpan(inputBox.TempFlowDocument, newspan);
         inputBox.CaretPosition = newspan.ElementEnd.GetInsertionPosition(LogicalDirection.Forward);
     }
     else
     {
         range = new TextRange(inputBox.TempFlowDocument.ContentStart, inputBox.TempFlowDocument.ContentEnd);
         using (MemoryStream stream = new MemoryStream())
         {
             using (StreamWriter writer = new StreamWriter(stream))
             {
                 writer.Write(data.Data);
                 writer.Flush();
                 stream.Position = 0L;
                 range.Load(stream, data.Format);
             }
         }
         range.ClearAllProperties();
         inputBox.Selection.Text = "";
         Span span3 = new Span(inputBox.Selection.Start, inputBox.Selection.End);
         ReplaceControls.AddBlocksToSpan(inputBox.TempFlowDocument, span3);
         inputBox.CaretPosition = span3.ElementEnd.GetInsertionPosition(LogicalDirection.Forward);
     }
     inputBox.TempFlowDocument.Blocks.Clear();
     return true;
 }
        public void FormatCurrentLine()
        {
            /*These checks are necessary to invoke FormatCurrentLinefunction as it uses Caret related properties
            and is included in TextChange event handler. So, if you need to execute this function elsewhere, include these checks.
            
            if (rtb1.Document == null)
                return;
            if (rtb1.CaretPosition.Paragraph == null)
                return;
            if (rtb1.CaretPosition.Paragraph.Inlines.FirstInline == null)
                return;
            */

            int i;
            string s;
            Run r;

            TextRange paragraphRange = new TextRange(rtb1.CaretPosition.Paragraph.ContentStart, rtb1.CaretPosition.Paragraph.ContentEnd);
            paragraphRange.ClearAllProperties();

            r = (Run)(rtb1.CaretPosition.Paragraph.Inlines.FirstInline);
            if (r.Text == null)
                return;
            s = r.Text;

            registerMatches = register.Matches(s);
            digitMatches = digit.Matches(s);
            keywordMatches = keyword.Matches(s);
            commentMatches = comment.Matches(s);

            for (i = 0; i <= registerMatches.Count - 1; i++)
            {
                TextRange temp = new TextRange(r.ContentStart.GetPositionAtOffset(registerMatches[i].Index, LogicalDirection.Forward), r.ContentStart.GetPositionAtOffset(registerMatches[i].Index + registerMatches[i].Length, LogicalDirection.Forward));
                registerAllMatches.Add(temp);
            }

            for (i = 0; i <= digitMatches.Count - 1; i++)
            {
                TextRange temp = new TextRange(r.ContentStart.GetPositionAtOffset(digitMatches[i].Index, LogicalDirection.Forward), r.ContentStart.GetPositionAtOffset(digitMatches[i].Index + digitMatches[i].Length, LogicalDirection.Forward));
                digitAllMatches.Add(temp);
            }

            for (i = 0; i <= keywordMatches.Count - 1; i++)
            {
                TextRange temp = new TextRange(r.ContentStart.GetPositionAtOffset(keywordMatches[i].Index, LogicalDirection.Forward), r.ContentStart.GetPositionAtOffset(keywordMatches[i].Index + keywordMatches[i].Length, LogicalDirection.Forward));
                keywordAllMatches.Add(temp);
            }

            for (i = 0; i <= commentMatches.Count - 1; i++)
            {
                TextRange temp = new TextRange(r.ContentStart.GetPositionAtOffset(commentMatches[i].Groups["comment"].Index, LogicalDirection.Forward), r.ContentStart.GetPositionAtOffset(commentMatches[i].Groups["comment"].Index + commentMatches[i].Groups["comment"].Length, LogicalDirection.Forward));
                commentAllMatches.Add(temp);
            }

            Format();
        }
 public void FormatOnFly()
 {
     TextPointer pos = _editor.CaretPosition;
     TextPointer start = pos.GetLineStartPosition(0);
     TextPointer end = pos.GetLineStartPosition(1);
     end = (end ?? pos.DocumentEnd).GetInsertionPosition(LogicalDirection.Backward);
     TextRange range = new TextRange(start, end);
     range.ClearAllProperties();
     CheckWords(range.Text, start);
 }
Esempio n. 8
0
 private void ComboBoxFontColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     if (!richMain.Selection.IsEmpty)
     {
         TextRange range = new TextRange(richMain.Selection.Start, richMain.Selection.End);
         if (ComboBoxFontColor.SelectedValue.ToString() != "None")
         {
             Brush b = (Brush)new BrushConverter().ConvertFromString(ComboBoxFontColor.SelectedValue.ToString());
             range.ApplyPropertyValue(RichTextBox.ForegroundProperty, b);
         }
         else
         {
             range.ClearAllProperties();
         }
     }
 }
        private void HeadersTextChanged(object sender, TextChangedEventArgs e)
        {
            if (Headers.Document == null) return;
            httpInterceptorViewModel.SetRequestHeaders(new TextRange(Headers.Document.ContentStart, Headers.Document.ContentEnd).Text);
            var documentRange = new TextRange(Headers.Document.ContentStart, Headers.Document.ContentEnd);
            documentRange.ClearAllProperties();

            TextPointer navigator = Headers.Document.ContentStart;
            while (navigator.CompareTo(Headers.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    //CheckHeaderWordsInRun((Run)navigator.Parent);
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
            //FormatHeaders();
        }
Esempio n. 10
0
 public static void GenerateClipBoardData(DataObjectCopyingEventArgs e, TextSelection selection)
 {
     string str;
     using (MemoryStream stream = new MemoryStream())
     {
         TextRange range = new TextRange(selection.Start, selection.End);
         range.ClearAllProperties();
         range.Save(stream, DataFormats.Xaml, true);
         stream.Flush();
         stream.Position = 0L;
         using (StreamReader reader = new StreamReader(stream))
         {
             str = reader.ReadToEnd();
         }
     }
     if (!string.IsNullOrEmpty(str))
     {
         string str2 = ReplaceControls.ReplaceGUIWithClipboardControl(str, selection.Start, selection.End);
         if (!string.IsNullOrEmpty(str2) && (str2 != str))
         {
             e.DataObject.SetData(BamaDataFormat, str2);
         }
     }
 }
Esempio n. 11
0
        private void ResponseHeadersTextChanged()
        {
            if (HeaderResponse.Document == null) return;
            var documentRange = new TextRange(HeaderResponse.Document.ContentStart, HeaderResponse.Document.ContentEnd);
            documentRange.ClearAllProperties();

            TextPointer navigator = HeaderResponse.Document.ContentStart;
            while (navigator.CompareTo(HeaderResponse.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    CheckHeaderWordsInRun((Run)navigator.Parent);
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
            FormatHeaders();
        }
Esempio n. 12
0
        private void ClearHighlighting(FrameworkElement container)
        {
            var searchFields = container?.ChildrenOfType<TextBlock>();
            if (searchFields == null || searchFields.Count == 0)
            {
                return;
            }

            foreach (var field in searchFields)
            {
                var contentRange = new TextRange(field.ContentStart, field.ContentEnd);
                contentRange.ClearAllProperties();
            }
        }
Esempio n. 13
0
        private void ToggleContainerVisibility(FrameworkElement container, TokenInfo tokenInfo)
        {
            if (container == null || tokenInfo == null)
            {
                return;
            }

            var searchFields = container.ChildrenOfType<TextBlock>();
            if (searchFields == null || searchFields.Count <= 0)
            {
                tokenInfo.Visibility = Visibility.Collapsed;
                return;
            }

            var isFilterContained = false;
            var highlightingForeground = HighlightingForeground;
            var highlightingBackground = this.HighlightingBackground;

            foreach (var field in searchFields)
            {
                if (TokenEditExtensions.GetIsExcludedFromSearch(field))
                {
                    continue;
                }

                // clear any previously applied formatting
                var contentRange = new TextRange(field.ContentStart, field.ContentEnd);
                contentRange.ClearAllProperties();

                var matches = Regex.Matches(field.Text, Filter, RegexOptions.IgnoreCase);
                if (matches.Count == 0)
                {
                    continue;
                }

                isFilterContained = true;
                foreach (Match match in matches)
                {
                    var textRange = field.GetTextRangeFromCharOffset(match.Index, match.Length);
                    textRange.ApplyPropertyValue(TextElement.BackgroundProperty, highlightingBackground);

                    if (highlightingForeground != null)
                    {
                        textRange.ApplyPropertyValue(TextElement.ForegroundProperty, highlightingForeground);
                    }
                }
            }

            tokenInfo.Visibility = isFilterContained ? Visibility.Visible : Visibility.Collapsed;
        }
Esempio n. 14
0
        public static void FormatTextRange(TextRange range, Row row)
        {
            range.ClearAllProperties();

            if (!row.HasText)
            {
                return;
            }

            if (row.IsCompleted)
            {
                range.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Gray);
                range.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
                return;
            }

            TextPointer start = range.Start;

            if (row.HasPriority)
            {
                TextPointer priorityStart = GetTextPointAtOffset(start, row.PriorityRange.Item1);
                TextPointer priorityEnd = GetTextPointAtOffset(start, row.PriorityRange.Item2);
                TextRange priorityRange = new TextRange(priorityStart, priorityEnd);

                priorityRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                priorityRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
            }

            if (row.HasCreationDate)
            {
                TextPointer dateStart = GetTextPointAtOffset(start, row.CreationDateRange.Item1);
                TextPointer dateEnd = GetTextPointAtOffset(start, row.CreationDateRange.Item2);
                TextRange dateRange = new TextRange(dateStart, dateEnd);

                dateRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                dateRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.DarkGreen);
            }

            if (row.HasContexts)
            {
                foreach (Tuple<int, int> contextRange in row.ContextRanges)
                {
                    TextPointer contextStart = GetTextPointAtOffset(start, contextRange.Item1);
                    TextPointer contextEnd = GetTextPointAtOffset(start, contextRange.Item2);
                    TextRange contextTextRange = new TextRange(contextStart, contextEnd);

                    contextTextRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                    contextTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.DarkCyan);
                }
            }

            if (row.HasProjects)
            {
                foreach (Tuple<int, int> projectRange in row.ProjectRanges)
                {
                    TextPointer projectStart = GetTextPointAtOffset(start, projectRange.Item1);
                    TextPointer projectEnd = GetTextPointAtOffset(start, projectRange.Item2);
                    TextRange projectTextRange = new TextRange(projectStart, projectEnd);

                    projectTextRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                    projectTextRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.SaddleBrown);
                }
            }
        }
 private void Highlight()
 {
     InputEditor.Background = Brushes.Black;
     var existingRange = new TextRange(InputEditor.Document.ContentStart, InputEditor.Document.ContentEnd);
     existingRange.ClearAllProperties();
     var navigator = InputEditor.Document.ContentStart;
     while (navigator.CompareTo(InputEditor.Document.ContentEnd) < 0)
     {
         var context = navigator.GetPointerContext(LogicalDirection.Backward);
         if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
         {
             CheckWordsInRun((Run) navigator.Parent);
         }
         navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
     }
     Format();
 }
        public virtual void FormatText()
        {
            // Clear all formatting properties in the document.
            // This is necessary since a paste command could have inserted text inside or at boundaries of a keyword from dictionary.
            TextRange documentRange = new TextRange(this.Document.ContentStart, this.Document.ContentEnd);
            documentRange.ClearAllProperties();

            // Reparse the document to scan for matching words.
            TextPointer navigator = this.Document.ContentStart;
            while (navigator.CompareTo(this.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    this.AddMatchingWordsInRun((Run)navigator.Parent);
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }

            // Format words found.
            this.FormatWords();
        }
Esempio n. 17
0
 private static void Highlight(FlowDocument document, int start, int length)
 {
     var range = new TextRange(document.ContentStart, document.ContentEnd);
     range.ClearAllProperties();
     range = new TextRange(document.ContentStart.GetPositionAtOffset(start + 2), document.ContentStart.GetPositionAtOffset(start + length + 2));
     range.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.LightGray);
 }
Esempio n. 18
0
        public override void Undo(RichTextBox edit)
        {
            FlowDocument document = edit.Document;

            __OffsetCursorPositionAfter = document.ContentStart.GetOffsetToPosition(edit.CaretPosition);

            TextPointer start = UndoHelpers.SafePositionAtOffset(document, document.ContentStart, __OffsetStart);
            TextPointer end = UndoHelpers.SafePositionAtOffset(document, document.ContentEnd, __OffsetEnd);
            TextRange range = new TextRange(start, end);
            __DataStream = new MemoryStream();
            range.Save(__DataStream, DataFormats.Xaml);
            range.ClearAllProperties();
            range.Text = "";

            edit.CaretPosition = UndoHelpers.SafePositionAtOffset(document, document.ContentStart, __OffsetCursorPositionBefore);
        }
 private string GetDocumentText(FlowDocument document)
 {
     StringBuilder strBuilder = new StringBuilder();
     foreach(var par in GetParagraphs(document.Blocks).ToList())
     {
         var completeTextRange = new TextRange(par.ContentStart, par.ContentEnd);
         completeTextRange.ClearAllProperties();
         strBuilder.AppendLine(completeTextRange.Text);
     }
     return strBuilder.ToString();
 }
Esempio n. 20
0
        private void UpdateColors()
        {
            if (rtbChat.Document == null)
                return;

            TextRange documentRange = new TextRange(rtbChat.Document.ContentStart, rtbChat.Document.ContentEnd);
            documentRange.ClearAllProperties();

            TextPointer navigator = rtbChat.Document.ContentStart;
            while (navigator.CompareTo(rtbChat.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    //navigator.Parent.
                    CheckWordsInRun((Run)navigator.Parent);

                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);

            }

            for (int i = 0; i < m_tags.Count; i++)
            {
                TextRange range = new TextRange(m_tags[i].StartPosition, m_tags[i].EndPosition);
                switch (m_tags[i].Type)
                {
                    case Networking.LobbyClient.LobbyChatTypes.Global:
                        if (range.Text.Equals("[" + Program.LClient.strUserName + "]:"))
                        {
                            range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
                            range.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                        }
                        else
                        {
                            range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Black));
                            range.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                        }
                    break;
                    case Networking.LobbyClient.LobbyChatTypes.Whisper:
                            range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Orange));
                            range.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
                    break;
                    case Networking.LobbyClient.LobbyChatTypes.System:
                            range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Gray));
                            range.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                    break;
                    case Networking.LobbyClient.LobbyChatTypes.Error:
                            range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));
                            range.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                    break;
                }
            }
            m_tags.Clear();
            //SendMessage(rtbChat.Handle, WM_VSCROLL, SB_BOTTOM, 0);
            rtbChat.ScrollToEnd();
        }
 async Task UpdateAllParagraphs(IEnumerable<Paragraph> paragraphs)
 {
     var materialParagraphs = paragraphs.ToList();
     if (materialParagraphs.Count == 0)
         return;
     var completeTextRange = new TextRange(materialParagraphs.First().ContentStart,
                                           materialParagraphs.Last().ContentEnd);
     completeTextRange.ClearAllProperties();
     await UpdateInlines(materialParagraphs.SelectMany(par => par.Inlines));
 }
 async Task UpdateParagraph(Paragraph par)
 {
     var completeTextRange = new TextRange(par.ContentStart, par.ContentEnd);
     completeTextRange.ClearAllProperties();
     await UpdateInlines(par.Inlines);
 }
Esempio n. 23
0
        private void TextInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextInput.TextChanged -= TextInput_TextChanged;
            string tokens = "(auto|double|int|struct|break|else|long|switch|case|"+
                              "enum|register|typedef|char|extern|return|union|const|"+
                             "float|short|unsigned|continue|for|signed|void |default|"+
                              "goto|sizeof|volatile|do|if|static|while|using)";
            Regex rex = new Regex(tokens);

            if (TextInput.Document == null)
                return;

            TextRange documentRange = new TextRange(TextInput.Document.ContentStart, TextInput.Document.ContentEnd);
            documentRange.ClearAllProperties();

            //Console.WriteLine(documentRange.Text);

            TextPointer position = TextInput.Document.ContentStart;
            while (position != null)
            {
                if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                    break;
                }
                position = position.GetNextContextPosition(LogicalDirection.Forward);
            }

            MatchCollection mc = rex.Matches(documentRange.Text);
            foreach (Match m in mc)
            {
                //Console.Write("{0}({1}-{2}), ", m.Value, m.Index, m.Index + m.Length);
                TextPointer t1 = GetTextPointAt(position, m.Index);
                TextRange tr = new TextRange(t1, GetTextPointAt(t1, m.Length));
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));
            }
            TextInput.TextChanged += TextInput_TextChanged;
            return;
            /*
            int line = 0;
            int b = 0;
            while (position != null)
            {
                if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                    string textRun = position.GetTextInRun(LogicalDirection.Forward);
                    MatchCollection mc = rex.Matches(textRun);
                    foreach (Match m in mc)
                    {
                        //Console.Write("{0}({1}-{2}), ", m.Value, m.Index, m.Index+m.Length);
                        TextPointer t1 = GetTextPointAt(position, m.Index);
                        TextRange tr = new TextRange(t1, GetTextPointAt(t1, m.Length));

                        if ((b % 3) == 0)
                        {
                            tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));
                        }
                        else if ((b % 3) == 1)
                        {
                            tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.LightBlue));
                        }
                        else
                        {
                            tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Yellow));
                        }
                        ++b;

                    }
                    // Find the starting index of any substring that matches "word".
                    /*int indexInRun = textRun.IndexOf("void");
                    if (indexInRun >= 0)
                    {
                        Console.WriteLine("Found: {0}, text:\"{1}\" ", indexInRun, textRun);
                        //position = position.GetPositionAtOffset(indexInRun);
                        //break;
                    }
                    position = position.GetLineStartPosition(1);
                    continue;
                }
                position = position.GetNextContextPosition(LogicalDirection.Forward);
            }
            */

            /*if (tp1 == null)
                return;
            while (tp1.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
            {
                tp1 = tp1.GetNextContextPosition(LogicalDirection.Forward);
            }
            //Console.WriteLine("");

            foreach (Match m in mc)
            {
                Console.WriteLine("Match: {0} [{1},{2}]", m.Value, m.Index, m.Length);
                //int startIdx = m.Index;
                //int stopIdx = m.Length;
                //documentRange.Select(TextInput.Document.ContentStart, TextInput.Document.ContentStart.GetPositionAtOffset(m.Index + m.Length, LogicalDirection.Forward));
                TextPointer tp2 = tp1.GetPositionAtOffset(m.Index, LogicalDirection.Forward);

                Console.WriteLine(tp2.GetOffsetToPosition(tp2.GetPositionAtOffset(m.Length, LogicalDirection.Forward)));

                TextRange tr = new TextRange(tp2, tp2.GetPositionAtOffset(m.Length, LogicalDirection.Forward));

                tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
            }*/

            TextInput.TextChanged += TextInput_TextChanged;
            //TextInput.TextChanged += TextInput_TextChanged;

            /*documentRange.ClearAllProperties();

            Console.WriteLine(documentRange.Text);

            TextPointer navigator = TextInput.Document.ContentStart;

            while (navigator.CompareTo(TextInput.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    Console.WriteLine(navigator.GetTextRunLength(LogicalDirection.Forward));
                }

                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }

            Format();
             * */
        }
Esempio n. 24
0
 private void On_ClearSearchButtonClick(object sender, RoutedEventArgs e)
 {
     FindLogTextBox.Text = "";
     RichTextBox logView = (RichTextBox)peerList.ElementAtOrDefault(tabControl.SelectedIndex).logTab.Content;
     TextRange completeContent = new TextRange(logView.Document.ContentStart, logView.Document.ContentEnd);
     completeContent.ClearAllProperties();
 }
 private static string GetInputText(RichTextBox rtb)
 {
     TextRange documentRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
     documentRange.ClearAllProperties();
     return documentRange.Text.Substring(0, documentRange.Text.Length - 2);
 }
Esempio n. 26
0
        private void TextInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textInput.Document == null) return;

            TextRange documentRange = new TextRange(textInput.Document.ContentStart, textInput.Document.ContentEnd);
            documentRange.ClearAllProperties();

            TextPointer navigator = textInput.Document.ContentStart;
            while (navigator.CompareTo(textInput.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    try
                    {
                        CheckWordsInRun((Run)navigator.Parent);
                    }
                    catch { }
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
            Format();
        }
Esempio n. 27
0
        public override void Undo(RichTextBox edit)
        {
            FlowDocument document = edit.Document;
            TextRange whole = new TextRange(document.ContentStart, document.ContentEnd);
            TextRange range = new TextRange(
                                    UndoHelpers.SafePositionAtOffset(document, document.ContentStart, __OffsetStart),
                                    UndoHelpers.SafePositionAtOffset(document, document.ContentStart, __OffsetEnd));

            __DataStream = new MemoryStream();
            range.Save(__DataStream, DataFormats.Xaml);

            range.ClearAllProperties();
            range.Text = "";
            UpdateOffsets(edit, range);

            TextPointer caretPosition = UndoHelpers.SafePositionAtOffset(document, document.ContentStart, __OffsetStart);
            edit.CaretPosition = caretPosition;
        }
        public void FormatWholeDocument()
        {
            int i;
            string s;
            TextPointer navigator;
            TextPointerContext context;
            Run r;

            TextRange documentRange = new TextRange(rtb1.Document.ContentStart, rtb1.Document.ContentEnd);      //Make a textRange document point to the entire document
            documentRange.ClearAllProperties();                                                                 //Clears all formatting, so all all inlines within a paragraph are converted to a single inline but all paragraphs are kept

            navigator = rtb1.Document.ContentStart;
            while (navigator.CompareTo(rtb1.Document.ContentEnd) < 0)
            {
                context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    r = (Run)navigator.Parent;
                    s = r.Text;

                    registerMatches = register.Matches(s);
                    digitMatches = digit.Matches(s);
                    keywordMatches = keyword.Matches(s);
                    commentMatches = comment.Matches(s);

                    for (i = 0; i <= registerMatches.Count - 1; i++)
                    {
                        TextRange temp = new TextRange(r.ContentStart.GetPositionAtOffset(registerMatches[i].Index, LogicalDirection.Forward), r.ContentStart.GetPositionAtOffset(registerMatches[i].Index + registerMatches[i].Length, LogicalDirection.Forward));
                        registerAllMatches.Add(temp);
                    }

                    for (i = 0; i <= digitMatches.Count - 1; i++)
                    {
                        TextRange temp = new TextRange(r.ContentStart.GetPositionAtOffset(digitMatches[i].Index, LogicalDirection.Forward), r.ContentStart.GetPositionAtOffset(digitMatches[i].Index + digitMatches[i].Length, LogicalDirection.Forward));
                        digitAllMatches.Add(temp);
                    }

                    for (i = 0; i <= keywordMatches.Count - 1; i++)
                    {
                        TextRange temp = new TextRange(r.ContentStart.GetPositionAtOffset(keywordMatches[i].Index, LogicalDirection.Forward), r.ContentStart.GetPositionAtOffset(keywordMatches[i].Index + keywordMatches[i].Length, LogicalDirection.Forward));
                        keywordAllMatches.Add(temp);
                    }

                    for (i = 0; i <= commentMatches.Count - 1; i++)
                    {
                        TextRange temp = new TextRange(r.ContentStart.GetPositionAtOffset(commentMatches[i].Groups["comment"].Index, LogicalDirection.Forward), r.ContentStart.GetPositionAtOffset(commentMatches[i].Groups["comment"].Index + commentMatches[i].Groups["comment"].Length, LogicalDirection.Forward));
                        commentAllMatches.Add(temp);
                    }
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }

            Format();
        }
Esempio n. 29
0
 private void InputEdit_TextChanged(object sender, TextChangedEventArgs e)
 {
     if (this.isClearingProperties)
     return;
       TextRange textRange = new TextRange(this.InputEdit.Document.ContentStart, this.InputEdit.Document.ContentEnd);
       this.isClearingProperties = true;
       textRange.ClearAllProperties();
       this.isClearingProperties = false;
 }
Esempio n. 30
0
        private void ExecutedHandler(object sender, ExecutedRoutedEventArgs e)
        {
            // При любой команде мержинг отменяется, мало ли что там команда навыполняла
            PushUndoAction(null, false);

            RichTextBox edit = (RichTextBox)sender;

            if (e.Command == EditingCommands.DeletePreviousWord ||
                e.Command == EditingCommands.DeleteNextWord)
            {
                if (!edit.Selection.IsEmpty)
                {
                    RemoveSelection(edit);
                }
                else
                {
                    int offsetFromEnd = edit.Document.ContentEnd.GetOffsetToPosition(edit.CaretPosition);
                    int offsetFromStart = edit.Document.ContentStart.GetOffsetToPosition(edit.CaretPosition);
                    TextPointer pointer = edit.CaretPosition;

                    if (e.Command == EditingCommands.DeletePreviousWord)
                    {
                        int resOffset = FindNextWhitespaceBackward(edit);

                        if (resOffset != -1)
                        {
                            TextRange range = new TextRange(
                                edit.Document.ContentStart.GetPositionAtOffset(resOffset),
                                edit.Document.ContentEnd.GetPositionAtOffset(offsetFromEnd));

                            PushUndoAction(new UndoBlockRemove(edit, range), true);
                            range.Text = "";
                        }
                    }
                    else if (e.Command == EditingCommands.DeleteNextWord)
                    {
                        int resOffset = FindNextWhitespaceForward(edit);

                        if (resOffset != -1)
                        {
                            TextRange range = new TextRange(
                                edit.Document.ContentStart.GetPositionAtOffset(resOffset),
                                edit.Document.ContentStart.GetPositionAtOffset(offsetFromStart));

                            PushUndoAction(new UndoBlockRemove(edit, range), true);
                            range.Text = "";
                        }
                    }
                }

                e.Handled = true;
                LinksCheck(CaretPosition);
            }

            if (e.Command == ApplicationCommands.Cut)
            {
                if (!edit.Selection.IsEmpty)
                {
                    edit.Copy();
                    RemoveSelection(this);
                    LinksCheck(CaretPosition);
                }

                e.Handled = true;
                return;
            }

            if (e.Command == ApplicationCommands.Paste)
            {
                if (!Clipboard.ContainsData(DataFormats.Rtf) && !Clipboard.ContainsData(DataFormats.Text))
                {
                    e.Handled = true;
                    return;
                }

                var undoGroup = new UndoGroup();

                // Удалить старое содержимое
                if (!edit.Selection.IsEmpty)
                {
                    FormatUndo undoFormat = new FormatUndo(edit.Document, edit.Selection, edit);
                    undoGroup.Add(undoFormat);
                    edit.Selection.Text = "";
                    undoFormat.UpdateSelectionOffsets(edit, edit.Selection);
                }

                int offsetStart = edit.Document.ContentStart.GetOffsetToPosition(edit.Selection.Start);
                int offsetEnd = edit.Document.ContentEnd.GetOffsetToPosition(edit.Selection.End);
                var undoPaste = new UndoPaste(edit, offsetStart, offsetEnd);

                bool wasError = false;
                if (Clipboard.ContainsData(DataFormats.Rtf))
                {
                    try
                    {
                        var rtfStream = MemoryStreamFromClipboard();
                        edit.Selection.Load(rtfStream, DataFormats.Rtf);
                    }
                    catch
                    {
                        wasError = true;
                    }
                }
                else if (Clipboard.ContainsData(DataFormats.Text))
                {
                    edit.Selection.Text = (string)Clipboard.GetData(DataFormats.Text);
                }

                // Если была ошибка добавления текста, то
                if (wasError == false)
                {
                    undoGroup.Add(undoPaste);
                    PushUndoAction(undoGroup, true);
                    edit.CaretPosition = edit.Selection.End;
                }

                // Проверить, все ли линки на месте
                LinksCheck(CaretPosition);

                // Обновить хендлеры у новых ссылок (которые могли прийти с клипбоардом)
                Links_Update();

                e.Handled = true;
                return;
            }

            if (e.Command == EditingCommands.AlignCenter ||
                e.Command == EditingCommands.AlignJustify ||
                e.Command == EditingCommands.AlignLeft ||
                e.Command == EditingCommands.AlignRight ||
                e.Command == EditingCommands.IncreaseIndentation ||
                e.Command == EditingCommands.TabBackward ||
                e.Command == EditingCommands.TabForward ||
                e.Command == EditingCommands.ToggleNumbering ||
                e.Command == EditingCommands.ToggleSubscript ||
                e.Command == EditingCommands.ToggleSuperscript)
            {
                e.Handled = true;
                return;
            }
            //e.Command == EditingCommands.ToggleUnderline

            if (e.Command == EditingCommands.IncreaseFontSize ||
                e.Command == EditingCommands.DecreaseFontSize ||
                e.Command == EditingCommands.ToggleBold ||
                e.Command == EditingCommands.ToggleItalic ||
                e.Command == EditingCommands.ToggleUnderline ||
                e.Command == OutlinerCommands.ToggleCrossed)
            {
                if (!edit.Selection.IsEmpty)
                {
                    var bu = new FormatUndo(edit.Document, edit.Selection, edit);
                    PushUndoAction(bu, true);

                    if (e.Command == EditingCommands.ToggleBold)
                        IsSelectionBold = !IsSelectionBold;
                    else if (e.Command == EditingCommands.ToggleItalic)
                        IsSelectionItalic = !IsSelectionItalic;
                    else if (e.Command == EditingCommands.ToggleUnderline)
                        IsSelectionUnderlined = !IsSelectionUnderlined;
                    else if (e.Command == OutlinerCommands.ToggleCrossed)
                        IsSelectionStrikethrough = !IsSelectionStrikethrough;
                    /*if (e.Command == OutlinerCommands.ToggleCrossed)
                        IsSelectionStrikethrough = !IsSelectionStrikethrough;*/

                    bu.UpdateSelectionOffsets(edit, edit.Selection);
                    e.Handled = true;
                }
                else
                {

                    TextRange paragraphRange;
                    if (edit.CaretPosition.Paragraph != null)
                        paragraphRange = new TextRange(edit.CaretPosition.Paragraph.ContentStart,
                                                             edit.CaretPosition.Paragraph.ContentEnd);
                    else
                        paragraphRange = new TextRange(edit.Document.ContentStart,
                                                             edit.Document.ContentEnd);

                    FontProperties fontProps = new FontProperties(paragraphRange);
                    var bu = new FormatUndo(edit.Document,
                                    paragraphRange,
                                    edit);

                    int caretPositionStart = edit.Document.ContentStart.GetOffsetToPosition(edit.CaretPosition);
                    int caretPositionEnd = edit.Document.ContentEnd.GetOffsetToPosition(edit.CaretPosition);

                    if (e.Command == EditingCommands.ToggleBold)
                        IsSelectionBold = !IsSelectionBold;
                    else if (e.Command == EditingCommands.ToggleItalic)
                        IsSelectionItalic = !IsSelectionItalic;
                    else if (e.Command == EditingCommands.ToggleUnderline)
                        IsSelectionUnderlined = !IsSelectionUnderlined;
                    else if (e.Command == OutlinerCommands.ToggleCrossed)
                        IsSelectionStrikethrough = !IsSelectionStrikethrough;

                    int newCaretPositionStart = edit.Document.ContentStart.GetOffsetToPosition(edit.CaretPosition);
                    int newCaretPositionEnd = edit.Document.ContentEnd.GetOffsetToPosition(edit.CaretPosition);

                    string paragraphRangeText = paragraphRange.Text;
                    if (caretPositionStart != newCaretPositionStart || caretPositionEnd != newCaretPositionEnd ||
                        !fontProps.HasSameStyle(paragraphRange))
                        PushUndoAction(bu, true);

                    e.Handled = true;
                }
                return;
            }

            if (e.Command == EditingCommands.Backspace ||
                e.Command == EditingCommands.Delete)
            {

                if (!edit.Selection.IsEmpty)
                {
                    RemoveSelection(edit);
                    LinksCheck(CaretPosition);
                    e.Handled = true;
                }
                else
                {
                    TextPointer right = edit.Selection.Start;
                    TextPointer left = right.GetNextInsertionPosition(LogicalDirection.Backward);

                    if (e.Command == EditingCommands.Delete)
                    {
                        left = edit.Selection.Start;
                        right = right.GetNextInsertionPosition(LogicalDirection.Forward);
                    }

                    if (right != null && left != null)
                    {

                        TextRange range = new TextRange(right, left);
                        if (range.Text != "")
                        {
                            var bu = new UndoBlockRemove(edit, range);
                            PushUndoAction(bu, true);

                            range.ClearAllProperties();
                            range.Text = "";

                            bu.UpdateOffsets(edit, range);
                        }
                    }

                    LinksCheck(CaretPosition);
                    e.Handled = true;
                }
            }
        }