Collection of DPF-based plugins for packaging
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.

199 lines
5.3KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2024 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 "SubWidgetPrivateData.hpp"
  17. #include "WidgetPrivateData.hpp"
  18. #include "../TopLevelWidget.hpp"
  19. START_NAMESPACE_DGL
  20. // --------------------------------------------------------------------------------------------------------------------
  21. SubWidget::SubWidget(Widget* const parentWidget)
  22. : Widget(parentWidget),
  23. pData(new PrivateData(this, parentWidget)) {}
  24. SubWidget::~SubWidget()
  25. {
  26. delete pData;
  27. }
  28. template<typename T>
  29. bool SubWidget::contains(const T x, const T y) const noexcept
  30. {
  31. return Rectangle<double>(0, 0,
  32. static_cast<double>(getWidth()),
  33. static_cast<double>(getHeight())).contains(x, y);
  34. }
  35. template<typename T>
  36. bool SubWidget::contains(const Point<T>& pos) const noexcept
  37. {
  38. return contains(pos.getX(), pos.getY());
  39. }
  40. int SubWidget::getAbsoluteX() const noexcept
  41. {
  42. return pData->absolutePos.getX();
  43. }
  44. int SubWidget::getAbsoluteY() const noexcept
  45. {
  46. return pData->absolutePos.getY();
  47. }
  48. Point<int> SubWidget::getAbsolutePos() const noexcept
  49. {
  50. return pData->absolutePos;
  51. }
  52. Rectangle<int> SubWidget::getAbsoluteArea() const noexcept
  53. {
  54. return Rectangle<int>(getAbsolutePos(), getSize().toInt());
  55. }
  56. Rectangle<uint> SubWidget::getConstrainedAbsoluteArea() const noexcept
  57. {
  58. const int x = getAbsoluteX();
  59. const int y = getAbsoluteY();
  60. if (x >= 0 && y >= 0)
  61. return Rectangle<uint>(static_cast<uint>(x), static_cast<uint>(y), getSize());
  62. const int xOffset = std::min(0, x);
  63. const int yOffset = std::min(0, y);
  64. const int width = std::max(0, static_cast<int>(getWidth()) + xOffset);
  65. const int height = std::max(0, static_cast<int>(getHeight()) + yOffset);
  66. return Rectangle<uint>(0, 0, static_cast<uint>(width), static_cast<uint>(height));
  67. }
  68. void SubWidget::setAbsoluteX(const int x) noexcept
  69. {
  70. setAbsolutePos(Point<int>(x, getAbsoluteY()));
  71. }
  72. void SubWidget::setAbsoluteY(const int y) noexcept
  73. {
  74. setAbsolutePos(Point<int>(getAbsoluteX(), y));
  75. }
  76. void SubWidget::setAbsolutePos(const int x, const int y) noexcept
  77. {
  78. setAbsolutePos(Point<int>(x, y));
  79. }
  80. void SubWidget::setAbsolutePos(const Point<int>& pos) noexcept
  81. {
  82. if (pData->absolutePos == pos)
  83. return;
  84. PositionChangedEvent ev;
  85. ev.oldPos = pData->absolutePos;
  86. ev.pos = pos;
  87. pData->absolutePos = pos;
  88. onPositionChanged(ev);
  89. repaint();
  90. }
  91. Point<int> SubWidget::getMargin() const noexcept
  92. {
  93. return pData->margin;
  94. }
  95. void SubWidget::setMargin(const int x, const int y) noexcept
  96. {
  97. pData->margin = Point<int>(x, y);
  98. }
  99. void SubWidget::setMargin(const Point<int>& offset) noexcept
  100. {
  101. pData->margin = offset;
  102. }
  103. Widget* SubWidget::getParentWidget() const noexcept
  104. {
  105. return pData->parentWidget;
  106. }
  107. void SubWidget::repaint() noexcept
  108. {
  109. if (! isVisible())
  110. return;
  111. if (TopLevelWidget* const topw = getTopLevelWidget())
  112. {
  113. if (pData->needsFullViewportForDrawing)
  114. // repaint is virtual and we want precisely the top-level specific implementation, not any higher level
  115. topw->TopLevelWidget::repaint();
  116. else
  117. topw->repaint(getConstrainedAbsoluteArea());
  118. }
  119. }
  120. void SubWidget::toBottom()
  121. {
  122. std::list<SubWidget*>& subwidgets(pData->parentWidget->pData->subWidgets);
  123. subwidgets.remove(this);
  124. subwidgets.insert(subwidgets.begin(), this);
  125. }
  126. void SubWidget::toFront()
  127. {
  128. std::list<SubWidget*>& subwidgets(pData->parentWidget->pData->subWidgets);
  129. subwidgets.remove(this);
  130. subwidgets.push_back(this);
  131. }
  132. void SubWidget::setNeedsFullViewportDrawing(const bool needsFullViewportForDrawing)
  133. {
  134. pData->needsFullViewportForDrawing = needsFullViewportForDrawing;
  135. }
  136. void SubWidget::setNeedsViewportScaling(const bool needsViewportScaling, const double autoScaleFactor)
  137. {
  138. pData->needsViewportScaling = needsViewportScaling;
  139. pData->viewportScaleFactor = autoScaleFactor;
  140. }
  141. void SubWidget::setSkipDrawing(const bool skipDrawing)
  142. {
  143. pData->skipDrawing = skipDrawing;
  144. }
  145. void SubWidget::onPositionChanged(const PositionChangedEvent&)
  146. {
  147. }
  148. // --------------------------------------------------------------------------------------------------------------------
  149. // Possible template data types
  150. template<>
  151. bool SubWidget::contains(const Point<double>& pos) const noexcept
  152. {
  153. return contains(pos.getX(), pos.getY());
  154. }
  155. // float, int, uint, short, ushort
  156. // --------------------------------------------------------------------------------------------------------------------
  157. END_NAMESPACE_DGL