PrevNextUpHome SophiaFramework UNIVERSE 5.3

5.5. Dialog

5.5.1. Create the Dialog

Add code to create the dialog in the image below.

Figure 5.19. MultiDialog

MultiDialog

Example 5.37. Declare the button handler for button5

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(OnButtonControl5)
 
    HANDLER_DECLARE_VOIDCONTROL(OnEditboxControl)

};

Example 5.38. Register the button handler for button5

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

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

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


    // 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 when text has been 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)));
    }
}

Example 5.39. Implement the button handler for button5

// Button handler ( this handler is executed when "button5" is pressed)
HANDLER_IMPLEMENT_VOIDCONTROL(MyWindow, OnButtonControl5, result, control)
{
    // structure for parameter to create multidialog
    SFRMultiDialog::ParamRec param;

    // text to be displayed in multidialog
    param.text = "Finish application?";

    // label for first button
    param.ok = "ok";

    // label for second button
    param.cancel = "no";

    // label for third button (null text: no button)
    param.another = ";

    // initial setting of focus position
    param.focus = SFRMultiDialog::FOCUS_OK;

    // image icon
    param.image = SFBImageSmp(null);

    // create multidialog
    SFRMultiDialogPtr multiDialog = new SFRMultiDialog(
        SFXRectangle(10, 40, 220, 140), "Finish?", param);

    // register dialog handler
    if (static_try()) {
        static_throw(
            multiDialog->RegisterHandler(
                SREVT_DIALOG, HANDLER_BEFORE,
                HANDLER_FUNCTION(OnDialog)));
    }
}

Example 5.40. Declare the multidialog handler

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(OnButtonControl5)
 
    HANDLER_DECLARE_VOIDCONTROL(OnEditboxControl)
    
    HANDLER_DECLARE_VOIDDIALOG(OnDialog)

};

Example 5.41. Implement the multidialog handler

// Multidialog handler ( this handler is executed when "ok" or "no" are pressed in multidialog )
HANDLER_IMPLEMENT_VOIDDIALOG(MyWindow, OnDialog, result, dialog) {

    switch(result) {
        case SRP16_OK: // when "ok" is pressed
            TRACE("OK");

            // terminate application
            SFRApplication::Terminate();
            break;
        case SRP16_CANCEL: // when "no" is pressed
            TRACE("CANCEL");

            // close the dialog
            dialog->DialogHandler();
            break;
    }
}

Figure 5.20. Execution Result

Execution Result

5.5.2. Customizing the Dialog

Customize the class which inherits from SFRFrameDialog. Register the key and drawing handler in the same way as the Window class.

Example 5.42. Define the dialog

SFMTYPEDEFCLASS(MyDialog)
class MyDialog : public SFRFrameDialog {
    SFMSEALCOPY(MyDialog)
public:
    MyDialog(Void) static_throws;
    virtual ~MyDialog(Void);
    HANDLER_DECLARE_VOIDRENDER(OnRenderContent)
    HANDLER_DECLARE_BOOLEVENT(OnKey)
};

In the constructor, register the key and drawing handlers for dialog.

Example 5.43. Implement the dialog

// Constructor
MyDialog::MyDialog() : SFRFrameDialog(SFXRectangle(10, 40, 220, 80)) 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)));
    }
}

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

// Drawing handler
HANDLER_IMPLEMENT_VOIDRENDER(MyDialog, OnRenderContent, graphics) {
    graphics->FillRectangle(GetContentWorld(), SFXRGBColor(0xCC, 0xFF, 0xFF, 0x00));
    graphics->DrawString(
        "when 3 key is pressed, close", SFXRectangle(10, 10, 200, 80), SFXRGBColor(0, 0, 0, 0),
        IDF_ALIGN_LEFT | IDF_ALIGN_MIDDLE);
}

// Key handler
HANDLER_IMPLEMENT_BOOLEVENT(MyDialog, OnKey, event)
{
    switch (event.GetP16()) {
        case AVK_3: // when "3" key is pressed
            // close 
            return Invoke(
                SFXEvent(SREVT_RESPONDER_TERMINATE, SRP16_TERMINATE_INVOKE, true));
    }
    return false;
}

Extend the key handler to display the dialog when the "2" key is pressed.

Example 5.44. Key handler for HelloWorld : dialog is displayed when the "2" key is pressed

// Key handler
HANDLER_IMPLEMENT_BOOLEVENT(MyWindow, OnKey, event)
{
    switch (event.GetP16()) {
        ...
        
        // *** add the codes below

        case AVK_2:
            if (static_try()) {
                MyDialogPtr dialog;
                // create dialog with new operator
                if ((dialog = new MyDialog()) != null) {
                    // rest of the code is for error handling
                    static_throw(*dialog);
                    if (!static_try()) {
                        delete dialog;
                    }
                }
                else {
                    static_throw(SFERR_NO_MEMORY);
                }
            }
            return true;

    }
    return false;
}

Figure 5.21. Execution Result

Execution Result