From 9c267cc87b78660aeb344db456b86ac4866cbfbc Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 20 Mar 2022 03:05:21 +0200 Subject: [PATCH] add on_click to buttons --- src/Button.cpp | 3 +++ src/Button.hpp | 3 +++ src/main.cpp | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/src/Button.cpp b/src/Button.cpp index 6f9ede6..6c55852 100644 --- a/src/Button.cpp +++ b/src/Button.cpp @@ -40,6 +40,9 @@ void Button::on_focus_update(FocusUpdateEvent &event) { void Button::on_activation_update(ActivationUpdateEvent &event) { update_color(); + if (event.get_activation_status() == false) { + on_click(); + } } } diff --git a/src/Button.hpp b/src/Button.hpp index 9e8355b..90b531c 100644 --- a/src/Button.hpp +++ b/src/Button.hpp @@ -1,4 +1,5 @@ #include +#include #include "Widget.hpp" #include "RGB.hpp" #include "pango/pango-font.h" @@ -19,6 +20,8 @@ public: void set_text(std::string text) { m_text = text; wants_repaint(); } std::string &get_text() { return m_text; } + std::function on_click { [](){} }; +protected: void on_paint(); void on_init(); void on_focus_update(FocusUpdateEvent &event); diff --git a/src/main.cpp b/src/main.cpp index 76a72c4..17e3edc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,10 @@ int main() { main_widget.set_do_background_fill(true); window.set_main_widget(&main_widget); + button.on_click = [&button]() { + button.set_text("clicked!"); + }; + main_widget.add_child(&button); window.run(true);