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.

299 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. 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& inf, 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. bool ok = false;
  140. ApplicationCommandInfo commandInfo (0);
  141. if (ApplicationCommandTarget* const target = getTargetForCommand (inf.commandID, commandInfo))
  142. {
  143. ApplicationCommandTarget::InvocationInfo info (inf);
  144. info.commandFlags = commandInfo.flags;
  145. sendListenerInvokeCallback (info);
  146. ok = target->invoke (info, asynchronously);
  147. commandStatusChanged();
  148. }
  149. return ok;
  150. }
  151. //==============================================================================
  152. ApplicationCommandTarget* ApplicationCommandManager::getFirstCommandTarget (const CommandID)
  153. {
  154. return firstTarget != nullptr ? firstTarget
  155. : findDefaultComponentTarget();
  156. }
  157. void ApplicationCommandManager::setFirstCommandTarget (ApplicationCommandTarget* const newTarget) noexcept
  158. {
  159. firstTarget = newTarget;
  160. }
  161. ApplicationCommandTarget* ApplicationCommandManager::getTargetForCommand (const CommandID commandID,
  162. ApplicationCommandInfo& upToDateInfo)
  163. {
  164. ApplicationCommandTarget* target = getFirstCommandTarget (commandID);
  165. if (target == nullptr)
  166. target = JUCEApplication::getInstance();
  167. if (target != nullptr)
  168. target = target->getTargetForCommand (commandID);
  169. if (target != nullptr)
  170. target->getCommandInfo (commandID, upToDateInfo);
  171. return target;
  172. }
  173. //==============================================================================
  174. ApplicationCommandTarget* ApplicationCommandManager::findTargetForComponent (Component* c)
  175. {
  176. ApplicationCommandTarget* target = dynamic_cast <ApplicationCommandTarget*> (c);
  177. if (target == nullptr && c != nullptr)
  178. target = c->findParentComponentOfClass<ApplicationCommandTarget>();
  179. return target;
  180. }
  181. ApplicationCommandTarget* ApplicationCommandManager::findDefaultComponentTarget()
  182. {
  183. Component* c = Component::getCurrentlyFocusedComponent();
  184. if (c == nullptr)
  185. {
  186. if (TopLevelWindow* const activeWindow = TopLevelWindow::getActiveTopLevelWindow())
  187. {
  188. c = activeWindow->getPeer()->getLastFocusedSubcomponent();
  189. if (c == nullptr)
  190. c = activeWindow;
  191. }
  192. }
  193. if (c == nullptr && Process::isForegroundProcess())
  194. {
  195. // getting a bit desperate now: try all desktop comps..
  196. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  197. if (ApplicationCommandTarget* const target = findTargetForComponent (Desktop::getInstance().getComponent (i)
  198. ->getPeer()->getLastFocusedSubcomponent()))
  199. return target;
  200. }
  201. if (c != nullptr)
  202. {
  203. // if we're focused on a ResizableWindow, chances are that it's the content
  204. // component that really should get the event. And if not, the event will
  205. // still be passed up to the top level window anyway, so let's send it to the
  206. // content comp.
  207. if (ResizableWindow* const resizableWindow = dynamic_cast <ResizableWindow*> (c))
  208. if (resizableWindow->getContentComponent() != nullptr)
  209. c = resizableWindow->getContentComponent();
  210. if (ApplicationCommandTarget* const target = findTargetForComponent (c))
  211. return target;
  212. }
  213. return JUCEApplication::getInstance();
  214. }
  215. //==============================================================================
  216. void ApplicationCommandManager::addListener (ApplicationCommandManagerListener* const listener)
  217. {
  218. listeners.add (listener);
  219. }
  220. void ApplicationCommandManager::removeListener (ApplicationCommandManagerListener* const listener)
  221. {
  222. listeners.remove (listener);
  223. }
  224. void ApplicationCommandManager::sendListenerInvokeCallback (const ApplicationCommandTarget::InvocationInfo& info)
  225. {
  226. listeners.call (&ApplicationCommandManagerListener::applicationCommandInvoked, info);
  227. }
  228. void ApplicationCommandManager::handleAsyncUpdate()
  229. {
  230. listeners.call (&ApplicationCommandManagerListener::applicationCommandListChanged);
  231. }
  232. void ApplicationCommandManager::globalFocusChanged (Component*)
  233. {
  234. commandStatusChanged();
  235. }