Creating your own styles

You can create your own style class by deriving from TStyle or one of the many subclasses. At a minimum, you need to:

If you decide to create a style of a kind other than Character or Paragraph, you need to understand the style runs mechanism used to maintain those kinds so you can create a style runs class to maintain your style kind. This section describes this mechanism, and two other mechanisms that may be appropriate for styles you create:

Style runs and style runs groups

The CommonPoint styled text storage facility--TStandardText--uses style runs to manage the style data associated with the character data. The text system currently provides style runs classes to manage the style kinds Character and Paragraph, described in "Style kinds" on page 45.

TStandardText uses an instance of TStyleRunsGroup to manage its style data. A TStyleRunsGroup instance owns a TStyleRuns instance for each kind of style associated with the text. TText style manipulation functions--such as AddStyles or CopyStyles--actually call the corresponding function in TStyleRunsGroup; these functions then call TStyleRuns functions, which are implemented to handle styles of a particular kind.

The style run for each style kind is created by the TStyleKind class that defines that kind. For example, when you add Character styles to a text object, the text object calls the TCharacterStyleKind CreateStyleRuns function to create a TCharacterStyleRuns in which to maintain those styles, as shown in this figure.


When you create your own style class, you need to decide whether to use one of the defined kinds or to create a new kind. To define the style's kind, override the TStyle::GetKind function to return the correct kind. If your style class is of a kind already defined by the CommonPoint application system, TStandardText can store and manipulate instances of that class automatically.

When you create a style of a new kind, you need to:

Using TSharedStyle

When you create a style that will store a significant amount of data, you can derive your style from TSharedStyle rather than TStyle. TStyle is designed to be used in style sets with value semantics; styles are frequently copied from one style set to another as a text object is modified. Styles that derive from TSharedStyle can be shared by style sets, removing some of the overhead of copying and storing styles.

TSharedStyle derives from TStyle and provides the same public protocol. An instance of a class derived from TSharedStyle is a surrogate for the actual style data, encapsulated in a TInternalAttribute instance. Multiple instances of a shared style with the same name and value share the same internal attribute. The internal attribute is created when the shared style is constructed, and it is immutable.


When you derive a class from TSharedStyle, the constructors (except the copy constructor) for your class must:

The copy constructor can just call:

    TSharedStyle::SetInternalAttribute( copyFrom.GetInternalAttribute );
See the chapter "Attributes" in the manual Foundation Services for more information on TInternalAttribute and the attribute mechanism. This information can help you determine whether the shared style mechanism is appropriate for your style class. It is generally recommended that you use shared attributes when your style requires more than 8 bytes of internal storage.

Style propagation

Most style behavior is identical to the general attribute protocol defined by the TAttribute class. However, the style class supports an important additional behavior: style propagation. Style propagation means that when you insert new characters into an existing text object, the style information for the characters immediately preceding the insertion point are automatically applied to the new text.

You generally want most style information to propagate. However, some style classes are not meant to propagate. For example, you might create a style class that embeds data other than text (perhaps graphics or sound) as a nondisplaying character. You would not want this style to propagate to characters typed immediately following the embedded data. The TStyle::IsPropagatingStyle member function by default returns the Boolean value True and the style propagates itself. To create a nonpropagating style, override the IsPropagatingStyle function in your style class to return the value False.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker