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.

315 lines
11KB

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