PrevNextUpHome SophiaFramework UNIVERSE 5.3

4.4. Implementing the Application Class

The application class of an SFC applet inherits directly from SFCApplication.

In the application class of the SFC applet, the XANDLER_DECLARE_..... macro is not used to declare a handler function. Instead a handler function is declared in the same way with other normal functions like the OnKey() function below.

Example 4.2. Define the helloworld application class

//
//      helloworld.hpp
//

#ifndef __HELLOWORLD_HPP
#define __HELLOWORLD_HPP

#define TARGET_BUILD_DEBUG
#include <SophiaFramework.hpp>
#include "helloworld.bid"

//
//  helloworld application class
//
SFMTYPEDEFCLASS(helloworld)  //  macro to generate useful types

class helloworld : public SFCApplication {

    SFMSEALCOPY(helloworld)    //  macro to prohibit developer from copying this instance

public:

    static SFCInvokerPtr Factory(Void);  // factory function

private:

    helloworld(Void);
    virtual ~helloworld(Void);

    virtual Bool HandleEvent(SFXEventConstRef event);  // event handler
    virtual Bool HandleRender(SFXEventConstRef event); // device screen drawing handler
    Bool OnKey(UInt16 key);                            // key handler
};

#endif // __HELLOWORLD_HPP //
[Tip] HandleEvent() function

The HandleEvent() function is the event handler of the SFC applet. This function will be automatically called when the applet receives an event dispatched from the BREW environment(BREW-defined event).

In the SFC applet development, event handling is described by overriding the SFCApplication::HandleEvent virtual function.

The HandleEvent() function has an argument which represents an event dispatched from the BREW environment, and boots up an appropriate handle function according to the event type.

For instance, in the implementation of the helloworld applet below, the SFCApplication::HandleEvent virtual function is overridden to boot up the OnKey() function, key handler when the applet receives the key event.

For more details, see the description of the SFCApplication::HandleEvent function.

[Tip] HandleRender() function

The HandleRender() function is the device screen drawing handler of the SFC applet. This function will be automatically called when an applet starts / resumes or a highest priority event handler ends.

In the SFC applet development, device screen drawing necessary when an applet starts / resumes or a highest priority event handler ends is described by overriding the SFCApplication::HandleRender virtual function.

For instance, in the implementation of the helloworld applet below, the SFCApplication::HandleRender virtual function is overridden to fill the screen in white and draw the "Hello World" string in black when an applet starts or resumes.

For more details, see the description of the SFCApplication::HandleRender function.

[Note] Highest Priority Event Handler

For more details, see the description of the SFCApplication::RegisterBypass function.

[Note] Drawing processing

Drawing processing other than device screen redrawing can be described anywhere in the program like the Draw() function mentioned later.

This is the same with the standard BREW applet development.

[Note] SFY applet

In case of an SFY applet which uses the GUI framework, its application class inherits directly from SFYApplication.

Event Handler

The HandleEvent() function is the event handler for SFCApplication. It receives all events dispatched from the BREW environment, and distributes each event to appropriate handler functions.

The event is transmitted through the argument of the HandleEvent() function, and sent to appropriate handler functions according to its event type. For instance, a key event boots up the key handler.

Implement the HandleEvent() Function for Handling an Event

The template of the HandleEvent() function for handling an event dispatched from the BREW environment is automatically generated by the SophiaFramework UNIVERSE AppWizard.

Example 4.3. Implement the HandleEvent() function

// event handler
Bool helloworld::HandleEvent(SFXEventConstRef event)
{
    // here describe the handling of an event dispatched from the BREW environment

    Bool result(false);

    switch (event.GetType()) {

        case SFEVT_KEY: 

            result = OnKey(event.GetP16());

            break;
    }

    return result;  // return "true" if the event has been handled, otherwise return "false"
}
[Note] HandleEvent() function

The HandleEvent() function will be automatically called when the applet receives an event dispatched from the BREW environment.

