Cleanup
[carla.git] / source / modules / dgl / src / WidgetPrivateData.cpp
blob59b87901cfb31b58be191772d6dce2fa725047c3
1 /*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
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.
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.
17 #include "WidgetPrivateData.hpp"
18 #include "SubWidgetPrivateData.hpp"
19 #include "../TopLevelWidget.hpp"
21 START_NAMESPACE_DGL
23 #define FOR_EACH_SUBWIDGET(it) \
24 for (std::list<SubWidget*>::iterator it = subWidgets.begin(); it != subWidgets.end(); ++it)
26 #define FOR_EACH_SUBWIDGET_INV(rit) \
27 for (std::list<SubWidget*>::reverse_iterator rit = subWidgets.rbegin(); rit != subWidgets.rend(); ++rit)
29 // -----------------------------------------------------------------------
31 Widget::PrivateData::PrivateData(Widget* const s, TopLevelWidget* const tlw)
32 : self(s),
33 topLevelWidget(tlw),
34 parentWidget(nullptr),
35 id(0),
36 needsScaling(false),
37 visible(true),
38 size(0, 0),
39 subWidgets() {}
41 Widget::PrivateData::PrivateData(Widget* const s, Widget* const pw)
42 : self(s),
43 topLevelWidget(findTopLevelWidget(pw)),
44 parentWidget(pw),
45 id(0),
46 needsScaling(false),
47 visible(true),
48 size(0, 0),
49 subWidgets() {}
51 Widget::PrivateData::~PrivateData()
53 subWidgets.clear();
56 void Widget::PrivateData::displaySubWidgets(const uint width, const uint height, const double autoScaleFactor)
58 if (subWidgets.size() == 0)
59 return;
61 for (std::list<SubWidget*>::iterator it = subWidgets.begin(); it != subWidgets.end(); ++it)
63 SubWidget* const subwidget(*it);
65 if (subwidget->isVisible())
66 subwidget->pData->display(width, height, autoScaleFactor);
70 // -----------------------------------------------------------------------
72 bool Widget::PrivateData::giveKeyboardEventForSubWidgets(const KeyboardEvent& ev)
74 if (! visible)
75 return false;
76 if (subWidgets.size() == 0)
77 return false;
79 FOR_EACH_SUBWIDGET_INV(rit)
81 SubWidget* const widget(*rit);
83 if (widget->isVisible() && widget->onKeyboard(ev))
84 return true;
87 return false;
90 bool Widget::PrivateData::giveCharacterInputEventForSubWidgets(const CharacterInputEvent& ev)
92 if (! visible)
93 return false;
94 if (subWidgets.size() == 0)
95 return false;
97 FOR_EACH_SUBWIDGET_INV(rit)
99 SubWidget* const widget(*rit);
101 if (widget->isVisible() && widget->onCharacterInput(ev))
102 return true;
105 return false;
108 bool Widget::PrivateData::giveMouseEventForSubWidgets(MouseEvent& ev)
110 if (! visible)
111 return false;
112 if (subWidgets.size() == 0)
113 return false;
115 const double x = ev.absolutePos.getX();
116 const double y = ev.absolutePos.getY();
118 if (SubWidget* const selfw = dynamic_cast<SubWidget*>(self))
120 if (selfw->pData->needsViewportScaling)
122 ev.absolutePos.setX(x - selfw->getAbsoluteX() + selfw->getMargin().getX());
123 ev.absolutePos.setY(y - selfw->getAbsoluteY() + selfw->getMargin().getY());
127 FOR_EACH_SUBWIDGET_INV(rit)
129 SubWidget* const widget(*rit);
131 if (! widget->isVisible())
132 continue;
134 ev.pos = Point<double>(x - widget->getAbsoluteX() + widget->getMargin().getX(),
135 y - widget->getAbsoluteY() + widget->getMargin().getY());
137 if (widget->onMouse(ev))
138 return true;
141 return false;
144 bool Widget::PrivateData::giveMotionEventForSubWidgets(MotionEvent& ev)
146 if (! visible)
147 return false;
148 if (subWidgets.size() == 0)
149 return false;
151 const double x = ev.absolutePos.getX();
152 const double y = ev.absolutePos.getY();
154 if (SubWidget* const selfw = dynamic_cast<SubWidget*>(self))
156 if (selfw->pData->needsViewportScaling)
158 ev.absolutePos.setX(x - selfw->getAbsoluteX() + selfw->getMargin().getX());
159 ev.absolutePos.setY(y - selfw->getAbsoluteY() + selfw->getMargin().getY());
163 FOR_EACH_SUBWIDGET_INV(rit)
165 SubWidget* const widget(*rit);
167 if (! widget->isVisible())
168 continue;
170 ev.pos = Point<double>(x - widget->getAbsoluteX() + widget->getMargin().getX(),
171 y - widget->getAbsoluteY() + widget->getMargin().getY());
173 if (widget->onMotion(ev))
174 return true;
177 return false;
180 bool Widget::PrivateData::giveScrollEventForSubWidgets(ScrollEvent& ev)
182 if (! visible)
183 return false;
184 if (subWidgets.size() == 0)
185 return false;
187 const double x = ev.absolutePos.getX();
188 const double y = ev.absolutePos.getY();
190 if (SubWidget* const selfw = dynamic_cast<SubWidget*>(self))
192 if (selfw->pData->needsViewportScaling)
194 ev.absolutePos.setX(x - selfw->getAbsoluteX() + selfw->getMargin().getX());
195 ev.absolutePos.setY(y - selfw->getAbsoluteY() + selfw->getMargin().getY());
199 FOR_EACH_SUBWIDGET_INV(rit)
201 SubWidget* const widget(*rit);
203 if (! widget->isVisible())
204 continue;
206 ev.pos = Point<double>(x - widget->getAbsoluteX() + widget->getMargin().getX(),
207 y - widget->getAbsoluteY() + widget->getMargin().getY());
209 if (widget->onScroll(ev))
210 return true;
213 return false;
216 // -----------------------------------------------------------------------
218 TopLevelWidget* Widget::PrivateData::findTopLevelWidget(Widget* const pw)
220 if (pw->pData->topLevelWidget != nullptr)
221 return pw->pData->topLevelWidget;
222 if (pw->pData->parentWidget != nullptr)
223 return findTopLevelWidget(pw->pData->parentWidget);
224 return nullptr;
227 // -----------------------------------------------------------------------
229 END_NAMESPACE_DGL