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.

juce_ApplicationCommandManager.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. ApplicationCommandManager::ApplicationCommandManager()
  18. : firstTarget (nullptr)
  19. {
  20. keyMappings = new KeyPressMappingSet (*this);
  21. Desktop::getInstance().addFocusChangeListener (this);
  22. }
  23. ApplicationCommandManager::~ApplicationCommandManager()
  24. {
  25. Desktop::getInstance().removeFocusChangeListener (this);
  26. keyMappings = nullptr;
  27. }
  28. //==============================================================================
  29. void ApplicationCommandManager::clearCommands()
  30. {
  31. commands.clear();
  32. keyMappings->clearAllKeyPresses();
  33. triggerAsyncUpdate();
  34. }
  35. void ApplicationCommandManager::registerCommand (const ApplicationCommandInfo& newCommand)
  36. {
  37. // zero isn't a valid command ID!
  38. jassert (newCommand.commandID != 0);
  39. // the name isn't optional!
  40. jassert (newCommand.shortName.isNotEmpty());
  41. if (getCommandForID (newCommand.commandID) == 0)
  42. {
  43. ApplicationCommandInfo* const newInfo = new ApplicationCommandInfo (newCommand);
  44. newInfo->flags &= ~ApplicationCommandInfo::isTicked;
  45. commands.add (newInfo);
  46. keyMappings->resetToDefaultMapping (newCommand.commandID);
  47. triggerAsyncUpdate();
  48. }
  49. else
  50. {
  51. // trying to re-register the same command ID with different parameters?
  52. jassert (newCommand.shortName == getCommandForID (newCommand.commandID)->shortName
  53. && (newCommand.description == getCommandForID (newCommand.commandID)->description || newCommand.description.isEmpty())
  54. && newCommand.categoryName == getCommandForID (newCommand.commandID)->categoryName
  55. && newCommand.defaultKeypresses == getCommandForID (newCommand.commandID)->defaultKeypresses
  56. && (newCommand.flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor))
  57. == (getCommandForID (newCommand.commandID)->flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor)));
  58. }
  59. }
  60. void ApplicationCommandManager::registerAllCommandsForTarget (ApplicationCommandTarget* target)
  61. {
  62. if (target != nullptr)
  63. {
  64. Array<CommandID> commandIDs;
  65. target->getAllCommands (commandIDs);
  66. for (int i = 0; i < commandIDs.size(); ++i)
  67. {
  68. ApplicationCommandInfo info (commandIDs.getUnchecked(i));
  69. target->getCommandInfo (info.commandID, info);
  70. registerCommand (info);
  71. }
  72. }
  73. }
  74. void ApplicationCommandManager::removeCommand (const CommandID commandID)
  75. {
  76. for (int i = commands.size(); --i >= 0;)
  77. {
  78. if (commands.getUnchecked (i)->commandID == commandID)
  79. {
  80. commands.remove (i);
  81. triggerAsyncUpdate();
  82. const Array<KeyPress> keys (keyMappings->getKeyPressesAssignedToCommand (commandID));
  83. for (int j = keys.size(); --j >= 0;)
  84. keyMappings->removeKeyPress (keys.getReference (j));
  85. }
  86. }
  87. }
  88. void ApplicationCommandManager::commandStatusChanged()
  89. {
  90. triggerAsyncUpdate();
  91. }
  92. //==============================================================================
  93. const ApplicationCommandInfo* ApplicationCommandManager::getCommandForID (const CommandID commandID) const noexcept
  94. {
  95. for (int i = commands.size(); --i >= 0;)
  96. if (commands.getUnchecked(i)->commandID == commandID)
  97. return commands.getUnchecked(i);
  98. return nullptr;
  99. }
  100. String ApplicationCommandManager::getNameOfCommand (const CommandID commandID) const noexcept
  101. {
  102. if (const ApplicationCommandInfo* const ci = getCommandForID (commandID))
  103. return ci->shortName;
  104. return String();
  105. }
  106. String ApplicationCommandManager::getDescriptionOfCommand (const CommandID commandID) const noexcept
  107. {
  108. if (const ApplicationCommandInfo* const ci = getCommandForID (commandID))
  109. return ci->description.isNotEmpty() ? ci->description
  110. : ci->shortName;
  111. return String();
  112. }
  113. StringArray ApplicationCommandManager::getCommandCategories() const
  114. {
  115. StringArray s;
  116. for (int i = 0; i < commands.size(); ++i)
  117. s.addIfNotAlreadyThere (commands.getUnchecked(i)->categoryName, false);
  118. return s;
  119. }
  120. Array<CommandID> ApplicationCommandManager::getCommandsInCategory (const String& categoryName) const
  121. {
  122. Array<CommandID> results;
  123. for (int i = 0; i < commands.size(); ++i)
  124. if (commands.getUnchecked(i)->categoryName == categoryName)
  125. results.add (commands.getUnchecked(i)->commandID);
  126. return results;
  127. }
  128. //==============================================================================
  129. bool ApplicationCommandManager::invokeDirectly (const CommandID commandID, const bool asynchronously)
  130. {
  131. ApplicationCommandTarget::InvocationInfo info (commandID);
  132. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::direct;
  133. return invoke (info, asynchronously);
  134. }
  135. bool ApplicationCommandManager::invoke (const ApplicationCommandTarget::InvocationInfo& inf, const bool asynchronously)
  136. {
  137. // This call isn't thread-safe for use from a non-UI thread without locking the message
  138. // manager first..
  139. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  140. bool ok = false;
  141. ApplicationCommandInfo commandInfo (0);
  142. if (ApplicationCommandTarget* const target = getTargetForCommand (inf.commandID, commandInfo))
  143. {
  144. ApplicationCommandTarget::InvocationInfo info (inf);
  145. info.commandFlags = commandInfo.flags;
  146. sendListenerInvokeCallback (info);
  147. ok = target->invoke (info, asynchronously);
  148. commandStatusChanged();
  149. }
  150. return ok;
  151. }
  152. //==============================================================================
  153. ApplicationCommandTarget* ApplicationCommandManager::getFirstCommandTarget (const CommandID)
  154. {
  155. return firstTarget != nullptr ? firstTarget
  156. : findDefaultComponentTarget();
  157. }
  158. void ApplicationCommandManager::setFirstCommandTarget (ApplicationCommandTarget* const newTarget) noexcept
  159. {
  160. firstTarget = newTarget;
  161. }
  162. ApplicationCommandTarget* ApplicationCommandManager::getTargetForCommand (const CommandID commandID,
  163. ApplicationCommandInfo& upToDateInfo)
  164. {
  165. ApplicationCommandTarget* target = getFirstCommandTarget (commandID);
  166. if (target == nullptr)
  167. target = JUCEApplication::getInstance();
  168. if (target != nullptr)
  169. target = target->getTargetForCommand (commandID);
  170. if (target != nullptr)
  171. target->getCommandInfo (commandID, upToDateInfo);
  172. return target;
  173. }
  174. //==============================================================================
  175. ApplicationCommandTarget* ApplicationCommandManager::findTargetForComponent (Component* c)
  176. {
  177. ApplicationCommandTarget* target = dynamic_cast <ApplicationCommandTarget*> (c);
  178. if (target == nullptr && c != nullptr)
  179. target = c->findParentComponentOfClass<ApplicationCommandTarget>();
  180. return target;
  181. }
  182. ApplicationCommandTarget* ApplicationCommandManager::findDefaultComponentTarget()
  183. {
  184. Component* c = Component::getCurrentlyFocusedComponent();
  185. if (c == nullptr)
  186. {
  187. if (TopLevelWindow* const activeWindow = TopLevelWindow::getActiveTopLevelWindow())
  188. {
  189. c = activeWindow->getPeer()->getLastFocusedSubcomponent();
  190. if (c == nullptr)
  191. c = activeWindow;
  192. }
  193. }
  194. if (c == nullptr && Process::isForegroundProcess())
  195. {
  196. Desktop& desktop = Desktop::getInstance();
  197. // getting a bit desperate now: try all desktop comps..
  198. for (int i = desktop.getNumComponents(); --i >= 0;)
  199. if (ComponentPeer* const peer = desktop.getComponent(i)->getPeer())
  200. if (ApplicationCommandTarget* const target = findTargetForComponent (peer->getLastFocusedSubcomponent()))
  201. return target;
  202. }
  203. if (c != nullptr)
  204. {
  205. // if we're focused on a ResizableWindow, chances are that it's the content
  206. // component that really should get the event. And if not, the event will
  207. // still be passed up to the top level window anyway, so let's send it to the
  208. // content comp.
  209. if (ResizableWindow* const resizableWindow = dynamic_cast <ResizableWindow*> (c))
  210. if (Component* const content = resizableWindow->getContentComponent())
  211. c = content;
  212. if (ApplicationCommandTarget* const target = findTargetForComponent (c))
  213. return target;
  214. }
  215. return JUCEApplication::getInstance();
  216. }
  217. //==============================================================================
  218. void ApplicationCommandManager::addListener (ApplicationCommandManagerListener* const listener)
  219. {
  220. listeners.add (listener);
  221. }
  222. void ApplicationCommandManager::removeListener (ApplicationCommandManagerListener* const listener)
  223. {
  224. listeners.remove (listener);
  225. }
  226. void ApplicationCommandManager::sendListenerInvokeCallback (const ApplicationCommandTarget::InvocationInfo& info)
  227. {
  228. listeners.call (&ApplicationCommandManagerListener::applicationCommandInvoked, info);
  229. }
  230. void ApplicationCommandManager::handleAsyncUpdate()
  231. {
  232. listeners.call (&ApplicationCommandManagerListener::applicationCommandListChanged);
  233. }
  234. void ApplicationCommandManager::globalFocusChanged (Component*)
  235. {
  236. commandStatusChanged();
  237. }