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.

145 lines
3.8KB

  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 "../TopLevelWidget.hpp"
  18. START_NAMESPACE_DGL
  19. // --------------------------------------------------------------------------------------------------------------------
  20. SubWidget::SubWidget(Widget* const parentWidget)
  21. : Widget(parentWidget),
  22. pData(new PrivateData(this, parentWidget)) {}
  23. SubWidget::~SubWidget()
  24. {
  25. delete pData;
  26. }
  27. template<typename T>
  28. bool SubWidget::contains(T x, T y) const noexcept
  29. {
  30. return Rectangle<double>(0, 0, getWidth(), getHeight()).contains(x, y);
  31. }
  32. template<typename T>
  33. bool SubWidget::contains(const Point<T>& pos) const noexcept
  34. {
  35. return contains(pos.getX(), pos.getY());
  36. }
  37. int SubWidget::getAbsoluteX() const noexcept
  38. {
  39. return pData->absolutePos.getX();
  40. }
  41. int SubWidget::getAbsoluteY() const noexcept
  42. {
  43. return pData->absolutePos.getY();
  44. }
  45. Point<int> SubWidget::getAbsolutePos() const noexcept
  46. {
  47. return pData->absolutePos;
  48. }
  49. Rectangle<int> SubWidget::getAbsoluteArea() const noexcept
  50. {
  51. return Rectangle<int>(getAbsolutePos(), getSize().toInt());
  52. }
  53. Rectangle<uint> SubWidget::getConstrainedAbsoluteArea() const noexcept
  54. {
  55. return Rectangle<uint>(static_cast<uint>(std::max(0, getAbsoluteX())),
  56. static_cast<uint>(std::max(0, getAbsoluteY())),
  57. getSize());
  58. }
  59. void SubWidget::setAbsoluteX(int x) noexcept
  60. {
  61. setAbsolutePos(Point<int>(x, getAbsoluteY()));
  62. }
  63. void SubWidget::setAbsoluteY(int y) noexcept
  64. {
  65. setAbsolutePos(Point<int>(getAbsoluteX(), y));
  66. }
  67. void SubWidget::setAbsolutePos(int x, int y) noexcept
  68. {
  69. setAbsolutePos(Point<int>(x, y));
  70. }
  71. void SubWidget::setAbsolutePos(const Point<int>& pos) noexcept
  72. {
  73. if (pData->absolutePos == pos)
  74. return;
  75. PositionChangedEvent ev;
  76. ev.oldPos = pData->absolutePos;
  77. ev.pos = pos;
  78. pData->absolutePos = pos;
  79. onPositionChanged(ev);
  80. // repaint the bounds of parent
  81. pData->parentWidget->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::setNeedsFullViewportDrawing(const bool needsFullViewportForDrawing)
  100. {
  101. pData->needsFullViewportForDrawing = needsFullViewportForDrawing;
  102. }
  103. void SubWidget::onPositionChanged(const PositionChangedEvent&)
  104. {
  105. }
  106. // --------------------------------------------------------------------------------------------------------------------
  107. // Possible template data types
  108. template<>
  109. bool SubWidget::contains(const Point<double>& pos) const noexcept
  110. {
  111. return contains(pos.getX(), pos.getY());
  112. }
  113. // float, int, uint, short, ushort
  114. // --------------------------------------------------------------------------------------------------------------------
  115. END_NAMESPACE_DGL