Audio plugin host https://kx.studio/carla
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.

195 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. /**
  22. A class that imposes restrictions on a Component's size or position.
  23. This is used by classes such as ResizableCornerComponent,
  24. ResizableBorderComponent and ResizableWindow.
  25. The base class can impose some basic size and position limits, but you can
  26. also subclass this for custom uses.
  27. @see ResizableCornerComponent, ResizableBorderComponent, ResizableWindow
  28. */
  29. class JUCE_API ComponentBoundsConstrainer
  30. {
  31. public:
  32. //==============================================================================
  33. /** When first created, the object will not impose any restrictions on the components. */
  34. ComponentBoundsConstrainer() noexcept;
  35. /** Destructor. */
  36. virtual ~ComponentBoundsConstrainer();
  37. //==============================================================================
  38. /** Imposes a minimum width limit. */
  39. void setMinimumWidth (int minimumWidth) noexcept;
  40. /** Returns the current minimum width. */
  41. int getMinimumWidth() const noexcept { return minW; }
  42. /** Imposes a maximum width limit. */
  43. void setMaximumWidth (int maximumWidth) noexcept;
  44. /** Returns the current maximum width. */
  45. int getMaximumWidth() const noexcept { return maxW; }
  46. /** Imposes a minimum height limit. */
  47. void setMinimumHeight (int minimumHeight) noexcept;
  48. /** Returns the current minimum height. */
  49. int getMinimumHeight() const noexcept { return minH; }
  50. /** Imposes a maximum height limit. */
  51. void setMaximumHeight (int maximumHeight) noexcept;
  52. /** Returns the current maximum height. */
  53. int getMaximumHeight() const noexcept { return maxH; }
  54. /** Imposes a minimum width and height limit. */
  55. void setMinimumSize (int minimumWidth,
  56. int minimumHeight) noexcept;
  57. /** Imposes a maximum width and height limit. */
  58. void setMaximumSize (int maximumWidth,
  59. int maximumHeight) noexcept;
  60. /** Set all the maximum and minimum dimensions. */
  61. void setSizeLimits (int minimumWidth,
  62. int minimumHeight,
  63. int maximumWidth,
  64. int maximumHeight) noexcept;
  65. //==============================================================================
  66. /** Sets the amount by which the component is allowed to go off-screen.
  67. The values indicate how many pixels must remain on-screen when dragged off
  68. one of its parent's edges, so e.g. if minimumWhenOffTheTop is set to 10, then
  69. when the component goes off the top of the screen, its y-position will be
  70. clipped so that there are always at least 10 pixels on-screen. In other words,
  71. the lowest y-position it can take would be (10 - the component's height).
  72. If you pass 0 or less for one of these amounts, the component is allowed
  73. to move beyond that edge completely, with no restrictions at all.
  74. If you pass a very large number (i.e. larger that the dimensions of the
  75. component itself), then the component won't be allowed to overlap that
  76. edge at all. So e.g. setting minimumWhenOffTheLeft to 0xffffff will mean that
  77. the component will bump into the left side of the screen and go no further.
  78. */
  79. void setMinimumOnscreenAmounts (int minimumWhenOffTheTop,
  80. int minimumWhenOffTheLeft,
  81. int minimumWhenOffTheBottom,
  82. int minimumWhenOffTheRight) noexcept;
  83. /** Returns the minimum distance the bounds can be off-screen. @see setMinimumOnscreenAmounts */
  84. int getMinimumWhenOffTheTop() const noexcept { return minOffTop; }
  85. /** Returns the minimum distance the bounds can be off-screen. @see setMinimumOnscreenAmounts */
  86. int getMinimumWhenOffTheLeft() const noexcept { return minOffLeft; }
  87. /** Returns the minimum distance the bounds can be off-screen. @see setMinimumOnscreenAmounts */
  88. int getMinimumWhenOffTheBottom() const noexcept { return minOffBottom; }
  89. /** Returns the minimum distance the bounds can be off-screen. @see setMinimumOnscreenAmounts */
  90. int getMinimumWhenOffTheRight() const noexcept { return minOffRight; }
  91. //==============================================================================
  92. /** Specifies a width-to-height ratio that the resizer should always maintain.
  93. If the value is 0, no aspect ratio is enforced. If it's non-zero, the width
  94. will always be maintained as this multiple of the height.
  95. @see setResizeLimits
  96. */
  97. void setFixedAspectRatio (double widthOverHeight) noexcept;
  98. /** Returns the aspect ratio that was set with setFixedAspectRatio().
  99. If no aspect ratio is being enforced, this will return 0.
  100. */
  101. double getFixedAspectRatio() const noexcept;
  102. //==============================================================================
  103. /** This callback changes the given coordinates to impose whatever the current
  104. constraints are set to be.
  105. @param bounds the target position that should be examined and adjusted
  106. @param previousBounds the component's current size
  107. @param limits the region in which the component can be positioned
  108. @param isStretchingTop whether the top edge of the component is being resized
  109. @param isStretchingLeft whether the left edge of the component is being resized
  110. @param isStretchingBottom whether the bottom edge of the component is being resized
  111. @param isStretchingRight whether the right edge of the component is being resized
  112. */
  113. virtual void checkBounds (Rectangle<int>& bounds,
  114. const Rectangle<int>& previousBounds,
  115. const Rectangle<int>& limits,
  116. bool isStretchingTop,
  117. bool isStretchingLeft,
  118. bool isStretchingBottom,
  119. bool isStretchingRight);
  120. /** This callback happens when the resizer is about to start dragging. */
  121. virtual void resizeStart();
  122. /** This callback happens when the resizer has finished dragging. */
  123. virtual void resizeEnd();
  124. /** Checks the given bounds, and then sets the component to the corrected size. */
  125. void setBoundsForComponent (Component* component,
  126. Rectangle<int> bounds,
  127. bool isStretchingTop,
  128. bool isStretchingLeft,
  129. bool isStretchingBottom,
  130. bool isStretchingRight);
  131. /** Performs a check on the current size of a component, and moves or resizes
  132. it if it fails the constraints.
  133. */
  134. void checkComponentBounds (Component* component);
  135. /** Called by setBoundsForComponent() to apply a new constrained size to a
  136. component.
  137. By default this just calls setBounds(), but is virtual in case it's needed for
  138. extremely cunning purposes.
  139. */
  140. virtual void applyBoundsToComponent (Component&, Rectangle<int> bounds);
  141. private:
  142. //==============================================================================
  143. int minW = 0, maxW = 0x3fffffff, minH = 0, maxH = 0x3fffffff;
  144. int minOffTop = 0, minOffLeft = 0, minOffBottom = 0, minOffRight = 0;
  145. double aspectRatio = 0;
  146. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentBoundsConstrainer)
  147. };