private void button1_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < Google.API.Translate.Language.TranslatableCollection.Count(); i++) { string richText = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text; richText.Contains(Google.API.Translate.Language.TranslatableCollection.ElementAt(i).ToString()); Translator translator = new Translator(); translator.translate(richText); } }
private bool IsCaretPositionValid() { var text = new TextRange(richTextBox1.CaretPosition, richTextBox1.CaretPosition.DocumentEnd).Text; return !text.Contains(">"); }
private void richTextBox1_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Space) { var command = new TextRange(richTextBox1.CaretPosition.GetLineStartPosition(0), richTextBox1.CaretPosition).Text; command = command.Substring(command.IndexOf(">") + 1).Trim(); ShowOptions(command); return; } else if (e.Key == Key.Tab || e.Key == Key.Down) { if (lstOptions.Visibility == Visibility.Visible) { lstOptions.Focus(); return; } } else { lstOptions.Visibility = Visibility.Collapsed; } if (e.Key == Key.Enter) { var command = new TextRange(richTextBox1.CaretPosition.GetLineStartPosition(0), richTextBox1.CaretPosition.GetLineStartPosition(1) ?? richTextBox1.CaretPosition.DocumentEnd).Text; command = command.Replace("\r", "").Replace("\n", ""); RunCommand(command); e.Handled = true; } else if (e.Key == Key.Up) { GetCommand(--commandIdx); e.Handled = true; } else if (e.Key == Key.Down) { GetCommand(++commandIdx); e.Handled = true; } else if (e.Key == Key.Escape) { ChangePrompt("", new SolidColorBrush(Colors.Black)); } else if (e.Key == Key.Back) { var text = new TextRange(richTextBox1.CaretPosition.GetLineStartPosition(0), richTextBox1.CaretPosition).Text; if (text.EndsWith(">")) e.Handled = true; } else { var text = new TextRange(richTextBox1.CaretPosition, richTextBox1.CaretPosition.DocumentEnd).Text; if (text.Contains(">")) //e.Handled = true; this.richTextBox1.CaretPosition = this.richTextBox1.CaretPosition.DocumentEnd; } }
/// <summary> /// Expands the range to an integral number of enclosing units. If the range is already an /// integral number of the specified units then it remains unchanged. /// </summary> private void ExpandToEnclosingUnit(TextUnit unit, bool expandStart, bool expandEnd) { ITextView textView; switch (unit) { case TextUnit.Character: if (expandStart && !TextPointerBase.IsAtInsertionPosition(_start)) { TextPointerBase.MoveToNextInsertionPosition(_start, LogicalDirection.Backward); } if (expandEnd && !TextPointerBase.IsAtInsertionPosition(_end)) { TextPointerBase.MoveToNextInsertionPosition(_end, LogicalDirection.Forward); } break; case TextUnit.Word: if (expandStart && !IsAtWordBoundary(_start)) { MoveToNextWordBoundary(_start, LogicalDirection.Backward); } if (expandEnd && !IsAtWordBoundary(_end)) { MoveToNextWordBoundary(_end, LogicalDirection.Forward); } break; case TextUnit.Format: // Formatting changes can be introduced by elements. Hence it is fair to // assume that formatting boundaries are defined by non-text context. if (expandStart) { TextPointerContext forwardContext = _start.GetPointerContext(LogicalDirection.Forward); while (true) { TextPointerContext backwardContext = _start.GetPointerContext(LogicalDirection.Backward); if (backwardContext == TextPointerContext.None) break; if (forwardContext == TextPointerContext.Text && backwardContext != TextPointerContext.Text) break; forwardContext = backwardContext; _start.MoveToNextContextPosition(LogicalDirection.Backward); } } if (expandEnd) { TextPointerContext backwardContext = _end.GetPointerContext(LogicalDirection.Backward); while (true) { TextPointerContext forwardContext = _end.GetPointerContext(LogicalDirection.Forward); if (forwardContext == TextPointerContext.None) break; if (forwardContext == TextPointerContext.Text && backwardContext != TextPointerContext.Text) break; backwardContext = forwardContext; _end.MoveToNextContextPosition(LogicalDirection.Forward); } } // Set LogicalDirection to prevent end points from crossing a formatting // boundary when normalized. _start.SetLogicalDirection(LogicalDirection.Forward); _end.SetLogicalDirection(LogicalDirection.Forward); break; case TextUnit.Line: // Positions are snapped to closest line boundaries. But since line information // is based on the layout, positions are not changed, if: // a) they are not currently in the view, or // b) containing line cannot be found. textView = _textAdaptor.GetUpdatedTextView(); if (textView != null && textView.IsValid) { bool snapEndPosition = true; if (expandStart && textView.Contains(_start)) { TextSegment lineRange = textView.GetLineRange(_start); if (!lineRange.IsNull) { // Move start position to the beginning of containing line. if (_start.CompareTo(lineRange.Start) != 0) { _start = lineRange.Start.CreatePointer(); } // If this line contains also end position, move it to the // end of this line. if (lineRange.Contains(_end)) { snapEndPosition = false; if (_end.CompareTo(lineRange.End) != 0) { _end = lineRange.End.CreatePointer(); } } } } if (expandEnd && snapEndPosition && textView.Contains(_end)) { TextSegment lineRange = textView.GetLineRange(_end); if (!lineRange.IsNull) { // Move end position to the end of containing line. if (_end.CompareTo(lineRange.End) != 0) { _end = lineRange.End.CreatePointer(); } } } } break; case TextUnit.Paragraph: // Utilize TextRange logic to determine paragraph boundaries. ITextRange textRange = new TextRange(_start, _end); TextRangeBase.SelectParagraph(textRange, _start); if (expandStart && _start.CompareTo(textRange.Start) != 0) { _start = textRange.Start.CreatePointer(); } if (expandEnd) { if (!textRange.Contains(_end)) { TextRangeBase.SelectParagraph(textRange, _end); } if (_end.CompareTo(textRange.End) != 0) { _end = textRange.End.CreatePointer(); } } break; case TextUnit.Page: // Positions are snapped to nearest page boundaries. But since page information // is based on the layout, positions are not changed, if they are not currently in the view. // We need to consider 2 types of scenarios: single page and multi-page. // In case of multi-page scenario, first need to find a page associated with the position. // If page is found, move the start position to the beginning of the first range of that page // and move the end position to the end of the last range of that page. textView = _textAdaptor.GetUpdatedTextView(); if (textView != null && textView.IsValid) { if (expandStart && textView.Contains(_start)) { ITextView pageTextView = textView; if (textView is MultiPageTextView) { // This is "multi page" case. Find page associated with the start position. pageTextView = ((MultiPageTextView)textView).GetPageTextViewFromPosition(_start); } ReadOnlyCollection<TextSegment> textSegments = pageTextView.TextSegments; if (textSegments != null && textSegments.Count > 0) { if (_start.CompareTo(textSegments[0].Start) != 0) { _start = textSegments[0].Start.CreatePointer(); } } } if (expandEnd && textView.Contains(_end)) { ITextView pageTextView = textView; if (textView is MultiPageTextView) { // This is "multi page" case. Find page associated with the start position. pageTextView = ((MultiPageTextView)textView).GetPageTextViewFromPosition(_end); } ReadOnlyCollection<TextSegment> textSegments = pageTextView.TextSegments; if (textSegments != null && textSegments.Count > 0) { if (_end.CompareTo(textSegments[textSegments.Count - 1].End) != 0) { _end = textSegments[textSegments.Count - 1].End.CreatePointer(); } } } } break; case TextUnit.Document: if (expandStart && _start.CompareTo(_start.TextContainer.Start) != 0) { _start = _start.TextContainer.Start.CreatePointer(); } if (expandEnd && _end.CompareTo(_start.TextContainer.End) != 0) { _end = _start.TextContainer.End.CreatePointer(); } break; default: // Unknown unit break; } }
private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e) { if (_isLoading) return; _isLoading = true; RichTextBox t = sender as RichTextBox; if (t == null) return; BlockCollection bc = t.Document.Blocks; foreach (Block b in bc) { if (b is Paragraph) { var para = b as Paragraph; string text = new TextRange(para.ContentStart, para.ContentEnd).Text; bool isProject = text.EndsWith(":"); bool isTask = text.StartsWith("-") || text.StartsWith("*"); bool isNote = !isProject && !isTask; bool isDone = text.Contains("@done"); para.Foreground = isNote ? Brushes.DimGray : Brushes.Black; para.FontSize = isProject ? 16 : 12; para.TextDecorations = isDone ? TextDecorations.Strikethrough : null; } } _isLoading = false; }