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_Viewport.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. */
  144. void setScrollBarsShown (bool showVerticalScrollbarIfNeeded,
  145. bool showHorizontalScrollbarIfNeeded);
  146. /** True if the vertical scrollbar is enabled.
  147. @see setScrollBarsShown
  148. */
  149. bool isVerticalScrollBarShown() const noexcept { return showVScrollbar; }
  150. /** True if the horizontal scrollbar is enabled.
  151. @see setScrollBarsShown
  152. */
  153. bool isHorizontalScrollBarShown() const noexcept { return showHScrollbar; }
  154. /** Changes the width of the scrollbars.
  155. If this isn't specified, the default width from the LookAndFeel class will be used.
  156. @see LookAndFeel::getDefaultScrollbarWidth
  157. */
  158. void setScrollBarThickness (int thickness);
  159. /** Returns the thickness of the scrollbars.
  160. @see setScrollBarThickness
  161. */
  162. int getScrollBarThickness() const;
  163. /** Changes the distance that a single-step click on a scrollbar button
  164. will move the viewport.
  165. */
  166. void setSingleStepSizes (int stepX, int stepY);
  167. /** Returns a pointer to the scrollbar component being used.
  168. Handy if you need to customise the bar somehow.
  169. */
  170. ScrollBar* getVerticalScrollBar() noexcept { return &verticalScrollBar; }
  171. /** Returns a pointer to the scrollbar component being used.
  172. Handy if you need to customise the bar somehow.
  173. */
  174. ScrollBar* getHorizontalScrollBar() noexcept { return &horizontalScrollBar; }
  175. //==============================================================================
  176. /** @internal */
  177. void resized() override;
  178. /** @internal */
  179. void scrollBarMoved (ScrollBar*, double newRangeStart) override;
  180. /** @internal */
  181. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  182. /** @internal */
  183. bool keyPressed (const KeyPress&) override;
  184. /** @internal */
  185. void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;
  186. /** @internal */
  187. bool useMouseWheelMoveIfNeeded (const MouseEvent&, const MouseWheelDetails&);
  188. /** @internal */
  189. static bool respondsToKey (const KeyPress&);
  190. private:
  191. //==============================================================================
  192. WeakReference<Component> contentComp;
  193. Rectangle<int> lastVisibleArea;
  194. int scrollBarThickness;
  195. int singleStepX, singleStepY;
  196. bool showHScrollbar, showVScrollbar, deleteContent;
  197. Component contentHolder;
  198. ScrollBar verticalScrollBar;
  199. ScrollBar horizontalScrollBar;
  200. Point<int> viewportPosToCompPos (Point<int>) const;
  201. void updateVisibleArea();
  202. void deleteContentComp();
  203. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  204. // If you get an error here, it's because this method's parameters have changed! See the new definition above..
  205. virtual int visibleAreaChanged (int, int, int, int) { return 0; }
  206. #endif
  207. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Viewport)
  208. };
  209. #endif // JUCE_VIEWPORT_H_INCLUDED