Boost.UI
User Interface Boost library
cpp11/snippet.cpp

Documentation snippets

// Copyright (c) 2017 Kolya Kosenko
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Compilable source code snippets that are used in documentation.
#include <boost/config.hpp>
#include <chrono>
#include <boost/ui.hpp>
#include <boost/ui/native/winmain.cpp>
namespace ui = boost::ui;
class button_dialog : public ui::dialog
{
public:
button_dialog() : ui::dialog("Button usage dialog")
{
ui::button(*this, "&Press me")
.on_press(&button_dialog::on_press_me, this);
show_modal();
}
private:
void on_press_me()
{
ui::info_dialog("Hello");
}
};
class my_dialog : public ui::dialog
{
public:
my_dialog() : ui::dialog("Dialog example")
{
ui::button(*this, "&Close this dialog")
.on_press(&my_dialog::on_close, this);
}
private:
void on_close()
{
close();
}
};
void run_dialog()
{
my_dialog().show_modal();
}
class my_frame : public ui::frame
{
typedef my_frame this_type;
public:
my_frame() : ui::frame("Frame example")
{
ui::button(*this, "&Close this frame")
.on_press(&this_type::on_close, this);
menu_bar()
<< ( ui::menu("&File")
<< ui::menu::item("&Quit")
.on_press(&this_type::on_close, this)
)
;
status_bar().text("Ready");
}
private:
void on_close()
{
close();
}
};
void run_frame()
{
my_frame().show_modal();
}
void run_gui()
{
ui::dialog parent("Boost.UI documentation snippets");
ui::vbox layout(parent);
layout << ui::button(parent, "&Button").on_press([]{ button_dialog(); });
layout << (ui::hbox()
<< ui::button(parent, "&Dialog").on_press([]{ run_dialog(); })
<< ui::button(parent, "&Frame") .on_press([]{ run_frame(); })
);
layout << ui::button(parent, "&Event").on_press([]
{
ui::dialog dlg("Example dialog");
ui::button(dlg, "&Quit")
.on_press(&ui::dialog::close, &dlg);
dlg.show_modal();
});
layout << ui::button(parent, "&Timeout").on_press([]
{
ui::on_timeout(std::chrono::milliseconds(1000),
[]{ BOOST_UI_LOG; }
);
});
ui::string_box string_box = ui::string_box(parent, "Initial text");
string_box.on_edit([&]
{
ui::log::info() << "Edited text: " << string_box.text();
});
layout << string_box.layout().justify();
ui::combo_box combo_box = ui::combo_box(parent,
"Initial text",
{ "Option 1", "Option 2", "Option 3" })
.on_select_event([&](ui::index_event& e)
{
ui::log::info() << "Selected option index: " << e.index()
<< ", text: " << combo_box.text();
});
layout << combo_box.layout().justify();
ui::choice choice = ui::choice(parent,
{ "Option 1", "Option 2", "Option 3" })
.on_select_event([&](ui::index_event& e)
{
ui::log::info() << "Selected option index: " << e.index()
<< ", text: " << choice.text();
});
layout << choice.layout().justify();
ui::list_box list_box = ui::list_box(parent,
{ "Option 1", "Option 2", "Option 3" })
.on_select_event([&](ui::index_event& e)
{
ui::log::info() << "Selected option index: " << e.index();
})
.on_activate_event([&](ui::index_event& e)
{
ui::log::info() << "Activated option index: " << e.index();
})
;
layout << list_box.layout().justify();
parent.resize(400, 600);
parent.show_modal();
}
int ui_main()
{
run_gui();
return 0;
}
int main(int argc, char* argv[])
{
return ui::entry(&ui_main, argc, argv);
}
boost::ui::log::BOOST_UI_LOG
#define BOOST_UI_LOG
Logs current file, line and function.
Definition: log.hpp:120
boost::ui::on_timeout
void on_timeout(const std::chrono::duration< Rep, Period > &d, F &&f, Args &&... args)
Calls function one time after the specified time duration.
Definition: application.hpp:105
boost::ui
Boost.UI library namespace.
Definition: window.hpp:20
boost::ui::info_dialog
BOOST_UI_DECL void info_dialog(const uistring &message, const uistring &title=ascii("Information"))
Display information in application-modal dialog.
boost::ui::entry
int entry(int(*ui_main)(int, char *[]), int argc, char *argv[])
UI application entry.
ui.hpp
Master Boost.UI file.