private void InnerWrite(string text, ConsoleColor foreground) { Run run = new Run(text) { Foreground = new SolidColorBrush(foreground.ToMediaColor()) }; this.ConsoleParagraph.Inlines.Add(run); InlineCollection list = this.ConsoleParagraph.Inlines; // 满1000行删除500行 if (list.Count > 1000) { int delLines = 500; while (delLines-- > 0) { list.Remove(list.FirstInline); } } if (ChkbIsConsoleAutoScrollToEnd.IsChecked.HasValue && ChkbIsConsoleAutoScrollToEnd.IsChecked.Value) { this.RichTextBox.ScrollToEnd(); } }
protected override void Render () { InlineCollection inlines = Inlines; while (inlines.Count > 1) inlines.Remove (inlines.LastInline); TwitterStatusViewer v = Owner; if (v == null) return; Status s = v.DataContext as Status; if (s == null) return; Status s1 = s; if (s.RetweetedStatus != null) s = s.RetweetedStatus; // 改行や連続する空白を削除 string text = s.Text; StringBuilder sb = new StringBuilder (text.Length); int state = 0; for (int i = 0; i < text.Length; i ++) { char c = text[i]; if (c == '\r' || c == '\n') continue; if (char.IsWhiteSpace (c)) { if (state == 1) continue; state = 1; } else { state = 0; } sb.Append (text[i]); } text = sb.ToString (); // Favoriteアイコン ToggleButton favBtn = v.CreateFavoriteButton (s); favBtn.Margin = new Thickness (0, 0, 3, 0); inlines.Add (favBtn); // 名前を追加 RoutedEventHandler defLinkHandler = new RoutedEventHandler (v.Hyperlink_Click); DependencyProperty nameFg = TwitterStatusViewer.NameForegroundProperty; inlines.Add (v.CreateHyperlink (s.User.ScreenName, "/" + s.User.ScreenName, nameFg, FontWeights.Bold, defLinkHandler)); inlines.Add (" "); // 本文を追加 v.CreateTweetBody (text, inlines); // 返信情報を追加 if (!string.IsNullOrEmpty (s.InReplyToScreenName)) { inlines.Add (v.CreateTextBlock (" in reply to ", FontWeights.Normal, nameFg)); inlines.Add (v.CreateHyperlink ("@" + s.InReplyToScreenName, "/" + s.InReplyToScreenName + (s.InReplyToStatusId == 0 ? string.Empty : "/status/" + s.InReplyToStatusId.ToString ()), nameFg, FontWeights.Bold, defLinkHandler)); } if (s != s1) { inlines.Add (v.CreateTextBlock (" RT by ", FontWeights.Normal, nameFg)); inlines.Add (v.CreateHyperlink ("@" + s1.User.ScreenName, "/" + s1.User.ScreenName, nameFg, FontWeights.Bold, defLinkHandler)); } }
private void DoMouseDownStuff(TextMediaBindableRun run, InlineCollection inlines) { DeactivateLastInlineBoxed(); TextMediaTextBox tb = new TextMediaTextBox(run.TextMedia); InlineUIContainer newInlineBoxed = new InlineUIContainer(tb); inlines.InsertAfter(run, newInlineBoxed); inlines.Remove(run); run.MouseDown -= new MouseButtonEventHandler(curRun_MouseDown); run.MouseEnter -= new MouseEventHandler(curRun_MouseEnter); run.MouseLeave -= new MouseEventHandler(curRun_MouseLeave); run.InvalidateBinding(); mLastInlineBoxed = tb; }
/// <summary> /// Renders an image element. /// </summary> /// <param name="inlineCollection"> The list to add to. </param> /// <param name="element"> The parsed inline element to render. </param> /// <param name="context"> Persistent state. </param> private async void RenderImage(InlineCollection inlineCollection, ImageInline element, RenderContext context) { var placeholder = RenderTextRun(inlineCollection, new TextRunInline { Text = element.Text, Type = MarkdownInlineType.TextRun }, context); var resolvedImage = await _imageResolver.ResolveImageAsync(element.Url, element.Tooltip); // if image can not be resolved we have to return if (resolvedImage == null) { return; } var image = new Image(); var imageContainer = new InlineUIContainer() { Child = image }; image.Source = resolvedImage; image.HorizontalAlignment = HorizontalAlignment.Left; image.VerticalAlignment = VerticalAlignment.Top; image.Stretch = ImageStretch; ToolTipService.SetToolTip(image, element.Tooltip); // Try to add it to the current inlines // Could fail because some containers like Hyperlink cannot have inlined images try { var placeholderIndex = inlineCollection.IndexOf(placeholder); inlineCollection.Remove(placeholder); inlineCollection.Insert(placeholderIndex, imageContainer); } catch { // Ignore error } }
private void AppendImpl(string text) { // 追加するRunを作成 Run run, oldrun = null; if (_stack.Count > 0 && (_stack.Peek() is Run)) { oldrun = (Run)_stack.Pop(); run = new Run(oldrun.Text + text); } else { run = new Run(text); } // Runを追加するべきコレクションを取得 InlineCollection inlines = GetNearestInlineCollection(); inlines.Remove(oldrun); inlines.Add(run); // スタックに乗せておく _stack.Push(run); }
private TextMediaBindableRun DoCloseSelf(bool saveContent, InlineUIContainer container, InlineCollection inlines) { if (saveContent) { BindingExpression be = GetBindingExpression(TextBox.TextProperty); be.UpdateSource(); //mLastInlineBoxed.OriginalText.setText(mLastInlineBoxed.Text); if (Text != OriginalText.Text) { //newRun.Text = mLastInlineBoxed.Text; } } TextMediaBindableRun newRun = new TextMediaBindableRun(OriginalText); InvalidateBinding(); inlines.InsertAfter(container, newRun); inlines.Remove(container); return(newRun); }
/// <summary> /// Event handler for KeyDown event to auto-detect hyperlinks on space, enter and backspace keys. /// </summary> private static void OnKeyDown(object sender, KeyEventArgs e) { var myRichTextBox = (MyRichTextBox)sender; if (e.Key != Key.Space && e.Key != Key.Back && e.Key != Key.Return) { return; } if (!myRichTextBox.Selection.IsEmpty) { myRichTextBox.Selection.Text = String.Empty; } TextPointer caretPosition = myRichTextBox.Selection.Start; if (e.Key == Key.Space || e.Key == Key.Return) { TextPointer wordStartPosition; string word = GetPreceedingWordInParagraph(caretPosition, out wordStartPosition); if (word == "www.microsoft.com") // A real app would need a more sophisticated RegEx match expression for hyperlinks. { // Insert hyperlink element at word boundaries. var start = wordStartPosition.GetPositionAtOffset(0, LogicalDirection.Backward); var end = caretPosition.GetPositionAtOffset(0, LogicalDirection.Forward); if (start != null) { if (end != null) { new Hyperlink(start , end); } } // No need to update RichTextBox caret position, // since we only inserted a Hyperlink ElementEnd following current caretPosition. // Subsequent handling of space input by base RichTextBox will update selection. } } else // Key.Back { TextPointer backspacePosition = caretPosition.GetNextInsertionPosition(LogicalDirection.Backward); Hyperlink hyperlink; if (backspacePosition != null && IsHyperlinkBoundaryCrossed(caretPosition, backspacePosition, out hyperlink)) { // Remember caretPosition with forward gravity. This is necessary since we are going to delete // the hyperlink element preceeding caretPosition and after deletion current caretPosition // (with backward gravity) will follow content preceeding the hyperlink. // We want to remember content following the hyperlink to set new caret position at. TextPointer newCaretPosition = caretPosition.GetPositionAtOffset(0, LogicalDirection.Forward); // Deleting the hyperlink is done using logic below. // 1. Copy its children Inline to a temporary array. InlineCollection hyperlinkChildren = hyperlink.Inlines; var inlines = new Inline[hyperlinkChildren.Count]; hyperlinkChildren.CopyTo(inlines, 0); // 2. Remove each child from parent hyperlink element and insert it after the hyperlink. for (int i = inlines.Length - 1; i >= 0; i--) { hyperlinkChildren.Remove(inlines[i]); if (hyperlink.SiblingInlines != null) { hyperlink.SiblingInlines.InsertAfter(hyperlink, inlines[i]); } } // 3. Apply hyperlink's local formatting properties to inlines (which are now outside hyperlink scope). LocalValueEnumerator localProperties = hyperlink.GetLocalValueEnumerator(); var inlineRange = new TextRange(inlines[0].ContentStart, inlines[inlines.Length - 1].ContentEnd); while (localProperties.MoveNext()) { LocalValueEntry property = localProperties.Current; DependencyProperty dp = property.Property; object value = property.Value; if (!dp.ReadOnly && dp != Inline.TextDecorationsProperty && // Ignore hyperlink defaults. dp != TextElement.ForegroundProperty && !IsHyperlinkProperty(dp)) { inlineRange.ApplyPropertyValue(dp, value); } } // 4. Delete the (empty) hyperlink element. if (hyperlink.SiblingInlines != null) { hyperlink.SiblingInlines.Remove(hyperlink); } // 5. Update selection, since we deleted Hyperlink element and caretPosition was at that Hyperlink's end boundary. if (newCaretPosition != null) { myRichTextBox.Selection.Select(newCaretPosition, newCaretPosition); } } } }
private void RemoveHyperlinkFormat() { TextPointer caretPosition = Selection.Start; TextPointer backspacePosition = caretPosition.GetNextInsertionPosition(LogicalDirection.Backward); Hyperlink hyperlink = default(Hyperlink); try { if (backspacePosition != null && IsHyperlinkBoundaryCrossed(caretPosition, backspacePosition, ref hyperlink)) { // Remember caretPosition with forward gravity. This is necessary since we are going to delete // the hyperlink element preceding caretPosition and after deletion current caretPosition // (with backward gravity) will follow content preceding the hyperlink. // We want to remember content following the hyperlink to set new caret position at. TextPointer newCaretPosition = caretPosition.GetPositionAtOffset(0, LogicalDirection.Forward); // 1. Copy its children Inline to a temporary array InlineCollection hyperlinkChildren = hyperlink.Inlines; Inline[] inlines = new Inline[hyperlinkChildren.Count]; hyperlinkChildren.CopyTo(inlines, 0); // 2. Remove each child from parent hyperlink element and insert it after the hyperlink for (int i = inlines.Length - 1; i >= 0; i--) { hyperlinkChildren.Remove(inlines[i]); if (hyperlink.SiblingInlines != null) { hyperlink.SiblingInlines.InsertAfter(hyperlink, inlines[i]); } } // 3. Apply hyperlink local formatting properties to inlines (which are now outside hyperlink scope) LocalValueEnumerator localProperties = hyperlink.GetLocalValueEnumerator(); TextRange inlineRange = new TextRange(inlines[0].ContentStart, inlines[inlines.Length - 1].ContentEnd); while (localProperties.MoveNext()) { LocalValueEntry property = localProperties.Current; DependencyProperty dependencyProperty = property.Property; object value = property.Value; // Ignore hyperlink defaults if (dependencyProperty.ReadOnly == false && dependencyProperty.Equals(Inline.TextDecorationsProperty) == false && dependencyProperty.Equals(TextElement.ForegroundProperty) == false && dependencyProperty.Equals(BaseUriHelper.BaseUriProperty) == false && IsHyperlinkProperty(dependencyProperty) == false && dependencyProperty.Name.Equals("IsEnabled") == false) { inlineRange.ApplyPropertyValue(dependencyProperty, value); } } // 4. Delete the (empty) hyperlink element if (hyperlink.SiblingInlines != null) { hyperlink.RequestNavigate -= OnRequestNavigate; hyperlink.SiblingInlines.Remove(hyperlink); } // 5. Update selection, since we deleted Hyperlink element and caretPosition was at that hyperlink's end boundary Selection.Select(newCaretPosition, newCaretPosition); } } catch (Exception ex) { Log.Error("Error while removing hyperlink format: {EX}", ex); } }