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.

194 lines
7.7KB

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