From ffbf1cd5c447cd2d05d04165699eb5d0dedccbc3 Mon Sep 17 00:00:00 2001 From: jules Date: Sat, 25 Aug 2012 20:36:45 +0100 Subject: [PATCH] Gave DrawableButton a bit of a spring-cleaning: got rid of its non-standard setBackgroundColours() method, and instead gave it some colour IDs for setting its colours, like other components do. --- extras/JuceDemo/Source/demos/WidgetsDemo.cpp | 3 +- modules/juce_gui_basics/buttons/juce_Button.h | 51 ++++----- .../buttons/juce_DrawableButton.cpp | 104 ++++-------------- .../buttons/juce_DrawableButton.h | 81 ++++++-------- .../lookandfeel/juce_LookAndFeel.cpp | 3 + 5 files changed, 83 insertions(+), 159 deletions(-) diff --git a/extras/JuceDemo/Source/demos/WidgetsDemo.cpp b/extras/JuceDemo/Source/demos/WidgetsDemo.cpp index 1e2eb7d903..9ca6b66c61 100644 --- a/extras/JuceDemo/Source/demos/WidgetsDemo.cpp +++ b/extras/JuceDemo/Source/demos/WidgetsDemo.cpp @@ -513,7 +513,8 @@ public: db = new DrawableButton ("Button 4", DrawableButton::ImageOnButtonBackground); db->setImages (&normal, &over, &down); db->setClickingTogglesState (true); - db->setBackgroundColours (Colours::white, Colours::yellow); + db->setColour (TextButton::buttonColourId, Colours::white); + db->setColour (TextButton::buttonOnColourId, Colours::yellow); db->setBounds (200, 70, 50, 50); db->setTooltip ("this is a DrawableButton on a standard button background"); db->addListener (buttonListener); diff --git a/modules/juce_gui_basics/buttons/juce_Button.h b/modules/juce_gui_basics/buttons/juce_Button.h index c3f5eb43f9..836bf47c09 100644 --- a/modules/juce_gui_basics/buttons/juce_Button.h +++ b/modules/juce_gui_basics/buttons/juce_Button.h @@ -29,7 +29,6 @@ #include "../components/juce_Component.h" #include "../keyboard/juce_KeyListener.h" #include "../commands/juce_ApplicationCommandManager.h" -#include "../windows/juce_TooltipWindow.h" //============================================================================== @@ -52,9 +51,9 @@ protected: //============================================================================== /** Creates a button. - @param buttonName the text to put in the button (the component's name is also - initially set to this string, but these can be changed later - using the setName() and setButtonText() methods) + @param buttonName the text to put in the button (the component's name is also + initially set to this string, but these can be changed later + using the setName() and setButtonText() methods) */ explicit Button (const String& buttonName); @@ -64,28 +63,23 @@ public: //============================================================================== /** Changes the button's text. - @see getButtonText */ void setButtonText (const String& newText); /** Returns the text displayed in the button. - @see setButtonText */ const String& getButtonText() const { return text; } //============================================================================== /** Returns true if the button is currently being held down by the mouse. - @see isOver */ bool isDown() const noexcept; /** Returns true if the mouse is currently over the button. - This will be also be true if the mouse is being held down. - @see isDown */ bool isOver() const noexcept; @@ -125,7 +119,7 @@ public: your own Value object. @see getToggleState, Value */ - Value& getToggleStateValue() { return isOn; } + Value& getToggleStateValue() noexcept { return isOn; } /** This tells the button to automatically flip the toggle state when the button is clicked. @@ -136,7 +130,6 @@ public: void setClickingTogglesState (bool shouldToggle) noexcept; /** Returns true if this button is set to be an automatic toggle-button. - This returns the last value that was passed to setClickingTogglesState(). */ bool getClickingTogglesState() const noexcept; @@ -162,7 +155,6 @@ public: void setRadioGroupId (int newGroupId); /** Returns the ID of the group to which this button belongs. - (See setRadioGroupId() for an explanation of this). */ int getRadioGroupId() const noexcept { return radioGroupId; } @@ -177,13 +169,13 @@ public: { public: /** Destructor. */ - virtual ~Listener() {} + virtual ~Listener() {} /** Called when the button is clicked. */ virtual void buttonClicked (Button* button) = 0; /** Called when the button's state changes. */ - virtual void buttonStateChanged (Button*) {} + virtual void buttonStateChanged (Button*) {} }; /** Registers a listener to receive events when this button's state changes. @@ -226,8 +218,7 @@ public: int commandID, bool generateTooltip); - /** Returns the command ID that was set by setCommandToTrigger(). - */ + /** Returns the command ID that was set by setCommandToTrigger(). */ int getCommandID() const noexcept { return commandID; } //============================================================================== @@ -243,13 +234,11 @@ public: void addShortcut (const KeyPress& key); /** Removes all key shortcuts that had been set for this button. - @see addShortcut */ void clearShortcuts(); /** Returns true if the given keypress is a shortcut for this button. - @see addShortcut */ bool isRegisteredForShortcut (const KeyPress& key) const; @@ -300,8 +289,7 @@ public: //============================================================================== - /** A combination of these flags are used by setConnectedEdges(). - */ + /** A combination of these flags are used by setConnectedEdges(). */ enum ConnectedEdgeFlags { ConnectedOnLeft = 1, @@ -324,27 +312,27 @@ public: void setConnectedEdges (int connectedEdgeFlags); /** Returns the set of flags passed into setConnectedEdges(). */ - int getConnectedEdgeFlags() const noexcept { return connectedEdgeFlags; } + int getConnectedEdgeFlags() const noexcept { return connectedEdgeFlags; } /** Indicates whether the button adjoins another one on its left edge. @see setConnectedEdges */ - bool isConnectedOnLeft() const noexcept { return (connectedEdgeFlags & ConnectedOnLeft) != 0; } + bool isConnectedOnLeft() const noexcept { return (connectedEdgeFlags & ConnectedOnLeft) != 0; } /** Indicates whether the button adjoins another one on its right edge. @see setConnectedEdges */ - bool isConnectedOnRight() const noexcept { return (connectedEdgeFlags & ConnectedOnRight) != 0; } + bool isConnectedOnRight() const noexcept { return (connectedEdgeFlags & ConnectedOnRight) != 0; } /** Indicates whether the button adjoins another one on its top edge. @see setConnectedEdges */ - bool isConnectedOnTop() const noexcept { return (connectedEdgeFlags & ConnectedOnTop) != 0; } + bool isConnectedOnTop() const noexcept { return (connectedEdgeFlags & ConnectedOnTop) != 0; } /** Indicates whether the button adjoins another one on its bottom edge. @see setConnectedEdges */ - bool isConnectedOnBottom() const noexcept { return (connectedEdgeFlags & ConnectedOnBottom) != 0; } + bool isConnectedOnBottom() const noexcept { return (connectedEdgeFlags & ConnectedOnBottom) != 0; } //============================================================================== @@ -367,11 +355,6 @@ public: void setState (const ButtonState newState); - //============================================================================== - // These are deprecated - please use addListener() and removeListener() instead! - JUCE_DEPRECATED (void addButtonListener (Listener*)); - JUCE_DEPRECATED (void removeButtonListener (Listener*)); - protected: //============================================================================== /** This method is called when the button has been clicked. @@ -420,7 +403,7 @@ protected: //============================================================================== /** @internal */ - virtual void internalClickCallback (const ModifierKeys& modifiers); + virtual void internalClickCallback (const ModifierKeys&); /** @internal */ void handleCommandMessage (int commandId); /** @internal */ @@ -495,9 +478,13 @@ private: void turnOffOtherButtonsInGroup (bool sendChangeNotification); void flashButtonState(); - void sendClickMessage (const ModifierKeys& modifiers); + void sendClickMessage (const ModifierKeys&); void sendStateMessage(); + // These are deprecated - please use addListener() and removeListener() instead! + JUCE_DEPRECATED (void addButtonListener (Listener*)); + JUCE_DEPRECATED (void removeButtonListener (Listener*)); + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Button); }; diff --git a/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp b/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp index 246d1cf610..c2e5c5b8e0 100644 --- a/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp +++ b/modules/juce_gui_basics/buttons/juce_DrawableButton.cpp @@ -30,16 +30,6 @@ DrawableButton::DrawableButton (const String& name, currentImage (nullptr), edgeIndent (3) { - if (buttonStyle == ImageOnButtonBackground) - { - backgroundOff = Colour (0xffbbbbff); - backgroundOn = Colour (0xff3333ff); - } - else - { - backgroundOff = Colours::transparentBlack; - backgroundOn = Colour (0xaabbbbff); - } } DrawableButton::~DrawableButton() @@ -80,25 +70,6 @@ void DrawableButton::setButtonStyle (const DrawableButton::ButtonStyle newStyle) } } -void DrawableButton::setBackgroundColours (const Colour& toggledOffColour, - const Colour& toggledOnColour) -{ - if (backgroundOff != toggledOffColour - || backgroundOn != toggledOnColour) - { - backgroundOff = toggledOffColour; - backgroundOn = toggledOnColour; - - repaint(); - } -} - -const Colour& DrawableButton::getBackgroundColour() const noexcept -{ - return getToggleState() ? backgroundOn - : backgroundOff; -} - void DrawableButton::setEdgeIndent (const int numPixelsIndent) { edgeIndent = numPixelsIndent; @@ -120,19 +91,20 @@ void DrawableButton::resized() { Rectangle imageSpace; + const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f)); + const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f)); + if (style == ImageOnButtonBackground) { - imageSpace = getLocalBounds().reduced (getWidth() / 4, getHeight() / 4); + imageSpace = getLocalBounds().reduced (jmax (getWidth() / 4, indentX), + jmax (getHeight() / 4, indentY)); } else { const int textH = (style == ImageAboveTextLabel) ? jmin (16, proportionOfHeight (0.25f)) : 0; - const int indentX = jmin (edgeIndent, proportionOfWidth (0.3f)); - const int indentY = jmin (edgeIndent, proportionOfHeight (0.3f)); - imageSpace.setBounds (indentX, indentY, - getWidth() - indentX * 2, + getWidth() - indentX * 2, getHeight() - indentY * 2 - textH); } @@ -187,20 +159,27 @@ void DrawableButton::enablementChanged() buttonStateChanged(); } +void DrawableButton::colourChanged() +{ + repaint(); +} + void DrawableButton::paintButton (Graphics& g, - bool isMouseOverButton, - bool isButtonDown) + const bool isMouseOverButton, + const bool isButtonDown) { if (style == ImageOnButtonBackground) { getLookAndFeel().drawButtonBackground (g, *this, - getBackgroundColour(), + findColour (getToggleState() ? TextButton::buttonOnColourId + : TextButton::buttonColourId), isMouseOverButton, isButtonDown); } else { - g.fillAll (getBackgroundColour()); + g.fillAll (findColour (getToggleState() ? backgroundOnColourId + : backgroundColourId)); const int textH = (style == ImageAboveTextLabel) ? jmin (16, proportionOfHeight (0.25f)) @@ -224,11 +203,8 @@ void DrawableButton::paintButton (Graphics& g, //============================================================================== Drawable* DrawableButton::getCurrentImage() const noexcept { - if (isDown()) - return getDownImage(); - - if (isOver()) - return getOverImage(); + if (isDown()) return getDownImage(); + if (isOver()) return getOverImage(); return getNormalImage(); } @@ -241,50 +217,18 @@ Drawable* DrawableButton::getNormalImage() const noexcept Drawable* DrawableButton::getOverImage() const noexcept { - Drawable* d = normalImage; - if (getToggleState()) { - if (overImageOn != nullptr) - d = overImageOn; - else if (normalImageOn != nullptr) - d = normalImageOn; - else if (overImage != nullptr) - d = overImage; - } - else - { - if (overImage != nullptr) - d = overImage; + if (overImageOn != nullptr) return overImageOn; + if (normalImageOn != nullptr) return normalImageOn; } - return d; + return overImage != nullptr ? overImage : normalImage; } Drawable* DrawableButton::getDownImage() const noexcept { - Drawable* d = normalImage; + Drawable* const d = getToggleState() ? downImageOn : downImage; - if (getToggleState()) - { - if (downImageOn != nullptr) - d = downImageOn; - else if (overImageOn != nullptr) - d = overImageOn; - else if (normalImageOn != nullptr) - d = normalImageOn; - else if (downImage != nullptr) - d = downImage; - else - d = getOverImage(); - } - else - { - if (downImage != nullptr) - d = downImage; - else - d = getOverImage(); - } - - return d; + return d != nullptr ? d : getOverImage(); } diff --git a/modules/juce_gui_basics/buttons/juce_DrawableButton.h b/modules/juce_gui_basics/buttons/juce_DrawableButton.h index 5315169f1d..a3c68c1864 100644 --- a/modules/juce_gui_basics/buttons/juce_DrawableButton.h +++ b/modules/juce_gui_basics/buttons/juce_DrawableButton.h @@ -49,7 +49,9 @@ public: ImageRaw, /**< The button will just display the images in their normal size and position. This leaves it up to the caller to make sure the images are the correct size and position for the button. */ ImageAboveTextLabel, /**< Draws the button as a text label across the bottom with the image resized and scaled to fit above it. */ - ImageOnButtonBackground /**< Draws the button as a standard rounded-rectangle button with the image on top. */ + ImageOnButtonBackground /**< Draws the button as a standard rounded-rectangle button with the image on top. + Note that if you use this style, the colour IDs that control the button colour are + TextButton::buttonColourId and TextButton::buttonOnColourId. */ }; //============================================================================== @@ -88,60 +90,33 @@ public: An internal copy will be made of the object passed-in if it is non-zero. @param normalImageOn same as the normalImage, but this is used when the button's toggle - state is 'on'. If this is 0, the normal image is used instead + state is 'on'. If this is nullptr, the normal image is used instead @param overImageOn same as the overImage, but this is used when the button's toggle - state is 'on'. If this is 0, the normalImageOn is drawn instead + state is 'on'. If this is nullptr, the normalImageOn is drawn instead @param downImageOn same as the downImage, but this is used when the button's toggle - state is 'on'. If this is 0, the overImageOn is drawn instead + state is 'on'. If this is nullptr, the overImageOn is drawn instead @param disabledImageOn same as the disabledImage, but this is used when the button's toggle - state is 'on'. If this is 0, the normal image will be drawn instead + state is 'on'. If this is nullptr, the normal image will be drawn instead with a reduced opacity */ void setImages (const Drawable* normalImage, - const Drawable* overImage = nullptr, - const Drawable* downImage = nullptr, - const Drawable* disabledImage = nullptr, - const Drawable* normalImageOn = nullptr, - const Drawable* overImageOn = nullptr, - const Drawable* downImageOn = nullptr, + const Drawable* overImage = nullptr, + const Drawable* downImage = nullptr, + const Drawable* disabledImage = nullptr, + const Drawable* normalImageOn = nullptr, + const Drawable* overImageOn = nullptr, + const Drawable* downImageOn = nullptr, const Drawable* disabledImageOn = nullptr); //============================================================================== /** Changes the button's style. - @see ButtonStyle */ void setButtonStyle (ButtonStyle newStyle); //============================================================================== - /** Changes the button's background colours. - - The toggledOffColour is the colour to use when the button's toggle state - is off, and toggledOnColour when it's on. - - For an ImageOnly or ImageAboveTextLabel style, the background colour is - used to fill the background of the component. - - For an ImageOnButtonBackground style, the colour is used to draw the - button's lozenge shape and exactly how the colour's used will depend - on the LookAndFeel. - */ - void setBackgroundColours (const Colour& toggledOffColour, - const Colour& toggledOnColour); - - /** Returns the current background colour being used. - - @see setBackgroundColour - */ - const Colour& getBackgroundColour() const noexcept; - /** Gives the button an optional amount of space around the edge of the drawable. - - This will only apply to ImageFitted or ImageRaw styles, it won't affect the - ones on a button background. If the button is too small for the given gap, a - smaller gap will be used. - By default there's a gap of about 3 pixels. */ void setEdgeIndent (int numPixelsIndent); @@ -149,8 +124,12 @@ public: //============================================================================== /** Returns the image that the button is currently displaying. */ Drawable* getCurrentImage() const noexcept; + + /** Returns the image that the button will use for its normal state. */ Drawable* getNormalImage() const noexcept; + /** Returns the image that the button will use when the mouse is over it. */ Drawable* getOverImage() const noexcept; + /** Returns the image that the button will use when the mouse is held down on it. */ Drawable* getDownImage() const noexcept; //============================================================================== @@ -159,33 +138,43 @@ public: These constants can be used either via the Component::setColour(), or LookAndFeel::setColour() methods. + Note that when the ImageOnButtonBackground style is used, the colour IDs that control + the button colour are TextButton::buttonColourId and TextButton::buttonOnColourId. + @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour */ enum ColourIds { - textColourId = 0x1004010, /**< The colour to use for the URL text. */ + textColourId = 0x1004010, /**< The colour to use for the button's text label. */ + + backgroundColourId = 0x1004011, /**< The colour used to fill the button's background (when + the button is toggled 'off'). Note that if you use the + ImageOnButtonBackground style, you should use TextButton::buttonColourId + to change the button's colour. */ + backgroundOnColourId = 0x1004012, /**< The colour used to fill the button's background (when + the button is toggled 'on'). Note that if you use the + ImageOnButtonBackground style, you should use TextButton::buttonOnColourId + to change the button's colour. */ }; -protected: //============================================================================== /** @internal */ - void paintButton (Graphics& g, - bool isMouseOverButton, - bool isButtonDown); + void paintButton (Graphics&, bool isMouseOverButton, bool isButtonDown); /** @internal */ void buttonStateChanged(); /** @internal */ void resized(); /** @internal */ void enablementChanged(); + /** @internal */ + void colourChanged(); private: //============================================================================== ButtonStyle style; - ScopedPointer normalImage, overImage, downImage, disabledImage; - ScopedPointer normalImageOn, overImageOn, downImageOn, disabledImageOn; + ScopedPointer normalImage, overImage, downImage, disabledImage, + normalImageOn, overImageOn, downImageOn, disabledImageOn; Drawable* currentImage; - Colour backgroundOff, backgroundOn; int edgeIndent; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DrawableButton); diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp index 8e7dae1559..20241a46ce 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp @@ -219,6 +219,9 @@ LookAndFeel::LookAndFeel() Toolbar::labelTextColourId, 0xff000000, Toolbar::editingModeOutlineColourId, 0xffff0000, + DrawableButton::backgroundColourId, 0x00000000, + DrawableButton::backgroundOnColourId, 0xaabbbbff, + HyperlinkButton::textColourId, 0xcc1111ee, GroupComponent::outlineColourId, 0x66000000,