문제

I am currently using AvalonEdit to develop a Movie Script editor in .NET 4.

Each element type in a script (i.e.: Character, dialog line, etc.) should have a right margin. In other words, after a certain number of characters per line, the following ones should go on another line.

Is it possible to assign a right margin per DocumentLine, or are we forced to handle each input individually, and determine when we have to skip to the next line ?

I tried using this approach, but having to calculate when to move, replace text and reposition the caret is not trivial and would require quite a bit of work. Unless I absolutely need to, I'd like to avoid this approach.

What alternatives are there to having right margins per DocumentLine ?

도움이 되었습니까?

해결책

This element generator will introduce a line break after every 20 columns:

public class WrapAtCol20 : VisualLineElementGenerator
{
    public override int GetFirstInterestedOffset(int startOffset)
    {
        DocumentLine line = CurrentContext.Document.GetLineByOffset(startOffset);
        int col = startOffset - line.Offset;
        int wrapCol = ((col / 20) + 1) * 20;
        if (wrapCol < line.Length) {
            return line.Offset + wrapCol;
        }
        return -1;
    }

    public override VisualLineElement ConstructElement(int offset)
    {
        return new WrapElement();
    }

    class WrapElement : VisualLineElement
    {
        public WrapElement() :  base(visualLength: 1, documentLength: 0)
        {
        }

        public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
        {
            return new TextEndOfLine(1);
        }
    }
}

Note that this may interact poorly with the upper-casing generator - that one generates a single element for the whole text that will be upper-cased, which prevents other generators from inserting an element in between. The solution for that would be to change AvalonEdit so that the upper-casing can be handled by a colorizer.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top