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.

313 lines
11KB

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