📄 mylistboxappview.cpp
字号:
// --------------------------------------------------------------------------
// MyListBoxAppView.cpp
//
// Copyright 2005, Antony Pranata
// http://www.antonypranata.com
//
// A view class for custom list box example.
// --------------------------------------------------------------------------
// INCLUDE FILES
#include <eikenv.h>
#include <eikappui.h>
#include <eiktxlbx.h> // CEikTextListBox
#include <eiktxlbm.h> // CTestListBoxModel
#include <badesca.h> // for array of descriptors
#include <eikon.mbg> // for the icon
#include <QuartzKeys.h> // keys for jog dial
#include <gulicon.h>
#include "MyListBoxAppView.h"
#include "CustomListBox.h"
// CONSTANTS
_LIT(KIconFileName, "z:\\system\\data\\eikon.mbm");
_LIT(KMessage, "Item #%d is clicked");
_LIT(KItemTextDouble1, "0\tFirst item\tDescription of the first item");
_LIT(KItemTextDouble2, "1\tSecond item\tDescription of the second item");
_LIT(KItemTextDouble3, "2\tThird item\tDescription of the third item");
_LIT(KItemTextDouble4, "3\tFourth item\tDescription of the fourth item");
_LIT(KItemTextDouble5, "0\tFifth item\tDescription of the fifth item");
_LIT(KItemTextDouble6, "1\tSixth item\tDescription of the sixth item");
_LIT(KItemTextSingle1, "0\tFirst item");
_LIT(KItemTextSingle2, "1\tSecond item");
_LIT(KItemTextSingle3, "2\tThird item");
_LIT(KItemTextSingle4, "3\tFourth item");
// MEMBER FUNCTIONS
void CMyListBoxAppView::ConstructL(const TRect& aRect)
{
iIsDoubleLine = ETrue;
CreateWindowL();
CreateListBoxL();
SetRect(aRect);
ActivateL();
}
CMyListBoxAppView::~CMyListBoxAppView()
{
delete iListBox;
}
void CMyListBoxAppView::UpdateListBoxL(TBool aIsDoubleLine)
{
iIsDoubleLine = aIsDoubleLine;
CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat(6);
CleanupStack::PushL(array);
if (aIsDoubleLine)
{
array->AppendL(KItemTextDouble1);
array->AppendL(KItemTextDouble2);
array->AppendL(KItemTextDouble3);
array->AppendL(KItemTextDouble4);
array->AppendL(KItemTextDouble5);
array->AppendL(KItemTextDouble6);
}
else
{
array->AppendL(KItemTextSingle1);
array->AppendL(KItemTextSingle2);
array->AppendL(KItemTextSingle3);
array->AppendL(KItemTextSingle4);
}
CleanupStack::Pop();
iListBox->Model()->SetItemTextArray(array);
// We need to set the current item index and call DrawNow()
// so that when the user changes the list box to single/double,
// it will be updated properly.
iListBox->SetCurrentItemIndex(0);
iListBox->DrawNow();
}
void CMyListBoxAppView::SizeChanged()
{
if (iListBox)
{
iListBox->SetRect(Rect());
}
}
TInt CMyListBoxAppView::CountComponentControls() const
{
if (iListBox)
{
return 1;
}
return 0;
}
CCoeControl* CMyListBoxAppView::ComponentControl(TInt aIndex) const
{
switch (aIndex)
{
case 0:
return iListBox;
default:
return NULL;
}
}
void CMyListBoxAppView::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
gc.SetPenStyle(CGraphicsContext::ENullPen);
gc.SetBrushColor(KRgbWhite);
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.DrawRect(aRect);
}
TKeyResponse CMyListBoxAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aType)
{
if (iListBox)
{
if ((EEventKeyDown == aType)
&& (EStdQuartzKeyConfirm == aKeyEvent.iScanCode))
{
HandleListBoxEventL(
iListBox, MEikListBoxObserver::EEventEnterKeyPressed);
}
}
return EKeyWasNotConsumed;
}
void CMyListBoxAppView::HandleListBoxEventL(CEikListBox* aListBox,
TListBoxEvent aEventType)
{
if ((MEikListBoxObserver::EEventEnterKeyPressed == aEventType)
|| (MEikListBoxObserver::EEventItemClicked == aEventType))
{
TBuf<30> buffer;
buffer.Format(KMessage, aListBox->CurrentItemIndex());
iEikonEnv->InfoMsg(buffer);
}
}
void CMyListBoxAppView::CreateListBoxL()
{
// Creates a new custom list box.
iListBox = new (ELeave) CCustomListBox();
iListBox->ConstructL(this,
CEikListBox::ENoFirstLetterMatching
| CEikListBox::ENoExtendedSelection);
iListBox->SetListBoxObserver(this);
// Fills the list box with some items.
UpdateListBoxL(ETrue);
// Creates a new icon array.
CArrayPtr<CGulIcon>* iconArray = new (ELeave)
CArrayPtrFlat<CGulIcon>(4);
CleanupStack::PushL(iconArray);
// Loads the icon and sets it as icon array for the list box.
iconArray->AppendL(iEikonEnv->CreateIconL(KIconFileName,
EMbmEikonFileclsd, EMbmEikonFileclsm));
iconArray->AppendL(iEikonEnv->CreateIconL(KIconFileName,
EMbmEikonFselprnt, EMbmEikonFselprnm));
iconArray->AppendL(iEikonEnv->CreateIconL(KIconFileName,
EMbmEikonFselfile, EMbmEikonFselfilm));
iconArray->AppendL(iEikonEnv->CreateIconL(KIconFileName,
EMbmEikonFileopen, EMbmEikonFileopem));
CleanupStack::Pop(); // iconArray
// Sets the icon array.
CCustomListItemDrawer* itemDrawer = static_cast<CCustomListItemDrawer*>
(iListBox->View()->ItemDrawer());
itemDrawer->SetIconArray(iconArray); // transfer ownership
// Sets the height of the list box item
// (= 2 * maximum height of the icon).
TSize size = itemDrawer->MaxIconSize();
iListBox->SetItemHeightL(2 * size.iHeight);
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -