From 9c5c7929a6234236c9370f5a79e9b4df82f2e4ad Mon Sep 17 00:00:00 2001 From: falkTX Date: Thu, 6 May 2021 20:10:30 +0100 Subject: [PATCH] Cleanup SubWidget class Signed-off-by: falkTX --- dgl/SubWidget.hpp | 18 +++++++++++------- dgl/src/SubWidget.cpp | 5 ++--- dgl/src/WidgetPrivateData.cpp | 20 +++++++++++--------- dgl/src/WidgetPrivateData.hpp | 4 ++-- tests/widgets/ExampleColorWidget.hpp | 17 ++++++++--------- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/dgl/SubWidget.hpp b/dgl/SubWidget.hpp index 45778703..7510ae83 100644 --- a/dgl/SubWidget.hpp +++ b/dgl/SubWidget.hpp @@ -21,16 +21,20 @@ START_NAMESPACE_DGL -// ----------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- /** Sub-Widget class. - This is a handy Widget class that can be freely positioned to be used directly on a Window. + This class is the main entry point for creating any reusable widgets from within DGL. + It can be freely positioned from within a parent widget, thus being named subwidget. - This widget takes the full size of the Window it is mapped to. - Sub-widgets can be added on top of this top-level widget, by creating them with this class as parent. - Doing so allows for custom position and sizes. + Many subwidgets can share the same parent, and subwidgets themselves can also have its own subwidgets. + It is subwidgets all the way down. + + TODO check absolute vs relative position and see what makes more sense. + + @see CairoSubWidget */ class SubWidget : public Widget { @@ -70,7 +74,7 @@ public: /** Get absolute position. */ - const Point& getAbsolutePos() const noexcept; + Point getAbsolutePos() const noexcept; /** Set absolute X. @@ -104,7 +108,7 @@ private: DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SubWidget) }; -// ----------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- END_NAMESPACE_DGL diff --git a/dgl/src/SubWidget.cpp b/dgl/src/SubWidget.cpp index 7a4838b6..48edb7d5 100644 --- a/dgl/src/SubWidget.cpp +++ b/dgl/src/SubWidget.cpp @@ -33,8 +33,7 @@ SubWidget::~SubWidget() template bool SubWidget::contains(T x, T y) const noexcept { - const Size& size(getSize()); - return (x >= 0 && y >= 0 && static_cast(x) < size.getWidth() && static_cast(y) < size.getHeight()); + return Rectangle(getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight()).contains(x, y); } template @@ -53,7 +52,7 @@ int SubWidget::getAbsoluteY() const noexcept return pData->absolutePos.getY(); } -const Point& SubWidget::getAbsolutePos() const noexcept +Point SubWidget::getAbsolutePos() const noexcept { return pData->absolutePos; } diff --git a/dgl/src/WidgetPrivateData.cpp b/dgl/src/WidgetPrivateData.cpp index 73f493c2..e09c8860 100644 --- a/dgl/src/WidgetPrivateData.cpp +++ b/dgl/src/WidgetPrivateData.cpp @@ -31,7 +31,7 @@ START_NAMESPACE_DGL Widget::PrivateData::PrivateData(Widget* const s, TopLevelWidget* const tlw) : self(s), topLevelWidget(tlw), - groupWidget(nullptr), + parentGroupWidget(nullptr), id(0), needsScaling(false), visible(true), @@ -43,20 +43,20 @@ Widget::PrivateData::PrivateData(Widget* const s, TopLevelWidget* const tlw) Widget::PrivateData::PrivateData(Widget* const s, Widget* const g) : self(s), topLevelWidget(findTopLevelWidget(g)), - groupWidget(g), + parentGroupWidget(g), id(0), needsScaling(false), visible(true), size(0, 0), subWidgets() { - groupWidget->pData->subWidgets.push_back(self); + parentGroupWidget->pData->subWidgets.push_back(self); } Widget::PrivateData::~PrivateData() { - if (groupWidget != nullptr) - groupWidget->pData->subWidgets.remove(self); + if (parentGroupWidget != nullptr) + parentGroupWidget->pData->subWidgets.remove(self); subWidgets.clear(); } @@ -74,8 +74,8 @@ void Widget::PrivateData::displaySubWidgets(const uint width, const uint height, void Widget::PrivateData::repaint() { - if (groupWidget != nullptr) - groupWidget->repaint(); + if (parentGroupWidget != nullptr) + parentGroupWidget->repaint(); else if (topLevelWidget != nullptr) topLevelWidget->repaint(); } @@ -84,10 +84,12 @@ void Widget::PrivateData::repaint() TopLevelWidget* Widget::PrivateData::findTopLevelWidget(Widget* const w) { + if (TopLevelWidget* const tlw = dynamic_cast(w)) + return tlw; if (w->pData->topLevelWidget != nullptr) return w->pData->topLevelWidget; - if (w->pData->groupWidget != nullptr) - return findTopLevelWidget(w->pData->groupWidget); + if (w->pData->parentGroupWidget != nullptr) + return findTopLevelWidget(w->pData->parentGroupWidget); return nullptr; } diff --git a/dgl/src/WidgetPrivateData.hpp b/dgl/src/WidgetPrivateData.hpp index 7df7a623..0331c0d1 100644 --- a/dgl/src/WidgetPrivateData.hpp +++ b/dgl/src/WidgetPrivateData.hpp @@ -28,7 +28,7 @@ START_NAMESPACE_DGL struct Widget::PrivateData { Widget* const self; TopLevelWidget* const topLevelWidget; - Widget* const groupWidget; + Widget* const parentGroupWidget; uint id; bool needsScaling; bool visible; @@ -36,7 +36,7 @@ struct Widget::PrivateData { std::list subWidgets; PrivateData(Widget* const s, TopLevelWidget* const tlw); - PrivateData(Widget* const s, Widget* const g); + PrivateData(Widget* const s, Widget* const pgw); ~PrivateData(); // NOTE display function is different depending on build type diff --git a/tests/widgets/ExampleColorWidget.hpp b/tests/widgets/ExampleColorWidget.hpp index ce35e5e6..f5b913b7 100644 --- a/tests/widgets/ExampleColorWidget.hpp +++ b/tests/widgets/ExampleColorWidget.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2015 Filipe Coelho + * Copyright (C) 2012-2021 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -21,7 +21,6 @@ // DGL Stuff #include "../../dgl/SubWidget.hpp" -// #include "Window.hpp" START_NAMESPACE_DGL @@ -31,6 +30,12 @@ START_NAMESPACE_DGL class ExampleColorWidget : public SubWidget, public IdleCallback { + char cur; + bool reverse; + int r, g, b; + + Rectangle bgFull, bgSmall; + public: ExampleColorWidget(TopLevelWidget* const topWidget) : SubWidget(topWidget), @@ -40,7 +45,7 @@ public: { setSize(300, 300); -// groupWidget->getApp().addIdleCallback(this); +// topWidget->getApp().addIdleCallback(this); } protected: @@ -119,12 +124,6 @@ protected: // small bg, centered 2/3 size bgSmall = Rectangle(width/6, height/6, width*2/3, height*2/3); } - - char cur; - bool reverse; - int r, g, b; - - Rectangle bgFull, bgSmall; }; // ------------------------------------------------------