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

📄 hellocontainer.cpp

📁 symbian电话簿引擎的经典例子
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CHelloContainer from CCoeControl, MCoeControlObserver
*  Part of  : Hello
*  Copyright (c) 2003 Nokia. All rights reserved.
* ============================================================================
*/

// INCLUDE FILES
#include "BookEngine.h"

#include <LDN02.rsg>
#include "LDN02.hrh"
#include "HelloContainer.h"
#include <eiklabel.h>  // for example label control
#include <barsread.h>  // for resource reader
#include <eikedwin.h>  // for CEikEdwin
#include <eikgted.h>   // for CEikGlobalTextEditor
#include <aknselectionlist.h>	//for CAknSelectionListDialog
#include <AknQueryDialog.h>		//for CAknQueryDialog
#include <eikmenub.h>
#include <aknlists.h>		//for CAknSingleNumberStyleListBox
#include <StringLoader.h>	//for StringLoader
#include "common.h"

// ================= MEMBER FUNCTIONS =======================

// C++ default constructor can NOT contain any code, that
// might leave.
//
CHelloContainer::CHelloContainer()
    {
	m_pListBox = NULL;
	m_pSearchBox = NULL;
	m_pListBoxArray = NULL;
    }

// EPOC default constructor can leave.
void CHelloContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();

    SetRect(aRect);
	MakeListBoxL();
    ActivateL();
    }

// Destructor
CHelloContainer::~CHelloContainer()
    {
	if (m_pListBox)
	{
		delete m_pListBox;
		m_pListBox = NULL;
	}
	if (m_pSearchBox)
	{
		delete m_pSearchBox;
		m_pSearchBox = NULL;
	}
	if (m_pListBoxArray)
	{
		delete m_pListBoxArray;
		m_pListBoxArray = NULL;
	}
    }

// ---------------------------------------------------------
// CHelloContainer::FocusTo(TInt aCommand)
// Change foccused control.
// (other items were commented in a header).
// ---------------------------------------------------------
//

void CHelloContainer::FocusTo(TInt aCommand)
    {
    }

// ---------------------------------------------------------
// CHelloContainer::SizeChanged()
// Called by framework when the view size is changed
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CHelloContainer::SizeChanged()
    {
	if (m_pListBox)
    {
        if (m_pSearchBox)
        {
			CAknSingleNumberStyleListBox* aknListBox = STATIC_CAST(CAknSingleNumberStyleListBox*,
				m_pListBox);
			AknFind::HandleFixedFindSizeChanged(this, aknListBox, m_pSearchBox);			
        }
        else
        {
            m_pListBox->SetRect(Rect()); // Sets rectangle of lstbox.
		}
    }
    }

// ---------------------------------------------------------
// CHelloContainer::CountComponentControls() const
// (other items were commented in a header).
// ---------------------------------------------------------
//
TInt CHelloContainer::CountComponentControls() const
    {
    if(m_pListBox && m_pSearchBox)
	{
		return 2;
	}
	else
	{
		return 0;
	} 
    }

// ---------------------------------------------------------
// CHelloContainer::ComponentControl(TInt aIndex) const
// (other items were commented in a header).
// ---------------------------------------------------------
//
CCoeControl* CHelloContainer::ComponentControl(TInt aIndex) const
    {
		if(m_pSearchBox && aIndex)
		{
			return m_pSearchBox;
		}	
		else
		{
			return m_pListBox;
		}	
    }

// ---------------------------------------------------------
// CHelloContainer::Draw(const TRect& aRect) const
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CHelloContainer::Draw(const TRect& aRect) const
    {
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushColor(KRgbGray);
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.DrawRect(aRect);


/*	const CFont* font = CEikonEnv::Static()->TitleFont();
	gc.UseFont(font);
	gc.Clear();
	gc.SetPenStyle(CGraphicsContext::ESolidPen);
	gc.SetPenColor(KRgbRed);
	gc.DrawText(_L("Welcome to you!"),TPoint(20,60));
	gc.DiscardFont();*/
    }

