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

📄 exampleclientappview.cpp

📁 一个http客户端连接的程序例子
💻 CPP
字号:
/*
 * ============================================================================
 *  Name     : ExampleClientAppView.cpp
 *  Part of  : HTTP Example
 *  Created  : 11/14/2003 by Forum Nokia
 *  Implementation notes:
 *
 *     
 *  Version  : 1.0
 *  Copyright: Nokia Corporation
 * ============================================================================
 */

#include <coemain.h>
#include <eikenv.h>
#include <gdi.h>
#include <txtrich.h>
#include "ExampleClient.pan"
#include "ExampleClientAppView.h"
#include "ExampleClientEngine.h"

// Constants for CEikRichTextEditors
const TInt KNumberOfControls = 2;
const TInt KNumberOfLines = 0;
const TInt KTextLimit = 128;

const TInt KUpperEditHeight = 102;
const TInt KLowerEditHeight = 36;
#define KUpperEditPosition	TPoint(2, 2)
#define KLowerEditPosition	TPoint(2, 106)

// Background color
#define KBackgroundColor	TRgb(128, 128, 128)

// ----------------------------------------------------------------------------
// CExampleClientAppView::NewL()
//
// Creates instance of CExampleClientAppView.
// ----------------------------------------------------------------------------
CExampleClientAppView* CExampleClientAppView::NewL(const TRect& aRect)
	{
    CExampleClientAppView* self = CExampleClientAppView::NewLC(aRect);
    CleanupStack::Pop(self);
    return self;
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::NewLC()
//
// Creates instance of CExampleClientAppView.
// ----------------------------------------------------------------------------
CExampleClientAppView* CExampleClientAppView::NewLC(const TRect& aRect)
	{
    CExampleClientAppView* self = new (ELeave) CExampleClientAppView;
    CleanupStack::PushL(self);
    self->ConstructL(aRect);
    return self;
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::CExampleClientAppView()
//
// First phase construction.
// ----------------------------------------------------------------------------
CExampleClientAppView::CExampleClientAppView()
	{
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::~CExampleClientAppView()
//
// Destructor.
// ----------------------------------------------------------------------------
CExampleClientAppView::~CExampleClientAppView()
	{
    delete iOutputWindow;
    iOutputWindow = NULL;

	delete iStatusWindow;
    iStatusWindow = NULL;
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::ConstructL()
//
// Second phase construction.
// ----------------------------------------------------------------------------
void CExampleClientAppView::ConstructL(const TRect& aRect)
	{
    // Create a window for this application view
    CreateWindowL();

	// Create output window
	iOutputWindow = new (ELeave) CEikRichTextEditor();
	iOutputWindow->SetContainerWindowL(*this);
	iOutputWindow->ConstructL(this, KNumberOfLines, KTextLimit, 
		EEikEdwinReadOnly, EGulFontControlAll, EGulNoSymbolFonts);
	iOutputWindow->SetExtent(KUpperEditPosition, 
		TSize(aRect.Width() - 4, KUpperEditHeight));

	// Create status window
	iStatusWindow = new (ELeave) CEikRichTextEditor();
	iStatusWindow->SetContainerWindowL(*this);
	iStatusWindow->ConstructL(this, KNumberOfLines, KTextLimit, 
		EEikEdwinReadOnly|EEikEdwinNoWrap, 
		EGulFontControlAll, EGulNoSymbolFonts);
	iStatusWindow->SetExtent(KLowerEditPosition, 
		TSize(aRect.Width() - 4, KLowerEditHeight));

	iStatusWindow->SetFocus(ETrue);

    // Set the windows size
    SetRect(aRect);

    // Activate the window, which makes it ready to be drawn
    ActivateL();
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::Draw()
// 
// Draw this application's view to the screen
// ----------------------------------------------------------------------------
void CExampleClientAppView::Draw(const TRect& aRect) const
	{
    CWindowGc& gc = SystemGc();

    gc.SetPenStyle( CGraphicsContext::ENullPen );
    gc.SetBrushColor( KBackgroundColor );
    gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
    gc.DrawRect( aRect );
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::CountComponentControls()
//
// Returns number of controls in this compound control.
// ----------------------------------------------------------------------------
TInt CExampleClientAppView::CountComponentControls() const
	{
    return KNumberOfControls;
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::ComponentControl()
//
// Returns pointer to control with index aIndex.
// ----------------------------------------------------------------------------
CCoeControl* CExampleClientAppView::ComponentControl(TInt aIndex) const
	{
    switch(aIndex) 
		{
		case 0:
			return iOutputWindow;
		case 1:
			return iStatusWindow;
		default:
			Panic(EClientView);
			return 0;
		}
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::HandleControlEventL()
//
// Handles control events.
// ----------------------------------------------------------------------------
void CExampleClientAppView::HandleControlEventL(CCoeControl* /* aControl */, 
											 TCoeEvent /* aEventType */ )
	{
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::Reset()
//
// Resets contents of output and status window.
// ----------------------------------------------------------------------------
void CExampleClientAppView::Reset()
	{
    iOutputWindow->Text()->Reset();
    iOutputWindow->HandleTextChangedL();
    iOutputWindow->SetCursorPosL(0, EFalse);

    iStatusWindow->Text()->Reset();
    iStatusWindow->HandleTextChangedL();
    iStatusWindow->SetCursorPosL(0, EFalse);
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::OfferKeyEventL()
//
// Handles key events.
// ----------------------------------------------------------------------------
TKeyResponse CExampleClientAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, 
												TEventCode aType)
	{

	// Catch EStdKeyNkp5 and EStdKeyDevice3; they are used here to switch
	// the active CEikRichTextEditor.
	if(aType == EEventKey) 
		{
		switch(aKeyEvent.iScanCode) 
		{
		case EStdKeyNkp5:
		case EStdKeyDevice3:
			if (iOutputWindow->IsFocused()) 
				{
					iOutputWindow->SetFocus(EFalse);
					iStatusWindow->SetFocus(ETrue);
				} else {
					iStatusWindow->SetFocus(EFalse);
					iOutputWindow->SetFocus(ETrue);
				}
			return EKeyWasConsumed;
		}	
		}

	// Redirect keyevents to controls
    if (iOutputWindow) 
		{
	    if (iOutputWindow->IsFocused())
			return iOutputWindow->OfferKeyEventL(aKeyEvent, aType);
		}

    if (iStatusWindow) 
		{
	    if (iStatusWindow->IsFocused())
			return iStatusWindow->OfferKeyEventL(aKeyEvent, aType);
		}

    return EKeyWasNotConsumed;
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::AddToStatusWindowL()
//
// Add a single line of text to the status window.
// ----------------------------------------------------------------------------
void CExampleClientAppView::AddToStatusWindowL(const TDesC& aText)
	{
	// Append aText to status window with line break
	CRichText* text = iStatusWindow->RichText();
	text->InsertL(text->DocumentLength(), aText);
	text->InsertL(text->DocumentLength(), CEditableText::ELineBreak);
	iStatusWindow->HandleTextChangedL();
	iStatusWindow->MoveCursorL(TCursorPosition::EFLineDown, EFalse);
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::AddToOutputWindowL()
//
// Add text to outputwindow. No formatting is provided.
// ----------------------------------------------------------------------------
void CExampleClientAppView::AddToOutputWindowL(const TDesC8& aText)
	{
	// Convert 8-bit aText to 16-bit and append it to end of output window
	HBufC* tempBuf = HBufC::NewL(aText.Length());
	CleanupStack::PushL(tempBuf);
	tempBuf->Des().Copy(aText);
	CRichText* text = iOutputWindow->RichText();
	text->InsertL(text->DocumentLength(), *tempBuf);
	iOutputWindow->HandleTextChangedL();

	CleanupStack::PopAndDestroy(); // tempBuf
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::ClientEvent()
//
// Called by CClientEngine to notify events to user
// ----------------------------------------------------------------------------
void CExampleClientAppView::ClientEvent(const TDesC& aEventDescription) 
	{
	TRAPD(err, AddToStatusWindowL(aEventDescription));
	if(err)
		Panic(EClientView);
	}

// ----------------------------------------------------------------------------
// CExampleClientAppView::ClientBodyReceived()
//
// Called by CClientEngine when part of response body received
// ----------------------------------------------------------------------------
void CExampleClientAppView::ClientBodyReceived(const TDesC8& aBodyData) 
	{
	TRAPD(err, AddToOutputWindowL(aBodyData));
	if(err)
		Panic(EClientView);
	}

⌨️ 快捷键说明

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