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.

188 lines
7.9KB

  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 component that is positioned on either the left- or right-hand side of its parent,
  24. containing a header and some content. This sort of component is typically used for
  25. navigation and forms in mobile applications.
  26. When triggered with the showOrHide() method, the SidePanel will animate itself to its
  27. new position. This component also contains some logic to reactively resize and dismiss
  28. itself when the user drags it.
  29. */
  30. //==============================================================================
  31. class SidePanel : public Component,
  32. private ComponentListener
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a SidePanel component.
  37. @param title the text to use for the SidePanel's title bar
  38. @param width the width of the SidePanel
  39. @param positionOnLeft if true, the SidePanel will be positioned on the left of its parent component and
  40. if false, the SidePanel will be positioned on the right of its parent component
  41. @param contentComponent the component to add to this SidePanel - this content will take up the full
  42. size of the SidePanel, minus the height of the title bar. You can pass nullptr
  43. to this if you like and set the content component later using the setContent() method
  44. @param deleteComponentWhenNoLongerNeeded if true, the component will be deleted automatically when
  45. the SidePanel is deleted or when a different component is added. If false,
  46. the caller must manage the lifetime of the component
  47. */
  48. SidePanel (StringRef title, int width, bool positionOnLeft,
  49. Component* contentComponent = nullptr,
  50. bool deleteComponentWhenNoLongerNeeded = true);
  51. /** Destructor */
  52. ~SidePanel();
  53. //==============================================================================
  54. /** Sets the component that this SidePanel will contain.
  55. This will add the given component to this SidePanel and position it below the title bar.
  56. (Don't add or remove any child components directly using the normal
  57. Component::addChildComponent() methods).
  58. @param newContentComponent the component to add to this SidePanel, or null to remove
  59. the current component.
  60. @param deleteComponentWhenNoLongerNeeded if true, the component will be deleted automatically when
  61. the SidePanel is deleted or when a different component is added. If false,
  62. the caller must manage the lifetime of the component
  63. @see getContent
  64. */
  65. void setContent (Component* newContentComponent,
  66. bool deleteComponentWhenNoLongerNeeded = true);
  67. /** Returns the component that's currently being used inside the SidePanel.
  68. @see setViewedComponent
  69. */
  70. Component* getContent() { return contentComponent.get(); }
  71. /** Shows or hides the SidePanel.
  72. This will animate the SidePanel to either its full width or to be hidden on the
  73. left- or right-hand side of its parent component depending on the value of positionOnLeft
  74. that was passed to the constructor.
  75. @param show if true, this will show the SidePanel and if false the SidePanel will be hidden
  76. */
  77. void showOrHide (bool show);
  78. //==============================================================================
  79. /** Returns true if the SidePanel is currently showing. */
  80. bool isPanelShowing() const noexcept { return isShowing; }
  81. /** Returns true if the SidePanel is positioned on the left of its parent. */
  82. bool isPanelOnLeft() const noexcept { return isOnLeft; }
  83. /** Sets the width of the shadow that will be drawn on the side of the panel. */
  84. void setShadowWidth (int newWidth) noexcept { shadowWidth = newWidth; }
  85. /** Sets the height of the title bar at the top of the SidePanel. */
  86. void setTitleBarHeight (int newHeight) noexcept { titleBarHeight = newHeight; }
  87. //==============================================================================
  88. void resized() override;
  89. void paint (Graphics& g) override;
  90. void parentHierarchyChanged() override;
  91. void mouseDrag (const MouseEvent&) override;
  92. void mouseUp (const MouseEvent&) override;
  93. //==============================================================================
  94. /** This abstract base class is implemented by LookAndFeel classes to provide
  95. SidePanel drawing functionality.
  96. */
  97. struct JUCE_API LookAndFeelMethods
  98. {
  99. virtual ~LookAndFeelMethods() {}
  100. virtual Font getSidePanelTitleFont (SidePanel&) = 0;
  101. virtual Justification getSidePanelTitleJustification (SidePanel&) = 0;
  102. virtual Path getSidePanelDismissButtonShape (SidePanel&) = 0;
  103. };
  104. /** A set of colour IDs to use to change the colour of various aspects of the SidePanel.
  105. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  106. methods.
  107. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  108. */
  109. enum ColourIds
  110. {
  111. backgroundColour = 0x100f001,
  112. titleTextColour = 0x100f002,
  113. shadowBaseColour = 0x100f003,
  114. dismissButtonNormalColour = 0x100f004,
  115. dismissButtonOverColour = 0x100f004,
  116. dismissButtonDownColour = 0x100f005
  117. };
  118. private:
  119. //==========================================================================
  120. Component* parent = nullptr;
  121. OptionalScopedPointer<Component> contentComponent;
  122. Label titleLabel;
  123. ShapeButton dismissButton { "dismissButton", Colours::lightgrey, Colours::lightgrey, Colours::white };
  124. Rectangle<int> shadowArea;
  125. bool isOnLeft = false;
  126. bool isShowing = false;
  127. int panelWidth = 0;
  128. int shadowWidth = 15;
  129. int titleBarHeight = 40;
  130. Rectangle<int> startingBounds;
  131. bool shouldResize = false;
  132. int amountMoved = 0;
  133. //==========================================================================
  134. void lookAndFeelChanged() override;
  135. void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;
  136. Rectangle<int> calculateBoundsInParent (Component&) const;
  137. void calculateAndRemoveShadowBounds (Rectangle<int>& bounds);
  138. bool isMouseEventInThisOrChildren (Component*);
  139. //==============================================================================
  140. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SidePanel)
  141. };
  142. } // namespace juce