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.

94 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. class ActionBroadcaster::ActionMessage : public MessageManager::MessageBase
  19. {
  20. public:
  21. ActionMessage (const ActionBroadcaster* const broadcaster_,
  22. const String& messageText,
  23. ActionListener* const listener_) noexcept
  24. : broadcaster (const_cast <ActionBroadcaster*> (broadcaster_)),
  25. message (messageText),
  26. listener (listener_)
  27. {}
  28. void messageCallback()
  29. {
  30. if (const ActionBroadcaster* const b = broadcaster)
  31. if (b->actionListeners.contains (listener))
  32. listener->actionListenerCallback (message);
  33. }
  34. private:
  35. WeakReference<ActionBroadcaster> broadcaster;
  36. const String message;
  37. ActionListener* const listener;
  38. JUCE_DECLARE_NON_COPYABLE (ActionMessage)
  39. };
  40. //==============================================================================
  41. ActionBroadcaster::ActionBroadcaster()
  42. {
  43. // are you trying to create this object before or after juce has been intialised??
  44. jassert (MessageManager::getInstanceWithoutCreating() != nullptr);
  45. }
  46. ActionBroadcaster::~ActionBroadcaster()
  47. {
  48. // all event-based objects must be deleted BEFORE juce is shut down!
  49. jassert (MessageManager::getInstanceWithoutCreating() != nullptr);
  50. masterReference.clear();
  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. (new ActionMessage (this, message, actionListeners.getUnchecked(i)))->post();
  73. }