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.

192 lines
7.9KB

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