Example #1
0
        /// <summary>
        /// calc the line number of the line the TextPointer is located on in the
        /// FlowDocument.
        /// </summary>
        /// <param name="Pointer"></param>
        /// <param name="Doc"></param>
        /// <returns></returns>
        public static int GetLineNumber(this TextPointer Pointer, FlowDocument Doc)
        {
            TextPointer pointerStart = Pointer.GetLineStartPosition(0);
            TextPointer p            = Doc.ContentStart.GetLineStartPosition(0);
            var         p2           = p.GetLineStartPosition(1);
            int         lineNumber   = 0;

            while (true)
            {
                if (pointerStart.CompareTo(p) <= 0)
                {
                    break;
                }

                p = p.GetLineStartPosition(1);
                if (p == null)
                {
                    break;
                }

                lineNumber++;
            }

            return(lineNumber);
        }
Example #2
0
        /// <summary>
        /// Pad the line of the text pointer to the length specified.
        /// </summary>
        /// <param name="Doc"></param>
        /// <param name="Pointer"></param>
        /// <param name="Length"></param>
        public static void PadLineToLength(
            this TextPointer Pointer, FlowDocument Doc, int Length, char PadChar = ' ')
        {
            TextPointer tp1 = Pointer.GetLineStartPosition(0);
            TextPointer tp2 = Pointer.GetLineStartPosition(1);

            if (tp2 != null)
            {
                tp2 = tp2.GetNextInsertionPosition(LogicalDirection.Backward);
            }
            else
            {
                tp2 = Doc.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
            }

            // current line text.
            TextRange r1       = new TextRange(tp1, tp2);
            string    lineText = r1.Text;

            // pad length
            string padText = null;
            int    padLx   = Length - lineText.Length;

            if (padLx > 0)
            {
                padText = new StringBuilder().AppendRepeat(PadChar, padLx).ToString();
                tp2.InsertTextInRun(padText);
            }
        }
Example #3
0
        public static string GetLine(this TextPointer pointer, int index)
        {
            var start = pointer.GetLineStartPosition(index);
            var end   = pointer.GetLineStartPosition(index + 1) ?? pointer.DocumentEnd;
            var line  = new TextRange(start, end);

            return(line.Text.Trim());
        }
Example #4
0
        /// <summary>
        /// Gets the current line of text in the RichTextBox
        /// </summary>
        /// <param name="rtb"></param>
        /// <returns></returns>
        public static String GetCurrentLine(this RichTextBox rtb)
        {
            TextPointer caretPos = rtb.CaretPosition;
            TextPointer start    = caretPos.GetLineStartPosition(0);
            TextPointer end      = (caretPos.GetLineStartPosition(1) != null ? caretPos.GetLineStartPosition(1) : caretPos.DocumentEnd);

            TextRange tr = new TextRange(start, end);

            return(tr.Text);
        }
Example #5
0
        // Given a RichTextBox, return the line text of the current caret position
        public static string Rtb_GetCaretLineText(RichTextBox rtb)
        {
            TextPointer caretPos = rtb.CaretPosition;

            if (caretPos.GetLineStartPosition(1) == null)
            {
                return("<null>");
            }
            return(new TextRange(caretPos.GetLineStartPosition(0), caretPos.GetLineStartPosition(1)).Text);
        }
Example #6
0
        private void UpdateCaretPosition()
        {
            TextPointer caretLineStart = richTextBox_Main.CaretPosition.GetLineStartPosition(0);
            TextPointer p = richTextBox_Main.Document.ContentStart.GetLineStartPosition(0);

            int columnNumber = Math.Max(richTextBox_Main.CaretPosition.GetLineStartPosition(0).GetOffsetToPosition(richTextBox_Main.CaretPosition) - 1, 0);

            int currentLineNumber = 1;

            while (true)
            {
                if (caretLineStart.CompareTo(p) < 0)
                {
                    break;
                }

                int result;
                p = p.GetLineStartPosition(1, out result);
                if (result == 0)
                {
                    break;
                }

                currentLineNumber++;
            }

            statusBarItem_Position.Content = $"{currentLineNumber}:{columnNumber}";
        }
Example #7
0
 public void GoToLine(int linenumber)
 {
     if (linenumber == 1)
     {
         this.CaretPosition = Document.ContentStart.DocumentStart.GetLineStartPosition(0);
     }
     else
     {
         TextPointer ls   = Document.ContentStart.DocumentStart.GetLineStartPosition(0);
         TextPointer p    = Document.ContentStart.GetLineStartPosition(0);
         int         @int = 2;
         while (true)
         {
             int r = 0;
             p = p.GetLineStartPosition(1, out r);
             if (r == 0)
             {
                 this.CaretPosition = p;
                 break;                         // TODO: might not be correct. Was : Exit While
             }
             if (linenumber == @int)
             {
                 this.CaretPosition = p;
                 break;                         // TODO: might not be correct. Was : Exit While
             }
             @int += 1;
         }
     }
 }
