PrevNextUpHome SophiaFramework UNIVERSE 5.3

5.3. Window

5.3.1. Defining and Implementing the Window class

Customize the code of HelloWorld application generated by the SophiaFramework AppWizard to display the window below.

Figure 5.1. HelloWorld application that draws the string " HelloWorld " within the window

HelloWorld application that draws the string " HelloWorld " within the window

Define MyWindow class.

Example 5.8. Define the Title-Window "MyWindow"

// MyWindow inherits from SFRTitleWindow
SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
};

Implement handler functions for drawing the string "Hello World" and processing when the "Select" key is pressed.

Example 5.9. Implement the Title-Window "MyWindow"

// Constructor ( create and initialize window ) 
// window is created at coordinates: (20, 20); width: 200; height: 250
// title: "my window"
MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(),
    SFXRectangle(20, 20, 200, 250), "my window") static_throws
{
    // register drawing handler (error handling omitted)
    RegisterHandler(SREVT_RESPONDER_RENDER, SRP16_RENDER_CONTENT,
                    HANDLER_BEFORE, HANDLER_FUNCTION(OnRenderContent));

    // register key handler 
    RegisterHandler(SFEVT_KEY, HANDLER_AFTER, HANDLER_FUNCTION(OnKey));

    return;
}

// Destructor (executed when window is terminated)
MyWindow::~MyWindow(Void)
{
    return;
}

// Drawing handler (handles drawing events within window)
HANDLER_IMPLEMENT_VOIDRENDER(MyWindow, OnRenderContent, graphics)
{
    // draw screen

    // paint drawing area in white
    // GetContentWorld()returns an SFXRectangle (representing drawing area in window)
    // SFXRGBColor(0xFF, 0xFF, 0xFF, 0x00) defines white
    graphics->FillRectangle(GetContentWorld(), SFXRGBColor(0xFF, 0xFF, 0xFF, 0x00));

    // draw "Hello Window"
    graphics->DrawText("Hello Window", GetContentWorld(), SFXRGBColor(0x00, 0x00, 0x00, 0x00));
    return;
}

// Key handler (executed when a key is pressed while MyWindow is displayed)
HANDLER_IMPLEMENT_BOOLEVENT(MyWindow, OnKey, event)
{
    switch (event.GetP16()) {
        case AVK_CLR: // when "clear" key is pressed

            // window is terminated (*)
            return Invoke(SFXEvent(SREVT_RESPONDER_TERMINATE, SRP16_TERMINATE_INVOKE, true));

        case AVK_1: // when "1" key is pressed 

            // display string for debugging on BREW simulator
            TRACE("1-key");
            return true;
    }
    return false;
}

5.3.2. Creating the Window

Customize the key handler to create and display the window when the "1" key is pressed.

Example 5.10. Customize the key handler

HANDLER_IMPLEMENT_BOOLEVENT(HelloWorld, OnKey, event)
{
    switch (event.GetP16()) {
        case AVK_SELECT: // when "Select" key is pressed
            Terminate(); // application is ended 
            return true;

        // *** added code segments are in bold

        case AVK_1:

            // create window with "new" operator 
	        // (error handling is omitted)

            new MyWindow();

            // process key event and return true
            return true;
    }
    return false;
}

Customize the drawing handler to paint the background in white.

Example 5.11. Customize the drawing handler

// Drawing handler
HANDLER_IMPLEMENT_VOIDRENDER(HelloWorld, OnRenderContent, graphics)
{

    // paint drawing area in white
    // GetContentWorld()returns an SFXRectangle (representing drawing area in window)
    // SFXRGBColor(0xFF, 0xFF, 0xFF, 0x00) defines white
        // *** add the following
        graphics->FillRectangle(GetContentWorld(), SFXRGBColor(0xFF, 0xFF, 0xFF, 0x00));
    // *** edit the following

    graphics->DrawText("Hello World", GetContentWorld(), SFXRGBColor(0x00, 0x00, 0x00, 0x00));
    return;
}

5.3.3. Destroying the Window

To destroy the instance of MyWindow class, send the event of SFXEvent(SREVT_RESPONDER_TERMINATE, SRP16_TERMINATE_INVOKE, true) by the Invoke function.

[Warning] Warning

The code below is to send the terminate event to the instance of MyWindow class.

Example 5.12. Destroy the window

// destroy window
return Invoke(SFXEvent(SREVT_RESPONDER_TERMINATE, SRP16_TERMINATE_INVOKE, true));
[Warning] Warning

The delete operator cannot be used to destroy the window.

5.3.4. Testing on the BREW Simulator

  1. MyWindow will be generated and displayed when the "1" key is pressed.
  2. "1-key" will be displayed in the BREW Output Window if the "1" key is pressed again.
  3. MyWindow will be terminated when the "clear" key is pressed.

Figure 5.2. Execution Result

Execution Result

BREW Output Window

Figure 5.3. BREW Output Window

BREW Output Window
[Note] Note:

The BREW Output Window can be displayed by clicking [menu]-[display]-[Output Window].