Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avalonedit how to programmatically change background of a text

I want to implement something that programmatically changes the background of the text when provided with a documentline.(Something that looks very similar to a block selection of a text. I'm going to be using this for debug breakpoints of an IDE I'm designing). I don't want to have to use selection as it causes the textbox to scroll.

I think I need to make use of DocumentColorizingTransformer but I'm not 100% sure how to go about this.

public class ColorizeAvalonEdit : ICSharpCode.AvalonEdit.Rendering.DocumentColorizingTransformer
    {
        protected override void ColorizeLine(ICSharpCode.AvalonEdit.Document.DocumentLine line)
        {
            int lineStartOffset = line.Offset;
            string text = CurrentContext.Document.GetText(line);
            int start = 0;
            int index;
            if (line.LineNumber == LogicSimViewCodeWPFCtrl.currentLine)
            {
                while ((index = text.IndexOf(text, start)) >= 0)
                {
                    base.ChangeLinePart(
                        lineStartOffset + index, // startOffset
                        lineStartOffset + index + text.Length, // endOffset
                        (VisualLineElement element) =>
                        {
                            element.TextRunProperties.SetBackgroundBrush(Brushes.Red);

                        });
                    start = index + 1; // search for next occurrence
                }
            }
        }
    }

currentLine is the portion that will be highlighted.

The above code does work properly.. only problem is if the currentLine ever changes while I am viewing that line, it doesn't highlight the updated line until I scroll to another portion of the document (hiding the updated line), and come back to the updated line.

Also, how do I make the line numbers start from zero?

like image 292
l46kok Avatar asked Aug 10 '12 00:08

l46kok


2 Answers

Since it was their creation, I peeked at SharpDevelop's source and how they did it.

They defined a bookmark type (BreakpointBookmark) and added bookmark to the line. bookmark itself sets the color of the line in CreateMarker method. It is strange that it is not possible to configure colors of the break-point in SharpDevelop.

Hope it helps.

    protected override ITextMarker CreateMarker(ITextMarkerService markerService)
    {
        IDocumentLine line = this.Document.GetLine(this.LineNumber);
        ITextMarker marker = markerService.Create(line.Offset, line.Length);
        marker.BackgroundColor = Color.FromRgb(180, 38, 38);
        marker.ForegroundColor = Colors.White;
        return marker;
    }
like image 115
Erdogan Kurtur Avatar answered Sep 24 '22 13:09

Erdogan Kurtur


I found the answer

TxtEditCodeViewer.TextArea.TextView.Redraw();
like image 28
l46kok Avatar answered Sep 21 '22 13:09

l46kok