Skip to content

Manipulating the Form

Alec Musasa edited this page Jul 12, 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({ 500, 300 }).set_minimum({ 200, 200 });

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;

class sample_form : public lecui::form {
    lecui::page_manager page_man_{ *this };
    lecui::dimensions dim_{ *this };

    bool on_initialize(std::string& error) override {
        dim_
            .set_size({ 500, 300 })
            .set_minimum({ 200, 200 });
        return true;
    }

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

        lecui::widgets::label_builder label(home);
        label()
            .text("This is a simple label")
            .rect({ 10.f, home.size().width - 10.f, 10.f, 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;

class sample_form : public lecui::form {
    lecui::page_manager page_man_{ *this };
    lecui::dimensions dim_{ *this };
    lecui::controls ctrls_{ *this };
    lecui::appearance apprnc_{ *this };

bool on_initialize(std::string& error) override {
    dim_
        .set_size({ 500, 300 })
        .set_minimum({ 200, 200 });

    ctrls_
        .allow_minimize(false)
        .allow_resize(false);

    apprnc_
        .theme(lecui::themes::dark)
        .shadow(false);

    return true;
}

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

        lecui::widgets::label_builder label(home);
        label()
            .text("This is a simple label")
            .rect({ 10.f, home.size().width - 10.f, 10.f, 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