From 7a0a7abc52e7aad185b581481d91395e18d6c178 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 13 Oct 2018 23:01:06 +0200 Subject: [PATCH] Try out a signal-like system like this: auto* b = new Button; b->onClick = [] (Button&) { printf("The button was clicked!\n"); }; --- Widgets/Button.cpp | 3 +++ Widgets/Button.h | 2 ++ Widgets/Widget.h | 1 + Widgets/test.cpp | 4 ++++ 4 files changed, 10 insertions(+) diff --git a/Widgets/Button.cpp b/Widgets/Button.cpp index 9313ed143b..10ec97aa50 100644 --- a/Widgets/Button.cpp +++ b/Widgets/Button.cpp @@ -87,5 +87,8 @@ void Button::mouseUpEvent(MouseEvent& event) update(); Widget::mouseUpEvent(event); + + if (onClick) + onClick(*this); } diff --git a/Widgets/Button.h b/Widgets/Button.h index 98c63596eb..09ad9e0458 100644 --- a/Widgets/Button.h +++ b/Widgets/Button.h @@ -11,6 +11,8 @@ public: String caption() const { return m_caption; } void setCaption(String&&); + std::function onClick; + private: virtual void paintEvent(PaintEvent&) override; virtual void mouseDownEvent(MouseEvent&) override; diff --git a/Widgets/Widget.h b/Widgets/Widget.h index d592e91375..e025dc3aed 100644 --- a/Widgets/Widget.h +++ b/Widgets/Widget.h @@ -5,6 +5,7 @@ #include "Rect.h" #include "Color.h" #include +#include class Window; diff --git a/Widgets/test.cpp b/Widgets/test.cpp index dab26bb5da..5e8f403779 100644 --- a/Widgets/test.cpp +++ b/Widgets/test.cpp @@ -63,6 +63,10 @@ int main(int argc, char** argv) b->setWindowRelativeRect({ 0, 20, 100, 20 }); b->setCaption("Button"); + b->onClick = [] (Button& button) { + printf("Button %p clicked!\n", &button); + }; + auto* c = new CheckBox(widgetTestWindowWidget); c->setWindowRelativeRect({ 0, 40, 100, 20 }); c->setCaption("CheckBox");