opponentformcontainer.cpp

来自「最新官方例子,图形,描述副,基本控件,通讯协议,等等,」· C++ 代码 · 共 284 行

CPP
284
字号
/**
* 
* @brief Definition of COpponentFormContainer
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/

// INCLUDE FILES

// Class include
#include "OpponentFormContainer.h"

// System includes
#include <aknlists.h> // CAknSingleHeadingStyleListBox
#include <barsread.h> // TResourceReader
#include <OpponentForm.rsg> //R_OPPONENTFORM_FORM, R_OPPONENTFORM_OPPONENTS_LISTBOX


// User includes
#include "OpponentFormForm.h"


// CONSTANTS

// 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 list and adds it to the window, which it then activates.
* @param aRect The rectangle for this window
*/		
void COpponentFormContainer::ConstructL(const TRect& aRect)
	{
	CreateWindowL();
	
	iOpponentsList = new (ELeave) CAknSingleHeadingStyleListBox;
	iOpponentsList->SetContainerWindowL(*this);
	
	// Second Phase Construction
	TResourceReader reader;
	CEikonEnv::Static()->CreateResourceReaderLC(reader, R_OPPONENTFORM_OPPONENTS_LISTBOX);
	iOpponentsList->ConstructFromResourceL(reader);
	CleanupStack::PopAndDestroy(); // reader
	
	iOpponentsList->CreateScrollBarFrameL(ETrue);
	iOpponentsList->ScrollBarFrame()->SetScrollBarVisibilityL(
		CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
	
	SetRect(aRect);
	ActivateL();
	}

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

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

/**
* Destructor. Frees up memory for the list and the array of opponents.
*/
COpponentFormContainer::~COpponentFormContainer()
	{
	delete iOpponentsList;
	iOpponents.Close();
	}


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

/**
* Called by the framework in compound controls	
* @return The number of controls in this COpponentFormContainer
*/
TInt COpponentFormContainer::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* COpponentFormContainer::ComponentControl(TInt aIndex) const
	{
	switch (aIndex)
		{
		case 0:
			return iOpponentsList;
		default:
			return NULL;
		}
	}

/**
* Called by the framework to draw this control.  Clears the area in 
* aRect.
* @param aRect in which to draw
*/
void COpponentFormContainer::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 opponents 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 COpponentFormContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
	{
	if (iOpponentsList)
		return iOpponentsList->OfferKeyEventL (aKeyEvent, aType);
	else
		return EKeyWasNotConsumed;
	}


/**
* Invokes the form in editable state, with a new opponenet, so that the details can be entered.
* Adds the opponent to the array of iOpponents and the list
*/
void COpponentFormContainer::NewOpponentL()
	{
	TOpponentFormOpponent opponent;
	COpponentFormForm* form = COpponentFormForm::NewL(opponent);
	form->ExecuteLD(R_OPPONENTFORM_FORM_DIALOG);
	AddOpponentToListL(opponent);
	}

/**
* Invokes the form in the view state, with the currently selected opponent so that the details 
* can be viewed and edited.
* Updates the opponent in the array of iOpponents and the list
*/
void COpponentFormContainer::OpenOpponentL()
	{
	TOpponentFormOpponent& opponent = iOpponents[iOpponentsList->CurrentItemIndex()];
	COpponentFormForm* form = COpponentFormForm::NewL(opponent);
	form->ExecuteLD(R_OPPONENTFORM_FORM_DIALOG);
	UpdateOpponentInListL (opponent);
	
	}



/**
* If the opponent is valid, adds it to the array of iOpponents and updates the list
* of opponents
* @param aOpponent reference to the opponent which should be added
*/
void COpponentFormContainer::AddOpponentToListL (const TOpponentFormOpponent& aOpponent)
	{
	if (aOpponent.IsValid())
		{
		iOpponents.Append(aOpponent);
		
		CTextListBoxModel* model = iOpponentsList->Model();  // not taking ownership
		model->SetOwnershipType (ELbmOwnsItemArray);
		CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
		itemArray->AppendL (ListStringFromOpponentL(aOpponent));
		iOpponentsList->HandleItemAdditionL();
		
		}
	
	}

/**
* Extracts the information from aOpponent, for display in the list.
* @param aOpponent the opponent to get the information from
*/
TBuf<KListItemLength> COpponentFormContainer::ListStringFromOpponentL (const TOpponentFormOpponent& aOpponent) const
	{
	TBuf<KListItemLength> string; 
	_LIT (KStringHeader, "%S\t%S");
	HBufC* name = aOpponent.Name().AllocLC();		
	HBufC* power = aOpponent.Power().AllocLC(); 	
	string.Format(KStringHeader(), name, power);
	CleanupStack::PopAndDestroy (power);
	CleanupStack::PopAndDestroy (name);
	
	return string;
	}


/**
* Updates the opponent in the array of iOpponents and updates the list
* of opponents
* @param aOpponent reference to the opponent which should be updated
*/
void COpponentFormContainer::UpdateOpponentInListL (TOpponentFormOpponent& aOpponent)
	{
	CTextListBoxModel* model = iOpponentsList->Model();  // not taking ownership
	model->SetOwnershipType (ELbmOwnsItemArray);
	CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
	
	TInt index = iOpponentsList->CurrentItemIndex();
	TPtrC item = (*itemArray)[index];
	item.Set (ListStringFromOpponentL(aOpponent));
	
	itemArray->Delete(index);
	itemArray->InsertL(index, item);
	iOpponentsList->DrawNow();
	}

/**
* Deletes the currently selected opponent in the array of iOpponents and updates the list of opponents
*/
void COpponentFormContainer::DeleteOpponentL()
	{
	
	CTextListBoxModel* model = iOpponentsList->Model();  // not taking ownership
	model->SetOwnershipType (ELbmOwnsItemArray);
	CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
	
	TInt index = iOpponentsList->CurrentItemIndex();
	
	iOpponents.Remove(index);
	itemArray->Delete(index);
	iOpponentsList->SetCurrentItemIndex(iOpponentsList->TopItemIndex());
	iOpponentsList->DrawNow();
	}

/**
* Returns the number of opponents currently in the list
*/
TInt COpponentFormContainer::NumberOfOpponents()
	{
	TInt numberOfOpponents = 0;
	
	if (iOpponentsList)
		numberOfOpponents = iOpponents.Count();
	
	return numberOfOpponents;
	}



// End of File	

⌨️ 快捷键说明

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