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.

192 lines
5.7KB

  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. class ApplicationCommandTarget::CommandMessage : public MessageManager::MessageBase
  19. {
  20. public:
  21. CommandMessage (ApplicationCommandTarget* const owner_, const InvocationInfo& info_)
  22. : owner (owner_), info (info_)
  23. {
  24. }
  25. void messageCallback()
  26. {
  27. ApplicationCommandTarget* const target = owner;
  28. if (target != nullptr)
  29. target->tryToInvoke (info, false);
  30. }
  31. private:
  32. WeakReference<ApplicationCommandTarget> owner;
  33. const InvocationInfo info;
  34. JUCE_DECLARE_NON_COPYABLE (CommandMessage);
  35. };
  36. //==============================================================================
  37. ApplicationCommandTarget::ApplicationCommandTarget()
  38. {
  39. }
  40. ApplicationCommandTarget::~ApplicationCommandTarget()
  41. {
  42. masterReference.clear();
  43. }
  44. //==============================================================================
  45. bool ApplicationCommandTarget::tryToInvoke (const InvocationInfo& info, const bool async)
  46. {
  47. if (isCommandActive (info.commandID))
  48. {
  49. if (async)
  50. {
  51. (new CommandMessage (this, info))->post();
  52. return true;
  53. }
  54. else
  55. {
  56. const bool success = perform (info);
  57. jassert (success); // hmm - your target should have been able to perform this command. If it can't
  58. // do it at the moment for some reason, it should clear the 'isActive' flag when it
  59. // returns the command's info.
  60. return success;
  61. }
  62. }
  63. return false;
  64. }
  65. ApplicationCommandTarget* ApplicationCommandTarget::findFirstTargetParentComponent()
  66. {
  67. Component* c = dynamic_cast <Component*> (this);
  68. if (c != nullptr)
  69. return c->findParentComponentOfClass<ApplicationCommandTarget>();
  70. return nullptr;
  71. }
  72. ApplicationCommandTarget* ApplicationCommandTarget::getTargetForCommand (const CommandID commandID)
  73. {
  74. ApplicationCommandTarget* target = this;
  75. int depth = 0;
  76. while (target != nullptr)
  77. {
  78. Array <CommandID> commandIDs;
  79. target->getAllCommands (commandIDs);
  80. if (commandIDs.contains (commandID))
  81. return target;
  82. target = target->getNextCommandTarget();
  83. ++depth;
  84. jassert (depth < 100); // could be a recursive command chain??
  85. jassert (target != this); // definitely a recursive command chain!
  86. if (depth > 100 || target == this)
  87. break;
  88. }
  89. if (target == nullptr)
  90. {
  91. target = JUCEApplication::getInstance();
  92. if (target != nullptr)
  93. {
  94. Array <CommandID> commandIDs;
  95. target->getAllCommands (commandIDs);
  96. if (commandIDs.contains (commandID))
  97. return target;
  98. }
  99. }
  100. return nullptr;
  101. }
  102. bool ApplicationCommandTarget::isCommandActive (const CommandID commandID)
  103. {
  104. ApplicationCommandInfo info (commandID);
  105. info.flags = ApplicationCommandInfo::isDisabled;
  106. getCommandInfo (commandID, info);
  107. return (info.flags & ApplicationCommandInfo::isDisabled) == 0;
  108. }
  109. //==============================================================================
  110. bool ApplicationCommandTarget::invoke (const InvocationInfo& info, const bool async)
  111. {
  112. ApplicationCommandTarget* target = this;
  113. int depth = 0;
  114. while (target != nullptr)
  115. {
  116. if (target->tryToInvoke (info, async))
  117. return true;
  118. target = target->getNextCommandTarget();
  119. ++depth;
  120. jassert (depth < 100); // could be a recursive command chain??
  121. jassert (target != this); // definitely a recursive command chain!
  122. if (depth > 100 || target == this)
  123. break;
  124. }
  125. if (target == nullptr)
  126. {
  127. target = JUCEApplication::getInstance();
  128. if (target != nullptr)
  129. return target->tryToInvoke (info, async);
  130. }
  131. return false;
  132. }
  133. bool ApplicationCommandTarget::invokeDirectly (const CommandID commandID, const bool asynchronously)
  134. {
  135. ApplicationCommandTarget::InvocationInfo info (commandID);
  136. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::direct;
  137. return invoke (info, asynchronously);
  138. }
  139. //==============================================================================
  140. ApplicationCommandTarget::InvocationInfo::InvocationInfo (const CommandID commandID_)
  141. : commandID (commandID_),
  142. commandFlags (0),
  143. invocationMethod (direct),
  144. originatingComponent (nullptr),
  145. isKeyDown (false),
  146. millisecsSinceKeyPressed (0)
  147. {
  148. }