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

8.4. Window

The OnlyWindow application draws the string "Hello World" in a window that will close when the "Select" key is pressed. The source code is shown below.

Figure 8.2. OnlyWindow application

OnlyWindow application

Example 8.10. OnlyWindow application : .hpp file (class definition)

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

#ifndef __ONLYWINDOW_HPP
#define __ONLYWINDOW_HPP

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

SFMTYPEDEFCLASS(OnlyWindow)
class OnlyWindow : public SFRApplication {
    SFMSEALCOPY(OnlyWindow)
public:
    static SFCInvokerPtr Factory(Void);
private:
    OnlyWindow(Void) static_throws;
    virtual ~OnlyWindow(Void);
};

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

#endif // __ONLYWINDOW_HPP //

Example 8.11.  OnlyWindow application: .cpp file (Boot Loader and Factory function)

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

#include "OnlyWindow.hpp"

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

    return (id == AEECLSID_ONLYWINDOW) ? (&OnlyWindow::Factory) : (null);
}

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

// Constructor
OnlyWindow::OnlyWindow(Void) static_throws
{
    SFRWindowPtr window;

    if (static_try()) {
        if ((window = ::new MyWindow()) != null) {
            static_throw(*window);
            if (!static_try()) {
                ::delete window;
            }
        }
        else {
            static_throw(SFERR_NO_MEMORY);
        }
    }
    return;
}

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

// Constructor
MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(), SFXGraphics::GetDeviceRectangle().Deflate(20, 20), "my window") 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
MyWindow::~MyWindow(Void)
{
    return;
}

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

// Key handler
HANDLER_IMPLEMENT_BOOLEVENT(MyWindow, OnKey, event)
{
    // Key events handling
    switch (event.GetP16()) {
        case AVK_SELECT:// if select key is pressed
            // close the window
            return Invoke(SFXEvent(SREVT_RESPONDER_TERMINATE, 
                                   SRP16_TERMINATE_INVOKE, 
                                   true));
    }
    return false;
}

In the above code, the MyWindow class that inherits from the SFRTitleWindow class is defined. Handlers for drawing string "Hello World" and handling the "Select" key are declared, registered, and implemented.

The instance of MyWindow class is created inside the Constructor of OnlyWindow Application class

The handler is registered inside the Constructor of MyWindow class. In the key handler of MyWindow class, SREVT_RESPONDER_TERMINATE event is transmitted when the "Select" key is pressed, then the window will destroy itself .

In the next example, the string "Hello World" and a button are displayed on the window, when the button is pressed, the window will close. The source code is shown below.

Figure 8.3. ButtonWindow Application

ButtonWindow Application

Example 8.12. ButtonWindow Application: .hpp file (class definition)

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

#ifndef __BUTTONWINDOW_HPP
#define __BUTTONWINDOW_HPP

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

SFMTYPEDEFCLASS(ButtonWindow)
class ButtonWindow : public SFRApplication {
    SFMSEALCOPY(ButtonWindow)
public:
    static SFCInvokerPtr Factory(Void);
private:
    ButtonWindow(Void) static_throws;
    virtual ~ButtonWindow(Void);
};

SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);
private:
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_VOIDVOID(OnButtonControl)
};

#endif // __BUTTONWINDOW_HPP //

Example 8.13. ButtonWindow application : .cpp file (Boot Loader and Factory function)

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

#include "ButtonWindow.hpp"

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

    return (id == AEECLSID_BUTTONWINDOW) ? (&ButtonWindow::Factory) : (null);
}

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

// Constructor
ButtonWindow::ButtonWindow(Void) static_throws
{
    SFRWindowPtr window;

    if (static_try()) {
        if ((window = ::new MyWindow()) != null) {
            static_throw(*window);
            if (!static_try()) {
                ::delete window;
            }
        }
        else {
            static_throw(SFERR_NO_MEMORY);
        }
    }
    return;
}

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

// Constructor
MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(), SFXGraphics::GetDeviceRectangle().Deflate(20, 20), "my window") static_throws
{
    SFRButtonControlPtr control;
    SFXRectangle rect;

    if (static_try()) {
        static_throw(RegisterHandler(SREVT_RESPONDER_RENDER, 
                                     SRP16_RENDER_CONTENT, 
                                     HANDLER_BEFORE, 
                                     HANDLER_FUNCTION(OnRenderContent)));
    }
    
     // calculate the coordinates of the button control
     rect = GetContentWorld();
     rect.AddTop(rect.GetHeight() - 28);
     rect.Deflate(2, 2);
					
     // generate button and register the handler
     control = ::new SFRButtonControl(this, rect, "Close");
					
     if (static_try()) {
         static_throw(control->RegisterHandler(SREVT_CONTROL, 
                                               HANDLER_BEFORE, 
                                               HANDLER_FUNCTION(OnButtonControl)));
     }

    return;
}

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

// Drawing handler
HANDLER_IMPLEMENT_VOIDRENDER(MyWindow, OnRenderContent, graphics)
{
    // call the default handler of parent class
    SFRTitleWindow::ContentHandler(graphics);

    // draw string "Hello World" to the screen
    graphics->DrawText("Hello World", 
                       GetContentWorld(), 
                       SFXRGBColor(0x00, 0x00, 0x00, 0x00));
    return;
}

// Button control handler
HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnButtonControl, result, control){
    // close the window if the button is pressed
    Invoke(SFXEvent(SREVT_RESPONDER_TERMINATE, 
                    SRP16_TERMINATE_INVOKE, 
                    true));
    return;
}

In this sample code, the button is created in the Constructor of MyWindow class, and the button handler is registered to the button. Window is destroyed in the button handler.