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

📄 stackcontainer.cpp

📁 Symbian 控件的使用
💻 CPP
字号:
/*
========================================================================
 Name        : StackContainer.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <aknviewappui.h>
#include <eikappui.h>
#include <Stack.rsg>
#include <aknutils.h> 
#include <eiklabel.h>
#include <eikedwin.h>
#include <eiktxlbx.h>
#include <aknlists.h>
#include <barsread.h>

// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]
#include "StackContainer.h"
#include "StackContainerView.h"
#include "Stack.hrh"
// ]]] end generated region [Generated User Includes]

#define PUSH_LABEL_POS TPoint(5,5)
#define POP_LABEL_POS TPoint(5,35)
#define POP_CONTENT_POS TPoint(88,35)
#define PUSH_EDIT_POS TPoint(88,5)
#define LIST_POS TPoint(0,60)
#define LISTBOX_SIZE TSize(250,150)

const TInt KColorGreen=185;

#define TEXT_SIZE TSize(60,20)
// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CStackContainer::CStackContainer()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]

	iPushLabel=NULL;

	iPopLabel=NULL;

	iPopContentLabel=NULL;

	iPushNumber=NULL;

	iListBox=NULL;
	}
/** 
 * Destroy child controls.
 */
CStackContainer::~CStackContainer()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	delete iPushLabel;
	iPushLabel=NULL;
	delete iPopLabel;
	iPopLabel=NULL;
	delete iPopContentLabel;
	iPopContentLabel=NULL;
	delete iPushNumber;
	iPushNumber=NULL;
	delete iListBox;
	iListBox=NULL;
	}
				
/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CStackContainer
 */
CStackContainer* CStackContainer::NewL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CStackContainer* self = CStackContainer::NewLC( 
			aRect, 
			aParent, 
			aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CStackContainer
 */
CStackContainer* CStackContainer::NewLC( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CStackContainer* self = new ( ELeave ) CStackContainer();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}
			
/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */ 
void CStackContainer::ConstructL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	if ( aParent == NULL )
	    {
		CreateWindowL();
	    }
	else
	    {
	    SetContainerWindowL( *aParent );
	    }
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	SetRect( aRect );
	ActivateL();
	// [[[ begin generated region: do not modify [Post-ActivateL initializations]
	// ]]] end generated region [Post-ActivateL initializations]
	
	}
			
/**
* Return the number of controls in the container (override)
* @return count
*/
TInt CStackContainer::CountComponentControls() const
	{
	return ( int ) ELastControl;
	}
				
/**
* Get the control with the given index (override)
* @param aIndex Control index [0...n) (limited by #CountComponentControls)
* @return Pointer to control
*/
CCoeControl* CStackContainer::ComponentControl( TInt aIndex ) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	switch ( aIndex )
		{
		case EPushLabel:
			return iPushLabel;
			
		case EPopLabel:
			return iPopLabel;
			
		case EPopContentLabel:
			return iPopContentLabel;
		case EPushNumber:
			return iPushNumber;
			
		case EListBox:
			return iListBox;
		}
	// ]]] end generated region [Generated Contents]
	
	// handle any user controls here...
	
	return NULL;
	}
				
/**
 *	Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */				
void CStackContainer::SizeChanged()
	{
	CCoeControl::SizeChanged();
	LayoutControls();
	// [[[ begin generated region: do not modify [Generated Contents]
			
	// ]]] end generated region [Generated Contents]
	
	}
				
// [[[ begin generated function: do not modify
/**
 * Layout components as specified in the UI Designer
 */
void CStackContainer::LayoutControls()
	{
	    iPushLabel->SetExtent(PUSH_LABEL_POS,TEXT_SIZE);
	    iPopLabel->SetExtent(POP_LABEL_POS,TEXT_SIZE);
	    iPopContentLabel->SetExtent(POP_CONTENT_POS,TEXT_SIZE);
	    iPushNumber->SetExtent(PUSH_EDIT_POS,TEXT_SIZE);
	    iListBox->SetExtent(LIST_POS,LISTBOX_SIZE);
	}
// ]]] end generated function

/**
 *	Handle key events.
 */				
