The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

292 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_VIEWPORT_JUCEHEADER__
  19. #define __JUCE_VIEWPORT_JUCEHEADER__
  20. #include "juce_ScrollBar.h"
  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. */
  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::empty);
  42. /** Destructor. */
  43. ~Viewport();
  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; }
  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 (const 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. */
  103. const Point<int>& getViewPosition() const noexcept { return lastVisibleArea.getPosition(); }
  104. /** Returns the position within the child component of the top-left of its visible area.
  105. @see getViewWidth, setViewPosition
  106. */
  107. int getViewPositionX() const noexcept { return lastVisibleArea.getX(); }
  108. /** Returns the position within the child component of the top-left of its visible area.
  109. @see getViewHeight, setViewPosition
  110. */
  111. int getViewPositionY() const noexcept { return lastVisibleArea.getY(); }
  112. /** Returns the width of the visible area of the child component.
  113. This may be less than the width of this Viewport if there's a vertical scrollbar
  114. or if the child component is itself smaller.
  115. */
  116. int getViewWidth() const noexcept { return lastVisibleArea.getWidth(); }
  117. /** Returns the height of the visible area of the child component.
  118. This may be less than the height of this Viewport if there's a horizontal scrollbar
  119. or if the child component is itself smaller.
  120. */
  121. int getViewHeight() const noexcept { return lastVisibleArea.getHeight(); }
  122. /** Returns the width available within this component for the contents.
  123. This will be the width of the viewport component minus the width of a
  124. vertical scrollbar (if visible).
  125. */
  126. int getMaximumVisibleWidth() const;
  127. /** Returns the height available within this component for the contents.
  128. This will be the height of the viewport component minus the space taken up
  129. by a horizontal scrollbar (if visible).
  130. */
  131. int getMaximumVisibleHeight() const;
  132. //==============================================================================
  133. /** Callback method that is called when the visible area changes.
  134. This will be called when the visible area is moved either be scrolling or
  135. by calls to setViewPosition(), etc.
  136. */
  137. virtual void visibleAreaChanged (const Rectangle<int>& newVisibleArea);
  138. /** Callback method that is called when the viewed component is added, removed or swapped. */
  139. virtual void viewedComponentChanged (Component* newComponent);
  140. //==============================================================================
  141. /** Turns scrollbars on or off.
  142. If set to false, the scrollbars won't ever appear. When true (the default)
  143. they will appear only when needed.
  144. */
  145. void setScrollBarsShown (bool showVerticalScrollbarIfNeeded,
  146. bool showHorizontalScrollbarIfNeeded);
  147. /** True if the vertical scrollbar is enabled.
  148. @see setScrollBarsShown
  149. */
  150. bool isVerticalScrollBarShown() const noexcept { return showVScrollbar; }
  151. /** True if the horizontal scrollbar is enabled.
  152. @see setScrollBarsShown
  153. */
  154. bool isHorizontalScrollBarShown() const noexcept { return showHScrollbar; }
  155. /** Changes the width of the scrollbars.
  156. If this isn't specified, the default width from the LookAndFeel class will be used.
  157. @see LookAndFeel::getDefaultScrollbarWidth
  158. */
  159. void setScrollBarThickness (int thickness);
  160. /** Returns the thickness of the scrollbars.
  161. @see setScrollBarThickness
  162. */
  163. int getScrollBarThickness() const;
  164. /** Changes the distance that a single-step click on a scrollbar button
  165. will move the viewport.
  166. */
  167. void setSingleStepSizes (int stepX, int stepY);
  168. /** Shows or hides the buttons on any scrollbars that are used.
  169. @see ScrollBar::setButtonVisibility
  170. */
  171. void setScrollBarButtonVisibility (bool buttonsVisible);
  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. struct Ids
  182. {
  183. static const Identifier showScrollBarV, showScrollBarH, scrollBarWidth;
  184. };
  185. void refreshFromValueTree (const ValueTree&, ComponentBuilder&);
  186. //==============================================================================
  187. /** @internal */
  188. void resized();
  189. /** @internal */
  190. void scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart);
  191. /** @internal */
  192. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&);
  193. /** @internal */
  194. bool keyPressed (const KeyPress&);
  195. /** @internal */
  196. void componentMovedOrResized (Component&, bool wasMoved, bool wasResized);
  197. /** @internal */
  198. bool useMouseWheelMoveIfNeeded (const MouseEvent&, const MouseWheelDetails&);
  199. private:
  200. //==============================================================================
  201. WeakReference<Component> contentComp;
  202. Rectangle<int> lastVisibleArea;
  203. int scrollBarThickness;
  204. int singleStepX, singleStepY;
  205. bool showHScrollbar, showVScrollbar, deleteContent;
  206. Component contentHolder;
  207. ScrollBar verticalScrollBar;
  208. ScrollBar horizontalScrollBar;
  209. Point<int> viewportPosToCompPos (const Point<int>&) const;
  210. void updateVisibleArea();
  211. void deleteContentComp();
  212. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  213. // If you get an error here, it's because this method's parameters have changed! See the new definition above..
  214. virtual int visibleAreaChanged (int, int, int, int) { return 0; }
  215. #endif
  216. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Viewport);
  217. };
  218. #endif // __JUCE_VIEWPORT_JUCEHEADER__