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.

198 lines
8.7KB

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