Example #8
0
        public int GetLineNumber(bool TillEnd = false)
        {
            TextPointer caretLineStart = MainRichTextBox.CaretPosition.GetLineStartPosition(0);
            TextPointer p = MainRichTextBox.Document.ContentStart.GetLineStartPosition(0);
            int         caretLineNumber = 1;

            while (true)
            {
                if (!TillEnd)
                {
                    if (caretLineStart.CompareTo(p) < 0)
                    {
                        break;
                    }
                }

                int result;
                p = p.GetLineStartPosition(1, out result);

                if (result == 0)
                {
                    break;
                }

                caretLineNumber++;
            }
            return(caretLineNumber);
        }
        private void INPUTBOX_TextChanged(object sender, TextChangedEventArgs e)
        {
            INPUTBOX.ClearValue(TextElement.FontWeightProperty);
            INPUTBOX.ClearValue(TextElement.ForegroundProperty);
            TextRange       tr      = new TextRange(INPUTBOX.Document.ContentStart, INPUTBOX.Document.ContentEnd);
            string          data    = tr.Text;
            MatchCollection matches = Regex.Matches(data, @"[\w]+");
            long            num     = matches.Count;

            WORDCOUNT.Content = num.ToString() + " Words  ";
            //Get Line number in the richtextbox...
            //INPUTBOX.CaretPosition = INPUTBOX.CaretPosition.GetLineStartPosition(0);
            TextPointer caretStart = INPUTBOX.CaretPosition.GetLineStartPosition(0);
            TextPointer tp         = INPUTBOX.Document.ContentStart.GetLineStartPosition(0);
            int         linesCount = 1;

            while (true)
            {
                if (caretStart.CompareTo(tp) < 0)
                {
                    break;
                }
                int result;
                tp = tp.GetLineStartPosition(1, out result);
                if (result == 0)
                {
                    break;
                }
                linesCount++;
            }
            if (linesCount > 9 && linesCount <= 99)
            {
                Thickness tk = INPUTBOX.Margin;
                tk.Left         = 40;
                INPUTBOX.Margin = tk;
                SCROLLNUM.Width = 30;
            }
            else if (linesCount > 99 && linesCount < 1000)
            {
                Thickness tk = INPUTBOX.Margin;
                tk.Left         = 50;
                INPUTBOX.Margin = tk;
                SCROLLNUM.Width = 40;
            }
            else if (linesCount >= 1000)
            {
                MessageBox.Show("Sorry, I cannot deal with so much text!");
            }
            //Display in Another richtextbox
            while (maxLine < linesCount)
            {
                maxLine++;
                SCROLLNUM.AppendText(Environment.NewLine + maxLine.ToString());
            }
            while (maxLine > linesCount)
            {
                maxLine--;
                SCROLLNUM.Document.Blocks.Remove(SCROLLNUM.Document.Blocks.LastBlock);
            }
        }
Example #10
0
        /// <summary>
        /// Gets the position at the end of the line containing pointer
        /// </summary>
        /// <param name="pointer">Position on the line being queried</param>
        /// <returns>The position at the end of the line containing pointer</returns>
        public static TextPointer GetLineEndPosition(TextPointer pointer)
        {
            if (pointer == null)
            {
                throw new ArgumentNullException("pointer");
            }

            TextPointer result;

            int         actualCount;
            TextPointer endOfLine = pointer.GetLineStartPosition(1, out actualCount);

            if (actualCount != 1)
            {
                result = pointer.DocumentEnd;
            }
            else
            {
                // There may be several consecutive line boundary positions
                // only one of them is really useful to us.
                // It's the line start position whose forward and backward character rects are on
                // different lines.
                // The fastest way to find such a position without testing character rects it to
                // walk backward searching for the first position that is no a line start position.
                do
                {
                    endOfLine = endOfLine.GetPositionAtOffset(-1);
                }while (endOfLine != null && endOfLine.IsAtLineStartPosition);

                result = endOfLine ?? pointer.DocumentEnd;
            }

            return(result);
        }
Example #11
0
        public static int GetPositionInLine(this TextPointer Pointer)
        {
            TextPointer tp1 = Pointer.GetLineStartPosition(0);
            int         ox  = tp1.GetOffsetToPosition(Pointer);

            return(ox - 1);
        }
