PrevNextUpHome SophiaFramework UNIVERSE 5.3

5.4. Control

5.4.1. Creating the Button

Add code to create the buttons shown in the figure below.

Figure 5.4. Button

Button

Example 5.13. Create the button

// Constructor
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));

    //*** added code segments are in bold

    // create buttons
    SFRButtonControlPtr button1 = new SFRButtonControl(
        this, SFXRectangle(10, 10, 50, 25), "b1");

    SFRButtonControlPtr button2 = new SFRButtonControl(
        this, SFXRectangle(10, 45, 50, 25), "b2");

    SFRButtonControlPtr button3 = new SFRButtonControl(
        this, SFXRectangle(10, 80, 50, 25), "b3");

    SFRButtonControlPtr button4 = new SFRButtonControl(
        this, SFXRectangle(70, 10, 50, 25), "b4");

    SFRButtonControlPtr button5 = new SFRButtonControl(
        this, SFXRectangle(70, 45, 50, 25), "b5");

    return;
}
[Note] Note:

A pointer is denoted by the postfix "Ptr" in SophiaFramework. For example, "SFRButtonControlPtr" is the same as "SFRButtonControl*".

A button is clicked by pressing the "Select" key.

Add code to the key handler to move focus.

Example 5.14. Implement the key handler

// Key handler
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 

            // string for debugging is displayed on BREW simulator
            TRACE("1-key");
            return true;

        // *** added code segments are in bold

        // move focus
        case AVK_UP:
            FocusUp();
            return true;
        case AVK_LEFT:
            FocusLeft();
            return true;
        case AVK_DOWN:
            FocusDown();
            return true;
        case AVK_RIGHT:
            FocusRight();
            return true;
    }
    return false;
}

Focus is moved to the next Responder in the direction of pressed arrow key.

5.4.2. Registering the Button Handler

Add code to handle the button events.

Example 5.15. Declare the button handler

SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
    
    //*** added code segments are in bold

    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl1)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl2)
};

Declaration

Example 5.16. Implement the button handler

// Button handler ( this handler is executed when "button1" is pressed )
HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnButtonControl1, result, control) {
    TRACE("Hello Button"); //  "Hello Button" is displayed on BREW Output Window
}

HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnButtonControl2, result, control) {
    // terminate window
    Invoke(SFXEvent(SREVT_RESPONDER_TERMINATE, SRP16_TERMINATE_INVOKE, true));
}

Example 5.17. Register the button handler

// Constructor
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));

    // create buttons
    SFRButtonControlPtr button1 = new SFRButtonControl(
        this, SFXRectangle(10, 10, 50, 25), "b1");

    SFRButtonControlPtr button2 = new SFRButtonControl(
        this, SFXRectangle(10, 45, 50, 25), "b2");

    SFRButtonControlPtr button3 = new SFRButtonControl(
        this, SFXRectangle(10, 80, 50, 25), "b3");

    SFRButtonControlPtr button4 = new SFRButtonControl(
        this, SFXRectangle(70, 10, 50, 25), "b4");

    SFRButtonControlPtr button5 = new SFRButtonControl(
        this, SFXRectangle(70, 45, 50, 25), "b5");

    //*** added code segments are in bold

    // register handler for when "button1" is pressed
    if (static_try()) {
        static_throw(
            button1->RegisterHandler(
                SREVT_CONTROL, HANDLER_BEFORE,
                HANDLER_FUNCTION(OnButtonControl1)));
    }
    // register handler for when "button2" is pressed
    if (static_try()) {
        static_throw(
            button2->RegisterHandler(
                SREVT_CONTROL, HANDLER_BEFORE,
                HANDLER_FUNCTION(OnButtonControl2)));
    }
}

Figure 5.5. Execution Result

Execution Result

Figure 5.6. BREW Output Window after button1 is pressed ( "Hello Button" is drawn. )

BREW Output Window after button1 is pressed ( "Hello Button" is drawn. )

5.4.3. Creating the Checkbox

Add code to create the checkboxes, radiobuttons and comboboxes in the figure below.

Figure 5.7. Checkbox

Checkbox

Example 5.18. Define the Checkbox

SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl1)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl2)
    
    //*** added code segments are in bold
    
private:
    // checkboxes refer to member functions
    // pointers are member variables here
    SFRCheckboxControlPtr _checkbox1;
    SFRCheckboxControlPtr _checkbox2;
    SFRRadiobuttonControlPtr _radiobutton1;
    SFRRadiobuttonControlPtr _radiobutton2;
    SFRComboboxControlPtr _combobox;
};

Example 5.19. Create the Checkbox

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));

    // create buttons
    SFRButtonControlPtr button1 = new SFRButtonControl(
        this, SFXRectangle(10, 10, 50, 25), "b1");

    SFRButtonControlPtr button2 = new SFRButtonControl(
        this, SFXRectangle(10, 45, 50, 25), "b2");

    SFRButtonControlPtr button3 = new SFRButtonControl(
        this, SFXRectangle(10, 80, 50, 25), "b3");

    SFRButtonControlPtr button4 = new SFRButtonControl(
        this, SFXRectangle(70, 10, 50, 25), "b4");

    SFRButtonControlPtr button5 = new SFRButtonControl(
        this, SFXRectangle(70, 45, 50, 25), "b5");

    // register handler for when "button1" is pressed
    if (static_try()) {
        static_throw(
            button1->RegisterHandler(
                SREVT_CONTROL, HANDLER_BEFORE,
                HANDLER_FUNCTION(OnButtonControl1)));
    }
    // register handler for when "button2" is pressed
    if (static_try()) {
        static_throw(
            button2->RegisterHandler(
                SREVT_CONTROL, HANDLER_BEFORE,
                HANDLER_FUNCTION(OnButtonControl2)));
    }

    // *** added code segments are in bold

    // create checkboxes
    _checkbox1 = new SFRCheckboxControl(
        this, SFXRectangle(10, 115, 90, 20), "check1");
    _checkbox2 = new SFRCheckboxControl(
        this, SFXRectangle(105, 115, 90, 20), "check2");
}

5.4.4. Obtaining the Status of Checkbox

Add code to obtain the status of checkbox when "button3" is pressed.

Example 5.20. Declare the button handler for button3

SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);
 
private:
    // define as member variable (type: pointer)
    SFRCheckboxControlPtr _checkbox1;
    SFRCheckboxControlPtr _checkbox2;
 
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl1)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl2)
 
    // *** added code segments are in bold
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl3)

Example 5.21. Implement the button handler for button3

// Button handler ( this handler is executed when "button3" is pressed)
HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnButtonControl3, result, control) {
    if (_checkbox1->GetStatusCheck()) { // when checkbox1 is checked
        TRACE("checkbox1: checked");
    } else {
        TRACE("checkbox1: unchecked");
    }
    if (_checkbox2->GetStatusCheck()) {
        TRACE("checkbox2: checked");
    } else {
        TRACE("checkbox2: unchecked");
    }
    if (_radiobutton1->GetStatusCheck()) { // when radiobutton1 is checked
        TRACE("radiobutton1: checked");
    } else {
        TRACE("radiobutton2: checked");
    }
    TRACE("combobox: %d", _combobox->GetValue()); // item selected in combobox 
}

Example 5.22. Register the button handler for button3

MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(),
    SFXRectangle(20, 20, 200, 250), "my window") static_throws
{
    ...

    // register handler for when "button3" is pressed
    if (static_try()) {
        static_throw(
            button3->RegisterHandler(
                SREVT_CONTROL, HANDLER_BEFORE,
                HANDLER_FUNCTION(OnButtonControl3)));
    }

    ...

}

Figure 5.8. Execution Result

Execution Result

Figure 5.9. BREW Output Window after button3 is pressed ( The status of checkbox is displayed. )

BREW Output Window after button3 is pressed ( The status of checkbox is displayed. )

5.4.5. Creating the Radiobutton

Add code to create the radiobuttons shown in the figure below.

Figure 5.10. Radio Buttons

Radio Buttons

Example 5.23. Define the Radiobutton

SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);
    
//*** added code segments are in bold

