DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
4.6KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2022 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. #include "WidgetPrivateData.hpp"
  17. #include "../TopLevelWidget.hpp"
  18. #include "../Window.hpp"
  19. START_NAMESPACE_DGL
  20. // --------------------------------------------------------------------------------------------------------------------
  21. // Widget
  22. Widget::Widget(TopLevelWidget* const topLevelWidget)
  23. : pData(new PrivateData(this, topLevelWidget)) {}
  24. Widget::Widget(Widget* const parentWidget)
  25. : pData(new PrivateData(this, parentWidget)) {}
  26. Widget::~Widget()
  27. {
  28. delete pData;
  29. }
  30. bool Widget::isVisible() const noexcept
  31. {
  32. return pData->visible;
  33. }
  34. void Widget::setVisible(bool visible)
  35. {
  36. if (pData->visible == visible)
  37. return;
  38. pData->visible = visible;
  39. repaint();
  40. // FIXME check case of hiding a previously visible widget, does it trigger a repaint?
  41. }
  42. void Widget::show()
  43. {
  44. setVisible(true);
  45. }
  46. void Widget::hide()
  47. {
  48. setVisible(false);
  49. }
  50. uint Widget::getWidth() const noexcept
  51. {
  52. return pData->size.getWidth();
  53. }
  54. uint Widget::getHeight() const noexcept
  55. {
  56. return pData->size.getHeight();
  57. }
  58. const Size<uint> Widget::getSize() const noexcept
  59. {
  60. return pData->size;
  61. }
  62. void Widget::setWidth(uint width) noexcept
  63. {
  64. if (pData->size.getWidth() == width)
  65. return;
  66. ResizeEvent ev;
  67. ev.oldSize = pData->size;
  68. ev.size = Size<uint>(width, pData->size.getHeight());
  69. pData->size.setWidth(width);
  70. onResize(ev);
  71. repaint();
  72. }
  73. void Widget::setHeight(uint height) noexcept
  74. {
  75. if (pData->size.getHeight() == height)
  76. return;
  77. ResizeEvent ev;
  78. ev.oldSize = pData->size;
  79. ev.size = Size<uint>(pData->size.getWidth(), height);
  80. pData->size.setHeight(height);
  81. onResize(ev);
  82. repaint();
  83. }
  84. void Widget::setSize(uint width, uint height) noexcept
  85. {
  86. setSize(Size<uint>(width, height));
  87. }
  88. void Widget::setSize(const Size<uint>& size) noexcept
  89. {
  90. if (pData->size == size)
  91. return;
  92. ResizeEvent ev;
  93. ev.oldSize = pData->size;
  94. ev.size = size;
  95. pData->size = size;
  96. onResize(ev);
  97. repaint();
  98. }
  99. Application& Widget::getApp() const noexcept
  100. {
  101. DISTRHO_SAFE_ASSERT(pData->topLevelWidget != nullptr);
  102. return pData->topLevelWidget->getApp();
  103. }
  104. Window& Widget::getWindow() const noexcept
  105. {
  106. DISTRHO_SAFE_ASSERT(pData->topLevelWidget != nullptr);
  107. return pData->topLevelWidget->getWindow();
  108. }
  109. const GraphicsContext& Widget::getGraphicsContext() const noexcept
  110. {
  111. DISTRHO_SAFE_ASSERT(pData->topLevelWidget != nullptr);
  112. return pData->topLevelWidget->getWindow().getGraphicsContext();
  113. }
  114. TopLevelWidget* Widget::getTopLevelWidget() const noexcept
  115. {
  116. return pData->topLevelWidget;
  117. }
  118. std::list<SubWidget*> Widget::getChildren() const noexcept
  119. {
  120. return pData->subWidgets;
  121. }
  122. void Widget::repaint() noexcept
  123. {
  124. }
  125. uint Widget::getId() const noexcept
  126. {
  127. return pData->id;
  128. }
  129. const char* Widget::getName() const noexcept
  130. {
  131. return pData->name != nullptr ? pData->name : "";
  132. }
  133. void Widget::setId(uint id) noexcept
  134. {
  135. pData->id = id;
  136. }
  137. void Widget::setName(const char* const name) noexcept
  138. {
  139. std::free(pData->name);
  140. pData->name = strdup(name);
  141. }
  142. bool Widget::onKeyboard(const KeyboardEvent& ev)
  143. {
  144. return pData->giveKeyboardEventForSubWidgets(ev);
  145. }
  146. bool Widget::onCharacterInput(const CharacterInputEvent& ev)
  147. {
  148. return pData->giveCharacterInputEventForSubWidgets(ev);
  149. }
  150. bool Widget::onMouse(const MouseEvent& ev)
  151. {
  152. MouseEvent rev = ev;
  153. return pData->giveMouseEventForSubWidgets(rev);
  154. }
  155. bool Widget::onMotion(const MotionEvent& ev)
  156. {
  157. MotionEvent rev = ev;
  158. return pData->giveMotionEventForSubWidgets(rev);
  159. }
  160. bool Widget::onScroll(const ScrollEvent& ev)
  161. {
  162. ScrollEvent rev = ev;
  163. return pData->giveScrollEventForSubWidgets(rev);
  164. }
  165. void Widget::onResize(const ResizeEvent&)
  166. {
  167. }
  168. // --------------------------------------------------------------------------------------------------------------------
  169. END_NAMESPACE_DGL