Skip to content
Alec Musasa edited this page Mar 21, 2022 · 12 revisions

Asynchronous event driven programming is at the heart of the lecui library. There are various kinds of events that can be handled.

Widget Events

All widgets have various events that can be handled, e.g. the click event.

To add an event handler to a widget we need to pass a lambda function to the relevant event property.

auto& label = widgets::label::add(home);
label
    .text("This is a simple label")
    .rect(rect()
        .left(10.f)
        .right(home.size().get_width() - 10.f)
        .top(10.f)
        .bottom(30.f))
    .events().action = [this]() { message("Label event handler called!"); };

Here we have added an action handler to the label. According to the embedded XML documentation the handler is called when either the space bar or enter key is pressed, or if the widget is clicked.

The result:

screenshot

Things to note:

  • Action handlers are added through the .events() property
  • Action handlers are lambda functions
  • When a widget has no action handler it is static, i.e. it will not respond to mouse movements, tab key navigation etc. After an event handler it added the widget comes alive and becomes active, responding to input.

Form Events

The form class has various events for which handlers can be defined.

caption event

The caption event is called when the form's caption receives an action (a click, enter key or space bar). The event is set through the .caption member in the form_events class.

events().caption = [this]() { message("Form caption clicked!"); };

drop_files event

The drop_files event is called when files are dropped on the form through a drag and drop operation from the operating system's file system. Refer to either the lecui html documentation or the in-code XML documentation for more details.

events().drop_files = [this](const std::string& file) { message("Receiving file: " + file); };

The drop_files handler has only one parameter, the full path to the file.

The result:

screenshot

receive_data event

The receive_data event is called when data is sent from another lecui app using the .send_data() method in the instance_manager class.

events().receive_data = [this](const std::string& data) { message("Data received: " + data); };

The result:

screenshot

For other events refer to the html documentation or the in-code XML documentation