// ---------------------------------------------------------
// CHelloContainer::OfferKeyEventL(...)
// Notify key events to editors.
// (other items were commented in a header).
// ---------------------------------------------------------
//
TKeyResponse CHelloContainer::OfferKeyEventL(
    const TKeyEvent& aKeyEvent, TEventCode aType)
	{
		TKeyResponse Ret = EKeyWasNotConsumed;	
		
		switch (aKeyEvent.iCode)
		{
		case EKeyDevice3:
			break;
		default:
			if(m_pListBox)
			{
				if ( m_pSearchBox )
				{
					TBool needRefresh( EFalse );
					
					// Offers the key event to find box.
					if ( AknFind::HandleFindOfferKeyEventL( aKeyEvent, aType, this,
						m_pListBox, m_pSearchBox,
						EFalse,
						needRefresh ) ==
						EKeyWasConsumed )
					{
						if ( needRefresh )
						{
							SizeChanged();
							DrawNow();
						}
						
						return EKeyWasConsumed;
					}
				}
            
				Ret = m_pListBox->OfferKeyEventL(aKeyEvent, aType);
			}
			break;
		}
			
		return Ret;
    }


// ---------------------------------------------------------
// CHelloContainer::HandleControlEventL(
//     CCoeControl* aControl,TCoeEvent aEventType)
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CHelloContainer::HandleControlEventL(
    CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
    {
    }

//
TInt CHelloContainer::GetSelectedIndexL()	
{
	TInt Ret(-1);
	if(m_pListBox)
	{
		TInt CurrItemInd = m_pListBox->CurrentItemIndex();
		CAknFilteredTextListBoxModel* model =
			STATIC_CAST(CAknFilteredTextListBoxModel*,m_pListBox->Model());		
		if(model && CurrItemInd >= 0)
		{	
			Ret = model->Filter()->FilteredItemIndex(CurrItemInd);	//It is used to fetch the content of a list item after filter has been used
		}				//Tests if cache cleanup should be performed on a message entry.
	}
	return Ret;
}




void CHelloContainer::HandleResourceChange(TInt aType)
{
	TRect rect;
	
    if ( aType==KEikDynamicLayoutVariantSwitch )
    {   
		CWsScreenDevice* ws = CEikonEnv::Static()->ScreenDevice(); 
		rect = ws->SizeInPixels();
        SetRect(rect);
    }
	CCoeControl::HandleResourceChange(aType);
}


void CHelloContainer::UpdateScrollBar(CAknSingleNumberStyleListBox* aListBox)
{
    if (aListBox)
	{   
        TInt pos(aListBox->View()->CurrentItemIndex());
        if (aListBox->ScrollBarFrame())
		{
            aListBox->ScrollBarFrame()->MoveVertThumbTo(pos);
		}
	}
}


CAknSearchField* CHelloContainer::CreateFindBoxL(CAknSingleNumberStyleListBox* aListBox,
													  CTextListBoxModel* aModel, CAknSearchField::TSearchFieldStyle aStyle)
{
    CAknSearchField* findbox = NULL;
	
    if (aListBox && aModel)
	{
        // Gets pointer of CAknFilteredTextListBoxModel.
        CAknFilteredTextListBoxModel* model =
			STATIC_CAST( CAknFilteredTextListBoxModel*, aModel );
        // Creates FindBox.
        findbox = CAknSearchField::NewL( *this, aStyle, NULL, 20);
        CleanupStack::PushL(findbox);
        // Creates CAknListBoxFilterItems class.
        model->CreateFilterL( aListBox, findbox );
        //Filter can get by model->Filter();
        CleanupStack::Pop(findbox); // findbox
	}
	
    return findbox;
}


void CHelloContainer::MakeListBoxL()
{
	TInt MySetIndex(0);
	
	if(m_pListBox)
	{
		MySetIndex = GetSelectedIndexL();
	}

    m_pListBox   = new( ELeave ) CAknSingleNumberStyleListBox;
	m_pListBox->ConstructL(this,EAknListBoxSelectionList);

	//无名片时显示(无名片)
	ShowNoCard();
	
	m_pListBoxArray = new (ELeave) CDesCArrayFlat (10);

	// get Model
	CTextListBoxModel* model = m_pListBox->Model();
	// setItemTextArray
	model->SetItemTextArray( m_pListBoxArray );
	// setOwnershipType NotOwn
	model->SetOwnershipType(ELbmDoesNotOwnItemArray);
	
	
	m_pListBox->CreateScrollBarFrameL( ETrue );
    m_pListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
		CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto );
	
	m_pListBox->SetRect(Rect());
	
	m_pSearchBox = CreateFindBoxL(m_pListBox, m_pListBox->Model(),
		CAknSearchField::ESearch);
	SizeChanged();
	
	m_pListBox->ActivateL();
	
	TInt ItemsCount = m_pListBox->Model()->ItemTextArray()->MdcaCount();
	
	if(ItemsCount > MySetIndex && MySetIndex >= 0)
		m_pListBox->SetCurrentItemIndex(MySetIndex);
	else if(ItemsCount > 0)
		m_pListBox->SetCurrentItemIndex(0);
	
	UpdateScrollBar(m_pListBox);
}


