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.

164 lines
4.2KB

  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(T x, T y) const noexcept
  30. {
  31. return Rectangle<double>(0, 0, getWidth(), getHeight()).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(int x) noexcept
  61. {
  62. setAbsolutePos(Point<int>(x, getAbsoluteY()));
  63. }
  64. void SubWidget::setAbsoluteY(int y) noexcept
  65. {
  66. setAbsolutePos(Point<int>(getAbsoluteX(), y));
  67. }
  68. void SubWidget::setAbsolutePos(int x, 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. Widget* SubWidget::getParentWidget() const noexcept
  84. {
  85. return pData->parentWidget;
  86. }
  87. void SubWidget::repaint() noexcept
  88. {
  89. if (! isVisible())
  90. return;
  91. if (TopLevelWidget* const topw = getTopLevelWidget())
  92. {
  93. if (pData->needsFullViewportForDrawing)
  94. topw->repaint();
  95. else
  96. topw->repaint(getConstrainedAbsoluteArea());
  97. }
  98. }
  99. void SubWidget::toFront()
  100. {
  101. std::list<SubWidget*>& subwidgets(pData->parentWidget->pData->subWidgets);
  102. subwidgets.remove(this);
  103. subwidgets.push_back(this);
  104. }
  105. void SubWidget::setNeedsFullViewportDrawing(const bool needsFullViewportForDrawing)
  106. {
  107. pData->needsFullViewportForDrawing = needsFullViewportForDrawing;
  108. }
  109. void SubWidget::setNeedsViewportScaling(const bool needsViewportScaling, const double autoScaleFactor)
  110. {
  111. pData->needsViewportScaling = needsViewportScaling;
  112. pData->viewportScaleFactor = autoScaleFactor;
  113. }
  114. void SubWidget::setSkipDrawing(const bool skipDrawing)
  115. {
  116. pData->skipDrawing = skipDrawing;
  117. }
  118. void SubWidget::onPositionChanged(const PositionChangedEvent&)
  119. {
  120. }
  121. // --------------------------------------------------------------------------------------------------------------------
  122. // Possible template data types
  123. template<>
  124. bool SubWidget::contains(const Point<double>& pos) const noexcept
  125. {
  126. return contains(pos.getX(), pos.getY());
  127. }
  128. // float, int, uint, short, ushort
  129. // --------------------------------------------------------------------------------------------------------------------
  130. END_NAMESPACE_DGL