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 16KB

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