juce_MenuBarModel.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. MenuBarModel::MenuBarModel() noexcept
  18. : manager (nullptr)
  19. {
  20. }
  21. MenuBarModel::~MenuBarModel()
  22. {
  23. setApplicationCommandManagerToWatch (nullptr);
  24. }
  25. //==============================================================================
  26. void MenuBarModel::menuItemsChanged()
  27. {
  28. triggerAsyncUpdate();
  29. }
  30. void MenuBarModel::setApplicationCommandManagerToWatch (ApplicationCommandManager* const newManager) noexcept
  31. {
  32. if (manager != newManager)
  33. {
  34. if (manager != nullptr)
  35. manager->removeListener (this);
  36. manager = newManager;
  37. if (manager != nullptr)
  38. manager->addListener (this);
  39. }
  40. }
  41. void MenuBarModel::addListener (Listener* const newListener) noexcept
  42. {
  43. listeners.add (newListener);
  44. }
  45. void MenuBarModel::removeListener (Listener* const listenerToRemove) noexcept
  46. {
  47. // Trying to remove a listener that isn't on the list!
  48. // If this assertion happens because this object is a dangling pointer, make sure you've not
  49. // deleted this menu model while it's still being used by something (e.g. by a MenuBarComponent)
  50. jassert (listeners.contains (listenerToRemove));
  51. listeners.remove (listenerToRemove);
  52. }
  53. //==============================================================================
  54. void MenuBarModel::handleAsyncUpdate()
  55. {
  56. listeners.call (&MenuBarModel::Listener::menuBarItemsChanged, this);
  57. }
  58. void MenuBarModel::applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo& info)
  59. {
  60. listeners.call (&MenuBarModel::Listener::menuCommandInvoked, this, info);
  61. }
  62. void MenuBarModel::applicationCommandListChanged()
  63. {
  64. menuItemsChanged();
  65. }