Welcome to the document of mousetrap, the stand-alone C++ component of the mousetrap.jl GUI engine. If you are here because you want to use mousetrap in Julia, you are in the wrong place. Visit the mousetrap.jl documentation instead.
This documentation is a stub, most of the documentation efforts were focused on the Julia component. This also includes testing, which is performed by having the Julia wrapper call downstream C++ functions from Julia, as opposed to doing the testing C++-side.
Installation
See here for how to install mousetrap.
Credits
Both this C++ component, the C++-Julia interface, and the Julia component of mousetrap were created by C. Cords.
C++ documentation was generated using doxygen, styled with a custom doxygen-awesome theme.
Usage
Most likely, you're here because you want to contribute to mousetrap.jl
by modifying its back-end, so this page will focus on how to use the C++ component after already having read the entire mousetrap.jl manual.
Syntax is directly translatable, as one would expect:
# in Julia:
button = Button()
set_child!(button, Label("Label"))
connect_signal_clicked!(button) do self::Button
# ...
end
auto button = Button();
button.set_child(Label("Label"));
button.connect_signal_clicked([](Button& self){
});
With the exception that some signals have additional unused arguments or slightly different signatures. Furthermore, C++ mousetrap does not have Julia mousetraps main
, in C++ we need to connect to the apps' signal and run
it manually.
Examples
In lieu of a proper manual, examples for how to do common tasks will be given in this section.
Hello World
Creating a window that display the label "Hello World".
using namespace mousetrap;
int main()
{
{
window.set_child(
Label(
"Hello World"));
window.present();
});
}
object representing an entire application, supplies the main render loop, mapping of actions
Definition: application.hpp:54
int run()
start the main render loop
Definition: application.cpp:130
widget displaying text, supports pango attribute syntax
Definition: label.hpp:67
window, registered to an application
Definition: window.hpp:55
See the GitHub README for an example CMakeLists.txt
to build this an all other examples in this section.
Action
Triggering an Action
using a Button
or a keyboard shortcut.
using namespace mousetrap;
int main()
{
{
auto action =
Action(
"example.action", app);
action.set_function([](
Action&){
std::cout << "action called" << std::endl;
});
button.set_child(
Label(
"Click for Action"));
button.set_action(action);
action.add_shortcut("<Control>space");
window.set_listens_for_shortcut_actions(action);
window.set_child(button);
window.present();
});
}
Command with a name, registered to an application. See the manual section on actions for more informa...
Definition: action.hpp:57
void connect_signal_activate(Function_t f, Data_t data)
connect handler, will be invoked when signal is emitted. Data is passed to the signal handler functio...
Definition: signal_component.hpp:254
Signals
Create a box of 3 widgets indicating a boolean state. Their states are kept in synch by connecting to each widgets signal, then setting the other 2 widgets states from within that signal handler.
Signals need to be blocked at strategic times to prevent an infinite loop.
using namespace mousetrap;
int main()
{
{
auto vbox =
Box(Orientation::VERTICAL);
static auto light_switch =
Switch();
toggle_button.connect_signal_toggled([](
ToggleButton& self){
self.set_signal_toggled_blocked(true);
light_switch.set_is_active(state);
check_button.set_state(state ? CheckButtonState::ACTIVE : CheckButtonState::INACTIVE);
self.set_signal_toggled_blocked(false);
});
light_switch.connect_signal_switched([](
Switch& self,
void*){
self.set_signal_switched_blocked(true);
toggle_button.set_is_active(state);
check_button.set_state(state ? CheckButtonState::ACTIVE : CheckButtonState::INACTIVE);
self.set_signal_switched_blocked(false);
});
check_button.connect_signal_toggled([](
CheckButton& self){
self.set_signal_toggled_blocked(true);
toggle_button.set_is_active(state);
light_switch.set_is_active(state);
self.set_signal_toggled_blocked(false);
});
for (auto* widget : std::vector<Widget*>{
&toggle_button, &light_switch, &check_button
})
{
widget->set_expand(false);
widget->set_margin(10);
widget->set_alignment(Alignment::CENTER);
}
vbox.push_back(toggle_button);
vbox.push_back(light_switch);
vbox.push_back(check_button);
vbox.set_margin(10);
window.set_child(vbox);
window.present();
});
}
container widget that arranges each of its children in series
Definition: box.hpp:34
switch, can be click dragged or clicked to change a binary state
Definition: switch.hpp:40
bool get_is_active() const
get whether the switch is in the "on" position
Definition: switch.cpp:48
AlertDialog
Create a Window
with a Button
that, when clicked, shows an AlertDialog
the user has to dismiss.
using namespace mousetrap;
int main()
{
{
static auto dialog = AlertDialog("Is this a Dialog?");
dialog.add_button("Yes");
dialog.add_button("No");
dialog.on_selection([&](AlertDialog& self, int button_id){
std::cout << "User clicked: " << self.get_button_label(button_id) << std::endl;
});
button.set_child(
Label(
"Show Dialog"));
button.connect_signal_clicked([](
Button&){
dialog.present();
});
window.set_child(button);
window.present();
});
}
MotionController
Create an area that queries the current cursor position, moving updating a label as the cursor moves around.
using namespace mousetrap;
int main()
{
{
static auto label =
Label(
"(0, 0)");
static auto fixed =
Fixed();
fixed.add_child(label, {50, 50});
{
auto sstream = std::stringstream();
sstream << "(" << x << ", " << y << ")";
label.set_text(sstream.str());
fixed.set_child_position(label, {x, y});
});
fixed.add_controller(motion_controller);
fixed.set_cursor(CursorType::CROSSHAIR);
auto background_frame =
Frame();
background_frame.set_child(background);
background_frame.set_margin(10);
overlay.set_child(background_frame);
overlay.add_overlay(fixed);
window.set_child(overlay);
window.present();
});
}
container that positions a widget at a specific pixel position
Definition: fixed.hpp:33
draws frame around widget, rounded corners. Has exactly one child and an optional label widget
Definition: frame.hpp:33
handles cursor motion events
Definition: motion_event_controller.hpp:33
a widget that allows to display multiple widgets above a common child
Definition: overlay.hpp:33
non-container widget that fills it's area with a dark color, used mousetrap::Widget::set_opacity to m...
Definition: separator.hpp:33
Image
Fill a new image with a hue gradient, then display it as a widget.
using namespace mousetrap;
int main()
{
{
image.create(300, 300,
RGBA(0, 0, 0, 0));
auto hue_step = 1.f / 300;
for (uint64_t i = 0; i < image.get_size().x; ++i)
for (uint64_t j = 0; j < image.get_size().y; ++j)
image.set_pixel(i, j,
HSVA(i * hue_step, 1, 1, 1));
image_view.create_from_image(image);
window.set_child(image_view);
window.present();
});
}
widget that display an iamge
Definition: image_display.hpp:35
a 2d buffer container RGBA pixels
Definition: image.hpp:36
color representation in HSVA
Definition: color.hpp:76
color representation in RGBA
Definition: color.hpp:24
ColumnView
Create a 3x2 table filled with random widgets.
using namespace mousetrap;
int main()
{
{
column_view.push_back_column("Column 01");
column_view.push_back_column("Column 02");
column_view.push_back_column("Column 03");
window.set_child(column_view);
window.present();
});
}
view that displays widget in a table, ordered by column
Definition: column_view.hpp:50
a single-line text entry
Definition: entry.hpp:42
ListView
Fill a vertical list with random widgets, then connect to is selection model to monitor user selection:
using namespace mousetrap;
int main()
{
{
auto root =
ListView(Orientation::VERTICAL, SelectionMode::SINGLE);
auto item_02_it = root.push_back(
Label(
"Collapsible Item"));
root.push_back(
Label(
"Not Collapsible Item"));
root.push_back(
Button(), item_02_it);
root.push_back(
Entry(), item_02_it);
root.push_back(
Switch(), item_02_it);
root.get_selection_model()->connect_signal_selection_changed([](
SelectionModel&, uint64_t item_index, uint64_t item_count){
std::cout << "Selected item is now: " << item_index << std::endl;
});
window.set_child(root);
window.present();
});
}
Container widget that displays a number of items in a single row, nested lists are possible.
Definition: list_view.hpp:46
selection model, provides interface and signals for selectable widgets
Definition: selection_model.hpp:44
Menus
Create a menu that can contain 4 different kind of items.
using namespace mousetrap;
int main()
{
{
auto menu_item_action =
Action(
"example.menu_item", app);
menu_item_action.set_function([](
Action&){
std::cout << "example.menu_item called" << std::endl;
});
model.add_action("Action Item", menu_item_action);
auto menu_item_widget =
Box(Orientation::HORIZONTAL);
menu_item_widget.push_back(
Label(
"Enter Text: "));
menu_item_widget.push_back(
Entry());
menu_item_widget.set_spacing(10);
menu_item_widget.set_margin(10);
model.add_widget(menu_item_widget);
submenu_model.add_widget(
Label(
"Submenu Item"));
model.add_submenu("Submenu", submenu_model);
auto section_dummy_action =
Action(
"example.section_dummy", app);
section_dummy_action.set_function([](
Action&){});
section_model.add_action("Section Item # 1", section_dummy_action);
section_model.add_action("Section Item # 2", section_dummy_action);
section_model.add_action("Section Item # 3", section_dummy_action);
model.add_section("Section", section_model);
window.set_child(button);
window.present();
});
}
Revealer
Create a Button
that, when clicked, plays an animation to show another widget.
using namespace mousetrap;
int main()
{
{
auto image =
Image(150, 150,
RGBA(1, 0, 1, 1));
image_display.create_from_image(image);
image_display.set_expand(true);
image_display.set_size_request({0, 100});
revealer.set_child(image_display);
revealer.set_transition_duration(seconds(1.0));
revealer.set_transition_type(RevealerTransitionType::SLIDE_DOWN);
revealer.set_is_revealed(false);
revealer.set_expand_vertically(true);
button.set_child(
Label(
"Click to Reveal"));
button.set_expand_vertically(false);
button.connect_signal_clicked([](
Button& button){
revealer.set_is_revealed(not revealer.get_is_revealed());
});
auto content =
Box(Orientation::VERTICAL);
content.push_back(button);
content.push_back(revealer);
window.set_child(content);
window.present();
});
}
widget that contains exactly one child and animates the process of hiding / showing it
Definition: revealer.hpp:36
RenderArea
Render a rectangle using OpenGL.
using namespace mousetrap;
int main()
{
{
auto shape = Shape::Rectangle({-0.5, 0.5}, {1, 1});
shape.set_vertex_color(0,
HSVA(0.1, 1, 1, 1));
shape.set_vertex_color(1,
HSVA(0.3, 1, 1, 1));
shape.set_vertex_color(2,
HSVA(0.6, 1, 1, 1));
shape.set_vertex_color(3,
HSVA(0.9, 1, 1, 1));
canvas.add_render_task(task);
frame.set_child(canvas);
frame.set_size_request({150, 150});
window.set_child(frame);
window.present();
});
}
container widget that assures childs size conforms to given aspect ratio
Definition: aspect_frame.hpp:33
area that allows OpenGL primitives to be rendered
Definition: render_area.hpp:95
bundles a shape in a render task that can be invoked more conveniently during the render cycle
Definition: render_task.hpp:47