PrevNextUpHome SophiaFramework UNIVERSE 5.3

3.5. Dialog

3.5.1. Message Dialog

Let's make the dialog(SFZMessageDialog) to display a notified message as the figure below.

Figure 3.28. Message dialog

Message dialog

The message dialog will be displayed when the item of text menu is selected.

The color scheme of the selected item of the text menu will be applied to this message dialog too.

The following is the code to define and implement the function to make the dialog in the helloworld application class.

Example 3.48. Define and implement the function to make the dialog

// define the helloworld class
SFMTYPEDEFCLASS(helloworld)  //  macro to generate the useful types
class helloworld : public SFYApplication {

    SFMSEALCOPY(helloworld)  //  macro to prohibit the developer from copying this instance

private:

    MyWindowSmp _myWindow;
    SFZTextMenuSmp _textMenu;

public:

    static SFCInvokerPtr Factory(Void);

private:

    explicit helloworld(Void) static_throws;
    virtual ~helloworld(Void);
    SFCError MakeMy(Void);
    SFCError MakeMenu(Void);
    Void SetMenuColors(UserColorConstRef color);

    // *** added code segments are in bold

    // make the dialog
    SFCError MakeColorDialog(UserColorConstRef color);

    XANDLER_DECLARE_VOIDRENDER(OnRenderRequest) // declare the drawing handler
    XANDLER_DECLARE_BOOLEVENT(OnKey)            // declare the key handler 
    XANDLER_DECLARE_VOIDRESULT(OnMenuResult)    // declare the result handler
};

// make the dialog
SFCError helloworld::MakeColorDialog(UserColorConstRef color)
{
    SFZMessageDialogSmp dlg;
    SFCError error(SFERR_NO_ERROR);

    // create the dialog
    if ((dlg = SFZMessageDialog::NewInstance(&error)) != null) {

        // set the dialog's parent responder to the application class(root)
        error = dlg->SetParent(GetThis());

        if (error == SFERR_NO_ERROR) {

            // set the dialog's message text to "Color Changed."
            error = dlg->SetMessageText("Color Changed.");

            if (error == SFERR_NO_ERROR) {

                // set the dialog's button text to "OK"
                error = dlg->SetButtonText("OK");

                if (error == SFERR_NO_ERROR) {

                    // set the background color
                    dlg->SetBackgroundColor(color.selBack);

                    // set the message text color
                    dlg->GetMessage()->SetTextColor(color.selFore);

                    // set the message alignment
                    dlg->GetMessage()->SetHorizontalAlign(SFZMultipleTextLabelControl::DEFAULT_HORIZONTAL);

                    // set the button control's bevel color
                    dlg->GetButton()->SetButtonColor(SFXBevelColor(color.light, color.base, color.dark));

                    // set the button control's text color
                    dlg->GetButton()->SetTextColor(color.itemFore);

                    // calculate the suitable region for the dialog
                    // with deflated device screen by (10,10)
                    SFXRectangle rect(GetLocalBound().Deflate(10, 10));
                    rect.Set(dlg->GetSuitableBound(rect));

                    // move the dialog to the center of the suitable region
                    rect.SnapCenterMiddle(GetLocalBound().GetCenterMiddle());

                    // set the dialog's real region
                    dlg->SetRealBound(rect);

                    // set the dialog's state to "visible" + "active" + "enable" + "focus" together 
                    dlg->SetState(true, true, true, true);

                    // move the dialog foremost
                    dlg->ToFront();

                    // schedule timer that HandleEscapeKey() will be booted up and the screen will be redrawn
                    // when neither the operation key, the ESCAPE key, nor the button has been pressed for 500 milliseconds
                    // * default implementation: close the dialog when neither the operation key, the ESCAPE key, nor the button has been pressed for 500 milliseconds
                    dlg->ScheduleTimer(500);
                }
            }
        }
    }
    return error;
}
[Tip] Parent responder of the dialog

In general, the parent responder of the dialog is set to the application class [i.e., root(SFZRoot) which the helloworld application class(SFYApplication) contains by default].

The root can be obtained by calling SFYApplication::GetRoot().

[Note] Internal control of the dialog

The text label control[SFZMultipleTextLabelControl] and the button control[SFZTextButtonControl] of the dialog can be obtained with the SFZMessageDialog::GetMessage function and the SFZMessageDialog::GetButton function respectively.

[Note] Timer processing of the dialog

With the SFZDialog::ScheduleTimer function, you can control the dialog same as it will handle the ESCAPE key when neither the operation key, the ESCAPE key, nor any button is pressed during the specified time period.

Concretely, when neither the operation key, the ESCAPE key, nor any button has been pressed for the time set with the SFZDialog::ScheduleTimer function elapses, the SFZDialog::HandleEscapeKey function will be called and the screen will be redrawn.

The SFZDialog::HandleEscapeKey function will send the result event [SFXEvent(SFEVT_RESPONDER_RESULT, SFP16_RESULT_ESCAPE, 0)], which will boot up the result handler.

In the above sample code, if the "dlg->ScheduleTimer(500);" statement is executed, the dlg dialog will receive the result event [SFXEvent(SFEVT_RESPONDER_RESULT, SFP16_RESULT_ESCAPE, 0)] when neither the operation key, the ESCAPE key, nor any button has been pressed for 500 milliseconds.

The default result handler which only closes this dialog will be booted up since no user-defined result handler is registered into the dlg dialog.

Next, let's update the result handler of the text menu to display the dialog when the item of the color scheme menu is selected.

Example 3.49. Update the result handler of the text menu

// result handler of the text menu
XANDLER_IMPLEMENT_VOIDRESULT(helloworld, OnMenuResult, invoker, reason, result)
{
    static UserColor::AtomRecConst color[] = {
        // ...(omitted)...
    };
    switch (reason) {
        case SFP16_RESULT_OK:
            TRACE("'%S' is selected", _textMenu->GetItemText(static_cast<SInt16>(result)).GetCString());
            switch (result) {
                case 0:
                case 1:
                case 2:
                case 3:
                    SetMenuColors((color[result]));

                    // *** added code segments are in bold

                    // make the dialog
                    MakeColorDialog((color[result]));

                    break;
                case 5:
                    _textMenu->SetMenuStyle(SFZTextMenu::SCROLLABLE_STYLE);
                    break;
                case 6:
                    _textMenu->SetMenuStyle(SFZTextMenu::PAGE_STYLE);
                    break;
            }
            break;
        case SFP16_RESULT_ESCAPE:
            invoker->Terminate();
            break;
    }
    return;
}

The following is execution result on BREW simulator.

Figure 3.29. Execution result

Execution result

Reference: Dialog(Basic)