PrevNextUpHome BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1

8.3. Event Handling

The extended Hello World code is used as an example to explain the processing flow from start up to termination of application that terminates automatically when the "Select" key is pressed.

Example 8.8. Hello World application: .hpp file (class definition)

//
//      HelloWorld.hpp
//
//      This source code was automatically
//      generated by SophiaFramework 4.1.
//

#ifndef __HELLOWORLD_HPP
#define __HELLOWORLD_HPP

#include <SophiaFramework.hpp>
#include "HelloWorld.bid"

SFMTYPEDEFCLASS(HelloWorld)
class HelloWorld : public SFRApplication {
    SFMSEALCOPY(HelloWorld)
public:
    static SFCInvokerPtr Factory(Void);
private:
    HelloWorld(Void) static_throws;
    virtual ~HelloWorld(Void);
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
};

#endif // __HELLOWORLD_HPP //

Example 8.9. Hello World application: .cpp file (Boot Loader and Factory function)

//
//      HelloWorld.cpp
//
//      This source code was automatically
//      generated by SophiaFramework 4.1.
//

#include "HelloWorld.hpp"

// Boot Loader
SFCApplet::FactorySPP SFCApplet::Boot(AEECLSID id, SFXAnsiStringPtr license)
{
    *license = "heap://";

    return (id == AEECLSID_HELLOWORLD) ? (&HelloWorld::Factory) : (null);
}

// Factory function
SFCInvokerPtr HelloWorld::Factory(Void)
{
    return ::new HelloWorld;
}

// Constructor
HelloWorld::HelloWorld(Void) static_throws
{
    if (static_try()) {
        static_throw(RegisterHandler(SREVT_RESPONDER_RENDER, 
                                     SRP16_RENDER_CONTENT, 
                                     HANDLER_BEFORE, 
                                     HANDLER_FUNCTION(OnRenderContent)));
    }
    if (static_try()) {
        static_throw(RegisterHandler(SFEVT_KEY, 
                                     HANDLER_AFTER, 
                                     HANDLER_FUNCTION(OnKey)));
    }
    return;
}

// Destructor
HelloWorld::~HelloWorld(Void)
{
    return;
}

// Drawing handler
HANDLER_IMPLEMENT_VOIDRENDER(HelloWorld, OnRenderContent, graphics)
{
    // draw "Hello World" to the screen
    graphics->DrawText("Hello World", GetContentWorld());
    return;
}

// Key handler
HANDLER_IMPLEMENT_BOOLEVENT(HelloWorld, OnKey, event)
{
    // Key events handling
    switch (event.GetP16()) {
        case AVK_SELECT:     // if select key is pressed
            Terminate();     // terminate application
            return true;
    }
    return false;
}

The first HelloWorld application is extended with the declaration, registration, and implementation of key handler.

In the declaration, registration, and implementation of key handler, the HANDLER_DECLARE_BOOLEVENT, RegisterHandler, and HANDLER_IMPLEMENT_BOOLEVENT functions are used respectively. The key handler is registered in the Constructor.

During the starting up of application, the Boot Loader, the Factory function, and the Constructor function are executed in turn.

When the instance of HelloWorld Application class is created, the SFEVT_APP_START event from BREW is transmitted to the application. In the above code, since the handler for SFEVT_APP_START event is not registered, the default handler of the SFRApplication class (that does nothing) is invoked, and the starting up is completed.

Then the HelloWorld application enters into the event waiting state, and the event handling for events such as key input by user or network's events can be available.

The handler for SFEVT_KEY event is declared, registered, and implemented in the above code. The key handler is invoked when a key is pressed. The key handler will verify which key is pressed, and if it is the "Select" key, the Terminate function will be called to terminate the HelloWorld application.

When the HelloWorld application enters into the terminating state, the SFEVT_APP_STOP event from BREW is transmitted to the application. Since the handler for SFEVT_APP_STOP event is not registered, the default handler of SFRApplication class (that does nothing) is invoked.

Then, the Destructor is called, and the instance of HelloWorld Application class is destroyed. Finally, after this termination is done inside SophiaFramework, the application's termination is finished. When the instance of HelloWorld Application class is destroyed, all UI components such as window, control, etc, are also automatically destroyed. (No need to explicitly destroy these UI components)