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.

289 lines
12KB

  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_VIEWPORT_H_INCLUDED
  18. #define JUCE_VIEWPORT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A Viewport is used to contain a larger child component, and allows the child
  22. to be automatically scrolled around.
  23. To use a Viewport, just create one and set the component that goes inside it
  24. using the setViewedComponent() method. When the child component changes size,
  25. the Viewport will adjust its scrollbars accordingly.
  26. A subclass of the viewport can be created which will receive calls to its
  27. visibleAreaChanged() method when the subcomponent changes position or size.
  28. */
  29. class JUCE_API Viewport : public Component,
  30. private ComponentListener,
  31. private ScrollBar::Listener
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates a Viewport.
  36. The viewport is initially empty - use the setViewedComponent() method to
  37. add a child component for it to manage.
  38. */
  39. explicit Viewport (const String& componentName = String::empty);
  40. /** Destructor. */
  41. ~Viewport();
  42. //==============================================================================
  43. /** Sets the component that this viewport will contain and scroll around.
  44. This will add the given component to this Viewport and position it at (0, 0).
  45. (Don't add or remove any child components directly using the normal
  46. Component::addChildComponent() methods).
  47. @param newViewedComponent the component to add to this viewport, or null to remove
  48. the current component.
  49. @param deleteComponentWhenNoLongerNeeded if true, the component will be deleted
  50. automatically when the viewport is deleted or when a different
  51. component is added. If false, the caller must manage the lifetime
  52. of the component
  53. @see getViewedComponent
  54. */
  55. void setViewedComponent (Component* newViewedComponent,
  56. bool deleteComponentWhenNoLongerNeeded = true);
  57. /** Returns the component that's currently being used inside the Viewport.
  58. @see setViewedComponent
  59. */
  60. Component* getViewedComponent() const noexcept { return contentComp; }
  61. //==============================================================================
  62. /** Changes the position of the viewed component.
  63. The inner component will be moved so that the pixel at the top left of
  64. the viewport will be the pixel at position (xPixelsOffset, yPixelsOffset)
  65. within the inner component.
  66. This will update the scrollbars and might cause a call to visibleAreaChanged().
  67. @see getViewPositionX, getViewPositionY, setViewPositionProportionately
  68. */
  69. void setViewPosition (int xPixelsOffset, int yPixelsOffset);
  70. /** Changes the position of the viewed component.
  71. The inner component will be moved so that the pixel at the top left of
  72. the viewport will be the pixel at the specified coordinates within the
  73. inner component.
  74. This will update the scrollbars and might cause a call to visibleAreaChanged().
  75. @see getViewPositionX, getViewPositionY, setViewPositionProportionately
  76. */
  77. void setViewPosition (Point<int> newPosition);
  78. /** Changes the view position as a proportion of the distance it can move.
  79. The values here are from 0.0 to 1.0 - where (0, 0) would put the
  80. visible area in the top-left, and (1, 1) would put it as far down and
  81. to the right as it's possible to go whilst keeping the child component
  82. on-screen.
  83. */
  84. void setViewPositionProportionately (double proportionX, double proportionY);
  85. /** If the specified position is at the edges of the viewport, this method scrolls
  86. the viewport to bring that position nearer to the centre.
  87. Call this if you're dragging an object inside a viewport and want to make it scroll
  88. when the user approaches an edge. You might also find Component::beginDragAutoRepeat()
  89. useful when auto-scrolling.
  90. @param mouseX the x position, relative to the Viewport's top-left
  91. @param mouseY the y position, relative to the Viewport's top-left
  92. @param distanceFromEdge specifies how close to an edge the position needs to be
  93. before the viewport should scroll in that direction
  94. @param maximumSpeed the maximum number of pixels that the viewport is allowed
  95. to scroll by.
  96. @returns true if the viewport was scrolled
  97. */
  98. bool autoScroll (int mouseX, int mouseY, int distanceFromEdge, int maximumSpeed);
  99. /** Returns the position within the child component of the top-left of its visible area. */
  100. Point<int> getViewPosition() const noexcept { return lastVisibleArea.getPosition(); }
  101. /** Returns the visible area of the child component, relative to its top-left */
  102. Rectangle<int> getViewArea() const noexcept { return lastVisibleArea; }
  103. /** Returns the position within the child component of the top-left of its visible area.
  104. @see getViewWidth, setViewPosition
  105. */
  106. int getViewPositionX() const noexcept { return lastVisibleArea.getX(); }
  107. /** Returns the position within the child component of the top-left of its visible area.
  108. @see getViewHeight, setViewPosition
  109. */
  110. int getViewPositionY() const noexcept { return lastVisibleArea.getY(); }
  111. /** Returns the width of the visible area of the child component.
  112. This may be less than the width of this Viewport if there's a vertical scrollbar
  113. or if the child component is itself smaller.
  114. */
  115. int getViewWidth() const noexcept { return lastVisibleArea.getWidth(); }
  116. /** Returns the height of the visible area of the child component.
  117. This may be less than the height of this Viewport if there's a horizontal scrollbar
  118. or if the child component is itself smaller.
  119. */
  120. int getViewHeight() const noexcept { return lastVisibleArea.getHeight(); }
  121. /** Returns the width available within this component for the contents.
  122. This will be the width of the viewport component minus the width of a
  123. vertical scrollbar (if visible).
  124. */
  125. int getMaximumVisibleWidth() const;
  126. /** Returns the height available within this component for the contents.
  127. This will be the height of the viewport component minus the space taken up
  128. by a horizontal scrollbar (if visible).
  129. */
  130. int getMaximumVisibleHeight() const;
  131. //==============================================================================
  132. /** Callback method that is called when the visible area changes.
  133. This will be called when the visible area is moved either be scrolling or
  134. by calls to setViewPosition(), etc.
  135. */
  136. virtual void visibleAreaChanged (const Rectangle<int>& newVisibleArea);
  137. /** Callback method that is called when the viewed component is added, removed or swapped. */
  138. virtual void viewedComponentChanged (Component* newComponent);
  139. //==============================================================================
  140. /** Turns scrollbars on or off.
  141. If set to false, the scrollbars won't ever appear. When true (the default)
  142. they will appear only when needed.
  143. The allowVerticalScrollingWithoutScrollbar parameters allow you to enable
  144. mouse-wheel scrolling even when there the scrollbars are hidden. When the
  145. scrollbars are visible, these parameters are ignored.
  146. */
  147. void setScrollBarsShown (bool showVerticalScrollbarIfNeeded,
  148. bool showHorizontalScrollbarIfNeeded,
  149. bool allowVerticalScrollingWithoutScrollbar = false,
  150. bool allowHorizontalScrollingWithoutScrollbar = false);
  151. /** True if the vertical scrollbar is enabled.
  152. @see setScrollBarsShown
  153. */
  154. bool isVerticalScrollBarShown() const noexcept { return showVScrollbar; }
  155. /** True if the horizontal scrollbar is enabled.
  156. @see setScrollBarsShown
  157. */
  158. bool isHorizontalScrollBarShown() const noexcept { return showHScrollbar; }
  159. /** Changes the width of the scrollbars.
  160. If this isn't specified, the default width from the LookAndFeel class will be used.
  161. @see LookAndFeel::getDefaultScrollbarWidth
  162. */
  163. void setScrollBarThickness (int thickness);
  164. /** Returns the thickness of the scrollbars.
  165. @see setScrollBarThickness
  166. */
  167. int getScrollBarThickness() const;
  168. /** Changes the distance that a single-step click on a scrollbar button
  169. will move the viewport.
  170. */
  171. void setSingleStepSizes (int stepX, int stepY);
  172. /** Returns a pointer to the scrollbar component being used.
  173. Handy if you need to customise the bar somehow.
  174. */
  175. ScrollBar* getVerticalScrollBar() noexcept { return &verticalScrollBar; }
  176. /** Returns a pointer to the scrollbar component being used.
  177. Handy if you need to customise the bar somehow.
  178. */
  179. ScrollBar* getHorizontalScrollBar() noexcept { return &horizontalScrollBar; }
  180. //==============================================================================
  181. /** @internal */
  182. void resized() override;
  183. /** @internal */
  184. void scrollBarMoved (ScrollBar*, double newRangeStart) override;
  185. /** @internal */
  186. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  187. /** @internal */
  188. bool keyPressed (const KeyPress&) override;
  189. /** @internal */
  190. void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;
  191. /** @internal */
  192. void lookAndFeelChanged() override;
  193. /** @internal */
  194. bool useMouseWheelMoveIfNeeded (const MouseEvent&, const MouseWheelDetails&);
  195. /** @internal */
  196. static bool respondsToKey (const KeyPress&);
  197. private:
  198. //==============================================================================
  199. WeakReference<Component> contentComp;
  200. Rectangle<int> lastVisibleArea;
  201. int scrollBarThickness;
  202. int singleStepX, singleStepY;
  203. bool showHScrollbar, showVScrollbar, deleteContent;
  204. bool customScrollBarThickness;
  205. bool allowScrollingWithoutScrollbarV, allowScrollingWithoutScrollbarH;
  206. Component contentHolder;
  207. ScrollBar verticalScrollBar, horizontalScrollBar;
  208. Point<int> viewportPosToCompPos (Point<int>) const;
  209. void updateVisibleArea();
  210. void deleteOrRemoveContentComp();
  211. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  212. // If you get an error here, it's because this method's parameters have changed! See the new definition above..
  213. virtual int visibleAreaChanged (int, int, int, int) { return 0; }
  214. #endif
  215. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Viewport)
  216. };
  217. #endif // JUCE_VIEWPORT_H_INCLUDED