The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

135 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. RectanglePlacement::RectanglePlacement (const RectanglePlacement& other) noexcept
  21. : flags (other.flags)
  22. {
  23. }
  24. RectanglePlacement& RectanglePlacement::operator= (const RectanglePlacement& other) noexcept
  25. {
  26. flags = other.flags;
  27. return *this;
  28. }
  29. bool RectanglePlacement::operator== (const RectanglePlacement& other) const noexcept
  30. {
  31. return flags == other.flags;
  32. }
  33. bool RectanglePlacement::operator!= (const RectanglePlacement& other) const noexcept
  34. {
  35. return flags != other.flags;
  36. }
  37. void RectanglePlacement::applyTo (double& x, double& y, double& w, double& h,
  38. const double dx, const double dy, const double dw, const double dh) const noexcept
  39. {
  40. if (w == 0 || h == 0)
  41. return;
  42. if ((flags & stretchToFit) != 0)
  43. {
  44. x = dx;
  45. y = dy;
  46. w = dw;
  47. h = dh;
  48. }
  49. else
  50. {
  51. double scale = (flags & fillDestination) != 0 ? jmax (dw / w, dh / h)
  52. : jmin (dw / w, dh / h);
  53. if ((flags & onlyReduceInSize) != 0)
  54. scale = jmin (scale, 1.0);
  55. if ((flags & onlyIncreaseInSize) != 0)
  56. scale = jmax (scale, 1.0);
  57. w *= scale;
  58. h *= scale;
  59. if ((flags & xLeft) != 0)
  60. x = dx;
  61. else if ((flags & xRight) != 0)
  62. x = dx + dw - w;
  63. else
  64. x = dx + (dw - w) * 0.5;
  65. if ((flags & yTop) != 0)
  66. y = dy;
  67. else if ((flags & yBottom) != 0)
  68. y = dy + dh - h;
  69. else
  70. y = dy + (dh - h) * 0.5;
  71. }
  72. }
  73. const AffineTransform RectanglePlacement::getTransformToFit (const Rectangle<float>& source, const Rectangle<float>& destination) const noexcept
  74. {
  75. if (source.isEmpty())
  76. return AffineTransform::identity;
  77. float newX = destination.getX();
  78. float newY = destination.getY();
  79. float scaleX = destination.getWidth() / source.getWidth();
  80. float scaleY = destination.getHeight() / source.getHeight();
  81. if ((flags & stretchToFit) == 0)
  82. {
  83. scaleX = (flags & fillDestination) != 0 ? jmax (scaleX, scaleY)
  84. : jmin (scaleX, scaleY);
  85. if ((flags & onlyReduceInSize) != 0)
  86. scaleX = jmin (scaleX, 1.0f);
  87. if ((flags & onlyIncreaseInSize) != 0)
  88. scaleX = jmax (scaleX, 1.0f);
  89. scaleY = scaleX;
  90. if ((flags & xRight) != 0)
  91. newX += destination.getWidth() - source.getWidth() * scaleX; // right
  92. else if ((flags & xLeft) == 0)
  93. newX += (destination.getWidth() - source.getWidth() * scaleX) / 2.0f; // centre
  94. if ((flags & yBottom) != 0)
  95. newY += destination.getHeight() - source.getHeight() * scaleX; // bottom
  96. else if ((flags & yTop) == 0)
  97. newY += (destination.getHeight() - source.getHeight() * scaleX) / 2.0f; // centre
  98. }
  99. return AffineTransform::translation (-source.getX(), -source.getY())
  100. .scaled (scaleX, scaleY)
  101. .translated (newX, newY);
  102. }
  103. END_JUCE_NAMESPACE