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

📄 viewexview.cpp

📁 Symbian OS C++ 手机应用开发(第一卷)部分事例代码
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CViewExView from ViewExView.h
*  Part of  : ViewEx
*  Implementation notes:
*
*  Copyright (c) 2004 Symbian Software Ltd. All rights reserved.
* ============================================================================
*/

// INCLUDE FILES
#include <eikappui.h>
#include <eikenv.h>
#include <eiktxlbm.h>
#include <ViewEx.rsg>
#include "ViewExView.h" 

// CONSTANTS
const TUid KUidViewEx = { 0x0257696A };
const TUid KUidListView = { 0x01000000 };
const TUid KUidCustomView = { 0x01000001 };
const TUid KUidEditView = { 0x01000002 };

/*
	 CViewExTextListBox
	 The customised listbox class, overriding OfferKeyEventL to offer custom key handling
*/
class CViewExTextListBox : public CEikTextListBox
	{
public: //Construction
	CViewExTextListBox(CListView* aListView);
public:
	TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
private: // Data
	CListView* iListView;
	};

/*
	Constructor
*/
CViewExTextListBox::CViewExTextListBox(CListView* aListView)
	{
	iListView=aListView;
	}

/*
	Handles key events from the list box
*/
TKeyResponse CViewExTextListBox::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
	{
	if(aKeyEvent.iCode==EKeyOK)
		{
		TListItem item=Model()->ItemTextArray()->MdcaPoint(CurrentItemIndex());
		iListView->ItemSelected(item);
		return EKeyWasConsumed;
		}
	// If the key is not the OK key, then handle the event in the default way
	return CEikTextListBox::OfferKeyEventL(aKeyEvent, aType);
	}


// CNamesListArray
// A very simple implementation of the MDesCArray interface required by Text Listboxes.

class CNamesListArray : public CBase, public MDesCArray
	{
public: // From MDesCArray
	void ConstructL();
	~CNamesListArray();
	TInt MdcaCount() const;
	TPtrC16 MdcaPoint(TInt aIndex) const;
private:
	CArrayFixFlat<TListItem> *iArray;
	};

void CNamesListArray::ConstructL()
	{
	iArray=new(ELeave)CArrayFixFlat<TListItem>(5);
	iArray->AppendL(_L("This"));
	iArray->AppendL(_L("Is"));
	iArray->AppendL(_L("An"));
	iArray->AppendL(_L("Example"));
	iArray->AppendL(_L("Listbox"));
	}

CNamesListArray::~CNamesListArray()
	{
	delete iArray;
	}

TInt CNamesListArray::MdcaCount() const
	{
	return 5;
	}

TPtrC16 CNamesListArray::MdcaPoint(TInt aIndex) const
	{
	return iArray->At(aIndex);
	}

// CListView

CListView::~CListView()
	{
	iEikonEnv->EikAppUi()->DeregisterView(*this);
	delete iListBox;
	}

/*
	Perform one-time initialisation of this view
*/
void CListView::ViewConstructL()
	{
	iListBox=new (ELeave) CViewExTextListBox(this);
	iListBox->ConstructL(NULL);

	// Construct a list of array items.
	CNamesListArray* array=new (ELeave) CNamesListArray;
	array->ConstructL();

	// Assign this array to the list box and transfer ownership
	iListBox->Model()->SetItemTextArray(array);
	iListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);

	// Set the list box size and current item
	SetRect(iEikonEnv->EikAppUi()->ClientRect());
	iListBox->SetRect(iEikonEnv->EikAppUi()->ClientRect());
	iListBox->SetCurrentItemIndex(3);
	}

/*
	Handle activation of this view
*/
void CListView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/)
	{
	iEikonEnv->AddDialogLikeControlToStackL(iListBox);
	iListBox->ActivateL();
	iListBox->MakeVisible(ETrue);
	iListBox->DrawNow();
	}

/*
	Handle deactivation of this view
*/
void CListView::ViewDeactivated()
	{
	iListBox->MakeVisible(EFalse);
	iEikonEnv->RemoveFromStack(iListBox);
	MakeVisible(EFalse);
	}

/*
	Return the UID for this view
*/
TVwsViewId CListView::ViewId() const
	{
	TVwsViewId KListViewId(KUidViewEx, KUidListView );
	return KListViewId;
	}

/*
	Handle the selection of an item in the list box
*/
void CListView::ItemSelected(TListItem aItem)
	{
	TVwsViewId KCustomViewId(KUidViewEx, KUidCustomView );
	TPckgBuf<TListItem> buffer(aItem);
	// Activate the custom view
	iEikonEnv->EikAppUi()->ActivateViewL(KCustomViewId, KUidCustomView, buffer);
	}

// CCustomView

/*
	Deregister this view upon destruction
*/
CCustomView::~CCustomView()
	{
	iEikonEnv->EikAppUi()->DeregisterView(*this);
	}

/* 
	Perform one-time initialisation of this view
*/
void CCustomView::ViewConstructL()
	{
	CreateWindowL();
	SetRect(iEikonEnv->EikAppUi()->ClientRect());
	iEikonEnv->EikAppUi()->AddToStackL(*this,this);
	ActivateL();
	}

