DISTRHO Plugin Framework
SubWidget.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any purpose with
6  * or without fee is hereby granted, provided that the above copyright notice and this
7  * permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef DGL_SUBWIDGET_HPP_INCLUDED
18 #define DGL_SUBWIDGET_HPP_INCLUDED
19 
20 #include "Widget.hpp"
21 
22 START_NAMESPACE_DGL
23 
24 // --------------------------------------------------------------------------------------------------------------------
25 
26 /**
27  Sub-Widget class.
28 
29  This class is the main entry point for creating any reusable widgets from within DGL.
30  It can be freely positioned from within a parent widget, thus being named subwidget.
31 
32  Many subwidgets can share the same parent, and subwidgets themselves can also have its own subwidgets.
33  It is subwidgets all the way down.
34 
35  TODO check absolute vs relative position and see what makes more sense.
36 
37  @see CairoSubWidget
38  */
39 class SubWidget : public Widget
40 {
41 public:
42  /**
43  Constructor.
44  */
45  explicit SubWidget(Widget* parentWidget);
46 
47  /**
48  Destructor.
49  */
50  virtual ~SubWidget();
51 
52  /**
53  Check if this widget contains the point defined by @a x and @a y.
54  */
55  // TODO rename as containsRelativePos
56  template<typename T>
57  bool contains(T x, T y) const noexcept;
58 
59  /**
60  Check if this widget contains the point @a pos.
61  */
62  // TODO rename as containsRelativePos
63  template<typename T>
64  bool contains(const Point<T>& pos) const noexcept;
65 
66  /**
67  Get absolute X.
68  */
69  int getAbsoluteX() const noexcept;
70 
71  /**
72  Get absolute Y.
73  */
74  int getAbsoluteY() const noexcept;
75 
76  /**
77  Get absolute position.
78  */
79  Point<int> getAbsolutePos() const noexcept;
80 
81  /**
82  Get absolute area of this subwidget.
83  This is the same as `Rectangle<int>(getAbsolutePos(), getSize());`
84  @see getConstrainedAbsoluteArea()
85  */
86  Rectangle<int> getAbsoluteArea() const noexcept;
87 
88  /**
89  Get absolute area of this subwidget, with special consideration for not allowing negative values.
90  @see getAbsoluteArea()
91  */
93 
94  /**
95  Set absolute X.
96  */
97  void setAbsoluteX(int x) noexcept;
98 
99  /**
100  Set absolute Y.
101  */
102  void setAbsoluteY(int y) noexcept;
103 
104  /**
105  Set absolute position using @a x and @a y values.
106  */
107  void setAbsolutePos(int x, int y) noexcept;
108 
109  /**
110  Set absolute position.
111  */
112  void setAbsolutePos(const Point<int>& pos) noexcept;
113 
114  /**
115  Get parent Widget, as passed in the constructor.
116  */
117  Widget* getParentWidget() const noexcept;
118 
119  /**
120  Request repaint of this subwidget's area to the window this widget belongs to.
121  */
122  void repaint() noexcept override;
123 
124  /**
125  Bring this widget to the "front" of the parent widget.
126  Makes the widget behave as if it was the last to be registered on the parent widget, thus being "in front".
127  */
128  virtual void toFront();
129 
130  /**
131  Indicate that this subwidget will draw out of bounds, and thus needs the entire viewport available for drawing.
132  */
133  void setNeedsFullViewportDrawing(bool needsFullViewportForDrawing = true);
134 
135  /**
136  Indicate that this subwidget will always draw at its own internal size and needs scaling to fit target size.
137  */
138  void setNeedsViewportScaling(bool needsViewportScaling = true, double autoScaleFactor = 0.0);
139 
140  /**
141  Indicate that this subwidget should not be drawn on screen, typically because it is managed by something else.
142  */
143  void setSkipDrawing(bool skipDrawing = true);
144 
145 protected:
146  /**
147  A function called when the subwidget's absolute position is changed.
148  */
149  virtual void onPositionChanged(const PositionChangedEvent&);
150 
151 private:
152  struct PrivateData;
153  PrivateData* const pData;
154  friend class Widget;
155  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SubWidget)
156 };
157 
158 // --------------------------------------------------------------------------------------------------------------------
159 
160 END_NAMESPACE_DGL
161 
162 #endif // DGL_SUBWIDGET_HPP_INCLUDED
163 
SubWidget::getAbsoluteArea
Rectangle< int > getAbsoluteArea() const noexcept
SubWidget::getAbsoluteX
int getAbsoluteX() const noexcept
SubWidget::setAbsolutePos
void setAbsolutePos(int x, int y) noexcept
SubWidget::setAbsoluteY
void setAbsoluteY(int y) noexcept
Rectangle
Definition: Geometry.hpp:30
SubWidget::setAbsoluteX
void setAbsoluteX(int x) noexcept
SubWidget::setNeedsViewportScaling
void setNeedsViewportScaling(bool needsViewportScaling=true, double autoScaleFactor=0.0)
SubWidget::setNeedsFullViewportDrawing
void setNeedsFullViewportDrawing(bool needsFullViewportForDrawing=true)
SubWidget::toFront
virtual void toFront()
SubWidget::setSkipDrawing
void setSkipDrawing(bool skipDrawing=true)
SubWidget::getAbsoluteY
int getAbsoluteY() const noexcept
Widget::PositionChangedEvent
Definition: Widget.hpp:250
SubWidget::getConstrainedAbsoluteArea
Rectangle< uint > getConstrainedAbsoluteArea() const noexcept
SubWidget::repaint
void repaint() noexcept override
SubWidget::contains
bool contains(T x, T y) const noexcept
SubWidget::onPositionChanged
virtual void onPositionChanged(const PositionChangedEvent &)
SubWidget::getParentWidget
Widget * getParentWidget() const noexcept
Point
Definition: Geometry.hpp:40
SubWidget
Definition: SubWidget.hpp:39
SubWidget::getAbsolutePos
Point< int > getAbsolutePos() const noexcept
Widget
Definition: Widget.hpp:53
SubWidget::~SubWidget
virtual ~SubWidget()