Skip to content

Manipulating the Form

Alec Musasa edited this page Aug 1, 2021 · 15 revisions

According to the embedded XML documentation, the best place to manipulate the form is in an override of the on_initialize() method.

bool on_initialize(std::string& error) override {
    // manipulate form here
    return true;
}

Form Size

#include <liblec/lecui/controls.h>

To manipulate the form's size we need to use the dimensions class. It's found in the controls.h header.

lecui::dimensions _dim(*this);
_dim.set_size(lecui::size().width(500.f).height(300.f))
    .set_minimum(lecui::size().width(200.f).height(200.f));

This makes a form with a size of 500x300 pixels. When the form is resized, the minimum allowed size if 200x200 pixels. If we do not specify this minimum size the library sets a default for us. It is recommended that you set this explicitly for maximum control over your design.

It is generally a good idea to declare an in-class variable for the manipulation classes as shown in the code below.

#include <liblec/lecui/controls.h>
#include <liblec/lecui/containers/page.h>
#include <liblec/lecui/widgets/label.h>
using namespace liblec::lecui;

class sample_form : public form {
    page_manager _page_man{ *this };
    dimensions _dim{ *this };

    bool on_initialize(std::string& error) override {
        _dim
            .set_size(size().width(500.f).height(300.f))
            .set_minimum(size().width(200.f).height(200.f));
        return true;
    }

    bool on_layout(std::string& error) override {
        auto& home = _page_man.add("home");

        auto& label = widgets::label::add(home);
        label
            .text("This is a simple label")
            .rect()
            .left(10.f)
            .right(home.size().get_width() - 10.f)
            .top(10.f)
            .bottom(30.f);

        _page_man.show("home");
        return true;
    }

public:
    sample_form() :
        form("Sample Form") {}
};

int main() {
    sample_form form;
    std::string error;
    if (!form.show(error))
        form.message(error);
    return 0;
}

The result:

screenshot

Control Buttons

#include <liblec/lecui/controls.h>

To manipulate the form's control buttons we need to use the controls class. It's found in the controls.h header.

lecui::controls _ctrls{ *this };
_ctrls
   .allow_minimize(false)
   .allow_resize(false);

The .allow_minimize(false); line makes a form without a minimize button on the top right. The .allow_resize(false) makes a form that cannot be resized (and so does not have a maximize button on the top right).

Appearance

#include <liblec/lecui/appearance.h>

To manipulate the form's appearance we use the appearance class in the appearance.h header.

lecui::appearance _apprnc{ *this };
_apprnc
    .theme(lecui::themes::dark)
    .shadow(false);

In this code we set the form to the built-in dark theme (the default is light theme). We also remove the form's drop-shadow.

Putting it all together

#include <liblec/lecui/controls.h>
#include <liblec/lecui/containers/page.h>
#include <liblec/lecui/widgets/label.h>
#include <liblec/lecui/appearance.h>
using namespace liblec::lecui;

class sample_form : public form {
    page_manager _page_man{ *this };
    dimensions _dim{ *this };
    controls _ctrls{ *this };
    appearance _apprnc{ *this };

    bool on_initialize(std::string& error) override {
        _dim
            .set_size(size().width(500.f).height(300.f))
            .set_minimum(size().width(200.f).height(200.f));

        _ctrls
            .allow_minimize(false)
            .allow_resize(false);

        _apprnc
            .theme(themes::dark)
            .shadow(false);

        return true;
    }

    bool on_layout(std::string& error) override {
        auto& home = _page_man.add("home");

        auto& label = widgets::label::add(home);
        label
            .text("This is a simple label")
            .rect()
            .left(10.f)
            .right(home.size().get_width() - 10.f)
            .top(10.f)
            .bottom(30.f);

        _page_man.show("home");
        return true;
    }

public:
    sample_form() :
        form("Sample Form") {}
};

int main() {
    sample_form form;
    std::string error;
    if (!form.show(error))
        form.message(error);
    return 0;
}

The result:

screenshot

To see all the form properties that can be manipulated refer to the lecui html documentation or the in-code XML documentation.