?? dynamiclistcontainer.cpp
字號:
/**
*
* @brief Definition of CDynamicListContainer
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/
// INCLUDE FILES
// Class include
#include "DynamicListContainer.h"
// System includes
#include <akniconarray.h> // CAknIcon
#include <aknlists.h> // CAknSingleStyleListBox
#include <barsread.h> // TResource Reader
#include <e32def.h> // STATIC_CAST
#include <eikclbd.h> // CColumnListBoxData
#include <eikmenub.h> // CEikMenuBar
#include <DynamicList.mbg> // icons
#include <DynamicList.rsg> // R_DYNAMICLIST_SAVED_GAMES_LISTBOX
#include <stringloader.h> // StringLoader
#include <uikon.hrh> // TKeyCode #defines
// CONSTANTS
const TInt KNumberOfIcons(2);
// N.B. #define'd as DLL cannot contain writeable static data
#define KListPosition TPoint(0,0)
// ================= MEMBER FUNCTIONS =======================
/**
* Symbian OS 2nd phase constructor. Creates a Window for the controls, which it contains.
* Constructs a label and adds it to the window, which it then activates.
* @param aRect The rectangle for this window
*/
void CDynamicListContainer::ConstructL(const TRect& aRect)
{
CreateWindowL();
// Create the list
CreateListL();
// Observe the list
iSavedGamesListBox->SetListBoxObserver(this);
// Set the icons in the list's drawer
SetupListIconsL();
// Set up scroll bars
SetupScrollBarsL();
// Set the list items
SetupListItemsL();
SetRect(aRect);
ActivateL();
}
/**
* Constructs the iSavedGamesList, setting its window.
*
*/
void CDynamicListContainer::CreateListL()
{
// First phase construction
iSavedGamesListBox = new (ELeave) CAknSingleGraphicStyleListBox;
iSavedGamesListBox->SetContainerWindowL(*this);
// Second Phase Construction
TResourceReader reader;
CEikonEnv::Static()->CreateResourceReaderLC(reader, R_DYNAMICLIST_SAVED_GAMES_LISTBOX);
iSavedGamesListBox->ConstructFromResourceL(reader);
CleanupStack::PopAndDestroy(); // reader
}
/**
* Creates vertical scrollbars for the list, which appear automatically when required.
*
*/
void CDynamicListContainer::SetupScrollBarsL()
{
iSavedGamesListBox->CreateScrollBarFrameL();
iSavedGamesListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
}
/**
* Loads icons from a file and sets them in the drawer for iSavedGamesList
*
*/
void CDynamicListContainer::SetupListIconsL()
{
// Get the name of the file containing the icons
HBufC* iconFileName;
iconFileName = StringLoader::LoadLC(R_ICON_FILE_NAME); // Pushes iconFileName onto the Cleanup Stack.
// Create an array of icons, reading them from the file
CArrayPtr<CGulIcon>* icons = new(ELeave) CAknIconArray(KNumberOfIcons);
CleanupStack::PushL(icons);
icons->AppendL(iEikonEnv->CreateIconL(*iconFileName, EMbmDynamiclist1player, EMbmDynamiclist1player_mask));
icons->AppendL(iEikonEnv->CreateIconL(*iconFileName, EMbmDynamiclist2player, EMbmDynamiclist2player_mask));
CleanupStack::Pop(icons);
CleanupStack::PopAndDestroy(iconFileName);
iSavedGamesListBox->ItemDrawer()->ColumnData()->SetIconArray(icons); // passing ownership of icons
}
/**
* Symbian OS 2 phase constructor.
* Constructs the CDynamicListContainer using the NewLC method, popping
* the constructed object from the CleanupStack before returning it.
*
* @param aRect The rectangle for this window
* @return The newly constructed CDynamicListContainer
*/
CDynamicListContainer* CDynamicListContainer::NewL(const TRect& aRect)
{
CDynamicListContainer* self = CDynamicListContainer::NewLC(aRect);
CleanupStack::Pop(self);
return self;
}
/**
* Symbian OS 2 phase constructor.
* Constructs the CDynamicListContainer using the constructor and ConstructL
* method, leaving the constructed object on the CleanupStack before returning it.
*
* @param aRect The rectangle for this window
* @return The newly constructed CDynamicListContainer
*/
CDynamicListContainer* CDynamicListContainer::NewLC(const TRect& aRect)
{
CDynamicListContainer* self = new (ELeave) CDynamicListContainer;
CleanupStack::PushL(self);
self->ConstructL(aRect);
return self;
}
/**
* Destructor. Frees up memory for the iLabel.
*/
CDynamicListContainer::~CDynamicListContainer()
{
delete iSavedGamesListBox;
}
/**
*
* Called by framework when the view size is changed. Resizes the
* iLabel accordingly.
*
*/
void CDynamicListContainer::SizeChanged()
{
iSavedGamesListBox->SetExtent (KListPosition, iSavedGamesListBox->MinimumSize());
}
/**
* Called by the framework in compound controls
* @return The number of controls in this CDynamicListContainer
*/
TInt CDynamicListContainer::CountComponentControls() const
{
return 1; // return number of controls inside this container
}
/**
* Called by the framework in compound controls
* @param The index of the control to return
* @return The control for aIndex
*/
CCoeControl* CDynamicListContainer::ComponentControl(TInt aIndex) const
{
switch (aIndex)
{
case 0:
return iSavedGamesListBox;
default:
return NULL;
}
}
/**
* Called by the framework to draw this control. Clears the area in
* aRect.
* @param aRect in which to draw
*/
void CDynamicListContainer::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
gc.Clear(aRect);
}
/**
* Called by the framework whenever a key event occurs.
* Passes the key event to the saved games list if it is not null, otherwise returns
* EKeyWasNotConsumed
* @param aKeyEvent the Key event which occured, e.g. select key pressed
* @param aType the type of Key event which occurred, e.g. key up, key down
* @return TKeyResponse EKeyWasNotConsumed if the key was not processed, EKeyWasConsumed if it was
*/
TKeyResponse CDynamicListContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if (iSavedGamesListBox)
return iSavedGamesListBox->OfferKeyEventL (aKeyEvent, aType);
else
return EKeyWasNotConsumed;
}
/**
* Plays the currently select game in the iSavedGamesListBox, if one exists.
* This is an empty implementation of this method
*/
void CDynamicListContainer::PlaySelectedGame()
{
}
/**
* Called by the framework whenever a list event occurs for which this container
* is an observer.
* @param aListBoxEvent The type of event which occured
*
*/
void CDynamicListContainer::HandleListBoxEventL(CEikListBox* /*aListBox*/, TListBoxEvent aListBoxEvent)
{
// if the Select Key has been pressed
if ((aListBoxEvent == MEikListBoxObserver::EEventEnterKeyPressed) ||
(aListBoxEvent == MEikListBoxObserver::EEventItemClicked))
{
PlaySelectedGame();
}
}
/**
* Populates the list model's item array with a list of saved games
*/
void CDynamicListContainer::SetupListItemsL()
{
CTextListBoxModel* model = iSavedGamesListBox->Model(); // not taking ownership
model->SetOwnershipType (ELbmOwnsItemArray);
CDesCArray* savedGamesArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
LoadSavedGamesL(*savedGamesArray);
}
/**
* Loads the list of saved games into aSavedGamesArray.
* This method merely creates some list items dynamically, and adds them
* to the array. In a real-world example, it is likely that the
* items would be read from a file
* @param aSavedGamesArray The array where the saved game items will be stored
*/
void CDynamicListContainer::LoadSavedGamesL(CDesCArray& aSavedGamesArray)
{
// Strings will be of the format "0\tSaved Game i", where i is a number from 1 to 10
_LIT (KStringHeader, "0\tSaved Game %d");
TBuf <16> aString;
for (TInt i = 1; i< 11; i++)
{
aString.Format(KStringHeader(), i);
aSavedGamesArray.AppendL (aString);
}
}
/**
* Deletes the currently selected game
*/
void CDynamicListContainer::DeleteSelectedL()
{
if (iSavedGamesListBox)
{
CTextListBoxModel* model = iSavedGamesListBox->Model();
if (model->NumberOfItems() > 0)
{
CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
TInt currentItem = iSavedGamesListBox->CurrentItemIndex();
itemArray->Delete(currentItem);
AknListBoxUtils::HandleItemRemovalAndPositionHighlightL(
iSavedGamesListBox,
currentItem,
ETrue);
}
}
}
// End of File
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -