KwikPeg Manuals
Home
PegTextThing
Examples
Examples
Index
Index


Members
Styles
Signals
(none)
Overview 

The PegTextThing class is the base class from which all KwikPeg objects that display or manipulate text are derived. Classes PegTitle, PegTextButton, PegPrompt and PegString are examples of text objects derived from the PegTextThing class.

PegTextThing provides string storage and manipulation services for all of its derived classes. In this manner, the KwikPeg text classes are isolated from the character encoding method. Hence, KwikPeg can support 8-bit ASCII and 16-bit Unicode character encoding methods without dramatic changes to derived text classes.

It is important to understand that, by default, PegTextThing does NOT copy text strings when strings are assigned to objects. PegTextThing normally copies only the pointer to the text string. However, if the TT_COPY style flag is set for a PegTextThing derived class, PegTextThing will copy the actual text string into private storage allocated with the object whenever a string is assigned to the object.

Hence, if you dynamically create a string that will be assigned to a PegTextThing derived class, you must use the TT_COPY style when the class is constructed. For example, if you build a string into a local character array using a C function like itoa or sprintf, the storage for that character string is temporary storage, usually on the stack. Once the function which built the string is finished (returns), the storage holding the string is no longer valid. However, if your PegTextThing class has the TT_COPY style, KwikPeg will copy your local string into private storage associated with the PegTextThing object, thereby avoiding the fault which would otherwise occur when your local string "disappears".

If the string associated with a text object is static (most often the case), the TT_COPY style flag should not be used. For example, when a PegTitle object is created as follows, the string assigned to the object is a string literal.

    PegTitle *pTitle = new PegTitle("Hello World");

The compiler and linker will allocate storage for this string and the storage will never be deleted. In this case, it is not necessary for PegTextThing to copy the actual string data to the text object.



 See Also

PegTitle

PegTextButton

PegPrompt

PegString



 Styles

PegTextThing supports only the TT_COPY style flag. An instance of a PegTextThing class is never actually created. When another class is derived from it, the style passed to a derived class constructor is forwarded to the PegTextThing constructor. The TT_COPY flag is therefore assigned indirectly during derived class construction.


Top of page


 Members

Constructors:

PegTextThing(const PEGCHAR *Text,
  WORD wCopy = 0,
  UCHAR uFontIndex = 0)
PegTextThing(WORD wCopy = 0,
  UCHAR uFontIndex = 0)

The first constructor creates a PegTextThing object and assigns it the text string specified by parameter Text.

The second constructor creates a PegTextThing object but does not assign it a text string.

In either case, if parameter wCopy is non-zero, the object will operate in its text copy mode. A separate copy of the text string will be created and kept with the object. Text copy mode must be enabled if any string assigned to the object is located in temporary storage such as a local variable on the stack.

If parameter wCopy is zero, the object will not maintain is own text string copy. If necessary, you can use function SetCopyMode() to enable text copy mode after the object has been constructed.

In both cases, parameter uFontIndex identifies the default font to be used to render text assigned to the object derived from the PegTextThing class. The allowable font index values have mnemonics of the form PEG_xxxx_FONT where xxxx is one of the strings used to describe font usage.

 

Public Functions:

PEGCHAR *DataGet(void) {return mpText;}

This inline function returns a pointer to the text string associated with an object.

virtual void DataSet(const PEGCHAR *Text)

This function is called to assign a string to an object derived from PegTextThing.

PegFont *GetDefaultFont(UCHAR uFontIndex)

This static function returns a pointer to the font currently being assigned as the default font for the text used with a particular category of objects derived from the PegTextThing class. Parameter uFontIndex specifies the font usage category of interest.

For example, if uFontIndex is PEG_TEXTBOX_FONT, the returned pointer will reference the default font currently being assigned to any textbox, an object of class PegTextBox derived from the PegTextThing class.

virtual PegFont *GetFont(void) {return mpFont;}

This inline function returns the font associated with a PegTextThing derived object.

void SetCopyMode(void)

This function is used to enable text copy mode after the object has been constructed. Once copy mode is enabled, it cannot be disabled. Using this function forces the object to behave as if you had passed the TT_COPY style flag to the constructor of an object derived from the PegTextThing class.

void SetDefaultFont(const UCHAR uFontIndex,
  PegFont *pFont)

This static function sets the default font to be used for the text of a particular category of objects derived from the PegTextThing class. The default font is set to the font referenced by parameter pFont.

Parameter uFontIndex specifies the font usage category to which the font will apply. For example, if uFontIndex is PEG_TITLE_FONT, all subsequent title bars (objects of class PegTitle derived from the PegTextThing class) will have the default font specified by pFont.

virtual void SetFont(PegFont *pFont) {mpFont = pFont;}

This inline function assigns the font to be used for the text of a PegTextThing derived object.

virtual WORD TextLength(void) {return mwStrLen;}

This inline function returns the number of characters in the string currently associated with a PegTextThing derived object.

 

Protected Members:

UCHAR mbCopy

This boolean value will have the value TRUE if string copy mode is enabled for the text object. If string copy mode is not enabled, mbCopy will have the value FALSE.

PegFont *mpFont

Pointer to the PegFont associated with the object.

PEGCHAR *mpText

Pointer to the text string associated with the object.

WORD mwStrLen

Number of characters in the text string currently assigned to the object.


Top of page


 Examples

The following function creates a PegTextButton, a PegTextThing derived class. A custom font is assigned to the button. The font is named "CustomButtonFont". The button is then added to the parent window.

extern PegFont CustomButtonFont;

void MyWindow::AddCustomButton(PEGCHAR *ButtonText)
{
    PegTextButton *pButton = new PegTextButton(10, 10, 80, ButtonText);
    pButton->SetFont(&CustomButtonFont);
    Add(pButton);
}


The following function fetches the string associated with a prompt. The string is converted to an integer and its value is range checked. If the value is out of range, the value is clipped and the modified value is reassigned to the prompt. Note that the prompt object must have been created with the TT_COPY style flag.

void MyWindow::CheckPromptVal(PegPrompt *pPrompt,
        SIGNED iMin, SIGNED iMax)
{
    PEGCHAR *pString = pPrompt->DataGet();
    BOOL bReplace = FALSE;

    if (pString)
    {
        SIGNED iVal = atoi(pString);

        if (iVal < iMin)
        {
            iVal = iMin;
            bReplace = TRUE;
        }
        if (iVal > iMax)
        {
            iVal = iMax;
            bReplace = TRUE;
        }
    }
    else
    {
        iVal = iMin;
        bReplace = TRUE;
    }

    if (bReplace)                       // prompt is out of range?
    {
        PEGCHAR cTemp[40];

        itoa(iVal, cTemp, 10);
        pPrompt->DataSet(cTemp);        // assign string
        pPrompt->Draw();                // and redraw
    }
}



Top of page
KwikPeg Manuals
Home
Index
Index

 
Copyright © 2000-2003