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.

juce_ComponentBoundsConstrainer.h 8.8KB

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