Code:
/*
============================================================================
Name : SelectKeyControl.cpp
Author : Usanov-Kornilov Nikolay (aka Kolay)
Version : 1.0
Copyright :
Contacts:
kolayuk@mail.ru
http://kolaysoft.ru
(c) KolaySoft, 2010
Description : CSelectKeyControl implementation
============================================================================
*/
#include "SelectKeyControl.h"
#include <aknsutils.h>
#include <aknutils.h>
#include <akniconutils.h>
#include <MusicBar_ui.mbg>
#include <BARSREAD.H>
#include <ButtonContainer.h>
#include <MusicBar.rsg>
#include "Main.hrh"
#include <MusicBarAppUi.h>
CSelectKeyDialog::CSelectKeyDialog(TInt& aVal): CAknDialog(), MButtonCB(),iVal(aVal)
{
// No implementation required
}
TInt CSelectKeyDialog::RunDlgLD(TInt& aVal)
{
CSelectKeyDialog* dlg = new (ELeave) CSelectKeyDialog(aVal);
return dlg->ExecuteLD(R_SELECT_KEY_DIALOG);
}
void CSelectKeyDialog::PostLayoutDynInitL()
{
CSelectKeyControl* control = (CSelectKeyControl*)Control(KMyCustomCtlId);
control->SetCallBack(this);
if (iVal!=0) {control->SetVal(iVal);}
ButtonGroupContainer().MakeVisible(EFalse);
TInt scrX=CEikonEnv::Static()->ScreenDevice()->SizeInPixels().iWidth;
TInt scrY=CEikonEnv::Static()->ScreenDevice()->SizeInPixels().iHeight;
TRect r=TRect(TPoint(scrX/4,scrY/3),TSize(scrX/2,scrY/3));
control->ShrinkRects(r,EDirectionVertical);
control->MakeVisible(ETrue);
}
SEikControlInfo CSelectKeyDialog::CreateCustomControlL(TInt aControlType)
{
SEikControlInfo controlInfo;
controlInfo.iControl = NULL ;
controlInfo.iTrailerTextId = 0 ;
controlInfo.iFlags = 0 ;
switch (aControlType)
{
// CMyControl custom control type (defined in multiviews.hrh)
case KMyCustomCtl:
{
controlInfo.iControl = new (ELeave)CSelectKeyControl();
break;
}
default:
break;
}
return controlInfo;
}
TBool CSelectKeyDialog::OkToExitL(TInt aButtonID)
{
CSelectKeyControl* control = (CSelectKeyControl*)Control(KMyCustomCtlId);
if (aButtonID=EAknSoftkeyOk)
{
iVal=control->GetVal();
}
//TRAPD(err,CEikonEnv::Static()->AppUi()->RemoveFromStack(control));
return ETrue; //breakpoint - ok
}
void CSelectKeyDialog::ButtonPressed()
{
CAknDialog::TryExitL(EAknSoftkeyOk);
//iButtonGroupContainer->
}
CSelectKeyControl::CSelectKeyControl(): CCoeControl(), MCoeControlObserver()
{
// No implementation required
}
CSelectKeyControl::~CSelectKeyControl()
{
for (TInt i=0;i<iHandles.Count();i++)
{
CEikonEnv::Static()->RootWin().CancelCaptureKeyUpAndDowns(iHandles[i]);
}
Components().ResetAndDestroy();
delete iIcon;
}
CSelectKeyControl* CSelectKeyControl::NewLC(CCoeControl* aParent,TInt aResId)
{
CSelectKeyControl* self = new (ELeave) CSelectKeyControl();
CleanupStack::PushL(self);
self->ConstructL(aParent,aResId);
return self;
}
CSelectKeyControl* CSelectKeyControl::NewL(CCoeControl* aParent,TInt aResId)
{
CSelectKeyControl* self = CSelectKeyControl::NewLC(aParent,aResId);
CleanupStack::Pop(); // self;
return self;
}
void CSelectKeyControl::SetVal(TInt aVal)
{
iVal=aVal;
TBuf<3> txt;
txt.Num(iVal);
iLabelKey->SetTextL(txt);
DrawNow();
}
TInt CSelectKeyControl::GetVal()
{
return iVal;
}
void CSelectKeyControl::ConstructL(CCoeControl* aParent,TInt aResId)
{
TResourceReader reader;
CEikonEnv::Static()->CreateResourceReaderLC(reader,aResId);
CleanupStack::Pop(1); //reader
//ConstructFromResourceL(reader);
}
void CSelectKeyControl::LoadPicture()
{
CFbsBitmap* mask;
CFbsBitmap* icon;
_LIT(KPath,"\\resource\\apps\\MusicBar_ui.mif");
AknIconUtils::CreateIconL(icon,mask,KPath,EMbmMusicbar_uiButton_dlg_bg,EMbmMusicbar_uiButton_dlg_bg_mask);
AknIconUtils::SetSize(icon,Size(),EAspectRatioNotPreserved);
AknIconUtils::SetSize(mask,Size(),EAspectRatioNotPreserved);
iIcon=CGulIcon::NewL(icon,mask);
}
void CSelectKeyControl::Draw(const TRect& aRect) const
{
CWindowGc& gc=SystemGc();
gc.Clear(Rect());
gc.BitBltMasked(TPoint(0,0),iIcon->Bitmap(),TRect(TPoint(0,0),iIcon->Bitmap()->SizeInPixels()),iIcon->Mask(),EFalse);
}
void CSelectKeyControl::ShrinkRects(TRect aRect,TInt aDir)
{
SetExtent(aRect.iTl,aRect.Size());
CCoeControlArray::TCursor cursor = Components().Begin();
CCoeControl* ctrl = NULL;
TInt i=0;
TRect r;
while ((ctrl = cursor.Control<CCoeControl>()) != NULL)
{
if (aDir==EDirectionHorizontal)
{
TInt w=aRect.Size().iWidth/Components().Count();
TInt h=aRect.Size().iHeight;
r=TRect(i*w,0,(i+1)*w,aRect.Height());
}
else if (aDir==EDirectionVertical)
{
TInt w=aRect.Size().iWidth;
TInt h=aRect.Size().iHeight/Components().Count();
r=TRect(0,h*i,aRect.Width(),h*(i+1));
}
ctrl->SetRect(r);
cursor.Next();
i++;
}
}
void CSelectKeyControl::HandleControlEventL(CCoeControl* aControl,enum TCoeEvent aEvent)
{
if (aControl==iButton&&aEvent==EEventStateChanged)
{
if (iCallBack){ iCallBack->ButtonPressed();}
}
}
TKeyResponse CSelectKeyControl::OfferKeyEventL(const TKeyEvent& aKeyEvent, enum TEventCode aType)
{
TBuf<3> txt;
iVal=aKeyEvent.iScanCode;
txt.Num(aKeyEvent.iScanCode);
iLabelKey->SetTextL(txt);
DrawNow();
return EKeyWasConsumed;
}
void CSelectKeyControl::SizeChanged()
{
if (iIcon)
{
AknIconUtils::SetSize(iIcon->Bitmap(),Size(),EAspectRatioNotPreserved);
AknIconUtils::SetSize(iIcon->Mask(),Size(),EAspectRatioNotPreserved);
}
if (iButton)
{
iButton->SetIconScaleMode(EAspectRatioNotPreserved);
iButton->SetIconSize(iButton->Size());
iButton->SetTextAndIconAlignment(CAknButton::EOverlay);
}
TInt scrX=CEikonEnv::Static()->ScreenDevice()->SizeInPixels().iWidth;
TInt scrY=CEikonEnv::Static()->ScreenDevice()->SizeInPixels().iHeight;
TRect r=TRect(TPoint(scrX/4,scrY/3),TSize(scrX/2,scrY/3));
if (Position().iY!=r.iTl.iY){ShrinkRects(r,EDirectionVertical);}
CCoeControl::SizeChanged();
}
void CSelectKeyControl::SetCallBack(MButtonCB* aCB)
{
iCallBack=aCB;
}
void CSelectKeyControl::ConstructFromResourceL(TResourceReader& reader)
{
iVal=0;
CreateWindowL();
//SetExtentToWholeScreen();
LoadPicture();
InitComponentArrayL();
iLabel = new (ELeave) CEikLabel;
iLabel->SetContainerWindowL(*this);
TRgb lbc;
MAknsSkinInstance* skin=AknsUtils::SkinInstance();
AknsUtils::GetCachedColor(skin,lbc,KAknsIIDQsnTextColors,EAknsCIQsnTextColorsCG6 );
iLabel->OverrideColorL(EColorLabelText,lbc);
iLabel->SetAlignment(EHCenterVCenter);
iLabel->MakeVisible(ETrue);
iLabel->SetTextL(reader.ReadTPtrC16());
Components().AppendLC(iLabel);
CleanupStack::Pop(iLabel);
iLabelKey = new (ELeave) CEikLabel;
iLabelKey->SetContainerWindowL(*this);
AknsUtils::GetCachedColor(skin,lbc,KAknsIIDQsnTextColors,EAknsCIQsnTextColorsCG6 );
iLabelKey->OverrideColorL(EColorLabelText,lbc);
iLabelKey->SetAlignment(EHCenterVCenter);
iLabelKey->MakeVisible(ETrue);
iLabelKey->SetTextL(reader.ReadTPtrC());
Components().AppendLC(iLabelKey);
CleanupStack::Pop(iLabelKey);
iButton=CAknButton::NewL();
iButton->SetContainerWindowL(*this);
TInt res=reader.ReadInt32();
iButton->ConstructFromResourceL(res);
iButton->SetObserver(this);
iButton->SetIconScaleMode(EAspectRatioNotPreserved);
iButton->SetIconSize(iButton->Size());
iButton->SetTextAndIconAlignment(CAknButton::EOverlay);
iButton->MakeVisible(ETrue);
Components().AppendLC(iButton,res);
CleanupStack::Pop(iButton);
MakeVisible(EFalse);
ActivateL();
for (TInt i=1; i<=255;i++)
{
iHandles.Append(CEikonEnv::Static()->RootWin().CaptureKeyUpAndDowns(i,0,0));
}
}
При нажатии на iButton в контейнер-контроле вызывается CSelectKeyDialog::ButtonPressed() и диалог должен закрываться.