The Actipro Blog

All the latest UI control development news from Actipro

SyntaxEditor for WPF – Hide text regions without using outlining (part 2)

In the last blog post we discussed how to hide text regions without using the code outlining feature in SyntaxEditor for WPF.  Today I’d like to show how you can replace the collapsed text regions with an intra-text adornment.

CollapsedRegion1 

Here is a another open source QuickStart that ships with SyntaxEditor.  In the editor, I will select some text and then press the Collapse Selected Text button at the top.

CollapsedRegion2

Similar to our last QuickStart, the text that was selected was hidden.  The difference is that here, we have reserved some space in-line with the text and rendered a custom adornment in that space.  The custom adornment could be anything, even a control.  For this QuickStart, we made it look like a standard collapsed text adornment. 

How does it work?

As in the previous post, we are using an ICollapsedRegionTag object to “tag” the region that should be hidden.  In this sample we also will tag the same text range with IIntraTextSpacerTag. The SyntaxEditor text formatting engine knows that when a collapsed region is found, it should look for an intra-text spacer with the same range.  If one is found, it will use that in place of where the collapsed text would have been.

Then the final step is to make an adornment manager that inherits our IntraTextAdornmentManagerBase class.  With that, it automatically finds intra-text spacer tags of the type you specify and asks you for an adornment element anytime one is found.  In the case of this sample, our adornment is the gray border with the ellipses inside.

The whole model is completely extensible in many ways, and allows you to achieve some really neat effects.

Summary

In SyntaxEditor for WPF, you now have three ways you can hide text:

  • Hide a text region and render nothing in its place, similar to the last post.
  • Hide a text region and render an adornment in its place, like above.
  • Use the code outlining feature to manage collapsible regions of text.

Some other uses for the feature demoed in this post… maybe you want to change any instance of a [ or ] character into some image that represents an open and close brace, thus providing more of a visual impact.  You would simple collapse the text ranges of either of those characters and render adornments (that are the images) in their place.  That sounds like another great idea for a QuickStart.  Maybe we’ll add a sample like that in the future! :)

Filed under: Actipro, New features, WPF

Comments (4) -

February 11, 2010 at 16:06  

Jesper Sweden

If you hide text regions like this, do you get to control what happens when a selection surrounding this region is deleted, or what it puts on the clipboard when copied?

February 11, 2010 at 16:12  

Bill Henning (Actipro) United States

With collapsed text, the text is still in the document.  So from the document's perspective, nothing is hidden.  You can always handle events like the document's TextChanging/ed events on any text change.

Also the SyntaxEditor.CutCopyDrag event fires before any text is placed on the clipboard.  So you can customize it there as needed.

February 11, 2010 at 16:51  

Jesper Sweden

That makes sense. I was just thinking that since you may use this as VS2010's inline adornments to replace a placeholder with something else like some sort of visualization, you could want to customize how that copies as well.

If you programmatically select halfway through stock outlining collapsed text (or a subrange of it), where does the editor draw the insertion point/caret? What does shift+left/right do?

February 11, 2010 at 17:02  

Bill Henning (Actipro) United States

I believe in that case the caret will render to the closest side of the adornment, and the caret should pop out of the collapsed region with a shift+left/right.

Pingbacks and trackbacks (1)+

Comments are closed