PrevNextUpHome SophiaFramework UNIVERSE 5.3

9.13. Window(Applied)

The method to draw a window and scroll its virtual region will be described here.

9.13.1. Drawing a Window (1)

Figure 9.34. Execution Result

Execution Result

In addintion to placing controls and/or containers in SFZWindow, texts or images can be drawn too.

To draw a window(SFZWindow) externally, you have to register the drawing handler into the window(SFZWindow).

Example 9.58. Declaration

SFMTYPEDEFCLASS(USRApplication)
class USRApplication: public SFYApplication {
    SFMSEALCOPY(USRApplication)
private:
    SFZWindowSmp _window;

    ...
private:
    SFCError Make(Void);

    // drawing handler for window
    XANDLER_DECLARE_VOIDRENDER(OnRenderRequest)
};

Example 9.59. Implementation

SFCError USRApplication::Make(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // make window
    if ((_window = SFZWindow::NewInstance(&error)) != null) {

        // set window's parent responder to USRApplication
        error = _window->SetParent(GetThis());
        if (error == SFERR_NO_ERROR) {

            // register drawing handler into window
            error = _window->RegisterHandler(
                SFXEventRange(SFEVT_RESPONDER_RENDER, SFEVT_RESPONDER_RENDER, SFP16_RENDER_REQUEST, SFP16_RENDER_REQUEST),
                XANDLER_INTERNAL(OnRenderRequest)
            );
            if (error == SFERR_NO_ERROR) {

                // set window's real region
                _window->SetRealBound(GetLocalBound().Deflate(10, 10));

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

                // move window foremost
                _window->ToFront();
            }
        }
    }

    return error;
}

// drawing handler
XANDLER_IMPLEMENT_VOIDRENDER(USRApplication, OnRenderRequest, invoker, reason, graphics)
{
    SFXRectangle local;
    SInt16 i;
    SInt16 j;

    local.Set(_window->GetLocalBound());

    // draw check pattern
    for (j = 0; j < local.GetBottom(); j += 10) {
        for (i = 0; i < local.GetRight(); i += 10) {
            if ((i + j) / 10 % 2 == 0) {
                graphics->FillRectangle(SFXRectangle(i, j, 10, 10), SFXRGBColor(0xCC, 0xFF, 0xCC, 0x00));
            }
            else {
                graphics->FillRectangle(SFXRectangle(i, j, 10, 10), SFXRGBColor(0xAA, 0xDD, 0xAA, 0x00));
            }
        }
    }

    return;
}

9.13.2. Drawing a Window (2)

Figure 9.35. Execution Result

Execution Result

In addintion to placing controls and/or containers in a window, texts or images can be drawn too.

To draw the user-defined window inside itself, override the SFYWidget::HandleRenderRequest virtual function. In this case, you don't have to register the drawing handler.

[Note] Merit to override HandleRenderRequest()

The SFYWidget::HandleRenderRequest virtual function is called in the drawing handler that is registered into SFYWidget and will be booted up first when the (SFEVT_RESPONDER_RENDER, SFP16_RENDER_REQUEST) drawing event occurs.

Though it is possible to draw a window by registering its drawing handler, you don't have to register any drawing handler using this method.

Example 9.60. Declaration

SFMTYPEDEFRESPONDER(USRWindow)
class USRWindow: public SFZWindow {
    SFMSEALRESPONDER(USRWindow)
    SFMRESPONDERINSTANTIATEFOUR(USRWindow, SFZWindow, SFYContainer, SFYWidget, SFYResponder)

    ...
protected:

    // override HandleRenderRequest() virtual function booted up when drawing event is received
    virtual Void HandleRenderRequest(SFXGraphicsPtr graphics) const;
};

Example 9.61. Implementation

// drawing handler
Void USRWindow::HandleRenderRequest(SFXGraphicsPtr graphics) const
{
    SFXRectangle local;
    SInt16 i;
    SInt16 j;

    local.Set(GetLocalBound());

    // draw check pattern
    for (j = 0; j < local.GetBottom(); j += 10) {

        for (i = 0; i < local.GetRight(); i += 10) {

            if ((i + j) / 10 % 2 == 0) {

                graphics->FillRectangle(SFXRectangle(i, j, 10, 10), SFXRGBColor(0xCC, 0xFF, 0xCC, 0x00));
            }
            else {

                graphics->FillRectangle(SFXRectangle(i, j, 10, 10), SFXRGBColor(0xAA, 0xDD, 0xAA, 0x00));
            }
        }
    }

    return;
}

9.13.3. Scrolling the Virtual Region of a Window

If the virtual region of a window is set to a region larger than its real region, it can be scrolled up and down.

Figure 9.36. Execution Result(Left: before scrolling, Right: after scrolling)

Execution Result(Left: before scrolling, Right: after scrolling)

The sample code to scroll up and down the virtual region of a window is as follows:

Example 9.62. Declaration

SFMTYPEDEFCLASS(USRApplication)
class USRApplication: public SFYApplication {
    SFMSEALCOPY(USRApplication)
private:
    SFZWindowSmp _window;

    ...
private:
    SFCError Make(Void);
    XANDLER_DECLARE_VOIDRENDER(OnRenderRequest)
};

Example 9.63. Implementation

SFCError USRApplication::Make(Void)
{
    SFCError error(SFERR_NO_ERROR);

    if ((_window = SFZWindow::NewInstance(&error)) != null) {

        error = _window->SetParent(GetThis());
        if (error == SFERR_NO_ERROR) {

            error = _window->RegisterHandler(
                SFXEventRange(SFEVT_RESPONDER_RENDER, SFEVT_RESPONDER_RENDER, SFP16_RENDER_REQUEST, SFP16_RENDER_REQUEST),
                XANDLER_INTERNAL(OnRenderRequest)
            );
            if (error == SFERR_NO_ERROR) {

                _window->SetRealBound(GetLocalBound().Deflate(10, 10));

                // set virtual region to region obtained by expanding virtual region down by 100 pixels
                _window->SetVirtualBound(SFXRectangle(_window->GetVirtualBound()).AddBottom(100));

                // set scroll step to 5 pixels
                _window->SetScrollStep(5);

                // set flag indicating whether or not to move to top or bottom by next scrolling when reaching at bottom or top
                _window->SetScrollRepeat(true);

                _window->SetState(true, true, true, true);

                _window->ToFront();
            }
        }
    }

    return error;
}

XANDLER_IMPLEMENT_VOIDRENDER(USRApplication, OnRenderRequest, invoker, reason, graphics)
{
    SFXRectangle local;
    SInt16 i;
    SInt16 j;

    local.Set(_window->GetLocalBound());

    for (j = 0; j < local.GetBottom(); j += 10) {

        for (i = 0; i < local.GetRight(); i += 10) {

            if ((i + j) / 10 % 2 == 0) {

                graphics->FillRectangle(SFXRectangle(i, j, 10, 10), SFXRGBColor(0xCC, 0xFF - j, 0xCC + j, 0x00));
            }
            else {

                graphics->FillRectangle(SFXRectangle(i, j, 10, 10), SFXRGBColor(0xAA, 0xDD - j, 0xAA + j, 0x00));
            }
        }
    }

    return;
}
[Note] Note
In case of the user-defined window, it is the same as the above.