/* 
	Handle activation of this view
*/
void CCustomView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/, const TDesC8& aCustomMessage)
	{
	// Copy the list box string from the custom message buffer
	TPckgBuf<TListItem> buffer;
	buffer.Copy(aCustomMessage);
	iName=buffer();
	MakeVisible(ETrue);
	DrawNow();
	}

/*
	Handle deactivation of this view
*/
void CCustomView::ViewDeactivated()
	{
	MakeVisible(EFalse);
	}

/*
	Return this view's UID
*/
TVwsViewId CCustomView::ViewId() const
	{
    TVwsViewId KCustomViewId(KUidViewEx, KUidCustomView );
	return KCustomViewId;
	}


/*
	Draw the custom graphics for this view
*/
void CCustomView::Draw(const TRect& /*aRect*/) const
	{
	CGraphicsContext& gc=SystemGc();
	gc.SetPenStyle(CGraphicsContext::ENullPen);
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.DrawRect(Rect());

	gc.SetPenStyle(CGraphicsContext::ESolidPen);

	gc.SetPenColor(KRgbRed);
	gc.SetBrushColor(KRgbRed);
	gc.DrawRect(TRect(TPoint(50,30), TSize(50,50)));

	gc.SetPenColor(KRgbRed);
	gc.SetBrushColor(KRgbRed);
	gc.DrawRect(TRect(TPoint(50,30), TSize(50,50)));

	gc.SetPenColor(KRgbGreen);
	gc.SetBrushColor(KRgbGreen);
	gc.DrawRoundRect(TRect(TPoint(100,70), TSize(50,50)), TSize(15,15));

	gc.SetPenColor(KRgbBlue);
	gc.SetBrushColor(KRgbBlue);
	gc.DrawEllipse(TRect(TPoint(10,90), TSize(50,50)));

	gc.UseFont(iCoeEnv->NormalFont());
	gc.SetPenColor(KRgbBlack);
	gc.DrawText(_L("Received:"), TPoint(10, 30));
	gc.DrawText(iName, TPoint(10,60));
	gc.DiscardFont();

	}

/*
	Handle key input for this view
*/
TKeyResponse CCustomView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
	{
	if(aKeyEvent.iCode==EKeyOK)
		{
		TVwsViewId KEditViewId(KUidViewEx, KUidEditView );
		TPckgBuf<TListItem> buffer(iName);
		iEikonEnv->EikAppUi()->ActivateViewL(KEditViewId, KUidEditView, buffer); // Activate the edit view
		return EKeyWasConsumed;
		}
	return EKeyWasNotConsumed;
	}

// CEditView

/*
	Deregister this view and destroy the list box upon destruction
*/
CEditView::~CEditView()
	{
	iEikonEnv->EikAppUi()->DeregisterView(*this);
	delete iListBox;
	}

/*
	Perform one-time initialisation of this view
*/
void CEditView::ViewConstructL()
	{
	CListView::ViewConstructL();
	CEikListBoxTextEditor* editor = new (ELeave) CEikListBoxTextEditor(iListBox->Model());
	editor->SetListBoxEditorObserver(this);
	iListBox->SetItemEditor(editor);
	}

/*
	Handle activation of this view
*/
void CEditView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/)
	{
	iEikonEnv->AddDialogLikeControlToStackL(iListBox);
	iListBox->ActivateL();
	iListBox->MakeVisible(ETrue);
	iListBox->DrawNow();
	if(!iListBox->ItemEditor())
		{
		// Create the editor and prepare to start editing
		CEikListBoxTextEditor* editor = new (ELeave) CEikListBoxTextEditor(iListBox->Model());
		editor->SetListBoxEditorObserver(this);
		iListBox->SetItemEditor(editor);	
		}
	iListBox->ItemEditor()->StartEditingL(*iListBox,iListBox->Rect(),3,15);
	}

/* 
	Stop editing upon deactivation of the view
*/
void CEditView::ViewDeactivated()
	{
	iListBox->StopEditingL(EFalse);
	iListBox->MakeVisible(EFalse);
	iEikonEnv->RemoveFromStack(iListBox);
	MakeVisible(EFalse);
	}

/*
	Return the view's UID
*/
TVwsViewId CEditView::ViewId() const
	{
    TVwsViewId KEditViewId(KUidViewEx, KUidEditView );
	return KEditViewId;
	}

/*
	Observe events from the list box, and stop editing when Enter or OK is pressed
*/
TKeyResponse CEditView::HandleListBoxEditorEventL(MEikListBoxEditor* /*aListBoxEditor*/, const TKeyEvent& aKeyEvent)
	{
	if(aKeyEvent.iCode==13||aKeyEvent.iCode==EKeyOK)
		{
		iListBox->StopEditingL(EFalse);
		return EKeyWasConsumed;
		}
	return EKeyWasNotConsumed;
	}

// End of File

⌨️ 快捷键说明

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