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.

314 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 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. const ApplicationCommandInfo* const ci = getCommandForID (commandID);
  104. return ci != nullptr ? ci->shortName : String::empty;
  105. }
  106. String ApplicationCommandManager::getDescriptionOfCommand (const CommandID commandID) const noexcept
  107. {
  108. const ApplicationCommandInfo* const ci = getCommandForID (commandID);
  109. return ci != nullptr ? (ci->description.isNotEmpty() ? ci->description : ci->shortName)
  110. : String::empty;
  111. }
  112. StringArray ApplicationCommandManager::getCommandCategories() const
  113. {
  114. StringArray s;
  115. for (int i = 0; i < commands.size(); ++i)
  116. s.addIfNotAlreadyThere (commands.getUnchecked(i)->categoryName, false);
  117. return s;
  118. }
  119. Array<CommandID> ApplicationCommandManager::getCommandsInCategory (const String& categoryName) const
  120. {
  121. Array <CommandID> results;
  122. for (int i = 0; i < commands.size(); ++i)
  123. if (commands.getUnchecked(i)->categoryName == categoryName)
  124. results.add (commands.getUnchecked(i)->commandID);
  125. return results;
  126. }
  127. //==============================================================================
  128. bool ApplicationCommandManager::invokeDirectly (const CommandID commandID, const bool asynchronously)
  129. {
  130. ApplicationCommandTarget::InvocationInfo info (commandID);
  131. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::direct;
  132. return invoke (info, asynchronously);
  133. }
  134. bool ApplicationCommandManager::invoke (const ApplicationCommandTarget::InvocationInfo& info_, const bool asynchronously)
  135. {
  136. // This call isn't thread-safe for use from a non-UI thread without locking the message
  137. // manager first..
  138. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  139. ApplicationCommandInfo commandInfo (0);
  140. ApplicationCommandTarget* const target = getTargetForCommand (info_.commandID, commandInfo);
  141. if (target == nullptr)
  142. return false;
  143. ApplicationCommandTarget::InvocationInfo info (info_);
  144. info.commandFlags = commandInfo.flags;
  145. sendListenerInvokeCallback (info);
  146. const bool ok = target->invoke (info, asynchronously);
  147. commandStatusChanged();
  148. return ok;
  149. }
  150. //==============================================================================
  151. ApplicationCommandTarget* ApplicationCommandManager::getFirstCommandTarget (const CommandID)
  152. {
  153. return firstTarget != nullptr ? firstTarget
  154. : findDefaultComponentTarget();
  155. }
  156. void ApplicationCommandManager::setFirstCommandTarget (ApplicationCommandTarget* const newTarget) noexcept
  157. {
  158. firstTarget = newTarget;
  159. }
  160. ApplicationCommandTarget* ApplicationCommandManager::getTargetForCommand (const CommandID commandID,
  161. ApplicationCommandInfo& upToDateInfo)
  162. {
  163. ApplicationCommandTarget* target = getFirstCommandTarget (commandID);
  164. if (target == nullptr)
  165. target = JUCEApplication::getInstance();
  166. if (target != nullptr)
  167. target = target->getTargetForCommand (commandID);
  168. if (target != nullptr)
  169. target->getCommandInfo (commandID, upToDateInfo);
  170. return target;
  171. }
  172. //==============================================================================
  173. ApplicationCommandTarget* ApplicationCommandManager::findTargetForComponent (Component* c)
  174. {
  175. ApplicationCommandTarget* target = dynamic_cast <ApplicationCommandTarget*> (c);
  176. if (target == nullptr && c != nullptr)
  177. // (unable to use the syntax findParentComponentOfClass <ApplicationCommandTarget> () because of a VC6 compiler bug)
  178. target = c->findParentComponentOfClass ((ApplicationCommandTarget*) nullptr);
  179. return target;
  180. }
  181. ApplicationCommandTarget* ApplicationCommandManager::findDefaultComponentTarget()
  182. {
  183. Component* c = Component::getCurrentlyFocusedComponent();
  184. if (c == nullptr)
  185. {
  186. TopLevelWindow* const activeWindow = TopLevelWindow::getActiveTopLevelWindow();
  187. if (activeWindow != nullptr)
  188. {
  189. c = activeWindow->getPeer()->getLastFocusedSubcomponent();
  190. if (c == nullptr)
  191. c = activeWindow;
  192. }
  193. }
  194. if (c == nullptr && Process::isForegroundProcess())
  195. {
  196. // getting a bit desperate now - try all desktop comps..
  197. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  198. {
  199. ApplicationCommandTarget* const target
  200. = findTargetForComponent (Desktop::getInstance().getComponent (i)
  201. ->getPeer()->getLastFocusedSubcomponent());
  202. if (target != nullptr)
  203. return target;
  204. }
  205. }
  206. if (c != nullptr)
  207. {
  208. ResizableWindow* const resizableWindow = dynamic_cast <ResizableWindow*> (c);
  209. // if we're focused on a ResizableWindow, chances are that it's the content
  210. // component that really should get the event. And if not, the event will
  211. // still be passed up to the top level window anyway, so let's send it to the
  212. // content comp.
  213. if (resizableWindow != nullptr && resizableWindow->getContentComponent() != nullptr)
  214. c = resizableWindow->getContentComponent();
  215. ApplicationCommandTarget* const target = findTargetForComponent (c);
  216. if (target != nullptr)
  217. return target;
  218. }
  219. return JUCEApplication::getInstance();
  220. }
  221. //==============================================================================
  222. void ApplicationCommandManager::addListener (ApplicationCommandManagerListener* const listener)
  223. {
  224. listeners.add (listener);
  225. }
  226. void ApplicationCommandManager::removeListener (ApplicationCommandManagerListener* const listener)
  227. {
  228. listeners.remove (listener);
  229. }
  230. void ApplicationCommandManager::sendListenerInvokeCallback (const ApplicationCommandTarget::InvocationInfo& info)
  231. {
  232. listeners.call (&ApplicationCommandManagerListener::applicationCommandInvoked, info);
  233. }
  234. void ApplicationCommandManager::handleAsyncUpdate()
  235. {
  236. listeners.call (&ApplicationCommandManagerListener::applicationCommandListChanged);
  237. }
  238. void ApplicationCommandManager::globalFocusChanged (Component*)
  239. {
  240. commandStatusChanged();
  241. }