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.

186 lines
7.4KB

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