Browse Source

Cleanup SubWidget class

Signed-off-by: falkTX <falktx@falktx.com>
pull/272/head
falkTX 4 years ago
parent
commit
9c5c7929a6
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
5 changed files with 34 additions and 30 deletions
  1. +11
    -7
      dgl/SubWidget.hpp
  2. +2
    -3
      dgl/src/SubWidget.cpp
  3. +11
    -9
      dgl/src/WidgetPrivateData.cpp
  4. +2
    -2
      dgl/src/WidgetPrivateData.hpp
  5. +8
    -9
      tests/widgets/ExampleColorWidget.hpp

+ 11
- 7
dgl/SubWidget.hpp View File

@@ -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



+ 2
- 3
dgl/src/SubWidget.cpp View File

@@ -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;
}


+ 11
- 9
dgl/src/WidgetPrivateData.cpp View File

@@ -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;
}



+ 2
- 2
dgl/src/WidgetPrivateData.hpp View File

@@ -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


+ 8
- 9
tests/widgets/ExampleColorWidget.hpp View File

@@ -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;
};

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


Loading…
Cancel
Save