private:
    // defined as member variables (type: pointer)
    SFRCheckboxControlPtr _checkbox1;
    SFRCheckboxControlPtr _checkbox2;
 
    SFRRadiobuttonControlPtr _radiobutton1;
    SFRRadiobuttonControlPtr _radiobutton2;
    
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl1)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl2)
};

Example 5.24. Create the Radiobutton

MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(),
    SFXRectangle(20, 20, 200, 250), "my window") static_throws
{
    // register drawing handler
    if (static_try()) {
        static_throw(
            RegisterHandler(SREVT_RESPONDER_RENDER, 
                            SRP16_RENDER_CONTENT,
                            HANDLER_BEFORE, 
                            HANDLER_FUNCTION(OnRenderContent)));
    }
 
    // register key handler
    if (static_try()) {
        static_throw(
            RegisterHandler(SFEVT_KEY, 
                            HANDLER_AFTER, 
                            HANDLER_FUNCTION(OnKey)));
    }

    // create buttons
    SFRButtonControlPtr button1 = new SFRButtonControl(this, 
                                                       SFXRectangle(10, 10, 50, 25), 
                                                       "b1");

    SFRButtonControlPtr button2 = new SFRButtonControl(this, 
                                                       SFXRectangle(10, 45, 50, 25), 
                                                       "b2");

    SFRButtonControlPtr button3 = new SFRButtonControl(this, 
                                                       SFXRectangle(10, 80, 50, 25), 
                                                       "b3");

    SFRButtonControlPtr button4 = new SFRButtonControl(this, 
                                                       SFXRectangle(70, 10, 50, 25), 
                                                       "b4");

    SFRButtonControlPtr button5 = new SFRButtonControl(this, 
                                                       SFXRectangle(70, 45, 50, 25), 
                                                       "b5");

    // register handler for when button1 is pressed
    if (static_try()) {
        static_throw(
            button1->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl1)));
    }
    // register handler for when button2 is pressed
    if (static_try()) {
        static_throw(
            button2->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl2)));
    }

    if (static_try()) {
        static_throw(
            button3->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl3)));
    }
 
    // create checkboxes
    _checkbox1 = new SFRCheckboxControl(this, 
                                        SFXRectangle(10, 115, 90, 20), 
                                        "check1");
    _checkbox2 = new SFRCheckboxControl(this, 
                                        SFXRectangle(105, 115, 90, 20), 
                                        "check2");

    //*** added code segments are in bold
    // create radiobuttons
    
    _radiobutton1 = new SFRRadiobuttonControl(this,
                                              SFXRectangle(10, 140, 90, 20),
                                              "radio1");
    _radiobutton2 = new SFRRadiobuttonControl(this,
                                              SFXRectangle(105, 140, 90, 20),
                                              "radio2");

    // group together radiobuttons
    _radiobutton2->Group(_radiobutton1);
    // set radiobutton1 to selected status
    _radiobutton1->SetStatusCheck(true);

}

The Group function is used to group together radiobuttons.

[Note] Obtaining the status of radiobutton

The status of radiobutton can also be obtained by the GetStatusCheck function.

5.4.6. Creating the ComboBox

Add the code to create the combobox shown in the figure below.

Figure 5.11. ComboBox

ComboBox

Example 5.25. Define the ComboBox

SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);
    
// *** added code segments are in bold

private:
    // defined as member variables (type: pointer)
    SFRCheckboxControlPtr _checkbox1;
    SFRCheckboxControlPtr _checkbox2;
    SFRRadiobuttonControlPtr _radiobutton1;
    SFRRadiobuttonControlPtr _radiobutton2;

    SFRComboboxControlPtr _combobox;

    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl1)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl2)
};

Example 5.26. Create the ComboBox

MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(),
    SFXRectangle(20, 20, 200, 250), "my window") static_throws
{
    // register drawing handler
    if (static_try()) {
        static_throw(
            RegisterHandler(SREVT_RESPONDER_RENDER, 
                            SRP16_RENDER_CONTENT,
                            HANDLER_BEFORE, 
                            HANDLER_FUNCTION(OnRenderContent)));
    }
 
    // register key handler
    if (static_try()) {
        static_throw(
            RegisterHandler(SFEVT_KEY, 
                            HANDLER_AFTER, 
                            HANDLER_FUNCTION(OnKey)));
    }

    // create buttons
    SFRButtonControlPtr button1 = new SFRButtonControl(this, 
                                                       SFXRectangle(10, 10, 50, 25), 
                                                       "b1");

    SFRButtonControlPtr button2 = new SFRButtonControl(this, 
                                                       SFXRectangle(10, 45, 50, 25), 
                                                       "b2");

    SFRButtonControlPtr button3 = new SFRButtonControl(this, 
                                                       SFXRectangle(10, 80, 50, 25), 
                                                       "b3");

    SFRButtonControlPtr button4 = new SFRButtonControl(this, 
                                                       SFXRectangle(70, 10, 50, 25), 
                                                       "b4");

    SFRButtonControlPtr button5 = new SFRButtonControl(this, 
                                                       SFXRectangle(70, 45, 50, 25), 
                                                       "b5");

    // register handler for when button1 is pressed
    if (static_try()) {
        static_throw(
            button1->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl1)));
    }
    // register handler for when button2 is pressed
    if (static_try()) {
        static_throw(
            button2->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl2)));
    }

    if (static_try()) {
        static_throw(
            button3->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl3)));
    }
 
    // create checkboxes
    _checkbox1 = new SFRCheckboxControl(this, 
                                        SFXRectangle(10, 115, 90, 20), 
                                        "check1");
    _checkbox2 = new SFRCheckboxControl(this, 
                                        SFXRectangle(105, 115, 90, 20), 
                                        "check2");
          
    _radiobutton1 = new SFRRadiobuttonControl(this, 
                                              SFXRectangle(10, 140, 90, 20), 
                                              "radio1");
    _radiobutton2 = new SFRRadiobuttonControl(this, 
                                              SFXRectangle(105, 140, 90, 20), 
                                              "radio2");

    // group together radiobuttons
    _radiobutton2->Group(_radiobutton1);
    // set radiobutton1 to selected status
    _radiobutton1->SetStatusCheck(true);
 
    // *** added code segments are in bold
    
    // create combobox
    SFXWideString item[] = {"item1", "item2", "item3"};
    _combobox = new SFRComboboxControl(this, 
                                       SFXRectangle(10, 165, 90, 20), 
                                       item, 
                                       lengthof(item));

}

5.4.7. Registering the Combobox Handler

You can register the handler which executes when a combobox item is selected.

Example 5.27. Declare the handler for combobox

SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);

// *** added code segments are in bold

private:
    // defined as member variables (type: pointer)
    SFRCheckboxControlPtr _checkbox1;
    SFRCheckboxControlPtr _checkbox2;
    SFRRadiobuttonControlPtr _radiobutton1;
    SFRRadiobuttonControlPtr _radiobutton2;
    SFRComboboxControlPtr _combobox;
 
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl1)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl2)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl3)  
 
    HANDLER_DECLARE_VOIDCONTROL(OnComboboxControl)
};

Example 5.28. Implement the hander for combobox

// Combobox handler ( this handler is executed when combobox item is selected )
HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnComboboxControl, result, control) {

    if (result == SRP16_ESCAPE) { 
        // when no item is selected, do nothing
    } else {
        // result: selected item number is contained
        // control: pointer to combobox is contained
        // title string of selected item is obtained through "GetTitle" 
        SFXAnsiString string(static_cast<SFRComboboxControlPtr>(control)->GetTitle(result));

        // display string on BREW Output Window
        // string.GetCString() returns string in C language
        // (argument of TRACE should be string in C language)
        TRACE("'%d: %s' is selected.", result, string.GetCString());
    }
}

Example 5.29. Register the hander for combobox

// register handler for combobox
MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(),
    SFXRectangle(20, 20, 200, 250), "my window") static_throws
{
    ...

    // register handler for when combobox item is selected
    if (static_try()) {
        static_throw(
            _combobox->RegisterHandler(
                SREVT_CONTROL, HANDLER_BEFORE,
                HANDLER_FUNCTION(OnComboboxControl)));
    }

    ...

}

Figure 5.12. A combobox item is being selected

A combobox item is being selected

Figure 5.13. BREW Output Window after a combobox item is selected

BREW Output Window after a combobox item is selected

5.4.8. Invalid and Invisible

