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.

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