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

10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. 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. bool useMouseWheelMoveIfNeeded (const MouseEvent&, const MouseWheelDetails&);
  193. /** @internal */
  194. static bool respondsToKey (const KeyPress&);
  195. private:
  196. //==============================================================================
  197. WeakReference<Component> contentComp;
  198. Rectangle<int> lastVisibleArea;
  199. int scrollBarThickness;
  200. int singleStepX, singleStepY;
  201. bool showHScrollbar, showVScrollbar, deleteContent;
  202. bool allowScrollingWithoutScrollbarV, allowScrollingWithoutScrollbarH;
  203. Component contentHolder;
  204. ScrollBar verticalScrollBar, horizontalScrollBar;
  205. Point<int> viewportPosToCompPos (Point<int>) const;
  206. void updateVisibleArea();
  207. void deleteContentComp();
  208. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  209. // If you get an error here, it's because this method's parameters have changed! See the new definition above..
  210. virtual int visibleAreaChanged (int, int, int, int) { return 0; }
  211. #endif
  212. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Viewport)
  213. };
  214. #endif // JUCE_VIEWPORT_H_INCLUDED