juce_Viewport.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A Viewport is used to contain a larger child component, and allows the child
  24. to be automatically scrolled around.
  25. To use a Viewport, just create one and set the component that goes inside it
  26. using the setViewedComponent() method. When the child component changes size,
  27. the Viewport will adjust its scrollbars accordingly.
  28. A subclass of the viewport can be created which will receive calls to its
  29. visibleAreaChanged() method when the subcomponent changes position or size.
  30. @tags{GUI}
  31. */
  32. class JUCE_API Viewport : public Component,
  33. private ComponentListener,
  34. private ScrollBar::Listener
  35. {
  36. public:
  37. //==============================================================================
  38. /** Creates a Viewport.
  39. The viewport is initially empty - use the setViewedComponent() method to
  40. add a child component for it to manage.
  41. */
  42. explicit Viewport (const String& componentName = String());
  43. /** Destructor. */
  44. ~Viewport() override;
  45. //==============================================================================
  46. /** Sets the component that this viewport will contain and scroll around.
  47. This will add the given component to this Viewport and position it at (0, 0).
  48. (Don't add or remove any child components directly using the normal
  49. Component::addChildComponent() methods).
  50. @param newViewedComponent the component to add to this viewport, or null to remove
  51. the current component.
  52. @param deleteComponentWhenNoLongerNeeded if true, the component will be deleted
  53. automatically when the viewport is deleted or when a different
  54. component is added. If false, the caller must manage the lifetime
  55. of the component
  56. @see getViewedComponent
  57. */
  58. void setViewedComponent (Component* newViewedComponent,
  59. bool deleteComponentWhenNoLongerNeeded = true);
  60. /** Returns the component that's currently being used inside the Viewport.
  61. @see setViewedComponent
  62. */
  63. Component* getViewedComponent() const noexcept { return contentComp.get(); }
  64. //==============================================================================
  65. /** Changes the position of the viewed component.
  66. The inner component will be moved so that the pixel at the top left of
  67. the viewport will be the pixel at position (xPixelsOffset, yPixelsOffset)
  68. within the inner component.
  69. This will update the scrollbars and might cause a call to visibleAreaChanged().
  70. @see getViewPositionX, getViewPositionY, setViewPositionProportionately
  71. */
  72. void setViewPosition (int xPixelsOffset, int yPixelsOffset);
  73. /** Changes the position of the viewed component.
  74. The inner component will be moved so that the pixel at the top left of
  75. the viewport will be the pixel at the specified coordinates within the
  76. inner component.
  77. This will update the scrollbars and might cause a call to visibleAreaChanged().
  78. @see getViewPositionX, getViewPositionY, setViewPositionProportionately
  79. */
  80. void setViewPosition (Point<int> newPosition);
  81. /** Changes the view position as a proportion of the distance it can move.
  82. The values here are from 0.0 to 1.0 - where (0, 0) would put the
  83. visible area in the top-left, and (1, 1) would put it as far down and
  84. to the right as it's possible to go whilst keeping the child component
  85. on-screen.
  86. */
  87. void setViewPositionProportionately (double proportionX, double proportionY);
  88. /** If the specified position is at the edges of the viewport, this method scrolls
  89. the viewport to bring that position nearer to the centre.
  90. Call this if you're dragging an object inside a viewport and want to make it scroll
  91. when the user approaches an edge. You might also find Component::beginDragAutoRepeat()
  92. useful when auto-scrolling.
  93. @param mouseX the x position, relative to the Viewport's top-left
  94. @param mouseY the y position, relative to the Viewport's top-left
  95. @param distanceFromEdge specifies how close to an edge the position needs to be
  96. before the viewport should scroll in that direction
  97. @param maximumSpeed the maximum number of pixels that the viewport is allowed
  98. to scroll by.
  99. @returns true if the viewport was scrolled
  100. */
  101. bool autoScroll (int mouseX, int mouseY, int distanceFromEdge, int maximumSpeed);
  102. /** Returns the position within the child component of the top-left of its visible area. */
  103. Point<int> getViewPosition() const noexcept { return lastVisibleArea.getPosition(); }
  104. /** Returns the visible area of the child component, relative to its top-left */
  105. Rectangle<int> getViewArea() const noexcept { return lastVisibleArea; }
  106. /** Returns the position within the child component of the top-left of its visible area.
  107. @see getViewWidth, setViewPosition
  108. */
  109. int getViewPositionX() const noexcept { return lastVisibleArea.getX(); }
  110. /** Returns the position within the child component of the top-left of its visible area.
  111. @see getViewHeight, setViewPosition
  112. */
  113. int getViewPositionY() const noexcept { return lastVisibleArea.getY(); }
  114. /** Returns the width of the visible area of the child component.
  115. This may be less than the width of this Viewport if there's a vertical scrollbar
  116. or if the child component is itself smaller.
  117. */
  118. int getViewWidth() const noexcept { return lastVisibleArea.getWidth(); }
  119. /** Returns the height of the visible area of the child component.
  120. This may be less than the height of this Viewport if there's a horizontal scrollbar
  121. or if the child component is itself smaller.
  122. */
  123. int getViewHeight() const noexcept { return lastVisibleArea.getHeight(); }
  124. /** Returns the width available within this component for the contents.
  125. This will be the width of the viewport component minus the width of a
  126. vertical scrollbar (if visible).
  127. */
  128. int getMaximumVisibleWidth() const;
  129. /** Returns the height available within this component for the contents.
  130. This will be the height of the viewport component minus the space taken up
  131. by a horizontal scrollbar (if visible).
  132. */
  133. int getMaximumVisibleHeight() const;
  134. //==============================================================================
  135. /** Callback method that is called when the visible area changes.
  136. This will be called when the visible area is moved either be scrolling or
  137. by calls to setViewPosition(), etc.
  138. */
  139. virtual void visibleAreaChanged (const Rectangle<int>& newVisibleArea);
  140. /** Callback method that is called when the viewed component is added, removed or swapped. */
  141. virtual void viewedComponentChanged (Component* newComponent);
  142. //==============================================================================
  143. /** Turns scrollbars on or off.
  144. If set to false, the scrollbars won't ever appear. When true (the default)
  145. they will appear only when needed.
  146. The allowVerticalScrollingWithoutScrollbar parameters allow you to enable
  147. mouse-wheel scrolling even when there the scrollbars are hidden. When the
  148. scrollbars are visible, these parameters are ignored.
  149. */
  150. void setScrollBarsShown (bool showVerticalScrollbarIfNeeded,
  151. bool showHorizontalScrollbarIfNeeded,
  152. bool allowVerticalScrollingWithoutScrollbar = false,
  153. bool allowHorizontalScrollingWithoutScrollbar = false);
  154. /** Changes where the scroll bars are positioned
  155. If verticalScrollbarOnRight is set to true, then the vertical scrollbar will
  156. appear on the right side of the view port's content (this is the default),
  157. otherwise it will be on the left side of the content.
  158. If horizontalScrollbarAtBottom is set to true, then the horizontal scrollbar
  159. will appear at the bottom of the view port's content (this is the default),
  160. otherwise it will be at the top.
  161. */
  162. void setScrollBarPosition (bool verticalScrollbarOnRight,
  163. bool horizontalScrollbarAtBottom);
  164. /** True if the vertical scrollbar will appear on the right side of the content */
  165. bool isVerticalScrollbarOnTheRight() const noexcept { return vScrollbarRight; }
  166. /** True if the horizontal scrollbar will appear at the bottom of the content */
  167. bool isHorizontalScrollbarAtBottom() const noexcept { return hScrollbarBottom; }
  168. /** True if the vertical scrollbar is enabled.
  169. @see setScrollBarsShown
  170. */
  171. bool isVerticalScrollBarShown() const noexcept { return showVScrollbar; }
  172. /** True if the horizontal scrollbar is enabled.
  173. @see setScrollBarsShown
  174. */
  175. bool isHorizontalScrollBarShown() const noexcept { return showHScrollbar; }
  176. /** Changes the width of the scrollbars.
  177. If this isn't specified, the default width from the LookAndFeel class will be used.
  178. @see LookAndFeel::getDefaultScrollbarWidth
  179. */
  180. void setScrollBarThickness (int thickness);
  181. /** Returns the thickness of the scrollbars.
  182. @see setScrollBarThickness
  183. */
  184. int getScrollBarThickness() const;
  185. /** Changes the distance that a single-step click on a scrollbar button
  186. will move the viewport.
  187. */
  188. void setSingleStepSizes (int stepX, int stepY);
  189. /** Returns a pointer to the scrollbar component being used.
  190. Handy if you need to customise the bar somehow.
  191. */
  192. ScrollBar& getVerticalScrollBar() noexcept { return *verticalScrollBar; }
  193. /** Returns a pointer to the scrollbar component being used.
  194. Handy if you need to customise the bar somehow.
  195. */
  196. ScrollBar& getHorizontalScrollBar() noexcept { return *horizontalScrollBar; }
  197. /** Re-instantiates the scrollbars, which is only really useful if you've overridden createScrollBarComponent(). */
  198. void recreateScrollbars();
  199. /** True if there's any off-screen content that could be scrolled vertically,
  200. or false if everything is currently visible.
  201. */
  202. bool canScrollVertically() const noexcept;
  203. /** True if there's any off-screen content that could be scrolled horizontally,
  204. or false if everything is currently visible.
  205. */
  206. bool canScrollHorizontally() const noexcept;
  207. /** Enables or disables drag-to-scroll functionality in the viewport.
  208. If your viewport contains a Component that you don't want to receive mouse events when the
  209. user is drag-scrolling, you can disable this with the Component::setViewportIgnoreDragFlag()
  210. method.
  211. */
  212. void setScrollOnDragEnabled (bool shouldScrollOnDrag);
  213. /** Returns true if drag-to-scroll functionality is enabled. */
  214. bool isScrollOnDragEnabled() const noexcept;
  215. /** Returns true if the user is currently dragging-to-scroll.
  216. @see setScrollOnDragEnabled
  217. */
  218. bool isCurrentlyScrollingOnDrag() const noexcept;
  219. //==============================================================================
  220. /** @internal */
  221. void resized() override;
  222. /** @internal */
  223. void scrollBarMoved (ScrollBar*, double newRangeStart) override;
  224. /** @internal */
  225. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  226. /** @internal */
  227. bool keyPressed (const KeyPress&) override;
  228. /** @internal */
  229. void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;
  230. /** @internal */
  231. void lookAndFeelChanged() override;
  232. /** @internal */
  233. bool useMouseWheelMoveIfNeeded (const MouseEvent&, const MouseWheelDetails&);
  234. /** @internal */
  235. static bool respondsToKey (const KeyPress&);
  236. protected:
  237. //==============================================================================
  238. /** Creates the Scrollbar components that will be added to the Viewport.
  239. Subclasses can override this if they need to customise the scrollbars in some way.
  240. */
  241. virtual ScrollBar* createScrollBarComponent (bool isVertical);
  242. private:
  243. //==============================================================================
  244. std::unique_ptr<ScrollBar> verticalScrollBar, horizontalScrollBar;
  245. Component contentHolder;
  246. WeakReference<Component> contentComp;
  247. Rectangle<int> lastVisibleArea;
  248. int scrollBarThickness = 0;
  249. int singleStepX = 16, singleStepY = 16;
  250. bool showHScrollbar = true, showVScrollbar = true, deleteContent = true;
  251. bool customScrollBarThickness = false;
  252. bool allowScrollingWithoutScrollbarV = false, allowScrollingWithoutScrollbarH = false;
  253. bool vScrollbarRight = true, hScrollbarBottom = true;
  254. struct DragToScrollListener;
  255. std::unique_ptr<DragToScrollListener> dragToScrollListener;
  256. Point<int> viewportPosToCompPos (Point<int>) const;
  257. void updateVisibleArea();
  258. void deleteOrRemoveContentComp();
  259. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Viewport)
  260. };
  261. } // namespace juce