DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

commit 9c5c7929a6234236c9370f5a79e9b4df82f2e4ad
parent c6e9bec69384bbb47fd6dffe30abb601d07804b8
Author: falkTX <falktx@falktx.com>
Date:   Thu,  6 May 2021 20:10:03 +0100

Cleanup SubWidget class

Signed-off-by: falkTX <falktx@falktx.com>

Diffstat:
Mdgl/SubWidget.hpp | 18+++++++++++-------
Mdgl/src/SubWidget.cpp | 5++---
Mdgl/src/WidgetPrivateData.cpp | 20+++++++++++---------
Mdgl/src/WidgetPrivateData.hpp | 4++--
Mtests/widgets/ExampleColorWidget.hpp | 17++++++++---------
5 files changed, 34 insertions(+), 30 deletions(-)

diff --git 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<int>& getAbsolutePos() const noexcept; + Point<int> 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 @@ -33,8 +33,7 @@ SubWidget::~SubWidget() template<typename T> bool SubWidget::contains(T x, T y) const noexcept { - const Size<uint>& size(getSize()); - return (x >= 0 && y >= 0 && static_cast<uint>(x) < size.getWidth() && static_cast<uint>(y) < size.getHeight()); + return Rectangle<double>(getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight()).contains(x, y); } template<typename T> @@ -53,7 +52,7 @@ int SubWidget::getAbsoluteY() const noexcept return pData->absolutePos.getY(); } -const Point<int>& SubWidget::getAbsolutePos() const noexcept +Point<int> SubWidget::getAbsolutePos() const noexcept { return pData->absolutePos; } diff --git 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<TopLevelWidget*>(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 @@ -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<Widget*> 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 @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2015 Filipe Coelho <falktx@falktx.com> + * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com> * * 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<uint> 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<uint>(width/6, height/6, width*2/3, height*2/3); } - - char cur; - bool reverse; - int r, g, b; - - Rectangle<uint> bgFull, bgSmall; }; // ------------------------------------------------------