Example #12
0
        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            if (this.completions == null)
            {
                this.completions = new ObservableCollection <string>();
            }
            var textRange = new TextRange(this.Document.ContentStart, this.Document.ContentEnd);

            this.code = textRange.Text;
            TextPointer caretLineStart = this.CaretPosition.GetLineStartPosition(0);
            TextPointer p     = this.Document.ContentStart.GetLineStartPosition(0);
            int         value = this.CaretPosition.GetLineStartPosition(0).GetOffsetToPosition(this.CaretPosition);

            this.column = (uint)value;
            this.line   = 0;

            while (true)
            {
                if (caretLineStart.CompareTo(p) < 0)
                {
                    break;
                }
                int result;
                p = p.GetLineStartPosition(1, out result);
                if (result == 0)
                {
                    break;
                }
                this.line++;
            }
            this.autoResetEvent.Set();
            base.OnTextChanged(e);
        }
Example #13
0
        // Given a RichTextBox, return the column number of the current caret position
        public static int Rtb_GetCaretColumnPos(RichTextBox rtb)
        {
            TextPointer caretPos     = rtb.CaretPosition;
            TextPointer lineStartPos = caretPos.GetLineStartPosition(0);

            return(Math.Max(lineStartPos.GetOffsetToPosition(caretPos) - 1, 0));
        }
Example #14
0
        public static int GetFirtsLineEndPosition(this TextBlock source)
        {
            var         text      = source.Text;
            TextPointer lineStart = source.ContentStart.GetPositionAtOffset(1, LogicalDirection.Forward);
            TextPointer lineEnd   = lineStart?.GetLineStartPosition(1);

            return(lineEnd != null?lineStart.GetOffsetToPosition(lineEnd) : text.Length);
        }
        internal void HighlightLine(Color clr)
        {
            // Get the where the next line start - this will be null if there is no next line, in which case it will use the document's end.
            var nextStart = StartPointer.GetLineStartPosition(1);

            // Create a highlight up from the start to the end of the line - the TextRange represents where the RichTextBox gets highlighted.
            highlights.Add(new CodeTextBoxHighlight(new TextRange(StartPointer, (nextStart ?? txtCode.Document.ContentEnd).GetInsertionPosition(LogicalDirection.Backward)), clr));
        }
Example #16
0
        public static TextPointer GetTextPointerForPosition(this RichTextBox rtb, int line, int column)
        {
            TextPointer docStartLine = rtb.Document.ContentStart.GetLineStartPosition(0);
            TextPointer tp           = docStartLine.GetLineStartPosition(line);

            for (int i = 0; i <= column; i++)
            {
                tp = tp.GetNextInsertionPosition(LogicalDirection.Forward);
            }

            return(tp);
        }
        /// <summary>
        /// NOTE: This method is slow and inneficient.  DO not yet know a better way...
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public static int GetLineNumberFromPosition(RichTextBox rtb, TextPointer position)
        {
            if (position == null)
            {
                return(0);
            }

            int lineMoved;

            position.GetLineStartPosition(-int.MaxValue, out lineMoved);
            return(-lineMoved);
        }
Example #18
0
        public static int GetLineForTextPointer(this RichTextBox rtb, TextPointer tp)
        {
            TextPointer tpLine = tp.GetLineStartPosition(0);
            int         lineNr = 1;

            for (TextPointer linePointer = rtb.Document.ContentStart.GetLineStartPosition(0);
                 linePointer.CompareTo(tpLine) < 0; linePointer = linePointer.GetLineStartPosition(1))
            {
                lineNr++;
            }

            return(lineNr);
        }
Example #19
0
        public static int GetColumnForTextPointer(this RichTextBox rtb, TextPointer tp)
        {
            TextPointer tpColumn = tp.GetInsertionPosition(LogicalDirection.Backward);
            int         columnNr = 1;

            for (TextPointer linePointer = tpColumn.GetLineStartPosition(0).GetNextInsertionPosition(LogicalDirection.Forward);
                 linePointer.CompareTo(tpColumn) < 0; linePointer = linePointer.GetNextInsertionPosition(LogicalDirection.Forward))
            {
                columnNr++;
            }

            return(columnNr);
        }
