juce_MenuBarModel.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifndef JUCE_MENUBARMODEL_H_INCLUDED
  18. #define JUCE_MENUBARMODEL_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A class for controlling MenuBar components.
  22. This class is used to tell a MenuBar what menus to show, and to respond
  23. to a menu being selected.
  24. @see MenuBarModel::Listener, MenuBarComponent, PopupMenu
  25. */
  26. class JUCE_API MenuBarModel : private AsyncUpdater,
  27. private ApplicationCommandManagerListener
  28. {
  29. public:
  30. //==============================================================================
  31. MenuBarModel() noexcept;
  32. /** Destructor. */
  33. virtual ~MenuBarModel();
  34. //==============================================================================
  35. /** Call this when some of your menu items have changed.
  36. This method will cause a callback to any MenuBarListener objects that
  37. are registered with this model.
  38. If this model is displaying items from an ApplicationCommandManager, you
  39. can use the setApplicationCommandManagerToWatch() method to cause
  40. change messages to be sent automatically when the ApplicationCommandManager
  41. is changed.
  42. @see addListener, removeListener, MenuBarListener
  43. */
  44. void menuItemsChanged();
  45. /** Tells the menu bar to listen to the specified command manager, and to update
  46. itself when the commands change.
  47. This will also allow it to flash a menu name when a command from that menu
  48. is invoked using a keystroke.
  49. */
  50. void setApplicationCommandManagerToWatch (ApplicationCommandManager* manager) noexcept;
  51. //==============================================================================
  52. /** A class to receive callbacks when a MenuBarModel changes.
  53. @see MenuBarModel::addListener, MenuBarModel::removeListener, MenuBarModel::menuItemsChanged
  54. */
  55. class JUCE_API Listener
  56. {
  57. public:
  58. /** Destructor. */
  59. virtual ~Listener() {}
  60. //==============================================================================
  61. /** This callback is made when items are changed in the menu bar model. */
  62. virtual void menuBarItemsChanged (MenuBarModel* menuBarModel) = 0;
  63. /** This callback is made when an application command is invoked that
  64. is represented by one of the items in the menu bar model.
  65. */
  66. virtual void menuCommandInvoked (MenuBarModel* menuBarModel,
  67. const ApplicationCommandTarget::InvocationInfo& info) = 0;
  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) noexcept;
  75. /** Removes a listener.
  76. @see addListener
  77. */
  78. void removeListener (Listener* listenerToRemove) noexcept;
  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. //==============================================================================
  97. #if JUCE_MAC || DOXYGEN
  98. /** OSX ONLY - Sets the model that is currently being shown as the main
  99. menu bar at the top of the screen on the Mac.
  100. You can pass 0 to stop the current model being displayed. Be careful
  101. not to delete a model while it is being used.
  102. An optional extra menu can be specified, containing items to add to the top of
  103. the apple menu. (Confusingly, the 'apple' menu isn't the one with a picture of
  104. an apple, it's the one next to it, with your application's name at the top
  105. and the services menu etc on it). When one of these items is selected, the
  106. menu bar model will be used to invoke it, and in the menuItemSelected() callback
  107. the topLevelMenuIndex parameter will be -1. If you pass in an extraAppleMenuItems
  108. object then newMenuBarModel must be non-null.
  109. If the recentItemsMenuName parameter is non-empty, then any sub-menus with this
  110. name will be replaced by OSX's special recent-files menu.
  111. */
  112. static void setMacMainMenu (MenuBarModel* newMenuBarModel,
  113. const PopupMenu* extraAppleMenuItems = nullptr,
  114. const String& recentItemsMenuName = String::empty);
  115. /** OSX ONLY - Returns the menu model that is currently being shown as
  116. the main menu bar.
  117. */
  118. static MenuBarModel* getMacMainMenu();
  119. /** OSX ONLY - Returns the menu that was last passed as the extraAppleMenuItems
  120. argument to setMacMainMenu(), or nullptr if none was specified.
  121. */
  122. static const PopupMenu* getMacExtraAppleItemsMenu();
  123. #endif
  124. //==============================================================================
  125. /** @internal */
  126. void applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo&) override;
  127. /** @internal */
  128. void applicationCommandListChanged() override;
  129. /** @internal */
  130. void handleAsyncUpdate() override;
  131. private:
  132. ApplicationCommandManager* manager;
  133. ListenerList<Listener> listeners;
  134. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MenuBarModel)
  135. };
  136. /** This typedef is just for compatibility with old code - newer code should use the MenuBarModel::Listener class directly. */
  137. typedef MenuBarModel::Listener MenuBarModelListener;
  138. #endif // JUCE_MENUBARMODEL_H_INCLUDED