123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 |
- /*
- ==============================================================================
- This file is part of the JUCE library.
- Copyright (c) 2013 - Raw Material Software Ltd.
- Permission is granted to use this software under the terms of either:
- a) the GPL v2 (or any later version)
- b) the Affero GPL v3
- Details of these licenses can be found at: www.gnu.org/licenses
- JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- ------------------------------------------------------------------------------
- To release a closed-source product which uses JUCE, commercial licenses are
- available: visit www.juce.com for more information.
- ==============================================================================
- */
- class Button::CallbackHelper : public Timer,
- public ApplicationCommandManagerListener,
- public ValueListener,
- public KeyListener
- {
- public:
- CallbackHelper (Button& b) : button (b) {}
- void timerCallback() override
- {
- button.repeatTimerCallback();
- }
- bool keyStateChanged (bool, Component*) override
- {
- return button.keyStateChangedCallback();
- }
- void valueChanged (Value& value) override
- {
- if (value.refersToSameSourceAs (button.isOn))
- button.setToggleState (button.isOn.getValue(), sendNotification);
- }
- bool keyPressed (const KeyPress&, Component*) override
- {
- // returning true will avoid forwarding events for keys that we're using as shortcuts
- return button.isShortcutPressed();
- }
- void applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo& info) override
- {
- if (info.commandID == button.commandID
- && (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) == 0)
- button.flashButtonState();
- }
- void applicationCommandListChanged() override
- {
- button.applicationCommandListChangeCallback();
- }
- private:
- Button& button;
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CallbackHelper)
- };
- //==============================================================================
- Button::Button (const String& name)
- : Component (name),
- text (name),
- buttonPressTime (0),
- lastRepeatTime (0),
- commandManagerToUse (nullptr),
- autoRepeatDelay (-1),
- autoRepeatSpeed (0),
- autoRepeatMinimumDelay (-1),
- radioGroupId (0),
- connectedEdgeFlags (0),
- commandID(),
- buttonState (buttonNormal),
- lastToggleState (false),
- clickTogglesState (false),
- needsToRelease (false),
- needsRepainting (false),
- isKeyDown (false),
- triggerOnMouseDown (false),
- generateTooltip (false)
- {
- callbackHelper = new CallbackHelper (*this);
- setWantsKeyboardFocus (true);
- isOn.addListener (callbackHelper);
- }
- Button::~Button()
- {
- clearShortcuts();
- if (commandManagerToUse != nullptr)
- commandManagerToUse->removeListener (callbackHelper);
- isOn.removeListener (callbackHelper);
- callbackHelper = nullptr;
- }
- //==============================================================================
- void Button::setButtonText (const String& newText)
- {
- if (text != newText)
- {
- text = newText;
- repaint();
- }
- }
- void Button::setTooltip (const String& newTooltip)
- {
- SettableTooltipClient::setTooltip (newTooltip);
- generateTooltip = false;
- }
- void Button::updateAutomaticTooltip (const ApplicationCommandInfo& info)
- {
- if (generateTooltip && commandManagerToUse != nullptr)
- {
- String tt (info.description.isNotEmpty() ? info.description
- : info.shortName);
- Array<KeyPress> keyPresses (commandManagerToUse->getKeyMappings()->getKeyPressesAssignedToCommand (commandID));
- for (int i = 0; i < keyPresses.size(); ++i)
- {
- const String key (keyPresses.getReference(i).getTextDescription());
- tt << " [";
- if (key.length() == 1)
- tt << TRANS("shortcut") << ": '" << key << "']";
- else
- tt << key << ']';
- }
- SettableTooltipClient::setTooltip (tt);
- }
- }
- void Button::setConnectedEdges (const int newFlags)
- {
- if (connectedEdgeFlags != newFlags)
- {
- connectedEdgeFlags = newFlags;
- repaint();
- }
- }
- //==============================================================================
- void Button::setToggleState (const bool shouldBeOn, const NotificationType notification)
- {
- if (shouldBeOn != lastToggleState)
- {
- WeakReference<Component> deletionWatcher (this);
- if (shouldBeOn)
- {
- turnOffOtherButtonsInGroup (notification);
- if (deletionWatcher == nullptr)
- return;
- }
- if (getToggleState() != shouldBeOn) // this test means that if the value is void rather than explicitly set to
- isOn = shouldBeOn; // false, it won't be changed unless the required value is true.
- lastToggleState = shouldBeOn;
- repaint();
- if (notification != dontSendNotification)
- {
- // async callbacks aren't possible here
- jassert (notification != sendNotificationAsync);
- sendClickMessage (ModifierKeys::getCurrentModifiers());
- if (deletionWatcher == nullptr)
- return;
- }
- if (notification != dontSendNotification)
- sendStateMessage();
- else
- buttonStateChanged();
- }
- }
- void Button::setToggleState (const bool shouldBeOn, bool sendChange)
- {
- setToggleState (shouldBeOn, sendChange ? sendNotification : dontSendNotification);
- }
- void Button::setClickingTogglesState (const bool shouldToggle) noexcept
- {
- clickTogglesState = shouldToggle;
- // if you've got clickTogglesState turned on, you shouldn't also connect the button
- // up to be a command invoker. Instead, your command handler must flip the state of whatever
- // it is that this button represents, and the button will update its state to reflect this
- // in the applicationCommandListChanged() method.
- jassert (commandManagerToUse == nullptr || ! clickTogglesState);
- }
- bool Button::getClickingTogglesState() const noexcept
- {
- return clickTogglesState;
- }
- void Button::setRadioGroupId (const int newGroupId, NotificationType notification)
- {
- if (radioGroupId != newGroupId)
- {
- radioGroupId = newGroupId;
- if (lastToggleState)
- turnOffOtherButtonsInGroup (notification);
- }
- }
- void Button::turnOffOtherButtonsInGroup (const NotificationType notification)
- {
- if (Component* const p = getParentComponent())
- {
- if (radioGroupId != 0)
- {
- WeakReference<Component> deletionWatcher (this);
- for (int i = p->getNumChildComponents(); --i >= 0;)
- {
- Component* const c = p->getChildComponent (i);
- if (c != this)
- {
- if (Button* const b = dynamic_cast <Button*> (c))
- {
- if (b->getRadioGroupId() == radioGroupId)
- {
- b->setToggleState (false, notification);
- if (deletionWatcher == nullptr)
- return;
- }
- }
- }
- }
- }
- }
- }
- //==============================================================================
- void Button::enablementChanged()
- {
- updateState();
- repaint();
- }
- Button::ButtonState Button::updateState()
- {
- return updateState (isMouseOver (true), isMouseButtonDown());
- }
- Button::ButtonState Button::updateState (const bool over, const bool down)
- {
- ButtonState newState = buttonNormal;
- if (isEnabled() && isVisible() && ! isCurrentlyBlockedByAnotherModalComponent())
- {
- if ((down && (over || (triggerOnMouseDown && buttonState == buttonDown))) || isKeyDown)
- newState = buttonDown;
- else if (over)
- newState = buttonOver;
- }
- setState (newState);
- return newState;
- }
- void Button::setState (const ButtonState newState)
- {
- if (buttonState != newState)
- {
- buttonState = newState;
- repaint();
- if (buttonState == buttonDown)
- {
- buttonPressTime = Time::getApproximateMillisecondCounter();
- lastRepeatTime = 0;
- }
- sendStateMessage();
- }
- }
- bool Button::isDown() const noexcept { return buttonState == buttonDown; }
- bool Button::isOver() const noexcept { return buttonState != buttonNormal; }
- void Button::buttonStateChanged() {}
- uint32 Button::getMillisecondsSinceButtonDown() const noexcept
- {
- const uint32 now = Time::getApproximateMillisecondCounter();
- return now > buttonPressTime ? now - buttonPressTime : 0;
- }
- void Button::setTriggeredOnMouseDown (const bool isTriggeredOnMouseDown) noexcept
- {
- triggerOnMouseDown = isTriggeredOnMouseDown;
- }
- //==============================================================================
- void Button::clicked()
- {
- }
- void Button::clicked (const ModifierKeys&)
- {
- clicked();
- }
- enum { clickMessageId = 0x2f3f4f99 };
- void Button::triggerClick()
- {
- postCommandMessage (clickMessageId);
- }
- void Button::internalClickCallback (const ModifierKeys& modifiers)
- {
- if (clickTogglesState)
- {
- const bool shouldBeOn = (radioGroupId != 0 || ! lastToggleState);
- if (shouldBeOn != getToggleState())
- {
- setToggleState (shouldBeOn, sendNotification);
- return;
- }
- }
- sendClickMessage (modifiers);
- }
- void Button::flashButtonState()
- {
- if (isEnabled())
- {
- needsToRelease = true;
- setState (buttonDown);
- callbackHelper->startTimer (100);
- }
- }
- void Button::handleCommandMessage (int commandId)
- {
- if (commandId == clickMessageId)
- {
- if (isEnabled())
- {
- flashButtonState();
- internalClickCallback (ModifierKeys::getCurrentModifiers());
- }
- }
- else
- {
- Component::handleCommandMessage (commandId);
- }
- }
- //==============================================================================
- void Button::addListener (ButtonListener* const newListener)
- {
- buttonListeners.add (newListener);
- }
- void Button::removeListener (ButtonListener* const listener)
- {
- buttonListeners.remove (listener);
- }
- void Button::sendClickMessage (const ModifierKeys& modifiers)
- {
- Component::BailOutChecker checker (this);
- if (commandManagerToUse != nullptr && commandID != 0)
- {
- ApplicationCommandTarget::InvocationInfo info (commandID);
- info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromButton;
- info.originatingComponent = this;
- commandManagerToUse->invoke (info, true);
- }
- clicked (modifiers);
- if (! checker.shouldBailOut())
- buttonListeners.callChecked (checker, &ButtonListener::buttonClicked, this); // (can't use Button::Listener due to idiotic VC2005 bug)
- }
- void Button::sendStateMessage()
- {
- Component::BailOutChecker checker (this);
- buttonStateChanged();
- if (! checker.shouldBailOut())
- buttonListeners.callChecked (checker, &ButtonListener::buttonStateChanged, this);
- }
- //==============================================================================
- void Button::paint (Graphics& g)
- {
- if (needsToRelease && isEnabled())
- {
- needsToRelease = false;
- needsRepainting = true;
- }
- paintButton (g, isOver(), isDown());
- }
- //==============================================================================
- void Button::mouseEnter (const MouseEvent&) { updateState (true, false); }
- void Button::mouseExit (const MouseEvent&) { updateState (false, false); }
- void Button::mouseDown (const MouseEvent& e)
- {
- updateState (true, true);
- if (isDown())
- {
- if (autoRepeatDelay >= 0)
- callbackHelper->startTimer (autoRepeatDelay);
- if (triggerOnMouseDown)
- internalClickCallback (e.mods);
- }
- }
- void Button::mouseUp (const MouseEvent& e)
- {
- const bool wasDown = isDown();
- const bool wasOver = isOver();
- updateState (isMouseOver(), false);
- if (wasDown && wasOver && ! triggerOnMouseDown)
- internalClickCallback (e.mods);
- }
- void Button::mouseDrag (const MouseEvent&)
- {
- const ButtonState oldState = buttonState;
- updateState (isMouseOver(), true);
- if (autoRepeatDelay >= 0 && buttonState != oldState && isDown())
- callbackHelper->startTimer (autoRepeatSpeed);
- }
- void Button::focusGained (FocusChangeType)
- {
- updateState();
- repaint();
- }
- void Button::focusLost (FocusChangeType)
- {
- updateState();
- repaint();
- }
- void Button::visibilityChanged()
- {
- needsToRelease = false;
- updateState();
- }
- void Button::parentHierarchyChanged()
- {
- Component* const newKeySource = (shortcuts.size() == 0) ? nullptr : getTopLevelComponent();
- if (newKeySource != keySource.get())
- {
- if (keySource != nullptr)
- keySource->removeKeyListener (callbackHelper);
- keySource = newKeySource;
- if (keySource != nullptr)
- keySource->addKeyListener (callbackHelper);
- }
- }
- //==============================================================================
- void Button::setCommandToTrigger (ApplicationCommandManager* const newCommandManager,
- const CommandID newCommandID, const bool generateTip)
- {
- commandID = newCommandID;
- generateTooltip = generateTip;
- if (commandManagerToUse != newCommandManager)
- {
- if (commandManagerToUse != nullptr)
- commandManagerToUse->removeListener (callbackHelper);
- commandManagerToUse = newCommandManager;
- if (commandManagerToUse != nullptr)
- commandManagerToUse->addListener (callbackHelper);
- // if you've got clickTogglesState turned on, you shouldn't also connect the button
- // up to be a command invoker. Instead, your command handler must flip the state of whatever
- // it is that this button represents, and the button will update its state to reflect this
- // in the applicationCommandListChanged() method.
- jassert (commandManagerToUse == nullptr || ! clickTogglesState);
- }
- if (commandManagerToUse != nullptr)
- applicationCommandListChangeCallback();
- else
- setEnabled (true);
- }
- void Button::applicationCommandListChangeCallback()
- {
- if (commandManagerToUse != nullptr)
- {
- ApplicationCommandInfo info (0);
- if (commandManagerToUse->getTargetForCommand (commandID, info) != nullptr)
- {
- updateAutomaticTooltip (info);
- setEnabled ((info.flags & ApplicationCommandInfo::isDisabled) == 0);
- setToggleState ((info.flags & ApplicationCommandInfo::isTicked) != 0, dontSendNotification);
- }
- else
- {
- setEnabled (false);
- }
- }
- }
- //==============================================================================
- void Button::addShortcut (const KeyPress& key)
- {
- if (key.isValid())
- {
- jassert (! isRegisteredForShortcut (key)); // already registered!
- shortcuts.add (key);
- parentHierarchyChanged();
- }
- }
- void Button::clearShortcuts()
- {
- shortcuts.clear();
- parentHierarchyChanged();
- }
- bool Button::isShortcutPressed() const
- {
- if (isShowing() && ! isCurrentlyBlockedByAnotherModalComponent())
- for (int i = shortcuts.size(); --i >= 0;)
- if (shortcuts.getReference(i).isCurrentlyDown())
- return true;
- return false;
- }
- bool Button::isRegisteredForShortcut (const KeyPress& key) const
- {
- for (int i = shortcuts.size(); --i >= 0;)
- if (key == shortcuts.getReference(i))
- return true;
- return false;
- }
- bool Button::keyStateChangedCallback()
- {
- if (! isEnabled())
- return false;
- const bool wasDown = isKeyDown;
- isKeyDown = isShortcutPressed();
- if (autoRepeatDelay >= 0 && (isKeyDown && ! wasDown))
- callbackHelper->startTimer (autoRepeatDelay);
- updateState();
- if (isEnabled() && wasDown && ! isKeyDown)
- {
- internalClickCallback (ModifierKeys::getCurrentModifiers());
- // (return immediately - this button may now have been deleted)
- return true;
- }
- return wasDown || isKeyDown;
- }
- bool Button::keyPressed (const KeyPress& key)
- {
- if (isEnabled() && key.isKeyCode (KeyPress::returnKey))
- {
- triggerClick();
- return true;
- }
- return false;
- }
- //==============================================================================
- void Button::setRepeatSpeed (const int initialDelayMillisecs,
- const int repeatMillisecs,
- const int minimumDelayInMillisecs) noexcept
- {
- autoRepeatDelay = initialDelayMillisecs;
- autoRepeatSpeed = repeatMillisecs;
- autoRepeatMinimumDelay = jmin (autoRepeatSpeed, minimumDelayInMillisecs);
- }
- void Button::repeatTimerCallback()
- {
- if (needsRepainting)
- {
- callbackHelper->stopTimer();
- updateState();
- needsRepainting = false;
- }
- else if (autoRepeatSpeed > 0 && (isKeyDown || (updateState() == buttonDown)))
- {
- int repeatSpeed = autoRepeatSpeed;
- if (autoRepeatMinimumDelay >= 0)
- {
- double timeHeldDown = jmin (1.0, getMillisecondsSinceButtonDown() / 4000.0);
- timeHeldDown *= timeHeldDown;
- repeatSpeed = repeatSpeed + (int) (timeHeldDown * (autoRepeatMinimumDelay - repeatSpeed));
- }
- repeatSpeed = jmax (1, repeatSpeed);
- const uint32 now = Time::getMillisecondCounter();
- // if we've been blocked from repeating often enough, speed up the repeat timer to compensate..
- if (lastRepeatTime != 0 && (int) (now - lastRepeatTime) > repeatSpeed * 2)
- repeatSpeed = jmax (1, repeatSpeed / 2);
- lastRepeatTime = now;
- callbackHelper->startTimer (repeatSpeed);
- internalClickCallback (ModifierKeys::getCurrentModifiers());
- }
- else if (! needsToRelease)
- {
- callbackHelper->stopTimer();
- }
- }
|