Cleanup
[carla.git] / source / modules / dgl / src / Widget.cpp
blob8aaa90d5d557c80cd7f2c016d53dbb6f44e94299
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 "../TopLevelWidget.hpp"
19 #include "../Window.hpp"
21 START_NAMESPACE_DGL
23 // --------------------------------------------------------------------------------------------------------------------
24 // Widget
26 Widget::Widget(TopLevelWidget* const topLevelWidget)
27 : pData(new PrivateData(this, topLevelWidget)) {}
29 Widget::Widget(Widget* const parentWidget)
30 : pData(new PrivateData(this, parentWidget)) {}
32 Widget::~Widget()
34 delete pData;
37 bool Widget::isVisible() const noexcept
39 return pData->visible;
42 void Widget::setVisible(bool visible)
44 if (pData->visible == visible)
45 return;
47 pData->visible = visible;
48 repaint();
50 // FIXME check case of hiding a previously visible widget, does it trigger a repaint?
53 void Widget::show()
55 setVisible(true);
58 void Widget::hide()
60 setVisible(false);
63 uint Widget::getWidth() const noexcept
65 return pData->size.getWidth();
68 uint Widget::getHeight() const noexcept
70 return pData->size.getHeight();
73 const Size<uint> Widget::getSize() const noexcept
75 return pData->size;
78 void Widget::setWidth(uint width) noexcept
80 if (pData->size.getWidth() == width)
81 return;
83 ResizeEvent ev;
84 ev.oldSize = pData->size;
85 ev.size = Size<uint>(width, pData->size.getHeight());
87 pData->size.setWidth(width);
88 onResize(ev);
90 repaint();
93 void Widget::setHeight(uint height) noexcept
95 if (pData->size.getHeight() == height)
96 return;
98 ResizeEvent ev;
99 ev.oldSize = pData->size;
100 ev.size = Size<uint>(pData->size.getWidth(), height);
102 pData->size.setHeight(height);
103 onResize(ev);
105 repaint();
108 void Widget::setSize(uint width, uint height) noexcept
110 setSize(Size<uint>(width, height));
113 void Widget::setSize(const Size<uint>& size) noexcept
115 if (pData->size == size)
116 return;
118 ResizeEvent ev;
119 ev.oldSize = pData->size;
120 ev.size = size;
122 pData->size = size;
123 onResize(ev);
125 repaint();
128 Application& Widget::getApp() const noexcept
130 DISTRHO_SAFE_ASSERT(pData->topLevelWidget != nullptr);
131 return pData->topLevelWidget->getApp();
134 Window& Widget::getWindow() const noexcept
136 DISTRHO_SAFE_ASSERT(pData->topLevelWidget != nullptr);
137 return pData->topLevelWidget->getWindow();
140 const GraphicsContext& Widget::getGraphicsContext() const noexcept
142 DISTRHO_SAFE_ASSERT(pData->topLevelWidget != nullptr);
143 return pData->topLevelWidget->getWindow().getGraphicsContext();
146 TopLevelWidget* Widget::getTopLevelWidget() const noexcept
148 return pData->topLevelWidget;
151 void Widget::repaint() noexcept
155 uint Widget::getId() const noexcept
157 return pData->id;
160 void Widget::setId(uint id) noexcept
162 pData->id = id;
165 bool Widget::onKeyboard(const KeyboardEvent& ev)
167 return pData->giveKeyboardEventForSubWidgets(ev);
170 bool Widget::onCharacterInput(const CharacterInputEvent& ev)
172 return pData->giveCharacterInputEventForSubWidgets(ev);
175 bool Widget::onMouse(const MouseEvent& ev)
177 MouseEvent rev = ev;
178 return pData->giveMouseEventForSubWidgets(rev);
181 bool Widget::onMotion(const MotionEvent& ev)
183 MotionEvent rev = ev;
184 return pData->giveMotionEventForSubWidgets(rev);
187 bool Widget::onScroll(const ScrollEvent& ev)
189 ScrollEvent rev = ev;
190 return pData->giveScrollEventForSubWidgets(rev);
193 void Widget::onResize(const ResizeEvent&)
197 // --------------------------------------------------------------------------------------------------------------------
199 END_NAMESPACE_DGL