×
Namespaces

Variants
Actions
Revision as of 13:01, 10 March 2008 by tepaa (Talk | contribs)

Constructing a Symbian container control

From Nokia Developer Wiki
Jump to: navigation, search


ID   Creation date March 7, 2008
Platform S60 3rd Edition Tested on devices Nokia N95
Category Symbian C++ Subcategory UI
APIs None Classes CCoeControl
TResourceReader
CCoeControlArray
Methods None    

Overview

This code snippet shows how to create container control that owns custom controls. Example shows how to focus the first component with rectangle. Later we show how to change focus.

Example extends the existing code snippet Custom_control.

Container controls stores its controls into CCoeControlArray.

Header

class CMyContainerControl : public CCoeControl
{
public:
static CMyContainerControl* NewL(const TRect& aRect);
static CMyContainerControl* NewLC(const TRect& aRect);
virtual ~CMyContainerControl();
 
private: // from CCoeControl
void Draw(const TRect& aRect) const;
void SizeChanged();
 
public: // own methods
// NOTE: Transfer ownership to CMyContainerControl
void AddControlL(CCoeControl* aControl,TInt aControlId);
void UpdateControls();
 
private: // own methods
CMyContainerControl();
void ConstructL(const TRect& aRect);
};


Source

CMyContainerControl* CMyContainerControl::NewL(const TRect& aRect)
{
CMyContainerControl* self = CMyContainerControl::NewLC(aRect);
CleanupStack::Pop(self);
return self;
}
 
CMyContainerControl* CMyContainerControl::NewLC(const TRect& aRect)
{
CMyContainerControl* self = new(ELeave) CMyContainerControl();
CleanupStack::PushL(self);
self->ConstructL(aRect);
return self;
}
 
CMyContainerControl::CMyContainerControl()
{
}
 
CMyContainerControl::~CMyContainerControl()
{
}
 
void CMyContainerControl::ConstructL(const TRect& aRect)
{
// No parent owner, so create an own window
CreateWindowL();
 
// Initialize component array
InitComponentArrayL();
 
SetRect(aRect);
 
ActivateL();
}
 
void CMyContainerControl::SizeChanged()
{
UpdateControls();
}
 
void CMyContainerControl::UpdateControls()
{
// Update contained controls position
TPoint position;
 
// Goes throught all components of this container controls
CCoeControlArray::TCursor cursor = Components().Begin();
CCoeControl* ctrl = NULL;
while ((ctrl = cursor.Control<CCoeControl>()) != NULL)
{
// Set position
ctrl->SetPosition(position);
 
// Set size
TSize size = ctrl->MinimumSize();
size.SetSize(Rect().Width(),size.iHeight);
ctrl->SetSize(size);
cursor.Next();
 
// Store position of last component
position.iY += size.iHeight;
}
}
 
void CMyContainerControl::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
gc.SetBrushColor(KRgbBlack);
gc.Clear(Rect());
}
 
void CMyContainerControl::AddControlL(CCoeControl* aControl,TInt aControlId)
{
// NOTE: Transfer ownership of CCoeControl to CMyContainerControl
// Add control into container control
Components().AppendLC(aControl,aControlId);
CleanupStack::Pop(aControl);
 
// Focus first component
if (Components().Count()==1)
{
aControl->SetFocus(ETrue);
}
 
// Update controls position
UpdateControls();
}

Focusing component

Folowing changes to CMyControl component.

  • CMyControl::Draw() must call DrawFocusFrame() for drawing focus rectangle
  • CMyControl::DrawFocusFrame() is new method where focus rectangle is drawed
void CMyControl::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
gc.SetBrushColor(KRgbBlue);
gc.Clear(Rect());
 
DrawFocusFrame(aRect);
}
 
void CMyControl::DrawFocusFrame(const TRect& aRect) const
{
// Nothing to draw if not focused
if ( IsFocused() == EFalse )
return;
 
// Prep for draw
CWindowGc& gc = SystemGc();
gc.SetPenStyle( CGraphicsContext::ESolidPen );
gc.SetPenSize( TSize(KFocusFrameWidth,KFocusFrameWidth) );
gc.SetBrushStyle( CGraphicsContext::ENullBrush );
gc.SetPenColor( KRgbDarkGray );
 
// Draw the rounded rectangle
gc.DrawRoundRect( aRect, TSize( KFrameRoundRadius, KFrameRoundRadius ) );
}

Postconditions

See also

89 page views in the last 30 days.
×