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

📄 markablelistcontainer.cpp

📁 symbian S60 2rd EMCClist
💻 CPP
字号:
/**
* 
* @brief Definition of CMarkableListContainer
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/

// INCLUDE FILES

// Class include
#include "MarkableListContainer.h"

// System includes
#include <akniconarray.h> // CAknIcon
#include <aknlists.h> // CAknSingleStyleGraphicListBox
#include <barsread.h> // TResource Reader
#include <e32def.h> // STATIC_CAST
#include <eikclbd.h> // CColumnListBoxData
#include <eiklbv.h> // CSelectionIndexArray
#include <MarkableList.mbg> // icons
#include <MarkableList.rsg> // R_MARKABLELIST_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 CMarkableListContainer::ConstructL(const TRect& aRect)
	{
	CreateWindowL();
	
	// Create the list
	CreateListL();	

	// 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 CMarkableListContainer::CreateListL()
	{
	// First phase construction
	iSavedGamesListBox = new (ELeave) CAknSingleGraphicStyleListBox;
	iSavedGamesListBox->SetContainerWindowL(*this);
	
	// Second Phase Construction
	TResourceReader reader;
	CEikonEnv::Static()->CreateResourceReaderLC(reader, R_MARKABLELIST_SAVED_GAMES_LISTBOX);
	iSavedGamesListBox->ConstructFromResourceL(reader);
	CleanupStack::PopAndDestroy(); // reader
	}

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

/**
* Loads icons from a file and sets them in the drawer for iSavedGamesList
*
*/
void CMarkableListContainer::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, EMbmMarkablelistTick, EMbmMarkablelistTick_mask));
	icons->AppendL(iEikonEnv->CreateIconL(*iconFileName, EMbmMarkablelist1player, EMbmMarkablelist1player_mask));
	icons->AppendL(iEikonEnv->CreateIconL(*iconFileName, EMbmMarkablelist2player, EMbmMarkablelist2player_mask));
	CleanupStack::Pop(icons);
	CleanupStack::PopAndDestroy(iconFileName);

	iSavedGamesListBox->ItemDrawer()->ColumnData()->SetIconArray(icons); // passing ownership of icons

	}



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

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

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

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

/**
* Called by the framework in compound controls	
* @return The number of controls in this CMarkableListContainer
*/
TInt CMarkableListContainer::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* CMarkableListContainer::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 CMarkableListContainer::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 CMarkableListContainer::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 CMarkableListContainer::PlaySelectedGame()
	{
	}


/**
* Populates the list model's item array with a list of saved games
*/
void CMarkableListContainer::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 CMarkableListContainer::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);
		}
	}

/**
* Deletes all currently marked games
*/
void CMarkableListContainer::DeleteSelectedL()
	{
	if (iSavedGamesListBox)
		{
		CTextListBoxModel* model = iSavedGamesListBox->Model();
		
		if (model->NumberOfItems() > 0)
			{
			
			// Create a copy of the currently selected items (copyIndices) in numeric order
			const CListBoxView::CSelectionIndexArray* selectionIndices = iSavedGamesListBox->SelectionIndexes();

			RArray<TInt> copyIndices; 
			CleanupClosePushL(copyIndices);
			TInt numberSelectedGames = selectionIndices->Count();
			for (TInt i=0; i< numberSelectedGames; i++)
				{
				copyIndices.InsertInOrder ((*selectionIndices)[i]);
				}	

			// Iterate through the copyIndices in reverse order, deleting them
			// from the list's item array
			TInt currentItem(0);
			CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
			for (TInt j=numberSelectedGames-1; j>=0; j--)
				{
				currentItem = copyIndices[j];
				itemArray->Delete(currentItem);
				}
			CleanupStack::PopAndDestroy(); // Close copyIndices and free memory


			// Redraw the list following removal
			AknListBoxUtils::HandleItemRemovalAndPositionHighlightL(
					iSavedGamesListBox, 
					currentItem, 
					ETrue);
		
			}
		}
	}

/**
* Handles marking commands for all currently selected games, by passing them 
* to the AknSelectionService
*/
void CMarkableListContainer::HandleMarkCommandL(TInt aCommand)
	{
	if (iSavedGamesListBox)
		{
		AknSelectionService::HandleMarkableListProcessCommandL (aCommand, iSavedGamesListBox);
		}
	}

/**
* 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 CMarkableListContainer::HandleListBoxEventL(CEikListBox* /*aListBox*/, TListBoxEvent aListBoxEvent)
	{
	
	// if the Select Key has been pressed
	if ((aListBoxEvent == MEikListBoxObserver::EEventEnterKeyPressed) ||
		(aListBoxEvent == MEikListBoxObserver::EEventItemClicked))
		{
			PlaySelectedGame();
		}
	}



// End of File	

⌨️ 快捷键说明

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