A control can be made invalid or invisible to select.

Figure 5.14. Invalid and invisible controls

Invalid and invisible controls

The controls become invalid or invisible when button4 is pressed.

Example 5.30. Declare the button handler for button4

SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);
 
// *** added code segments are in bold 
 
private:  
    // declared as member variable (pointer type)
    SFRCheckboxControlPtr _checkbox1;
    SFRCheckboxControlPtr _checkbox2;
    SFRRadiobuttonControlPtr _radiobutton1;
    SFRRadiobuttonControlPtr _radiobutton2;
    SFRComboboxControlPtr _combobox;
  
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl1)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl2)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl3)   
 
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl4)

};

Example 5.31. Register the button handler for button4

MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(),
    SFXRectangle(20, 20, 200, 250), "my window") static_throws
{
    // register drawing handler
    if (static_try()) {
        static_throw(
            RegisterHandler(SREVT_RESPONDER_RENDER, 
                            SRP16_RENDER_CONTENT,
                            HANDLER_BEFORE, 
                            HANDLER_FUNCTION(OnRenderContent)));
    }
 
    // register key handler
    if (static_try()) {
        static_throw(
            RegisterHandler(SFEVT_KEY, 
                            HANDLER_AFTER, 
                            HANDLER_FUNCTION(OnKey)));
    }
 
    // create buttons
    SFRButtonControlPtr button1 = new SFRButtonControl(this, 
                                                       SFXRectangle(10, 10, 50, 25), 
                                                       "b1");

    SFRButtonControlPtr button2 = new SFRButtonControl(this, 
                                                       SFXRectangle(10, 45, 50, 25), 
                                                       "b2");

    SFRButtonControlPtr button3 = new SFRButtonControl(this, 
                                                       SFXRectangle(10, 80, 50, 25), 
                                                       "b3");

    SFRButtonControlPtr button4 = new SFRButtonControl(this, 
                                                       SFXRectangle(70, 10, 50, 25), 
                                                       "b4");

    SFRButtonControlPtr button5 = new SFRButtonControl(this, 
                                                       SFXRectangle(70, 45, 50, 25), 
                                                       "b5");

    // register handler for when button1 is pressed
    if (static_try()) {
        static_throw(
            button1->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl1)));
    }
    // register handler for when button2 is pressed
    if (static_try()) {
        static_throw(
            button2->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl2)));
    } 
    // register handler for when button3 is pressed
    if (static_try()) {
        static_throw(
            button3->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl3)));
    }
    
    // *** added code segments are in bold

    // register handler for when button4 is pressed
    if (static_try()) {
        static_throw(
            button4->RegisterHandler(SREVT_CONTROL, 
                                     HANDLER_BEFORE,
                                     HANDLER_FUNCTION(OnButtonControl4)));
    }

    // create checkboxes
    _checkbox1 = new SFRCheckboxControl(this, 
                                        SFXRectangle(10, 115, 90, 20), 
                                        "check1");
    _checkbox2 = new SFRCheckboxControl(this, 
                                        SFXRectangle(105, 115, 90, 20), 
                                        "check2");

    // create radiobuttons
    _radiobutton1 = new SFRRadiobuttonControl(this, 
                                              SFXRectangle(10, 140, 90, 20), 
                                              "radio1");
    _radiobutton2 = new SFRRadiobuttonControl(this, 
                                              SFXRectangle(105, 140, 90, 20), 
                                              "radio2");

    // group together radiobuttons 
    _radiobutton2->Group(_radiobutton1);
    // set radiobutton1 to selected status
    _radiobutton1->SetStatusCheck(true);

    // create combobox
    SFXWideString item[] = {"item1", "item2", "item3"};
    _combobox = new SFRComboboxControl(this, 
                                       SFXRectangle(10, 165, 90, 20), 
                                       item, 
                                       lengthof(item));
  
    // register handler for when combobox item is selected
    if (static_try()) {
        static_throw(
            _combobox->RegisterHandler(SREVT_CONTROL,
                                       HANDLER_BEFORE,
                                       HANDLER_FUNCTION(OnComboboxControl)));
    }

}

Implement the button handler to make controls invalid or invisible.

