Solutions 66

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

CHAPTER 2: Text Text formatting 66

Solution

If you have a selection, obtaining the text attribute suite interface (ITextAttributeSuite) from the same boss
class as the text selection (ISelectionUtils::QueryActiveTextSelectionSuite) provides you with methods that
allow you to modify the format of the selection (for example, ITextAttributeSuite::ToggleItalic, or
ITextAttributeSuite::IncrementPointSize). The suite also provides mechanisms for setting the value of
attributes that have nonexotic data requirements. For example, use ITextAttributeSuite::SetInt16Attribute
to specify that an override for an attribute of a particular type should be applied to the text, with a
particular value. For attributes with more exotic data requirements, use
ITextAttributeSuite::ApplyAttribute, though the onus for the creation of the attribute falls on the client.

Before manipulating the selection in this way, test that the operation is valid
(ITextAttributeSuite::CanApplyAttributes).

To modify the attribute given an arbitrary range of text, use ITextModelCmds interface (on
kTextStoryBoss), which provides a command through the ITextModelCmds::ApplyCmd API that modifies
the attributes. This command expects a K2::shared_ptr. The AttributeBossList that defines the attributes to
be applied should be allocated on the heap and wrapped in a K2::shared_ptr. This implements reference
counting for the attribute list and automatically deletes it when it is no longer used.

It is important to apply attributes to the correct strand: paragraph attributes should be applied to the
paragraph-attribute strand, and character attributes should be applied to the character-attribute strand.
For example, it would make no sense to try to set the justification of text on the character-attribute strand,
as justification is a paragraph attribute. Likewise, it would not make sense to set the point size of text on a
paragraph strand.

Although you cannot apply character-attribute overrides directly to the paragraph-attribute strand, you
can do so indirectly by defining a paragraph style with the character-attribute override defined, and
applying this style to the paragraph-attribute strand.

Before performing the modification, check the model to ensure it is not locked
(ITextModel::IsModelLocked).

Sample code

 See SnpApplyTextStyleAttributes::ApplyFontVariant for an example of using


ITextAttributeSuite::ApplyAttribute.

 See SnpTextModelHelper::ApplyOverrides for an example of using ITextModelCmds::ApplyCmd.

Clearing attribute overrides for text


You can remove local formatting overrides for a text selection or text range (leaving the text formatted to
the specification of whatever style is applied).

Solution

If you have a selection, obtaining the text attribute suite interface (ITextAttributeSuite) from the same boss
class as the text selection (ISelectionUtils::QueryActiveTextSelectionSuite) provides you with methods that
allow you to remove character-attribute overrides (ITextAttributeSuite::ClearCharacterOverrides),
paragraph-attribute overrides (ITextAttributeSuite::ClearParagraphOverrides), or both
(ITextAttributeSuite::ClearAllOverrides).

You might also like