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.

348 lines
12KB

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