TKeyResponse CStackContainer::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	
	// ]]] end generated region [Generated Contents]
	//if(aKeyEvent.iCode==EKeyLeftSoft)
	if ( iFocusControl != NULL
		&& iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
		{
		return EKeyWasConsumed;
		}
	if (iPushNumber&&iPushNumber->IsFocused())
		{
			if(aKeyEvent.iCode==EKeyDownArrow)
				{
					iPushNumber->SetFocus(EFalse);
					iListBox->SetFocus(ETrue);
					return EKeyWasConsumed;
				
				}
			else 
				{
					return iPushNumber->OfferKeyEventL(aKeyEvent,aType);
				
				}	
		}
	else if(iListBox&&iListBox->IsFocused())
		{
			if(iListBox->CurrentItemIndex()==0&&aKeyEvent.iCode==EKeyUpArrow)
				{
					iPushNumber->SetFocus(ETrue);
					iListBox->SetFocus(EFalse);
					return EKeyWasConsumed;
				}
			else
				{
					return iListBox->OfferKeyEventL(aKeyEvent,aType);
				
				}
		
		}
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
	}
				
// [[[ begin generated function: do not modify
/**
 *	Initialize each control upon creation.
 */				
void CStackContainer::InitializeControlsL()
	{
	     iPushLabel=new(ELeave)CEikLabel;
	     iPushLabel->SetContainerWindowL(*this);
	     iPushLabel->SetTextL(_L("To Push"));
	     
	     iPopLabel=new(ELeave)CEikLabel;
	     iPopLabel->SetContainerWindowL(*this);
	     iPopLabel->SetTextL(_L("Poped"));
	     
	     iPopContentLabel=new(ELeave)CEikLabel;
	     iPopContentLabel->SetContainerWindowL(*this);
	     iPopContentLabel->SetTextL(_L("None"));
	     
	     TResourceReader reader;
	     iCoeEnv->CreateResourceReaderLC(reader,R_EDWINSS);
	     iPushNumber=new(ELeave)CEikEdwin;
	     iPushNumber->SetContainerWindowL(*this);
	     iPushNumber->ConstructFromResourceL(reader);
	     CleanupStack::PopAndDestroy();
	     iPushNumber->SetBorder(TGulBorder::ESingleBlack);
	     iPushNumber->SetFocus(ETrue);
	     
	     iListBox=new(ELeave)CAknSingleNumberStyleListBox;
	     iListBox->SetContainerWindowL(*this);
	     iListBox->ConstructL(this,0);
	     
	     CDesCArrayFlat* array=new(ELeave)CDesCArrayFlat(5);
	     CleanupStack::PushL(array);
	     array->AppendL(_L("\tBottom of Stack"));
	     CleanupStack::Pop();
	     iListBox->Model()->SetItemTextArray(array);
	     iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);
	     iListBox->SetBorder(TGulBorder::EShallowRaised);
	     iListBox->ActivateL();
	     iListBox->CreateScrollBarFrameL(ETrue);
	     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn,CEikScrollBarFrame::EAuto);
	}
// ]]] end generated function

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CStackContainer::HandleCommandL(TInt aComdaCommand)
	{
		switch(aComdaCommand)
			{
			case ECommand1:
				            TBuf<32> buf;
				            buf.Zero();
				            iPushNumber->GetText(buf);
				            if (buf.Length()==0)
				                {
				                iEikonEnv->InfoMsg( _L("Can't push empty text") );
				                }
				            else
				                {
				                buf.Insert(0, _L("\t"));
				                CDesCArray* array = static_cast<CDesCArray*>(iListBox->Model()->ItemTextArray());
				                array->InsertL(0,buf);
				                iListBox->HandleItemAdditionL();
				                buf.Zero();
				                iPushNumber->SetTextL(&buf);
				                }            
				            break;
			case ECommand2:
				            CDesCArray* array = static_cast<CDesCArray*>(iListBox->Model()->ItemTextArray());
				            if (array->Count()==1)
				                {
				                iEikonEnv->InfoMsg( _L("The stack is empty") );
				                }
				            else
				                {
				                TBuf<32> buf;
				                TPtrC ptr = (*array)[0];
				                buf.Copy(ptr);
				                buf.Delete(0,1);
				                iEikonEnv->InfoMsg(buf);
				                iPopContentLabel->SetTextL(buf);
				                array->Delete(0);
				                iListBox->HandleItemRemovalL();
				                }
				            break;
			default:
				break;
			
			}
	
	}
void CStackContainer::HandleResourceChange( TInt aType )
	{
	CCoeControl::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( EStackContainerViewId ) )->ClientRect() );
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
				
/**
 *	Draw container contents.
 */				
void CStackContainer::Draw( const TRect& aRect ) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CWindowGc& gc = SystemGc();
	//gc.Clear( aRect );
	gc.SetPenStyle(CGraphicsContext::ENullPen);
	TRgb colorGreen=AKN_LAF_COLOR(KColorGreen);
	gc.SetBrushColor(KRgbGreen);
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc.DrawRect(aRect);
	
	// ]]] end generated region [Generated Contents]
	
	}
				

⌨️ 快捷键说明

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