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 the margin currently in use for widget coordinates.
116  By default this value is (0,0).
117  */
118  Point<int> getMargin() const noexcept;
119 
120  /**
121  Set a margin to be used for widget coordinates using @a x and @a y values.
122  */
123  void setMargin(int x, int y) noexcept;
124 
125  /**
126  Set a margin to be used for widget coordinates.
127  */
128  void setMargin(const Point<int>& offset) noexcept;
129 
130  /**
131  Get parent Widget, as passed in the constructor.
132  */
133  Widget* getParentWidget() const noexcept;
134 
135  /**
136  Request repaint of this subwidget's area to the window this widget belongs to.
137  */
138  void repaint() noexcept override;
139 
140  /**
141  Bring this widget to the "front" of the parent widget.
142  Makes the widget behave as if it was the last to be registered on the parent widget, thus being "in front".
143  */
144  virtual void toFront();
145 
146  /**
147  Indicate that this subwidget will draw out of bounds, and thus needs the entire viewport available for drawing.
148  */
149  void setNeedsFullViewportDrawing(bool needsFullViewportForDrawing = true);
150 
151  /**
152  Indicate that this subwidget will always draw at its own internal size and needs scaling to fit target size.
153  */
154  void setNeedsViewportScaling(bool needsViewportScaling = true, double autoScaleFactor = 0.0);
155 
156  /**
157  Indicate that this subwidget should not be drawn on screen, typically because it is managed by something else.
158  */
159  void setSkipDrawing(bool skipDrawing = true);
160 
161 protected:
162  /**
163  A function called when the subwidget's absolute position is changed.
164  */
165  virtual void onPositionChanged(const PositionChangedEvent&);
166 
167 private:
168  struct PrivateData;
169  PrivateData* const pData;
170  friend class Widget;
171  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SubWidget)
172 };
173 
174 // --------------------------------------------------------------------------------------------------------------------
175 
176 END_NAMESPACE_DGL
177 
178 #endif // DGL_SUBWIDGET_HPP_INCLUDED
179 
SubWidget::getAbsoluteArea
Rectangle< int > getAbsoluteArea() const noexcept
SubWidget::setMargin
void setMargin(int x, int y) 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::getMargin
Point< int > getMargin() const noexcept
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()