Example #20
0
        private void RtbxTexto_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextPointer posCursor = rtbxTexto.CaretPosition;

            TextPointer linea = posCursor.GetLineStartPosition(0);

            TextRange rangoColumna = new TextRange(linea, posCursor);

            int indiceColumna = rangoColumna.Text.Length;
            int indiceLinea   = rtbxTexto.Document.Blocks.Count;

            stbItemColumna.Content = "Col " + indiceColumna;
            stbItemLinea.Content   = "Lin " + indiceLinea;
        }
Example #21
0
        private void DocumentEditor_SelectionChanged(object sender, RoutedEventArgs e)
        {
            TextPointer ls   = CaretPosition.GetLineStartPosition(0);
            TextPointer p    = Document.ContentStart.GetLineStartPosition(0);
            int         @int = 1;
            int         int2 = 1;

            while (true)
            {
                if (ls.CompareTo(p) < 1)
                {
                    break;                     // TODO: might not be correct. Was : Exit While
                }
                int r = 0;
                p = p.GetLineStartPosition(1, out r);
                if (r == 0)
                {
                    break;                     // TODO: might not be correct. Was : Exit While
                }
                @int += 1;
            }
            TextPointer ls2 = Document.ContentStart.DocumentEnd.GetLineStartPosition(0);
            TextPointer p2  = Document.ContentEnd.DocumentStart.GetLineStartPosition(0);

            while (true)
            {
                if (ls2.CompareTo(p2) < 1)
                {
                    break;                     // TODO: might not be correct. Was : Exit While
                }
                int r = 0;
                p2 = p2.GetLineStartPosition(1, out r);
                if (r == 0)
                {
                    break;                     // TODO: might not be correct. Was : Exit While
                }
                int2 += 1;
            }
            SelectedLineNumber = @int;
            LineCount          = int2;
            TextRange   t                   = new TextRange(Document.ContentStart, Document.ContentEnd);
            TextPointer caretPos            = CaretPosition;
            TextPointer poi                 = CaretPosition.GetLineStartPosition(0);
            int         currentColumnNumber = Math.Max(p.GetOffsetToPosition(caretPos) - 1, 0) + 1;
            int         currentColumnCount  = currentColumnNumber;

            currentColumnCount  += CaretPosition.GetTextRunLength(System.Windows.Documents.LogicalDirection.Forward);
            SelectedColumnNumber = currentColumnNumber;
            ColumnCount          = currentColumnCount;
        }
 private void OnKeyUp(object sender, KeyEventArgs e)
 {
     // Indentation.
     if (e.Key == Key.Enter || e.Key == Key.Return)
     {
         TextPointer caret = richTextBox.CaretPosition;
         caret.GetLineStartPosition(0).InsertTextInRun(this.CurrentIndentation);
         TextPointer moveTo = caret.Paragraph.ContentEnd;
         if (moveTo != null)
         {
             richTextBox.CaretPosition = moveTo;
         }
     }
 }
