1 /*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2019 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 #include "WidgetPrivateData.hpp"
18
19 START_NAMESPACE_DGL
20
21 // -----------------------------------------------------------------------
22 // Widget
23
Widget(Window & parent)24 Widget::Widget(Window& parent)
25 : pData(new PrivateData(this, parent, nullptr, false))
26 {
27 parent._addWidget(this);
28 }
29
Widget(Widget * groupWidget)30 Widget::Widget(Widget* groupWidget)
31 : pData(new PrivateData(this, groupWidget->getParentWindow(), groupWidget, true))
32 {
33 pData->parent._addWidget(this);
34 }
35
Widget(Widget * groupWidget,bool addToSubWidgets)36 Widget::Widget(Widget* groupWidget, bool addToSubWidgets)
37 : pData(new PrivateData(this, groupWidget->getParentWindow(), groupWidget, addToSubWidgets))
38 {
39 pData->parent._addWidget(this);
40 }
41
~Widget()42 Widget::~Widget()
43 {
44 pData->parent._removeWidget(this);
45 delete pData;
46 }
47
isVisible() const48 bool Widget::isVisible() const noexcept
49 {
50 return pData->visible;
51 }
52
setVisible(bool yesNo)53 void Widget::setVisible(bool yesNo)
54 {
55 if (pData->visible == yesNo)
56 return;
57
58 pData->visible = yesNo;
59 pData->parent.repaint();
60 }
61
show()62 void Widget::show()
63 {
64 setVisible(true);
65 }
66
hide()67 void Widget::hide()
68 {
69 setVisible(false);
70 }
71
getWidth() const72 uint Widget::getWidth() const noexcept
73 {
74 return pData->size.getWidth();
75 }
76
getHeight() const77 uint Widget::getHeight() const noexcept
78 {
79 return pData->size.getHeight();
80 }
81
getSize() const82 const Size<uint>& Widget::getSize() const noexcept
83 {
84 return pData->size;
85 }
86
setWidth(uint width)87 void Widget::setWidth(uint width) noexcept
88 {
89 if (pData->size.getWidth() == width)
90 return;
91
92 ResizeEvent ev;
93 ev.oldSize = pData->size;
94 ev.size = Size<uint>(width, pData->size.getHeight());
95
96 pData->size.setWidth(width);
97 onResize(ev);
98
99 pData->parent.repaint();
100 }
101
setHeight(uint height)102 void Widget::setHeight(uint height) noexcept
103 {
104 if (pData->size.getHeight() == height)
105 return;
106
107 ResizeEvent ev;
108 ev.oldSize = pData->size;
109 ev.size = Size<uint>(pData->size.getWidth(), height);
110
111 pData->size.setHeight(height);
112 onResize(ev);
113
114 pData->parent.repaint();
115 }
116
setSize(uint width,uint height)117 void Widget::setSize(uint width, uint height) noexcept
118 {
119 setSize(Size<uint>(width, height));
120 }
121
setSize(const Size<uint> & size)122 void Widget::setSize(const Size<uint>& size) noexcept
123 {
124 if (pData->size == size)
125 return;
126
127 ResizeEvent ev;
128 ev.oldSize = pData->size;
129 ev.size = size;
130
131 pData->size = size;
132 onResize(ev);
133
134 pData->parent.repaint();
135 }
136
getAbsoluteX() const137 int Widget::getAbsoluteX() const noexcept
138 {
139 return pData->absolutePos.getX();
140 }
141
getAbsoluteY() const142 int Widget::getAbsoluteY() const noexcept
143 {
144 return pData->absolutePos.getY();
145 }
146
getAbsolutePos() const147 const Point<int>& Widget::getAbsolutePos() const noexcept
148 {
149 return pData->absolutePos;
150 }
151
setAbsoluteX(int x)152 void Widget::setAbsoluteX(int x) noexcept
153 {
154 setAbsolutePos(Point<int>(x, getAbsoluteY()));
155 }
156
setAbsoluteY(int y)157 void Widget::setAbsoluteY(int y) noexcept
158 {
159 setAbsolutePos(Point<int>(getAbsoluteX(), y));
160 }
161
setAbsolutePos(int x,int y)162 void Widget::setAbsolutePos(int x, int y) noexcept
163 {
164 setAbsolutePos(Point<int>(x, y));
165 }
166
setAbsolutePos(const Point<int> & pos)167 void Widget::setAbsolutePos(const Point<int>& pos) noexcept
168 {
169 if (pData->absolutePos == pos)
170 return;
171
172 PositionChangedEvent ev;
173 ev.oldPos = pData->absolutePos;
174 ev.pos = pos;
175
176 pData->absolutePos = pos;
177 onPositionChanged(ev);
178
179 pData->parent.repaint();
180 }
181
getParentApp() const182 Application& Widget::getParentApp() const noexcept
183 {
184 return pData->parent.getApp();
185 }
186
getParentWindow() const187 Window& Widget::getParentWindow() const noexcept
188 {
189 return pData->parent;
190 }
191
contains(int x,int y) const192 bool Widget::contains(int x, int y) const noexcept
193 {
194 return (x >= 0 && y >= 0 && static_cast<uint>(x) < pData->size.getWidth() && static_cast<uint>(y) < pData->size.getHeight());
195 }
196
contains(const Point<int> & pos) const197 bool Widget::contains(const Point<int>& pos) const noexcept
198 {
199 return contains(pos.getX(), pos.getY());
200 }
201
repaint()202 void Widget::repaint() noexcept
203 {
204 pData->parent.repaint();
205 }
206
getId() const207 uint Widget::getId() const noexcept
208 {
209 return pData->id;
210 }
211
setId(uint id)212 void Widget::setId(uint id) noexcept
213 {
214 pData->id = id;
215 }
216
onKeyboard(const KeyboardEvent &)217 bool Widget::onKeyboard(const KeyboardEvent&)
218 {
219 return false;
220 }
221
onSpecial(const SpecialEvent &)222 bool Widget::onSpecial(const SpecialEvent&)
223 {
224 return false;
225 }
226
onMouse(const MouseEvent &)227 bool Widget::onMouse(const MouseEvent&)
228 {
229 return false;
230 }
231
onMotion(const MotionEvent &)232 bool Widget::onMotion(const MotionEvent&)
233 {
234 return false;
235 }
236
onScroll(const ScrollEvent &)237 bool Widget::onScroll(const ScrollEvent&)
238 {
239 return false;
240 }
241
onResize(const ResizeEvent &)242 void Widget::onResize(const ResizeEvent&)
243 {
244 }
245
onPositionChanged(const PositionChangedEvent &)246 void Widget::onPositionChanged(const PositionChangedEvent&)
247 {
248 }
249
250 // -----------------------------------------------------------------------
251
252 END_NAMESPACE_DGL
253