From 010ba396e97147a2e62e943ab966e5fc86fd3c81 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 11 Nov 2013 11:01:16 +0000 Subject: [PATCH] Removed some base classes from the Button class. --- .../juce_gui_basics/buttons/juce_Button.cpp | 115 ++++++++++-------- modules/juce_gui_basics/buttons/juce_Button.h | 26 ++-- 2 files changed, 69 insertions(+), 72 deletions(-) diff --git a/modules/juce_gui_basics/buttons/juce_Button.cpp b/modules/juce_gui_basics/buttons/juce_Button.cpp index 9bc863edd6..613f33cfb0 100644 --- a/modules/juce_gui_basics/buttons/juce_Button.cpp +++ b/modules/juce_gui_basics/buttons/juce_Button.cpp @@ -22,16 +22,52 @@ ============================================================================== */ -class Button::RepeatTimer : public Timer +class Button::CallbackHelper : public Timer, + public ApplicationCommandManagerListener, + public ValueListener, + public KeyListener { public: - RepeatTimer (Button& b) : owner (b) {} - void timerCallback() override { owner.repeatTimerCallback(); } + CallbackHelper (Button& b) : button (b) {} + + void timerCallback() override + { + button.repeatTimerCallback(); + } + + bool keyStateChanged (const 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& owner; + Button& button; - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RepeatTimer) + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CallbackHelper) }; //============================================================================== @@ -56,19 +92,21 @@ Button::Button (const String& name) triggerOnMouseDown (false), generateTooltip (false) { + callbackHelper = new CallbackHelper (*this); + setWantsKeyboardFocus (true); - isOn.addListener (this); + isOn.addListener (callbackHelper); } Button::~Button() { - isOn.removeListener (this); + clearShortcuts(); if (commandManagerToUse != nullptr) - commandManagerToUse->removeListener (this); + commandManagerToUse->removeListener (callbackHelper); - repeatTimer = nullptr; - clearShortcuts(); + isOn.removeListener (callbackHelper); + callbackHelper = nullptr; } //============================================================================== @@ -180,12 +218,6 @@ bool Button::getClickingTogglesState() const noexcept return clickTogglesState; } -void Button::valueChanged (Value& value) -{ - if (value.refersToSameSourceAs (isOn)) - setToggleState (isOn.getValue(), sendNotification); -} - void Button::setRadioGroupId (const int newGroupId, NotificationType notification) { if (radioGroupId != newGroupId) @@ -327,7 +359,7 @@ void Button::flashButtonState() { needsToRelease = true; setState (buttonDown); - getRepeatTimer().startTimer (100); + callbackHelper->startTimer (100); } } @@ -410,7 +442,7 @@ void Button::mouseDown (const MouseEvent& e) if (isDown()) { if (autoRepeatDelay >= 0) - getRepeatTimer().startTimer (autoRepeatDelay); + callbackHelper->startTimer (autoRepeatDelay); if (triggerOnMouseDown) internalClickCallback (e.mods); @@ -433,7 +465,7 @@ void Button::mouseDrag (const MouseEvent&) updateState (isMouseOver(), true); if (autoRepeatDelay >= 0 && buttonState != oldState && isDown()) - getRepeatTimer().startTimer (autoRepeatSpeed); + callbackHelper->startTimer (autoRepeatSpeed); } void Button::focusGained (FocusChangeType) @@ -461,12 +493,12 @@ void Button::parentHierarchyChanged() if (newKeySource != keySource.get()) { if (keySource != nullptr) - keySource->removeKeyListener (this); + keySource->removeKeyListener (callbackHelper); keySource = newKeySource; if (keySource != nullptr) - keySource->addKeyListener (this); + keySource->addKeyListener (callbackHelper); } } @@ -480,12 +512,12 @@ void Button::setCommandToTrigger (ApplicationCommandManager* const newCommandMan if (commandManagerToUse != newCommandManager) { if (commandManagerToUse != nullptr) - commandManagerToUse->removeListener (this); + commandManagerToUse->removeListener (callbackHelper); commandManagerToUse = newCommandManager; if (commandManagerToUse != nullptr) - commandManagerToUse->addListener (this); + 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 @@ -495,21 +527,12 @@ void Button::setCommandToTrigger (ApplicationCommandManager* const newCommandMan } if (commandManagerToUse != nullptr) - applicationCommandListChanged(); + applicationCommandListChangeCallback(); else setEnabled (true); } -void Button::applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo& info) -{ - if (info.commandID == commandID - && (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) == 0) - { - flashButtonState(); - } -} - -void Button::applicationCommandListChanged() +void Button::applicationCommandListChangeCallback() { if (commandManagerToUse != nullptr) { @@ -564,7 +587,7 @@ bool Button::isRegisteredForShortcut (const KeyPress& key) const return false; } -bool Button::keyStateChanged (const bool, Component*) +bool Button::keyStateChangedCallback() { if (! isEnabled()) return false; @@ -573,7 +596,7 @@ bool Button::keyStateChanged (const bool, Component*) isKeyDown = isShortcutPressed(); if (autoRepeatDelay >= 0 && (isKeyDown && ! wasDown)) - getRepeatTimer().startTimer (autoRepeatDelay); + callbackHelper->startTimer (autoRepeatDelay); updateState(); @@ -588,12 +611,6 @@ bool Button::keyStateChanged (const bool, Component*) return wasDown || isKeyDown; } -bool Button::keyPressed (const KeyPress&, Component*) -{ - // returning true will avoid forwarding events for keys that we're using as shortcuts - return isShortcutPressed(); -} - bool Button::keyPressed (const KeyPress& key) { if (isEnabled() && key.isKeyCode (KeyPress::returnKey)) @@ -619,7 +636,7 @@ void Button::repeatTimerCallback() { if (needsRepainting) { - getRepeatTimer().stopTimer(); + callbackHelper->stopTimer(); updateState(); needsRepainting = false; } @@ -644,20 +661,12 @@ void Button::repeatTimerCallback() repeatSpeed = jmax (1, repeatSpeed / 2); lastRepeatTime = now; - getRepeatTimer().startTimer (repeatSpeed); + callbackHelper->startTimer (repeatSpeed); internalClickCallback (ModifierKeys::getCurrentModifiers()); } else if (! needsToRelease) { - getRepeatTimer().stopTimer(); + callbackHelper->stopTimer(); } } - -Button::RepeatTimer& Button::getRepeatTimer() -{ - if (repeatTimer == nullptr) - repeatTimer = new RepeatTimer (*this); - - return *repeatTimer; -} diff --git a/modules/juce_gui_basics/buttons/juce_Button.h b/modules/juce_gui_basics/buttons/juce_Button.h index 0516915b33..26613548b9 100644 --- a/modules/juce_gui_basics/buttons/juce_Button.h +++ b/modules/juce_gui_basics/buttons/juce_Button.h @@ -37,10 +37,7 @@ @see TextButton, DrawableButton, ToggleButton */ class JUCE_API Button : public Component, - public SettableTooltipClient, - public ApplicationCommandManagerListener, - public ValueListener, - private KeyListener + public SettableTooltipClient { protected: //============================================================================== @@ -446,10 +443,6 @@ protected: /** @internal */ bool keyPressed (const KeyPress&) override; /** @internal */ - bool keyPressed (const KeyPress&, Component*) override; - /** @internal */ - bool keyStateChanged (bool isKeyDown, Component*) override; - /** @internal */ using Component::keyStateChanged; /** @internal */ void paint (Graphics&) override; @@ -463,12 +456,6 @@ protected: void focusLost (FocusChangeType) override; /** @internal */ void enablementChanged() override; - /** @internal */ - void applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo&) override; - /** @internal */ - void applicationCommandListChanged() override; - /** @internal */ - void valueChanged (Value&) override; private: //============================================================================== @@ -477,10 +464,10 @@ private: String text; ListenerList buttonListeners; - class RepeatTimer; - friend class RepeatTimer; - friend struct ContainerDeletePolicy; - ScopedPointer repeatTimer; + class CallbackHelper; + friend class CallbackHelper; + friend struct ContainerDeletePolicy; + ScopedPointer callbackHelper; uint32 buttonPressTime, lastRepeatTime; ApplicationCommandManager* commandManagerToUse; int autoRepeatDelay, autoRepeatSpeed, autoRepeatMinimumDelay; @@ -498,7 +485,8 @@ private: bool generateTooltip; void repeatTimerCallback(); - RepeatTimer& getRepeatTimer(); + bool keyStateChangedCallback(); + void applicationCommandListChangeCallback(); ButtonState updateState(); ButtonState updateState (bool isOver, bool isDown);