Audio plugin host https://kx.studio/carla
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.

193 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A class for controlling MenuBar components.
  23. This class is used to tell a MenuBar what menus to show, and to respond
  24. to a menu being selected.
  25. @see MenuBarModel::Listener, MenuBarComponent, PopupMenu
  26. @tags{GUI}
  27. */
  28. class JUCE_API MenuBarModel : private AsyncUpdater,
  29. private ApplicationCommandManagerListener
  30. {
  31. public:
  32. //==============================================================================
  33. MenuBarModel() noexcept;
  34. /** Destructor. */
  35. ~MenuBarModel() override;
  36. //==============================================================================
  37. /** Call this when some of your menu items have changed.
  38. This method will cause a callback to any MenuBarListener objects that
  39. are registered with this model.
  40. If this model is displaying items from an ApplicationCommandManager, you
  41. can use the setApplicationCommandManagerToWatch() method to cause
  42. change messages to be sent automatically when the ApplicationCommandManager
  43. is changed.
  44. @see addListener, removeListener, MenuBarListener
  45. */
  46. void menuItemsChanged();
  47. /** Tells the menu bar to listen to the specified command manager, and to update
  48. itself when the commands change.
  49. This will also allow it to flash a menu name when a command from that menu
  50. is invoked using a keystroke.
  51. */
  52. void setApplicationCommandManagerToWatch (ApplicationCommandManager* manager);
  53. //==============================================================================
  54. /** A class to receive callbacks when a MenuBarModel changes.
  55. @see MenuBarModel::addListener, MenuBarModel::removeListener, MenuBarModel::menuItemsChanged
  56. */
  57. class JUCE_API Listener
  58. {
  59. public:
  60. /** Destructor. */
  61. virtual ~Listener() = default;
  62. //==============================================================================
  63. /** This callback is made when items are changed in the menu bar model. */
  64. virtual void menuBarItemsChanged (MenuBarModel* menuBarModel) = 0;
  65. /** This callback is made when an application command is invoked that
  66. is represented by one of the items in the menu bar model.
  67. */
  68. virtual void menuCommandInvoked (MenuBarModel* menuBarModel,
  69. const ApplicationCommandTarget::InvocationInfo& info) = 0;
  70. /** Called when the menu bar is first activated or when the user finished interacting
  71. with the menu bar. */
  72. virtual void menuBarActivated (MenuBarModel* menuBarModel, bool isActive);
  73. };
  74. /** Registers a listener for callbacks when the menu items in this model change.
  75. The listener object will get callbacks when this object's menuItemsChanged()
  76. method is called.
  77. @see removeListener
  78. */
  79. void addListener (Listener* listenerToAdd);
  80. /** Removes a listener.
  81. @see addListener
  82. */
  83. void removeListener (Listener* listenerToRemove);
  84. //==============================================================================
  85. /** This method must return a list of the names of the menus. */
  86. virtual StringArray getMenuBarNames() = 0;
  87. /** This should return the popup menu to display for a given top-level menu.
  88. @param topLevelMenuIndex the index of the top-level menu to show
  89. @param menuName the name of the top-level menu item to show
  90. */
  91. virtual PopupMenu getMenuForIndex (int topLevelMenuIndex,
  92. const String& menuName) = 0;
  93. /** This is called when a menu item has been clicked on.
  94. @param menuItemID the item ID of the PopupMenu item that was selected
  95. @param topLevelMenuIndex the index of the top-level menu from which the item was
  96. chosen (just in case you've used duplicate ID numbers
  97. on more than one of the popup menus)
  98. */
  99. virtual void menuItemSelected (int menuItemID,
  100. int topLevelMenuIndex) = 0;
  101. /** This is called when the user starts/stops navigating the menu bar.
  102. @param isActive true when the user starts navigating the menu bar
  103. */
  104. virtual void menuBarActivated (bool isActive);
  105. //==============================================================================
  106. #if JUCE_MAC || DOXYGEN
  107. /** OSX ONLY - Sets the model that is currently being shown as the main
  108. menu bar at the top of the screen on the Mac.
  109. You can pass nullptr to stop the current model being displayed. Be careful
  110. not to delete a model while it is being used.
  111. An optional extra menu can be specified, containing items to add to the top of
  112. the apple menu. (Confusingly, the 'apple' menu isn't the one with a picture of
  113. an apple, it's the one next to it, with your application's name at the top
  114. and the services menu etc on it). When one of these items is selected, the
  115. menu bar model will be used to invoke it, and in the menuItemSelected() callback
  116. the topLevelMenuIndex parameter will be -1. If you pass in an extraAppleMenuItems
  117. object then newMenuBarModel must be non-null.
  118. If the recentItemsMenuName parameter is non-empty, then any sub-menus with this
  119. name will be replaced by OSX's special recent-files menu.
  120. */
  121. static void setMacMainMenu (MenuBarModel* newMenuBarModel,
  122. const PopupMenu* extraAppleMenuItems = nullptr,
  123. const String& recentItemsMenuName = String());
  124. /** OSX ONLY - Returns the menu model that is currently being shown as
  125. the main menu bar.
  126. */
  127. static MenuBarModel* getMacMainMenu();
  128. /** OSX ONLY - Returns the menu that was last passed as the extraAppleMenuItems
  129. argument to setMacMainMenu(), or nullptr if none was specified.
  130. */
  131. static const PopupMenu* getMacExtraAppleItemsMenu();
  132. #endif
  133. //==============================================================================
  134. /** @internal */
  135. void applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo&) override;
  136. /** @internal */
  137. void applicationCommandListChanged() override;
  138. /** @internal */
  139. void handleAsyncUpdate() override;
  140. /** @internal */
  141. void handleMenuBarActivate (bool isActive);
  142. private:
  143. ApplicationCommandManager* manager;
  144. ListenerList<Listener> listeners;
  145. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MenuBarModel)
  146. };
  147. } // namespace juce