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.

303 lines
11KB

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