Example: Creating a text control
To create a text control, you need to:
- Create a text control state.
- Instantiate TTextControl, passing the control state in the constructor.
- Launch the control in a window.
You can also control the appearance and behavior of the text control by calling TTextControl functions. The following lists some functions you will probably use:
- SetTextState lets you specify a TText object as initial text for the control. You can also set initial text by including it in the control state constructor.
- SetAllocatedArea lets you set the size of the text control. You can specify a fixed size, or use the value returned by GetPreferredSize to create a control that grows to accommodate all the input text.
- SetInitialTextStyles lets you specify character styles to apply to input text. If your control includes character styling menus, any styles the user applies override the initial styles.
- EnableTextMenus lets you specify whether to include the standard text menu in your text control. By default, this is set to False.
See the online class and member function documentation for a complete list of TTextControl functions.
This code demonstrates how to create a text control in a window. The resulting text control resizes itself to fit the input text.
// Copyright (C) 1994 Taligent, Inc. All rights reserved.
TGPoint kWindowSize = TGPoint(575, 125);
TGPoint kWindowOffset = TGPoint(100, 25);
TGPoint kTextControlOffset = TGPoint( 25, 25);
TRequestHandler* rh = new TRequestHandler( new TRequestQueue() );
TWindowGroup windowGroup( rh->GetRequestQueue() );
TStandardText windowTitle( "SampleText Control" );
TStandardWindow* window = new TStandardWindow(windowTitle, TRUE, TRUE, TRUE, TRUE, TRUE);
windowGroup.AdoptContentWindow( window );
window->SetWindowSize( kWindowSize );
window->MoveWindowTo( kWindowOffset );
TSimpleView* contentView = new TSimpleView;
window->AdoptChildView( contentView );
TStandardText initialText( "This is only a text of the text control system." );
TSimpleTextControlState* state = new TSimpleTextControlState( initialText );
TTextLabel* label = new TTextLabel( TStandardText( "Test Field:" );
TTextControl* control = new TTextControl( state, label );
control->SetTextBorderThickness( 5 );
control->SetControlLayout( MControl::kLeftToRight );
control->SetEnabled( TRUE );
control->SetAllocatedArea( TGRect( TGPoint::GetOrigin(), TGPoint( 525, 50 ) ) );
TStyleSet initialStyles;
initialStyles.Add( TTextColorStyle::GetRed() );
control->SetInitialTextStyles( initialStyles );
contentView->AdoptChild( control );
control->TranslateAllocatedAreaInParentTo( kTextControlOffset );
windowGroup->Activate();
window->Open();
while ( window->IsOpen() ) {
rh->WaitAndDispatchNextRequest();
}
delete rh;
To create a control that resizes itself to fit the input text, use this call instead of specifying a fixed size:
control->SetAllocatedArea( TGRect( TGPoint::GetOrigin(), control->GetPreferredSize() ) );
You can also use text controls in conjunction with other controls to create more complicated user interface elements--for example, enabling push buttons when the user enters text. See the manual Desktop Frameworks Concepts for general information on including controls in your applications. You can also use the UI Builder to build controls. See the manual UI Builder for the CommonPoint Application System for information on creating controls.
[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