Example 5.32. Implement the button handler for button4

// Button handler ( this handler is executed when "button4" is pressed)
HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnButtonControl4, result, control) {
    
    // invalidate checkbox2
    _checkbox2->SetStatusEnable(false);
    
    // invalidate radiobutton1
    _radiobutton1->SetStatusEnable(false);
    
    // make radiobotton2 invisible
    _radiobutton2->SetStatusVisible(false);
}

The entire window can be invalidated.

Figure 5.15. Invalidated window

Invalidated window

Example 5.33. Code to make the entire window invalidated

HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnButtonControl4, result, control) {
    
    //  invalidate checkbox2
    _checkbox2->SetStatusEnable(false);
    
    // invalidate radiobutton1
    _radiobutton1->SetStatusEnable(false);
    
    //  make radiobotton2 invisible
    _radiobutton2->SetStatusVisible(false);
    
    // add the following
    // invalidate entire window
    this->SetStatusEnable(false);
}

5.4.9. Creating the Editbox Control

Add code to create the editbox control shown in the image below.

Figure 5.16. Editbox Control

Editbox Control

Example 5.34. Define the editbox control and declaring its handler

SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
SFMTYPEDEFCLASS(MyWindow)
class MyWindow : public SFRTitleWindow {
    SFMSEALCOPY(MyWindow)
public:
    MyWindow(Void) static_throws;
    virtual ~MyWindow(Void);
 
// *** added code segments are in bold
 
private:  
    // defined as member variables (type: pointer)
    SFRCheckboxControlPtr _checkbox1;
    SFRCheckboxControlPtr _checkbox2;
    SFRRadiobuttonControlPtr _radiobutton1;
    SFRRadiobuttonControlPtr _radiobutton2;
    SFRComboboxControlPtr _combobox;
  
    SFREditboxControlPtr _editbox;
 
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl1)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl2)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl3)
    HANDLER_DECLARE_VOIDCONTROL(OnButtonControl4)
 
    HANDLER_DECLARE_VOIDCONTROL(OnEditboxControl)
};

Example 5.35. Create the editbox control and registering its handler

MyWindow::MyWindow(Void) : SFRTitleWindow(SFRApplication::GetInstance(),
    SFXRectangle(20, 20, 200, 250), "my window") static_throws
{
    ...

    // *** add the codes below

    // create editbox control
    // "hello textbox" is displayed by default setting
    _editbox = new SFREditboxControl(
        this, SFXRectangle(10, 190, 150, 28), "hello textbox");

    // set maximum size for input
    _editbox->SetMaxSize(255);

    // register handler for when text is entered,
    // or when focused is moved 
    if (static_try()) {
        static_throw(
            _editbox->RegisterHandler(
                SREVT_CONTROL, HANDLER_BEFORE,
                HANDLER_FUNCTION(OnEditboxControl)));
    }

    // change into input mode when "Select" key is pressed 
    // while edit control is focused
    // (SelectHandler is handler for "Select" key event) 
    if (static_try()) {
        static_throw(
            RegisterHandler(
                SFEVT_KEY, AVK_SELECT, HANDLER_AFTER,
                HANDLER_FUNCTION(SelectHandler)));
    }
}

// *** add the codes below

// handler after text is entered, 
// or when edit control focus is moved
HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnEditboxControl, result, control)
{
    // remove targeted status
    control->SetStatusTarget(false);

    if (result) { // when text is drawn 
        
        // move focus to next responder
        FocusNext();

    } 
    else {        // when focus is moved
        //...
    }
}

5.4.10. Obtaining the Text

Obtain the entered text with the GetText function.

Example 5.36. Implement the button handler for button3

// Button handler ( this handler is executed when "button3" is pressed)
HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnButtonControl3)
{
    ...

    // *** add the codes below

    // obtain text from editbox control

    SFXAnsiString string = static_cast<SFXAnsiString>(_editbox->GetText());

    // display text
    TRACE("textcontrol: %s", string.GetCString());

    //*** the above codes are added
}

Figure 5.17. Execution Result

Execution Result

The string inputted into the Editbox control is displayed on the BREW Output Window.

Figure 5.18. BREW Output Window

BREW Output Window