Home |
Class PegTextThing HMI (Human-Machine Interface) Classes |
Class PegTextThingPegTextThing serves as a base class for all PEG objects that display or manipulate text. This provides a common set of API functions for all PEG classes that display text, such as PegTitle, PegTextButton, PegPrompt, PegString and others. PegTextThing provides string storage and manipulation functionality for all PegTextThing derived classes. This insulates the PEG classes from the character encoding method, which enables PEG to support both 8-bit ASCII and 16-bit UNICODE character encoding methods without dramatic changes to any of the derived classes. It is important to understand that by default PegTextThing does not copy text strings when string assignment is made. PegTextThing normally copies only the pointer to the text string. If the TT_COPYstyle flag is associated with the PegTextThing derived class, PegTextThing copies the actual text string when an assignment is made. For this reason, if you dynamically create a string that will be associated with a PegTextThing derived class, you should use the TT_COPY style when the class is constructed. For example, if you build up a string in an automatic character array using a function like itoa or sprintf, the storage for that character string is temporary storage, usually on the stack. After the function returns, the storage is no longer valid. If you are using a PegTextThing class in this way, the TT_COPY should be used. If the string associated with an object is static, which is most often the case, the TT_COPY flag should not be used. For example, when a PegTitle is created like this: PegTitle * pTitle = new PegTitle("Hello World"); the string is a string literal. The compile/linker will allocate storage space for this string and the storage space will never be deleted. In this case it is not necessary for PegTextThing to copy the actual string data. class PegTextThing { public: PegTextThing(const PEGCHAR * Text, PEGUINT wCopy = 0); PegTextThing(PEGUINT wCopy = 0); virtual void DataSet(const PEGCHAR * Text); PEGCHAR * DataGet(void); virtual void SetFont(PegFont * Font); virtual PegFont * GetFont(void); virtual PEGUINT TextLength(void); void SetCopyMode(void); static void SetDefaultFont(const UCHAR uIndex, PegFont * pFont); static PegFont * GetDefaultFont(UCHAR uIndex); }; ConstructorsThe wCopy parameter indicates whether the string should be copied. The second form of the constructor creates a PegTextThing with no initial text data. Method DataGetReturns a pointer to the text string associated with an object. Method DataSetAssigns the string associated with any object derived from PegTextThing. Method GetFontReturns the font associated with a PegTextThing derived object. Method SetFontAssigns the font associated with any PegTextThing derived object. Method TextLengthReturns the number of characters in the string. Method SetCopyModeAllows copy mode to be set to true after the object has been constructed. Once set to true, it can not be set back to false. This forces the object to behave as if you had passed the TT_COPY flag in the constructor. In that it makes a copy of the text and stores it internally. Method SetDefaultFontThe function sets a default font for an application and not just for one particular object. Parameter uIndex specifies for which purpose the given font is the new default. See section Default Fonts for details. Method GetDefaultFontThis function returns a pointer to a default font. Parameter uIndex specifies for which default font to query. See section Default Fonts for details. Examples:The following function creates a PegTextButton, which is a PegTextThing derived class, and assigns a custom font 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 obtains the string associated with a prompt. The string is converted to an integer, a range check is made, and the modified value is then re-assigned to the prompt. Note that in this case, the prompt should be created with the TT_COPY flag enabled. void MyWindow::CheckPromptVal(PegPrompt * pPrompt, PEGINT iMin, PEGINT iMax) { PEGCHAR * pString = pPrompt->DataGet(); BOOL bReplace = FALSE; if (pString) { PEGINT 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); // re-assign string pPrompt->Draw(); // and re-draw } }
|