S60 3rd Edition SDK for Symbian OS Example Applications Guide

# Hello World Basic Example

1. About this Example
2. Prerequisites
3. Design and Implementation
4. Class Hierarchy

## 1. About this Example

The Hello World Basic example demonstrates how to create a simple interactive application on Symbian OS. Although the program described here only displays a simple string, it can be easily adapted to make much larger applications.

## 2. Prerequisites

This example requires a familiarity with the C++ programming language, and access to the Symbian OS documentation.

## 3. Design and Implementation

### 3.1 Capabilities

The program capabilities are defined in helloworldbasic.mmp file: CAPABILITY NONE.

### 3.2 ScalableUI

The program takes screen parameters in the start of the application and works with all screen modes. The program supports changing the screen layout while the program is running.

### 3.3 Static structure

All the classes are derived from CBase. CBase has a number of useful features: it initialises all member data to zero, it has a virtual destructor, and it implements support for the Symbian OS cleanup stack.

### 3.4 Launch sequence and command handling

This section explains the sequence of interactions that occur on the objects described in the last section when the application is run and the user selects the Hello menu option. The following sections describe the steps in more detail.

1 The application is launched, the framework loads the application EXE and the E32Main method is called.

2 The framework calls the function NewApplication. This creates an instance of the HelloWorldApplication class, and returns a pointer to it. The framework subsequently uses this pointer to complete construction of the application.

3 The framework calls AppDllUid to get the UID of this particular application. The UID is used to, for example, see if an instance of the application is already running, so than the existing instance can be switched to, rather than a new instance created.

4 The framework calls CreateDocumentL on the CHelloWorldApplication object. This creates an instance of the application Document and returns a pointer to it. This allows the framework to call certain Document methods directly. The Document object is constructed using the Symbian OS construction sequence, using the Document object's static method NewL.

5 The framework calls AppDllUid, to see if it is necessary to load a file from the filing system. The HelloWorld example does not need to persist any information, and so does not have a file associated with it.

6 The framework calls the Document object's CreateAppUiL method. This creates the AppUI object and returns a pointer to it.

7 The framework completes the construction of the AppUI object by calling its ConstructL method. The fact the framework calls the AppUi object's constructor and ConstructL separately allows the framework to complete any necessary initialisation before construction of the AppUI object is completed.

8 The ConstructL method first calls the base class's BaseConstructL. This performs such tasks as reading the resource file associated with an application.

9 The AppUi calls NewL to create the AppView object, using Symbian two phase construction.

10 The framework calls the Draw method is called by the framework to draw the view. In this case it will cause a blank screen to be displayed.

11 Whenever the user selects a menu option, HandleCommandL is called by the framework and is passed an argument, aCommand, that indicates the command the user selected. The AppUI can then perform whatever actions are required for that particular menu option:

11 a) In case of "Hello", it creates an Information note with the text "Hello World" in it, and displays it.

11 b) In case of "Hello from file", it reads text from Hello.txt -file and displays it in Information note.

#### 3.4.1 Creating the Application object

It is important to note that the NewApplication method is exported by the EXE. This is achieved by adding the EXPORT_C directive before this function in the HelloWorld.cpp file. If the EXPORT_C directive is missing, the code will fail to link.

The framework calls the function that is exported by the application at ordinal 1 in the DLL. This means that the first function defined in the DEF file for an application must be the NewApplication method. By default, the tool chain automatically ensures that this is the case for a Symbian OS application.

Note that this construction does not use the usual Symbian OS new (ELeave) method, but the C++ style new method. This is because a TRAP harness has not been created for the application at this point. If the system is unable to allocate memory for a new instance of the application, the returned instance pointer will be NULL. The framework checks for, and copes with, this possibility.

#### 3.4.2 Getting the application's UID

AppDllUid which gets the application's UID must be present, or the application will fail to link.

#### 3.4.3 Creating the Document object

The Application object is responsible for creating an instance of the Document object. However, it is not required to destroy it. Destruction of the Document object is performed by the framework.

#### 3.4.4 Creating the AppUI and View objects

Note that creation of the AppUi object does not use the Symbian OS two phase construction sequence. Instead the C++ constructor is called, and a pointer to the created instance is returned to the framework. The framework completes construction by calling ConstructL later. Ownership of the AppUI is passed to the framework, and so the Document object is not responsible for destroying it.

### 3.4.5 Redrawing the screen

The Draw method is called by the framework when the view needs to be redrawn, and contains all the code necessary to draw the view for this application. Note that the framework only calls the Draw method in response to general system events. So, for example, the framework automatically calls Draw when the application is launched, when the Options menu disappears, and if the application regains focus.

The programmer cannot call the Draw method directly, as it requires the system's graphics context to be activated before it is called. Instead, if the programmer wishes to have the screen redrawn, they should call the DrawNow method.

Note that the Draw function cannot leave. This is because the function may be called directly by the framework, and the framework would not know how to handle any errors that might occur. Instead the Draw function must TRAP and handle any errors that might occur.

#### 3.4.6 Handling commands

HandleCommandL is called on the AppUi object whenever the user selects a menu option. The argument aCommand indicates the command the user selected. HandleCommandL typically takes the form of a switch statement which invokes different actions according to the cvalue of aCommand.

#### 3.4.7 Handling command "Hello from file"

A RFileStream object is created from the file "hello.txt".

An HBufC descriptor is created from the RFileReadStream object.

The descriptor reads from the stream both the length of the data followed by the data. After this descriptor has been read the stream will be positioned at the beginning of the data.

Information note is shown from descriptor

### 3.5 Handling key events

If the application is required to respond to user keypresses, the AddToStackL method must be called by the AppUi object's ConstructL method, with the AppView object as an argument. This adds the AppView object to the control stack.

If the view is on the control stack, then whenever the user presses a key the OfferKeyEventL method is called. This has arguments that indicate the type of key event (key down, key press or key up) as well as the code of the key pressed.

When the AppUi object is destroyed, the AppView must be removed from the stack. This is done by calling RemoveFromStack in the AppUi object's destructor.

## 4. Class Hierarchy

This inheritance list is sorted roughly, but not completely, alphabetically:

- CHelloWorldBasicApplication
- CHelloWorldBasicAppUi
- CHelloWorldBasicAppView
- CHelloWorldBasicDocument

© Nokia 2006 |