c# – Caret navigation in Text Formatter API

Question:

Using the Text Formatter API in WPF to design a simple text editor, I needed to add caret control. In the documentation, I found the GetNextCaretCharacterHit , GetPreviousCaretCharacterHit and GetBackspaceCaretCharacterHit methods in the description of the TextLine class. But all 3 methods are abstract. I haven't really found examples of their implementation (perhaps a couple in the .NET source code).

Can anyone explain / provide an example what a working implementation of these methods is and how they work? Or can the carriage work be done in a different way? I would be glad for any help!

PS: I previously asked the same question on the main Stackoverflow , but it's already lost there. So if you find this question useful and vote for it here, then please vote there, if not difficult, because the likelihood that the question will be answered will be greater. I hope my request is not incorrect.

UPD:

Briefly about the problem to be solved: I need to add the usual carriage navigation through the text, i.e. its control by the cursor movement keys, its displacement when deleting and entering text (I somehow implemented the last two points through the GetCharacterHitFromDistance and GetDistanceFromCharacterHit , though all this somehow turned out awkwardly, because in case of a carriage shift when entering text, I directly create an instance of CharacterHit, specifying a unit in the constructor as the second argument (which represents trailingLength – what it is, see below): CharacterHit chHit = new CharacterHit(CurrentPosition, 1); ( CurrentPosition here is just a property of type int.

What is trailingLength in this case? The trailingLength documentation says: "In the case of the leading edge, the value is 0. In the case of the trailing edge, this value is equal to the number of code points up to the next valid cursor position." After reading about what code points are and about Unicode in general, everything becomes clear: most commonly used characters consist of a single code point. An example of compound ones is combining characters with diacritics, for example: \ u0418 \ u0306 = Y in Unicode (although \ u0419 = Y also).

By the way, I still haven't figured out how to ensure that the caret works correctly (that is, using those built-in Get*CaretCharacterHit ). I tried storing the current position not as an int value, but as a CharacterHit , in order to use those methods (they all take the current CharacterHit and return a new one). But in the end, everything practically ceased to work adequately.

Answer:

the GetNextCaretCharacterHit, GetPreviousCaretCharacterHit and GetBackspaceCaretCharacterHit methods
Can anyone explain / provide an example what the working implementation of these methods is and how they work?

GetPreviousCaretCharacterHit implementation.
the rest of the implementations can be found there, at http://referencesource.microsoft.com

Scroll to Top