In the above example, the SFCApplication::HandleEvent virtual function is overridden to call the key handler of the OnKey() function when a key event dispatched from the BREW environment is received.

[Note] Return value of the HandleEvent() function

The HandleEvent() function will return "true" if it handles an event. Otherwise it will return "false".

Implement the HandleRender() Function for Redrawing the Device Screen

The template of the HandleRender() function for redrawing the device screen is automatically generated by the SophiaFramework UNIVERSE AppWizard.

Example 4.4. Implement the HandleRender() function

// device screen redrawing handler
Bool helloworld::HandleRender(SFXEventConstRef event)
{
    // here describe full screen redrawing when an applet starts / resumes or a highest priority event handler ends

    static SFXRGBColor::AtomRecConst            theme = {
        {{0x00, 0xFF, 0xFF, 0xFF}}
    };
    SFXGraphicsPtr graphics;
    Bool           result(false);

    // get the SFXGraphics instance
    if ((graphics = SFXGraphics::GetInstance()) != null) {

        // fill the device screen in white
        // get the device screen by calling the  SFXGraphics::GetDeviceRectangle() 
        graphics->ClearRectangle(SFXGraphics::GetDeviceRectangle(), theme);

        // draw "Hello World" in the device screen
        graphics->DrawSingleText("Hello World", SFXGraphics::GetDeviceRectangle(), SFXRGBColor(0x00, 0x00, 0x00, 0x00));

        // call the SFXGraphics::Update() to update the device screen
        // * screen will not be redrawn or updated if line below is NOT described
        graphics->Update();

        result = true;  // set result to "true" since the device screen is redrawn
    }
    return result; // return "true" if the device screen has been redrawn, otherwise return "false"
}

Reference: SFCApplication::HandleRender | SFCApplication::HandleError | SFXGraphics | SFXGraphics::GetInstance | SFXGraphics::GetDeviceRectangle | SFXGraphics::ClearRectangle | SFXGraphics::Update

[Note] HandleRender() function

The device screen has to be redrawn when the applet starts / resumes or a highest priority event handler ends. The HandleRender() function will be automatically called at these timings.

In the above example, the SFCApplication::HandleRender virtual function is overridden to fill the screen in white and draw the "Hello World" string.

[Note] Return value of the HandleRender() function

The HandleRender() function will return "true" if the device screen is redrawn. Otherwise it will return "false".

[Caution] "graphics->Update();"

When the device screen redrawing handler of SFCApplication::HandleRender is overridden, the screen will not be redrawn unless the SFXGraphics::Update function is called. Therefore, you have to describe the "graphics->Update();" statement at the end of the drawing processing.

Implement the OnKey() Function for for Handling a Key Event

The template of the OnKey() function for handling a key event dispatched from the BREW environment is automatically generated by the SophiaFramework UNIVERSE AppWizard.

Example 4.5. Implement the OnKey() function

// key handler
Bool helloworld::OnKey(UInt16 key)
{
    // here describe the handling of the key event dispatched from the BREW environment

    switch (key) {

        case AVK_SELECT: // when the SELECT key is pressed

            Terminate(); // terminate the application

            return true; // return "true" since the key event is handled
    }

    return false; // return "false" since the key event is NOT handled
}
[Note] Terminating an applet

To terminate an applet, call the SFCApplication::Terminate function.

For more details on terminating the applet, see ending the applet.

Implement the Constructor

Since an SFC applet is implemented with its own event handler, no event handler functions are registered in the constructor.

Example 4.6. Implement the Constructor

// constructor
helloworld::helloworld(Void) static_throws
{
// In case of using GCC, the "return" statement must not be described here.
}

Implement the Destructor

Example 4.7. Implement the Destructor

// destructor
helloworld::~helloworld(Void)
{
// In case of using GCC, the "return" statement must not be described here.
}
[Caution] Return statement

The return statement should not be described in the constructor nor in the destructor with C++.

The following bug is confirmed when GCC is used. If the return statement is described in the constructor or destructor, the compiler will freeze when a particular inheritance relation exists.