void CHelloContainer::SetItemToListBox (RPointerArray<CBookInfo>& aBookInfo)
{
// 	m_pListBox->DrawNow();
	
	m_pListBoxArray->Reset();
	TInt i = 0;		
	for (i = 0; i < aBookInfo.Count(); i++)
	{
		TBuf<KMaxPath> szTemp;
		szTemp.Copy(_L("\t"));
		HBufC * sNameBuf = NULL;

		sNameBuf = aBookInfo[i]->GetLastName();
		// 判断是否为空
		if (sNameBuf != NULL)
		{
			szTemp.Append(sNameBuf->Des());
		}
		sNameBuf = aBookInfo[i]->GetFirstName();
		if (sNameBuf != NULL)
		{
			szTemp.Append(sNameBuf->Des());
		}
		if ((aBookInfo[i]->GetLastName()==NULL) && (aBookInfo[i]->GetFirstName()==NULL))	
		{
			szTemp.Copy(_L("\t"));
			TBuf<32> szNoName;
			StringLoader::Load(szNoName, R_QTN_IM_PBMAIN_NAME);
			szTemp.Append(szNoName);
		}

//		iEikonEnv->InfoMsg(szTemp);
		m_pListBoxArray->AppendL(szTemp);
	}
		m_pListBox->HandleItemAdditionL();
	
}



void CHelloContainer::GetDeleteResult (TInt & anResult)
{
	// querDialog
	CAknQueryDialog* pdialog = CAknQueryDialog::NewL(CAknQueryDialog::ENoTone);

	TBuf <32>szTemp;
	StringLoader::Load(szTemp, R_QTN_IM_PBDELETE_NAME);
	
	szTemp.Append(_L("\n"));
	
	TBuf <KMaxName> sInfo;
	sInfo.Copy(m_pListBox->Model()->ItemText(m_pListBox->CurrentItemIndex()));
	sInfo.TrimLeft();
	szTemp.Append(sInfo);
	szTemp.Append(_L("?"));
	pdialog->SetPromptL(szTemp);
	anResult = pdialog->ExecuteLD( R_QTN_PROMPT_DIALOG);
}


void CHelloContainer::ShowNoCard()
{
	//无名片时显示(无名片)
	TBuf<32> szNoCard;
	StringLoader::Load(szNoCard, R_QTN_IM_PBMAIN_PB);
	m_pListBox->View()->SetListEmptyTextL(szNoCard);
}

// End of File  

⌨️ 快捷键说明

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