Skip to content

Commit

Permalink
Cleanup SubWidget class
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed May 6, 2021
1 parent c6e9bec commit 9c5c792
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
18 changes: 11 additions & 7 deletions dgl/SubWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -70,7 +74,7 @@ class SubWidget : public Widget
/**
Get absolute position.
*/
const Point<int>& getAbsolutePos() const noexcept;
Point<int> getAbsolutePos() const noexcept;

/**
Set absolute X.
Expand Down Expand Up @@ -104,7 +108,7 @@ class SubWidget : public Widget
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SubWidget)
};

// -----------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------

END_NAMESPACE_DGL

Expand Down
5 changes: 2 additions & 3 deletions dgl/src/SubWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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;
}
Expand Down
20 changes: 11 additions & 9 deletions dgl/src/WidgetPrivateData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions dgl/src/WidgetPrivateData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ START_NAMESPACE_DGL
struct Widget::PrivateData {
Widget* const self;
TopLevelWidget* const topLevelWidget;
Widget* const groupWidget;
Widget* const parentGroupWidget;
uint id;
bool needsScaling;
bool visible;
Size<uint> size;
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
Expand Down
17 changes: 8 additions & 9 deletions tests/widgets/ExampleColorWidget.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2015 Filipe Coelho <[email protected]>
* Copyright (C) 2012-2021 Filipe Coelho <[email protected]>
*
* 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
Expand All @@ -21,7 +21,6 @@
// DGL Stuff

#include "../../dgl/SubWidget.hpp"
// #include "Window.hpp"

START_NAMESPACE_DGL

Expand All @@ -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),
Expand All @@ -40,7 +45,7 @@ class ExampleColorWidget : public SubWidget,
{
setSize(300, 300);

// groupWidget->getApp().addIdleCallback(this);
// topWidget->getApp().addIdleCallback(this);
}

protected:
Expand Down Expand Up @@ -119,12 +124,6 @@ class ExampleColorWidget : public SubWidget,
// 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;
};

// ------------------------------------------------------
Expand Down

0 comments on commit 9c5c792

Please sign in to comment.