Showing posts with label caret. Show all posts
Showing posts with label caret. Show all posts

Wednesday, September 15, 2010

Where is the caret in a text selection?

This wasn't intended but eventually becomes a followup on "Where is the caret?"

There are two directions to select some text in a rich edit: from left to right, or from right to left. The initial position of a selection is called an anchor point, and the ending position is called an active end. These are the names given in the EM_SETSEL documentation. The four other related messages and notification are: EM_GETSEL, EM_EXSETSEL, EM_EXSETSEL, and EN_SELCHANGE.

Since the caret marks the active position, to reshow or do something interesting with the caret, I need to know where the anchor and active positions are, given a selection range. As it turns out, all of above messages or notification (except EM_SETSEL) assume the start/min being anchor, and end/max being active.

Quite surprisingly, TOM can provide this information, through ITextDocument:GetSelection and ITextSelection:GetFlags (tomSelStartActive). I said surprising because TOM looks more like wrapper interfaces around Word and rich edit controls, but hey, what do I know?

Tuesday, December 30, 2008

Where is the caret?

Carefully minded will spot at least one subtle difference between the Visual Studio editor, and the RichEdit control: the caret in selection. Why oh why I can get this cute blinking cursor when I select some text in Visual Studio, but not in WordPad or in RichTextBox?

Maybe it's hidden for some bizarre reasons, I'll just call ShowCaret to bring it back. Argh, doesn't work? OK, may be HideCaret is called multiple times, so I'll just call ShowCaret a zillion times to see what gives. What? The caret is actually destroyed, and I have to call CreateCaret?

How am I supposed to know the height of the caret? It's not exactly Font.Height, and I can't always get the position of two lines and take the difference.

Text Object Model (TOM) comes into rescue. It's implemented by RichEdit (not Edit, so no hope there, yet). Get an IRichEditOle with EM_GETOLEINTERFACE, then an ITextDocument, then an ITextRange from ITextDocument::Range, and finally the coordinates from ITextRange::GetPoint.

I'll leave it as an exercise to declare the TOM interfaces for .net. Yea this whole thing is purely artistic...