/* ============================================================================== This file is part of the JUCE library. Copyright (c) 2017 - ROLI Ltd. JUCE is an open source library subject to commercial or open-source licensing. By using JUCE, you agree to the terms of both the JUCE 5 End-User License Agreement and JUCE 5 Privacy Policy (both updated and effective as of the 27th April 2017). End User License Agreement: www.juce.com/juce-5-licence Privacy Policy: www.juce.com/juce-5-privacy-policy Or: You may also use this code under the terms of the GPL v3 (see www.gnu.org/licenses). JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE DISCLAIMED. ============================================================================== */ namespace juce { //============================================================================== /** A base class for buttons. This contains all the logic for button behaviours such as enabling/disabling, responding to shortcut keystrokes, auto-repeating when held down, toggle-buttons and radio groups, etc. @see TextButton, DrawableButton, ToggleButton */ class JUCE_API Button : public Component, public SettableTooltipClient { 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) */ explicit Button (const String& buttonName); public: /** Destructor. */ virtual ~Button(); //============================================================================== /** 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. @see isOver */ bool isDown() const noexcept; /** Returns true if the mouse is currently over the button. This will be also be true if the button is being held down. @see isDown */ bool isOver() const noexcept; //============================================================================== /** A button has an on/off state associated with it, and this changes that. By default buttons are 'off' and for simple buttons that you click to perform an action you won't change this. Toggle buttons, however will want to change their state when turned on or off. @param shouldBeOn whether to set the button's toggle state to be on or off. If it's a member of a button group, this will always try to turn it on, and to turn off any other buttons in the group @param notification determines the behaviour if the value changes - this can invoke a synchronous call to clicked(), but sendNotificationAsync is not supported @see getToggleState, setRadioGroupId */ void setToggleState (bool shouldBeOn, NotificationType notification); /** Returns true if the button is 'on'. By default buttons are 'off' and for simple buttons that you click to perform an action you won't change this. Toggle buttons, however will want to change their state when turned on or off. @see setToggleState */ bool getToggleState() const noexcept { return isOn.getValue(); } /** Returns the Value object that represents the botton's toggle state. You can use this Value object to connect the button's state to external values or setters, either by taking a copy of the Value, or by using Value::referTo() to make it point to your own Value object. @see getToggleState, Value */ Value& getToggleStateValue() noexcept { return isOn; } /** This tells the button to automatically flip the toggle state when the button is clicked. If set to true, then before the clicked() callback occurs, the toggle-state of the button is flipped. */ void setClickingTogglesState (bool shouldAutoToggleOnClick) 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; //============================================================================== /** Enables the button to act as a member of a mutually-exclusive group of 'radio buttons'. If the group ID is set to a non-zero number, then this button will act as part of a group of buttons with the same ID, only one of which can be 'on' at the same time. Note that when it's part of a group, clicking a toggle-button that's 'on' won't turn it off. To find other buttons with the same ID, this button will search through its sibling components for ToggleButtons, so all the buttons for a particular group must be placed inside the same parent component. Set the group ID back to zero if you want it to act as a normal toggle button again. The notification argument lets you specify how other buttons should react to being turned on or off in response to this call. @see getRadioGroupId */ void setRadioGroupId (int newGroupId, NotificationType notification = sendNotification); /** Returns the ID of the group to which this button belongs. (See setRadioGroupId() for an explanation of this). */ int getRadioGroupId() const noexcept { return radioGroupId; } //============================================================================== /** Used to receive callbacks when a button is clicked. @see Button::addListener, Button::removeListener */ class JUCE_API Listener { public: /** Destructor. */ virtual ~Listener() {} /** Called when the button is clicked. */ virtual void buttonClicked (Button*) = 0; /** Called when the button's state changes. */ virtual void buttonStateChanged (Button*) {} }; /** Registers a listener to receive events when this button's state changes. If the listener is already registered, this will not register it again. @see removeListener */ void addListener (Listener* newListener); /** Removes a previously-registered button listener @see addListener */ void removeListener (Listener* listener); /** You can assign a lambda to this callback object to have it called when the button is clicked. */ EventHandler