⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 popuplistcontainer.cpp

📁 series60 应用程序开发的源代码 series60 应用程序开发的源代码
💻 CPP
字号:
/**
* 
* @brief Definition of CPopUpListContainer
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/

// INCLUDE FILES

// Class include
#include "PopUpListContainer.h"

// System includes
#include <akniconarray.h> // CAknIcon
#include <aknlists.h> // CAknSingleStyleGraphicListBox
#include <aknpopup.h> // CAknPopupList
#include <barsread.h> // TResource Reader
#include <e32def.h> // STATIC_CAST
#include <eikclbd.h> // CColumnListBoxData
#include <PopUpList.mbg> // icons
#include <PopUpList.rsg> // R_POPUPLIST_SAVED_GAMES_LISTBOX
#include <stringloader.h> // StringLoader

// 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 CPopUpListContainer::ConstructL(const TRect& aRect)
{
    CreateWindowL();
    
    // Create the list
    CreateListL();    
    
    // Set the icons in the list's drawer
    SetupListIconsL();
    
    // Set up scroll bars
    SetupScrollBarsL(*iSavedGamesListBox);
    
    // Set the list items
    SetupSavedGameListItemsL();
    
    // Observe the list
    iSavedGamesListBox->SetListBoxObserver(this);

    SetRect(aRect);
    ActivateL();
}

/**
* Constructs the iSavedGamesList, setting its window.
*
*/
void CPopUpListContainer::CreateListL()
{
    // First phase construction
    iSavedGamesListBox = new (ELeave) CAknSingleGraphicStyleListBox;
    iSavedGamesListBox->SetContainerWindowL(*this);
    
    // Second Phase Construction
    TResourceReader reader;
    CEikonEnv::Static()->CreateResourceReaderLC(reader, R_POPUPLIST_SAVED_GAMES_LISTBOX);
    iSavedGamesListBox->ConstructFromResourceL(reader);
    CleanupStack::PopAndDestroy(); // reader
}

/**
* Creates vertical scrollbars for the list, which appear automatically when required.
*
*/
void CPopUpListContainer::SetupScrollBarsL(CEikListBox& aListBox)
{
    aListBox.CreateScrollBarFrameL(ETrue);
    aListBox.ScrollBarFrame()->SetScrollBarVisibilityL(
        CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
}

/**
* Loads icons from a file and sets them in the drawer for iSavedGamesList
*
*/
void CPopUpListContainer::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, EMbmPopuplistTick, EMbmPopuplistTick_mask));
    icons->AppendL(iEikonEnv->CreateIconL(*iconFileName, EMbmPopuplist1player, EMbmPopuplist1player_mask));
    icons->AppendL(iEikonEnv->CreateIconL(*iconFileName, EMbmPopuplist2player, EMbmPopuplist2player_mask));
    CleanupStack::Pop(icons);
    CleanupStack::PopAndDestroy(iconFileName);
    
    iSavedGamesListBox->ItemDrawer()->ColumnData()->SetIconArray(icons); // passing ownership of icons
    
}



/**
* Symbian OS 2 phase constructor.
* Constructs the CPopUpListContainer 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 CPopUpListContainer
*/
CPopUpListContainer* CPopUpListContainer::NewL(const TRect& aRect)
{
    CPopUpListContainer* self = CPopUpListContainer::NewLC(aRect);
    CleanupStack::Pop(self);
    return self;
}

/**
* Symbian OS 2 phase constructor.
* Constructs the CPopUpListContainer 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 CPopUpListContainer
*/
CPopUpListContainer* CPopUpListContainer::NewLC(const TRect& aRect)
{
    CPopUpListContainer* self = new (ELeave) CPopUpListContainer;
    CleanupStack::PushL(self);
    self->ConstructL(aRect);
    return self;
}

/** 
* Destructor.  Frees up memory for the iLabel.
*/
CPopUpListContainer::~CPopUpListContainer()
{
    delete iSavedGamesListBox;
}

/**
*    
* Called by framework when the view size is changed.  Resizes the
* iLabel accordingly.
*
*/
void CPopUpListContainer::SizeChanged()
{
    iSavedGamesListBox->SetExtent (KListPosition, iSavedGamesListBox->MinimumSize());
}

/**
* Called by the framework in compound controls    
* @return The number of controls in this CPopUpListContainer
*/
TInt CPopUpListContainer::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* CPopUpListContainer::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 CPopUpListContainer::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 CPopUpListContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
    if (iSavedGamesListBox)
        return iSavedGamesListBox->OfferKeyEventL (aKeyEvent, aType);
    else
        return EKeyWasNotConsumed;
}

