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.

179 lines
4.6KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 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, getWidth()-pData->margin.getX(), getHeight()-pData->margin.getY()).contains(x, y);
  32. }
  33. template<typename T>
  34. bool SubWidget::contains(const Point<T>& pos) const noexcept
  35. {
  36. return contains(pos.getX(), pos.getY());
  37. }
  38. int SubWidget::getAbsoluteX() const noexcept
  39. {
  40. return pData->absolutePos.getX();
  41. }
  42. int SubWidget::getAbsoluteY() const noexcept
  43. {
  44. return pData->absolutePos.getY();
  45. }
  46. Point<int> SubWidget::getAbsolutePos() const noexcept
  47. {
  48. return pData->absolutePos;
  49. }
  50. Rectangle<int> SubWidget::getAbsoluteArea() const noexcept
  51. {
  52. return Rectangle<int>(getAbsolutePos(), getSize().toInt());
  53. }
  54. Rectangle<uint> SubWidget::getConstrainedAbsoluteArea() const noexcept
  55. {
  56. return Rectangle<uint>(static_cast<uint>(std::max(0, getAbsoluteX())),
  57. static_cast<uint>(std::max(0, getAbsoluteY())),
  58. getSize());
  59. }
  60. void SubWidget::setAbsoluteX(const int x) noexcept
  61. {
  62. setAbsolutePos(Point<int>(x, getAbsoluteY()));
  63. }
  64. void SubWidget::setAbsoluteY(const int y) noexcept
  65. {
  66. setAbsolutePos(Point<int>(getAbsoluteX(), y));
  67. }
  68. void SubWidget::setAbsolutePos(const int x, const int y) noexcept
  69. {
  70. setAbsolutePos(Point<int>(x, y));
  71. }
  72. void SubWidget::setAbsolutePos(const Point<int>& pos) noexcept
  73. {
  74. if (pData->absolutePos == pos)
  75. return;
  76. PositionChangedEvent ev;
  77. ev.oldPos = pData->absolutePos;
  78. ev.pos = pos;
  79. pData->absolutePos = pos;
  80. onPositionChanged(ev);
  81. repaint();
  82. }
  83. Point<int> SubWidget::getMargin() const noexcept
  84. {
  85. return pData->margin;
  86. }
  87. void SubWidget::setMargin(const int x, const int y) noexcept
  88. {
  89. pData->margin = Point<int>(x, y);
  90. }
  91. void SubWidget::setMargin(const Point<int>& offset) noexcept
  92. {
  93. pData->margin = offset;
  94. }
  95. Widget* SubWidget::getParentWidget() const noexcept
  96. {
  97. return pData->parentWidget;
  98. }
  99. void SubWidget::repaint() noexcept
  100. {
  101. if (! isVisible())
  102. return;
  103. if (TopLevelWidget* const topw = getTopLevelWidget())
  104. {
  105. if (pData->needsFullViewportForDrawing)
  106. topw->repaint();
  107. else
  108. topw->repaint(getConstrainedAbsoluteArea());
  109. }
  110. }
  111. void SubWidget::toFront()
  112. {
  113. std::list<SubWidget*>& subwidgets(pData->parentWidget->pData->subWidgets);
  114. subwidgets.remove(this);
  115. subwidgets.push_back(this);
  116. }
  117. void SubWidget::setNeedsFullViewportDrawing(const bool needsFullViewportForDrawing)
  118. {
  119. pData->needsFullViewportForDrawing = needsFullViewportForDrawing;
  120. }
  121. void SubWidget::setNeedsViewportScaling(const bool needsViewportScaling, const double autoScaleFactor)
  122. {
  123. pData->needsViewportScaling = needsViewportScaling;
  124. pData->viewportScaleFactor = autoScaleFactor;
  125. }
  126. void SubWidget::setSkipDrawing(const bool skipDrawing)
  127. {
  128. pData->skipDrawing = skipDrawing;
  129. }
  130. void SubWidget::onPositionChanged(const PositionChangedEvent&)
  131. {
  132. }
  133. // --------------------------------------------------------------------------------------------------------------------
  134. // Possible template data types
  135. template<>
  136. bool SubWidget::contains(const Point<double>& pos) const noexcept
  137. {
  138. return contains(pos.getX(), pos.getY());
  139. }
  140. // float, int, uint, short, ushort
  141. // --------------------------------------------------------------------------------------------------------------------
  142. END_NAMESPACE_DGL