Read-only archive of the All About Symbian forum (2001–2013) · About this archive

Waht this iButton_back->SetObserver( ( MCoeControlObserver* ) iCommandObserver ) mean

2 replies · 3,466 views · Started 11 July 2010

Hi to all,
Hope you all will be fine. Actually i want to write button handler code in my application. I read for button handler, first derive class from MCoeControlObserver and then implement method like this


class CUsingButtonsAPIAppView : public CCoeControl, public MCoeControlObserver
{
....
....
//From MCoeControlObserver
virtual void HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType);
..........
.........
private:
CAknButton* iFirstButton;
CAknButton* iSecondButton;
// end of file

void CUsingButtonsAPIAppView::CreateButtonUsingResourceL()
{
...........
...........
iFirstButton->SetObserver(this);
iFirstButton->MakeVisible(ETrue);
iFirstButton->ActivateL();
}

Here iFirstButton->SetObserver(this); means that you want to implement code in the current class like this


void CUsingButtonsAPIAppView::HandleControlEventL( CCoeControl* aControl,
TCoeEvent aEventType )
{
switch ( aEventType )
{
case EEventStateChanged:
{
if(aControl == iFirstButton)
{
if(iSecondButton->State()->Flags()==KAknButtonStateHasLatchedFrame)
iSecondButton->SetCurrentState(KAknButtonTextInsideFrame,ETrue);
}
else if(aControl ==iSecondButton)
..........
.........

I want to ask if i set my observer like this


iButton_back->SetObserver( ( MCoeControlObserver* ) iCommandObserver );

then what is this mean, is it mean the same as the above line i.e., iFirstButton->SetObserver(this);

My class declaration is like this


class CHelloWorldContainer : public CCoeControl
{
public:
// constructors and destructor
CHelloWorldContainer();
static CHelloWorldContainer* NewL(
const TRect& aRect,
const CCoeControl* aParent,
MEikCommandObserver* aCommandObserver );
.........
.........

private:
void InitializeControlsL();
void LayoutControls();
CCoeControl* iFocusControl;
MEikCommandObserver* iCommandObserver;

private:
CEikLabel* iLabel_frequency;
CEikFloatingPointEditor* iFloatingPointEditor_frequency;
CAknButton* iButton_back;
CAknButton* iButton_forward;
CAknButton* iButton_record;

//end of file

void CHelloWorldContainer::ConstructL(
const TRect& aRect,
const CCoeControl* aParent,
MEikCommandObserver* aCommandObserver )
{
if ( aParent == NULL )
{
CreateWindowL();
}
else
{
SetContainerWindowL( *aParent );
}
iFocusControl = NULL;
iCommandObserver = aCommandObserver;
InitializeControlsL();
SetRect( aRect );
ActivateL();
// [[[ begin generated region: do not modify [Post-ActivateL initializations]
// ]]] end generated region [Post-ActivateL initializations]

}

void CHelloWorldContainer::InitializeControlsL()
{
..............
..............
iButton_back->ConstructFromResourceL ( R_HELLO_WORLD_CONTAINER_BUTTON_BACK );

iButton_back->SetContainerWindowL ( *this );
iButton_back->SetRect ( TRect (
TPoint ( 6, 83 ) ,
TSize ( 67, 46 ) )
) ;
iButton_back->SetObserver( ( MCoeControlObserver* ) iCommandObserver );
}

As you can see that my class is not derived from MCoeControlObserver, So i want to ask in this case can i write button handler ? or i should modify my code i.e., derived my class from MCoeControlObserver and provide
virtual void HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType);

and then modify this line
iButton_back->SetObserver( ( MCoeControlObserver* ) iCommandObserver );

to

iButton_back->SetObserver( this );

Thanks