private static TextPointer GetPoint(FlowDocument document, int x) { TextPointer ret = document.ContentStart; int i = 0; while (ret != null && i < x) { if (ret.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { String textRun = ret.GetTextInRun(LogicalDirection.Forward); i += textRun.Length; ret = ret.GetPositionAtOffset(textRun.Length); } else { ret = ret.GetNextInsertionPosition(LogicalDirection.Forward); i++; } } return ret == null ? document.ContentEnd : ret; }
/// <summary> /// Searches for all chidren comboboxes and deattaches event handlers from them. /// </summary> private void DettachEventHandlersFromAllComboBoxes(FlowDocument document) { System.Diagnostics.Debug.WriteLine("Detaching eventhandlers from combobox statements"); for (TextPointer position = document.ContentStart; position != null && position.CompareTo(document.ContentEnd) <= 0; position = position.GetNextContextPosition(LogicalDirection.Forward)) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd) { // process only UiElements. all other elements are ignored InlineUIContainer uiContainer; if ((uiContainer = position.Parent as InlineUIContainer) != null) { ComboBoxStatement statementBox = uiContainer.Child as ComboBoxStatement; statementBox.ComboBoxDropDownOpened -= OnComboBoxStatementDropDownOpened; statementBox.ComboBoxDropDownClosed -= OnComboBoxStatementDropDownClosed; } } } }
public TextRange FindWordFromPosition(TextPointer position, string word) { while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); // Find the starting index of any substring that matches "word". int indexInRun = textRun.IndexOf(word); if (indexInRun >= 0) { TextPointer start = position.GetPositionAtOffset(indexInRun); TextPointer end = start.GetPositionAtOffset(word.Length); return(new TextRange(start, end)); } } position = position.GetNextContextPosition(LogicalDirection.Forward); } // position will be null if "word" is not found. return(null); }
//A method i got from the internet to make my pointer work private static TextPointer GetTextPointAt(TextPointer from, int pos) { TextPointer ret = from; int i = 0; while ((i < pos) && (ret != null)) { if ((ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text) || (ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None)) { i++; } if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null) { return(ret); } ret = ret.GetPositionAtOffset(1, LogicalDirection.Forward); } return(ret); }
private void HighlightText(TextPointer position, string word) { while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); // Find the starting index of any substring that matches "word". int indexInRun = textRun.IndexOf(word); if (indexInRun >= 0) { TextPointer start = position.GetPositionAtOffset(indexInRun); TextPointer end = start.GetPositionAtOffset(word.Length); new TextRange(start, end).ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); } } position = position.GetNextContextPosition(LogicalDirection.Forward); } }
private TextRange FindMatch(TextPointer position, string text) { while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); int index = textRun.IndexOf(text); if (index >= 0) { TextPointer start = position.GetPositionAtOffset(index); TextPointer end = start.GetPositionAtOffset(text.Length); return(new TextRange(start, end)); } } position = position.GetNextContextPosition(LogicalDirection.Forward); } return(null); }
static TextRange FindWordFromPosition(TextPointer position, string word) { while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); int indexInRun = textRun.IndexOf(word, StringComparison.OrdinalIgnoreCase); if (indexInRun >= 0) { TextPointer start = position.GetPositionAtOffset(indexInRun); TextPointer end = start.GetPositionAtOffset(word.Length); return(new TextRange(start, end)); } } position = position.GetNextContextPosition(LogicalDirection.Forward); } return(null); }
private static IEnumerable <TextElement> GetRunsAndParagraphs(FlowDocument doc) { for (TextPointer position = doc.ContentStart; position != null && position.CompareTo(doc.ContentEnd) <= 0; position = position.GetNextContextPosition(LogicalDirection.Forward)) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd) { if (position.Parent is Run run) { yield return(run); } else { if (position.Parent is Paragraph para) { yield return(para); } } } } }
/// <summary> /// Converts flowDocument data to string between start and end textpointer /// </summary> /// <param name="start">start textpointer</param> /// <param name="end">end textpointer</param> /// <returns></returns> public string ConvertFlowDocumentDataToStringWithinSelection(TextPointer start, TextPointer end) { // NOT WORKING - it should check if the start and end document are within the same document... //if (start.IsInSameDocument(end)) //{ // throw new ArgumentException("Start and end pointers has to be within the same document."); //} StringBuilder codeBuilder = new StringBuilder(); for (TextPointer position = start; position != null && position.CompareTo(end) <= 0; position = position.GetNextContextPosition(LogicalDirection.Forward)) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd) { // process only runs, paragraphs, and UiElements. all other elements are ignored Paragraph paragraph; Run run; InlineUIContainer uiContainer; if ((paragraph = position.Parent as Paragraph) != null) { codeBuilder.AppendLine(); } else if ((run = position.Parent as Run) != null) { codeBuilder.Append(run.Text); } else if ((uiContainer = position.Parent as InlineUIContainer) != null) { ComboBoxStatement statementBox = uiContainer.Child as ComboBoxStatement; codeBuilder.Append(statementBox.FormattedVisualStatement); } } } return(codeBuilder.ToString()); }
//TODO: see how we can reuse with AvalonEdit // function to return all words in doc which match the regex pattern, then we send it to the formater public static IEnumerable <TextRange> GetAllWordRanges(FlowDocument document, string pattern) { TextPointer pointer = document.ContentStart; while (pointer != null) { if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = pointer.GetTextInRun(LogicalDirection.Forward); MatchCollection matches = Regex.Matches(textRun, pattern); foreach (Match match in matches) { int startIndex = match.Index; int length = match.Length; TextPointer start = pointer.GetPositionAtOffset(startIndex); TextPointer end = start.GetPositionAtOffset(length); yield return(new TextRange(start, end)); } } pointer = pointer.GetNextContextPosition(LogicalDirection.Forward); } }
private void textChanged(object sender, TextChangedEventArgs e) { if (Document == null) { return; } TextRange documentRange = new TextRange(Document.ContentStart, Document.ContentEnd); documentRange.ClearAllProperties(); TextPointer navigator = Document.ContentStart; while (navigator.CompareTo(Document.ContentEnd) < 0) { TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward); if (context == TextPointerContext.ElementStart && navigator.Parent is Run) { CheckWordsInRun((Run)navigator.Parent); } navigator = navigator.GetNextContextPosition(LogicalDirection.Forward); } }
public TextRange FindWordFromPosition(TextPointer position, string word) { while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); // Find the starting index of any substring that matches "word". int indexInRun = textRun.IndexOf(word, StringComparison.InvariantCultureIgnoreCase); if (indexInRun >= 0) { TextPointer start = position.GetPositionAtOffset(indexInRun); TextPointer end = start.GetPositionAtOffset(word.Length); return(new TextRange(start, end)); } } position = position.GetNextContextPosition(LogicalDirection.Forward); } return(null); }
private IEnumerable <TextElement> GetAnimatedBitmaps(TextRange range) { List <TextElement> list = new List <TextElement>(); for (TextPointer position = range.Start; position != null && position.CompareTo(range.End) <= 0; position = position.GetNextContextPosition(LogicalDirection.Forward)) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd) { if (position.Parent is InlineUIContainer) { if ((position.Parent as InlineUIContainer).Child is AnimatedImage) { list.Add(position.Parent as TextElement); } } } } return(list); }
List <TextRange> FindWordFromPosition(TextPointer position, string word) { List <TextRange> ranges = new List <TextRange>(); while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); // Find the starting index of any substring that matches "word". int indexInRun = textRun.IndexOf(word); if (indexInRun >= 0) { TextPointer start = position.GetPositionAtOffset(indexInRun); TextPointer end = start.GetPositionAtOffset(word.Length); ranges.Add(new TextRange(start, end)); } } position = position.GetNextContextPosition(LogicalDirection.Forward); } return(ranges); }
// </Snippet_TextPointer_TextPointer1> // <Snippet_TextPointer_TextPointer2> // This method will search for a specified word (string) starting at a specified position. TextPointer FindWordFromPosition(TextPointer position, string word) { while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); // Find the starting index of any substring that matches "word". int indexInRun = textRun.IndexOf(word); if (indexInRun >= 0) { position = position.GetPositionAtOffset(indexInRun); break; } } else position = position.GetNextContextPosition(LogicalDirection.Forward); } // position will be null if "word" is not found. return position; }
// Token: 0x0600686D RID: 26733 RVA: 0x001D7434 File Offset: 0x001D5634 internal IInputElement InputHitTest(int urOffset) { DependencyObject dependencyObject = null; int num = this.CalculateUOffsetShift(); CharacterHit characterHitFromDistance; if (this._line.HasOverflowed && this.TextParagraph.Properties.TextTrimming != TextTrimming.None) { Invariant.Assert(num == 0); TextLine textLine = this._line.Collapse(new TextCollapsingProperties[] { this.GetCollapsingProps(this._wrappingWidth, this.TextParagraph.Properties) }); Invariant.Assert(textLine.HasCollapsed, "Line has not been collapsed"); characterHitFromDistance = textLine.GetCharacterHitFromDistance(TextDpi.FromTextDpi(urOffset)); } else { characterHitFromDistance = this._line.GetCharacterHitFromDistance(TextDpi.FromTextDpi(urOffset - num)); } int cp = this._paraClient.Paragraph.ParagraphStartCharacterPosition + characterHitFromDistance.FirstCharacterIndex + characterHitFromDistance.TrailingLength; TextPointer textPointer = TextContainerHelper.GetTextPointerFromCP(this._paraClient.Paragraph.StructuralCache.TextContainer, cp, LogicalDirection.Forward) as TextPointer; if (textPointer != null) { TextPointerContext pointerContext = textPointer.GetPointerContext((characterHitFromDistance.TrailingLength == 0) ? LogicalDirection.Forward : LogicalDirection.Backward); if (pointerContext == TextPointerContext.Text || pointerContext == TextPointerContext.ElementEnd) { dependencyObject = textPointer.Parent; } else if (pointerContext == TextPointerContext.ElementStart) { dependencyObject = textPointer.GetAdjacentElementFromOuterPosition(LogicalDirection.Forward); } } return(dependencyObject as IInputElement); }
TextPointer FindPointerAtTextOffset(TextPointer from, int offset, bool seekStart) { if (from == null) { return(null); } TextPointer current = from; TextPointer end = from.DocumentEnd; int charsToGo = offset; while (current.CompareTo(end) != 0) { Run currentRun; if (current.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text && (currentRun = current.Parent as Run) != null) { var remainingLengthInRun = current.GetOffsetToPosition(currentRun.ContentEnd); if (charsToGo < remainingLengthInRun || (charsToGo == remainingLengthInRun && !seekStart)) { return(current.GetPositionAtOffset(charsToGo)); } charsToGo -= remainingLengthInRun; current = currentRun.ElementEnd; } else { current = current.GetNextContextPosition(LogicalDirection.Forward); } } if (charsToGo == 0 && !seekStart) { return(end); } return(null); }
public void PaintSingleLineComments() { TextPointer position = editor.Document.ContentStart; TextPointer endPosition; while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); // Find the starting index of any substring that matches "word". int indexInRun = textRun.IndexOf("//"); if (indexInRun >= 0) { position = position.GetPositionAtOffset(indexInRun); endPosition = position.GetLineStartPosition(1); if (endPosition == null) { endPosition = position.GetPositionAtOffset(textRun.Length - 1); } TextRange tr = new TextRange(position, endPosition); tr.ApplyPropertyValue(TextElement.ForegroundProperty, (SolidColorBrush)(br.ConvertFrom("#C0C0C0"))); position = position.GetNextContextPosition(LogicalDirection.Forward); } else { position = position.GetNextContextPosition(LogicalDirection.Forward); } } else { position = position.GetNextContextPosition(LogicalDirection.Forward); } } }
// <Snippet_TextPointer_GetNextContextPosition> // This method will extract and return a string that contains a representation of // the XAML structure of content elements in a given TextElement. string GetXaml(TextElement element) { StringBuilder buffer = new StringBuilder(); // Position a "navigator" pointer before the opening tag of the element. TextPointer navigator = element.ElementStart; while (navigator.CompareTo(element.ElementEnd) < 0) { switch (navigator.GetPointerContext(LogicalDirection.Forward)) { case TextPointerContext.ElementStart : // Output opening tag of a TextElement buffer.AppendFormat("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name); break; case TextPointerContext.ElementEnd : // Output closing tag of a TextElement buffer.AppendFormat("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name); break; case TextPointerContext.EmbeddedElement : // Output simple tag for embedded element buffer.AppendFormat("<{0}/>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name); break; case TextPointerContext.Text : // Output the text content of this text run buffer.Append(navigator.GetTextInRun(LogicalDirection.Forward)); break; } // Advance the naviagtor to the next context position. navigator = navigator.GetNextContextPosition(LogicalDirection.Forward); } // End while. return buffer.ToString(); } // End GetXaml method.
private void _CollectRangeProperties(TextRange TargetRange) { if (TargetRange != null) { _rangeProperty.Clear(); var start = TargetRange.Start; var end = TargetRange.End; int targetlength = TargetRange.Text.Length; int totallength = 0; TextPointer current = start; int length = 0; while (current.CompareTo(end) < 0) { if (current.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { length = current.GetTextRunLength(LogicalDirection.Forward); totallength += length; if (totallength > targetlength) { length = targetlength - totallength + length; } var ad = current.GetAdjacentElement(LogicalDirection.Forward); var tempRange = new TextRange(current, current.GetPositionAtOffset(length, LogicalDirection.Forward)); var backgroundProperty = tempRange.GetPropertyValue(Run.BackgroundProperty); _rangeProperty.Add(tempRange, backgroundProperty); } var nextPos = current.GetNextContextPosition(LogicalDirection.Forward); if (nextPos.CompareTo(end) > 0) { break; } current = nextPos; } } }
/// <summary> /// Test if the text pointer is in a hyperlink /// </summary> /// <param name="pos"></param> /// <returns></returns> private bool searchHyperlink(TextPointer pos) { if (null == pos) { return(false); } TextPointerContext context = pos.GetPointerContext(LogicalDirection.Backward); if (null == context) { return(false); } if (TextPointerContext.ElementStart == context) { object elem = pos.GetAdjacentElement(LogicalDirection.Backward); if (elem is Hyperlink) { return(true); } } else if (TextPointerContext.ElementEnd == context) { object elem = pos.GetAdjacentElement(LogicalDirection.Backward); if (elem is Hyperlink) { return(false); } } // go backwards... TextPointer back = pos.GetNextContextPosition(LogicalDirection.Backward); return(searchHyperlink(back)); }
// yield return returs object of IEnumerable type, each element one at a time // *yield break: stops private static IEnumerable <TextRange> ConvertWordsToTextRanges(FlowDocument doc) { TextPointer pointer = doc.ContentStart; while (pointer != null) // While there is some more text { if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = pointer.GetTextInRun(LogicalDirection.Forward); MatchCollection matches = Regex.Matches(textRun, @"\w+"); foreach (Match match in matches) { int startIndex = match.Index; // First char int length = match.Length; // The lenght TextPointer start = pointer.GetPositionAtOffset(startIndex); // Gets to the first char TextPointer end = start.GetPositionAtOffset(length); // Gets to the last char yield return(new TextRange(start, end)); // Selects the match } } pointer = pointer.GetNextContextPosition(LogicalDirection.Forward); // Checks if there is more text } }
private void OnTextChangedPrivate(object sender, TextChangedEventArgs e) { if (this.Document == null) { return; } TextRange docRange = new TextRange(this.Document.ContentStart, this.Document.ContentEnd); docRange.ClearAllProperties(); 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 System.Windows.Documents.Run)) { CheckWordsInRun(navigator.Parent); } navigator = navigator.GetNextContextPosition(LogicalDirection.Forward); } FormatText(); }
TextRange FindWordFromPosition(TextPointer position, string word) { while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { //带有内容的文本 string textRun = position.GetTextInRun(LogicalDirection.Forward); //查找关键字在这文本中的位置 int indexInRun = textRun.IndexOf(word); int indexHistory = 0; while (indexInRun >= 0) { TextPointer start = position.GetPositionAtOffset(indexInRun + indexHistory); TextPointer end = start.GetPositionAtOffset(word.Length); return(new TextRange(start, end)); } } position = position.GetNextContextPosition(LogicalDirection.Forward); } return(null); }
void FindWordFromPosition(TextPointer position, string pattern) { while (position != null) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = position.GetTextInRun(LogicalDirection.Forward); // Find the starting index of any substring that matches "word". var r = new System.Text.RegularExpressions.Regex(pattern); System.Text.RegularExpressions.Match m = r.Match(textRun); TextPointer textPointerEnd = position; { var position_start = position.GetPositionAtOffset(m.Index); textPointerEnd = position.GetPositionAtOffset(m.Index + m.Length); TextRange tr = new TextRange(position_start, textPointerEnd); tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); } } position = position.GetNextContextPosition(LogicalDirection.Forward); } }
public ReadingWIndow(int fontSize, string path, int _TraversalSpeed, System.Windows.Media.Color color1) { try { this.path = path; this.brush1 = new SolidColorBrush(color1); this.textSize = fontSize; InitializeComponent(); traversallTimer.Elapsed += new ElapsedEventHandler(TraversalEvent); traversallTimer.Interval = 1000 / _TraversalSpeed; StreamReader reader = new StreamReader(@path, Encoding.Default, true); prompterText.AppendText(reader.ReadToEnd()); prompterText.FontSize = textSize; prompterText.HorizontalAlignment = HorizontalAlignment.Center; TextPointer text = prompterText.Document.ContentStart; while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text) { text = text.GetNextContextPosition(LogicalDirection.Forward); } TextPointer startPos = text.GetPositionAtOffset(index); TextPointer endPos = text.GetPositionAtOffset(index + 30); prompterText.Selection.Select(startPos, endPos); prompterText.SelectionBrush = brush1; this.KeyDown += new KeyEventHandler(PrompterWindow_KeyDown_AsyncAndSync); } catch (Exception Ex) { MessageBox.Show(Ex.Message); } }
public static int ConvertTextPosition(this FlowDocument doc, TextPointer p) { int pos = 0; for (TextPointer position = doc.ContentStart; position != null && position.CompareTo(p) <= 0; position = position.GetNextContextPosition(LogicalDirection.Forward)) { if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd) { // } else { TextPointer nextPosition = position.GetNextContextPosition(LogicalDirection.Forward); if (position.Parent is Run) { Run run = position.Parent as Run; TextPointer p1 = run.ContentStart; TextPointer p2 = (nextPosition.CompareTo(p) <= 0) ? nextPosition : p; int x2 = -p2.GetOffsetToPosition(p1); pos += x2; } else if (position.Parent is InlineUIContainer) { InlineUIContainer inline = position.Parent as InlineUIContainer; if (inline.Child is TextBox) { string variable = (inline.Child as TextBox).Tag as String; pos += variable.Length + 2; // +2 for brackets } } } } return(pos); }
/// <summary> /// Event handler for RichTextBox.TextChanged event. /// </summary> private void TextChangedEventHandler(object sender, TextChangedEventArgs e) { // 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(); }
public static string GetTextBeforePointer(this RichTextBox rtb, TextPointer p, int length = 0) { StringBuilder stringBuilder = new StringBuilder(); while (p.CompareTo(rtb.Document.ContentStart) > 0) { if (p.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text) { stringBuilder.Insert(0, p.GetTextInRun(LogicalDirection.Backward)); if (length > 0 && stringBuilder.Length > length) { break; } } p = p.GetNextContextPosition(LogicalDirection.Backward); } string text = stringBuilder.ToString(); if (length > 0 && text.Length > length) { text = text.Substring(text.Length - length); } return(text); }
public static string GetTextAfterPointer(this RichTextBox rtb, TextPointer p, int length = 0) { var sb = new StringBuilder(); while (p.CompareTo(rtb.Document.ContentEnd) < 0) { if (p.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { sb.Append(p.GetTextInRun(LogicalDirection.Forward)); if (length > 0 && sb.Length > length) { break; } } p = p.GetNextContextPosition(LogicalDirection.Forward); } string text = sb.ToString(); if (length > 0 && text.Length > length) { text = text.Substring(0, length); } return(text); }