03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

hellogui's application view is a control whose sole purpose is to draw the text 'Hello<br />

world!' on the screen in a reasonably pleasing manner. It doesn't handle key or pointer<br />

events.<br />

Like all controls, CHelloGuiAppView is derived from CCoeControl, which has virtual<br />

functions that you override to implement a particular control's functionality. In this case, the<br />

only function of interest is Draw(). The definition, from HelloGui_AppView.h is as<br />

follows:<br />

class CHelloGuiAppView : public CCoeControl<br />

{<br />

public:<br />

static CHelloGuiAppView* NewL(const TRect& aRect);<br />

~CHelloGuiAppView();<br />

void ConstructL(const TRect& aRect);<br />

private:<br />

void Draw(const TRect& /* aRect */) const;<br />

private:<br />

HBufC* iHelloText;<br />

};<br />

Let's start with the implementation of the second-phase constructor:<br />

CHelloGuiAppView* CHelloGuiAppView::NewL(const TRect& aRect)<br />

{<br />

CHelloGuiAppView * self = new(ELeave) CHelloGuiAppView;<br />

CleanupStack::PushL(self);<br />

self->ConstructL(aRect);<br />

CleanupStack::Pop();<br />

return self;<br />

}<br />

void CHelloGuiAppView::ConstructL(const TRect& aRect)<br />

{<br />

CreateWindowL();<br />

SetRect(aRect);<br />

ActivateL();<br />

// Fetch the text from the resource file.<br />

iHelloText = iEikonEnv->AllocReadResourceL(R_HELLOGUI_TEXT_HELLO);<br />

}<br />

ConstructL() uses CCoeControl() base-class library functions to create a window, set<br />

it to the rectangle offered, and activate it. It then reads the 'Hello World!' text from a resource<br />

file into an HBufC, which is allocated the appropriate length. This is a common pattern in<br />

application programming. The memory for the HBufC is, of course deleted, in the destructor:<br />

CHelloGuiAppView::~CHelloGuiAppView()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!