From a604c49a354f0575308aa45a23d39aed0aa4862c Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 19 Feb 2018 16:07:40 +0000 Subject: [PATCH] Added some methods to SidePanel to set a custom title bar --- .../juce_gui_basics/layout/juce_SidePanel.cpp | 38 ++++++++++++++++-- .../juce_gui_basics/layout/juce_SidePanel.h | 40 ++++++++++++++++++- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/modules/juce_gui_basics/layout/juce_SidePanel.cpp b/modules/juce_gui_basics/layout/juce_SidePanel.cpp index f7257d0189..ac3fa1d173 100644 --- a/modules/juce_gui_basics/layout/juce_SidePanel.cpp +++ b/modules/juce_gui_basics/layout/juce_SidePanel.cpp @@ -69,6 +69,25 @@ void SidePanel::setContent (Component* newContent, bool deleteComponentWhenNoLon } } +void SidePanel::setTitleBarComponent (Component* titleBarComponentToUse, + bool keepDismissButton, + bool deleteComponentWhenNoLongerNeeded) +{ + if (titleBarComponent.get() != titleBarComponentToUse) + { + if (deleteComponentWhenNoLongerNeeded) + titleBarComponent.setOwned (titleBarComponentToUse); + else + titleBarComponent.setNonOwned (titleBarComponentToUse); + + addAndMakeVisible (titleBarComponent); + + resized(); + } + + shouldShowDismissButton = keepDismissButton; +} + void SidePanel::showOrHide (bool show) { if (parent != nullptr) @@ -97,11 +116,22 @@ void SidePanel::resized() auto titleBounds = bounds.removeFromTop (titleBarHeight); - dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10) - : titleBounds.removeFromLeft (30).withTrimmedLeft (10)); + if (titleBarComponent != nullptr) + { + if (shouldShowDismissButton) + dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10) + : titleBounds.removeFromLeft (30).withTrimmedLeft (10)); - titleLabel.setBounds (isOnLeft ? titleBounds.withTrimmedRight (40) - : titleBounds.withTrimmedLeft (40)); + titleBarComponent->setBounds (titleBounds); + } + else + { + dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10) + : titleBounds.removeFromLeft (30).withTrimmedLeft (10)); + + titleLabel.setBounds (isOnLeft ? titleBounds.withTrimmedRight (40) + : titleBounds.withTrimmedLeft (40)); + } if (contentComponent != nullptr) contentComponent->setBounds (bounds); diff --git a/modules/juce_gui_basics/layout/juce_SidePanel.h b/modules/juce_gui_basics/layout/juce_SidePanel.h index fb909ce346..2f16f2d9d7 100644 --- a/modules/juce_gui_basics/layout/juce_SidePanel.h +++ b/modules/juce_gui_basics/layout/juce_SidePanel.h @@ -71,7 +71,7 @@ public: (Don't add or remove any child components directly using the normal Component::addChildComponent() methods). - @param newContentComponent the component to add to this SidePanel, or null to remove + @param newContentComponent the component to add to this SidePanel, or nullptr to remove the current component. @param deleteComponentWhenNoLongerNeeded if true, the component will be deleted automatically when the SidePanel is deleted or when a different component is added. If false, @@ -86,7 +86,31 @@ public: @see setViewedComponent */ - Component* getContent() { return contentComponent.get(); } + Component* getContent() const noexcept { return contentComponent.get(); } + + /** Sets a custom component to be used for the title bar of this SidePanel, replacing + the default. You can pass a nullptr to revert to the default title bar. + + @param titleBarComponentToUse the component to use as the title bar, or nullptr to use + the default + @param keepDismissButton if false the specified component will take up the full width of + the title bar including the dismiss button but if true, the default + dismiss button will be kept + @param deleteComponentWhenNoLongerNeeded if true, the component will be deleted automatically when + the SidePanel is deleted or when a different component is added. If false, + the caller must manage the lifetime of the component + + @see getTitleBarComponent + */ + void setTitleBarComponent (Component* titleBarComponentToUse, + bool keepDismissButton, + bool deleteComponentWhenNoLongerNeeded = true); + + /** Returns the component that is currently being used as the title bar of the SidePanel. + + @see setTitleBarComponent + */ + Component* getTitleBarComponent() const noexcept { return titleBarComponent.get(); } /** Shows or hides the SidePanel. @@ -108,9 +132,18 @@ public: /** Sets the width of the shadow that will be drawn on the side of the panel. */ void setShadowWidth (int newWidth) noexcept { shadowWidth = newWidth; } + /** Returns the width of the shadow that will be drawn on the side of the panel. */ + int getShadowWidth() const noexcept { return shadowWidth; } + /** Sets the height of the title bar at the top of the SidePanel. */ void setTitleBarHeight (int newHeight) noexcept { titleBarHeight = newHeight; } + /** Returns the height of the title bar at the top of the SidePanel. */ + int getTitleBarHeight() const noexcept { return titleBarHeight; } + + /** Returns the text that is displayed in the title bar at the top of the SidePanel. */ + String getTitleText() const noexcept { return titleLabel.getText(); } + //============================================================================== void moved() override; void resized() override; @@ -162,6 +195,7 @@ private: //========================================================================== Component* parent = nullptr; OptionalScopedPointer contentComponent; + OptionalScopedPointer titleBarComponent; Label titleLabel; ShapeButton dismissButton { "dismissButton", Colours::lightgrey, Colours::lightgrey, Colours::white }; @@ -179,6 +213,8 @@ private: bool shouldResize = false; int amountMoved = 0; + bool shouldShowDismissButton = true; + //========================================================================== void lookAndFeelChanged() override; void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;