Example #23
0
        /// <summary>
        /// return a TextPointer positioned before a specified position on the line.
        /// If the position exceeds the end of the line, either pad the line with
        /// blanks or set the position to the last char on the line.
        /// </summary>
        /// <param name="Doc"></param>
        /// <param name="Pointer"></param>
        /// <param name="Position"></param>
        /// <param name="Pad"></param>
        /// <returns></returns>
        public static TextPointer SetPositionOnLine(
            this TextPointer Pointer,
            FlowDocument Doc, int Position,
            bool Pad = false)
        {
            TextPointer tp  = Pointer.GetLineStartPosition(0);
            int         pos = Position;

            // make sure length of the line can accomodate the position.
            string lineText = Pointer.GetLineText(Doc, 0, "");

            if (pos > (lineText.Length - 1))
            {
                if (Pad == false)
                {
                    pos = lineText.Length - 1;
                }
                else
                {
                    Pointer.PadLineToLength(Doc, pos + 1);
                    lineText = Pointer.GetLineText(Doc);
                }
            }

            // position to the last char on the line.
            if (lineText.Length == (pos + 1))
            {
                tp = Pointer.GetLineEndPosition(Doc);
            }

            // advance a char at a time from the start of the line.
            else
            {
                for (int ix = 0; ix < pos; ++ix)
                {
                    var nextTp = tp.GetNextInsertionPosition(LogicalDirection.Forward);
                    if (nextTp != null)
                    {
                        tp = nextTp;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(tp);
        }
Example #24
0
        /// <summary>
        /// WARNING: Be carefull with this function.
        /// When the text is wrapped (text longer then the visual-text-box) this function returns only the text until the end of the visual line
        /// As long as there is no text-wrapping it should work fine. (Is disabled now by setting PageWidht to 100000)
        /// If this is a problem this function needs to be extended to handle this case.
        /// </summary>
        public static TextRange GetVisualLine(this RichTextBox richTextBox, int lineNumber)
        {
            TextPointer textStart = richTextBox.Document.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);

            var searchedLineStart = textStart.GetLineStartPosition(lineNumber);

            if (searchedLineStart == null)
            {
                // Line does not exist
                throw new Exception($"GetLineNotifierLine() - Line {lineNumber} does not exist");
            }
            var       nextLineStart   = searchedLineStart.GetLineStartPosition(1);
            var       searchedLineEnd = (nextLineStart != null ? nextLineStart : textStart.DocumentEnd).GetInsertionPosition(LogicalDirection.Backward);
            TextRange textRange       = new TextRange(searchedLineStart, searchedLineEnd);

            return(textRange);
        }
Example #25
0
        private void GetCaretPosition(out int ColumnNumber, out int LineNumber)
        {
            TextPointer current = rtbConsole.CaretPosition;
            var         start   = current.GetEdgeTextPointer(LogicalDirection.Backward); // Word position before caret
            var         end     = current.GetEdgeTextPointer(LogicalDirection.Forward);  // Word position after caret

            current.GetCharacterRect(LogicalDirection.Forward);
            current.GetLineStartPosition(-int.MaxValue, out LineNumber);
            if (LineNumber < 0)
            {
                LineNumber *= -1;
            }
            LineNumber++;

            //var p = rtbConsole.Document.Blocks.ElementAt(lineNumber - 1) as Paragraph;
            ColumnNumber = start.GetLineStartPosition(0).GetOffsetToPosition(current) - 3;
        }
Example #26
0
        internal static int GetLineNumberFromSelection(TextPointer position)
        {
            if (position == null)
            {
                return(0);
            }

            int lineNumber = 0;
            int linesMoved;

            do
            {
                position = position.GetLineStartPosition(-1, out linesMoved);
                lineNumber++;
            }while (position != null && linesMoved != 0);

            return(lineNumber);
        }
        public static IEnumerable <string> GetLines(this TextBlock source)
        {
            var         text      = source.Text;
            int         offset    = 0;
            TextPointer lineStart = source.ContentStart.GetPositionAtOffset(1, LogicalDirection.Forward);

            do
            {
                TextPointer lineEnd = lineStart != null?lineStart.GetLineStartPosition(1) : null;

                int length = lineEnd != null?lineStart.GetOffsetToPosition(lineEnd) : text.Length - offset;

                yield return(text.Substring(offset, length));

                offset   += length;
                lineStart = lineEnd;
            }while (lineStart != null);
        }
Example #28
0
        /// <summary>
        /// Find the input string within the document, starting at the specified position.
        /// </summary>
        /// <param name="position">the current text position</param>
        /// <param name="input">input text</param>
        /// <returns>An <see cref="TextRange"/> representing the (next) matching string within the text container. Null if there are no matches.</returns>
        public TextRange FindNext(ref TextPointer position, String input)
        {
            FindText(input);

            foreach (var result in _searchHits)
            {
                if (position.CompareTo(result.End) < 0)
                {
                    position = result.Start;
                    double top = PointToScreen(position.GetLineStartPosition(0).GetCharacterRect(LogicalDirection.Forward).TopLeft).Y + PointFromScreen(new Point(0, 0)).Y;
                    Trace.WriteLine($" Top: {top}, CharOffset: {position}");
                    ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset + top);
                    position = result.End;
                    return(result);
                }
            }
            return(null);
        }
        private static void LogRemoved(object sender, LogEventArgs e)
        {
            RichTextBox richTextBox;

            if (!items.TryGetValue((ComponentViewModel)sender, out richTextBox))
            {
                return;
            }

            TextPointer pointer1 = richTextBox.Document.ContentStart.GetNextContextPosition(LogicalDirection.Forward);
            TextPointer pointer2 = pointer1.GetLineStartPosition(1);

            if ((pointer1 == null) || (pointer2 == null))
            {
                return;
            }
            richTextBox.Selection.Select(pointer1, pointer2);
            richTextBox.Selection.Text = "";
        }
Example #30
0
        internal static int GetColumnNumberFromSelection(TextPointer position)
        {
            if (position == null)
            {
                return(0);
            }

            int         linesMoved;
            TextPointer lineStartPosition = position.GetLineStartPosition(0, out linesMoved);

            int columnNumber = 0;

            do
            {
                columnNumber++;
                position = position.GetNextInsertionPosition(LogicalDirection.Backward);
            }while (position != null && position.CompareTo(lineStartPosition) > 0);

            return(columnNumber);
        }