The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. class ActionMessage : public Message
  21. {
  22. public:
  23. ActionMessage (const String& messageText, ActionListener* const listener_) noexcept
  24. : message (messageText),
  25. listener (listener_)
  26. {
  27. }
  28. const String message;
  29. ActionListener* const listener;
  30. private:
  31. JUCE_DECLARE_NON_COPYABLE (ActionMessage);
  32. };
  33. ActionBroadcaster::CallbackReceiver::CallbackReceiver() {}
  34. void ActionBroadcaster::CallbackReceiver::handleMessage (const Message& message)
  35. {
  36. const ActionMessage& am = static_cast <const ActionMessage&> (message);
  37. if (owner->actionListeners.contains (am.listener))
  38. am.listener->actionListenerCallback (am.message);
  39. }
  40. //==============================================================================
  41. ActionBroadcaster::ActionBroadcaster()
  42. {
  43. // are you trying to create this object before or after juce has been intialised??
  44. jassert (MessageManager::instance != nullptr);
  45. callback.owner = this;
  46. }
  47. ActionBroadcaster::~ActionBroadcaster()
  48. {
  49. // all event-based objects must be deleted BEFORE juce is shut down!
  50. jassert (MessageManager::instance != nullptr);
  51. }
  52. void ActionBroadcaster::addActionListener (ActionListener* const listener)
  53. {
  54. const ScopedLock sl (actionListenerLock);
  55. if (listener != nullptr)
  56. actionListeners.add (listener);
  57. }
  58. void ActionBroadcaster::removeActionListener (ActionListener* const listener)
  59. {
  60. const ScopedLock sl (actionListenerLock);
  61. actionListeners.removeValue (listener);
  62. }
  63. void ActionBroadcaster::removeAllActionListeners()
  64. {
  65. const ScopedLock sl (actionListenerLock);
  66. actionListeners.clear();
  67. }
  68. void ActionBroadcaster::sendActionMessage (const String& message) const
  69. {
  70. const ScopedLock sl (actionListenerLock);
  71. for (int i = actionListeners.size(); --i >= 0;)
  72. callback.postMessage (new ActionMessage (message, actionListeners.getUnchecked(i)));
  73. }
  74. END_JUCE_NAMESPACE