/**
* Plays aSavedGame at aLevel
* This is an empty implementation of this method
*/
void CPopUpListContainer::PlayGame(TDesC& /*aSavedGameName*/, TDesC& /*aLevel*/)
{
}


/**
* Populates the list model's item array with a list of saved games
*/
void CPopUpListContainer::SetupSavedGameListItemsL()
{
    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 CPopUpListContainer::LoadSavedGamesL(CDesCArray& aSavedGamesArray)
{
    // Strings will be of the format "2\tSaved Game i", where i is a number from 1 to 10
    _LIT (KStringHeader, "2\tSaved Game %d");
    TBuf <16> aString;
    for (TInt i = 1; i< 11; i++)
    {
        aString.Format(KStringHeader(), i);
        aSavedGamesArray.AppendL (aString);
    }
}

/**
* Loads the levels for aSavedGameMenuListArray.    
* 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 aSavedGameMenuListArray The array where the saved game levels will be stored
*/
void CPopUpListContainer::LoadLevelsForSavedGameL(TDesC& /*aSavedGameName*/, CDesCArray& aSavedGameMenuListArray)
{
    // Strings will be of the format "Level i", where i is a number from 1 to 10
    _LIT (KStringMenuItem, "Level %d");
    TBuf <16> aString;
    for (TInt i = 1; i< 4; i++)
    {
        aString.Format(KStringMenuItem(), i);
        aSavedGameMenuListArray.AppendL (aString);
    }
}

/**
* 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 CPopUpListContainer::HandleListBoxEventL(CEikListBox* /*aListBox*/, TListBoxEvent aListBoxEvent)
{
    
    // if the Select Key has been pressed
    if ((aListBoxEvent == MEikListBoxObserver::EEventEnterKeyPressed) ||
        (aListBoxEvent == MEikListBoxObserver::EEventItemClicked))
    {
        CDesCArray* savedGameArray = STATIC_CAST(CDesCArray*, iSavedGamesListBox->Model()->ItemTextArray());
        TDesC savedGameName = (*savedGameArray)[iSavedGamesListBox->CurrentItemIndex()];
        OpenMenuListForSavedGameL(savedGameName);
    }
}

/**
* Loads the levels for aSavedGameMenuListArray.    
* 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 aSavedGameMenuListArray The array where the saved game levels will be stored
*/
void CPopUpListContainer::OpenMenuListForSavedGameL(TDesC& aSavedGameName)
{
    
    // First phase construction of menu list
    CAknSinglePopupMenuStyleListBox* savedGameMenuList =
        new (ELeave) CAknSinglePopupMenuStyleListBox;
    CleanupStack::PushL(savedGameMenuList);
    
    // Create a popuplist to show the menu list in
    CAknPopupList* popupList = CAknPopupList::NewL(
                                    savedGameMenuList, 
                                    R_AVKON_SOFTKEYS_OK_BACK);
    
    CleanupStack::PushL(popupList);

    // Second phase construction of menulist
    savedGameMenuList->ConstructL(popupList,EAknListBoxMenuList);
    // Set up scroll bars
    SetupScrollBarsL (*savedGameMenuList);

    // Set up menu items
    CTextListBoxModel* model = savedGameMenuList->Model();  // not taking ownership
    model->SetOwnershipType (ELbmOwnsItemArray);
    CDesCArray* savedGameMenuListArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
    LoadLevelsForSavedGameL(aSavedGameName, *savedGameMenuListArray);
    
    // Set title
    HBufC* title;
    title = StringLoader::LoadLC(R_SAVED_GAME_MENU_LIST_TITLE);    // Pushes title onto the Cleanup Stack.
    popupList->SetTitleL(*title);
    CleanupStack::PopAndDestroy(title);

    // Show the menu in the popup list
    TInt popupOk = popupList->ExecuteLD();

    CleanupStack::Pop(popupList);

    // if the user selected a level to play, play the game at that level
    if (popupOk)
    {
        TDesC level = (*savedGameMenuListArray)[savedGameMenuList->CurrentItemIndex()];
        CleanupStack::PopAndDestroy(savedGameMenuList);
        PlayGame (aSavedGameName, level);
    }
    // otherwise return to the saved game list
    else
        CleanupStack::PopAndDestroy(savedGameMenuList);    
}

// End of File    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -