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.

188 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class ApplicationCommandTarget::CommandMessage : public MessageManager::MessageBase
  18. {
  19. public:
  20. CommandMessage (ApplicationCommandTarget* const target, const InvocationInfo& inf)
  21. : owner (target), info (inf)
  22. {
  23. }
  24. void messageCallback()
  25. {
  26. if (ApplicationCommandTarget* const target = owner)
  27. target->tryToInvoke (info, false);
  28. }
  29. private:
  30. WeakReference<ApplicationCommandTarget> owner;
  31. const InvocationInfo info;
  32. JUCE_DECLARE_NON_COPYABLE (CommandMessage)
  33. };
  34. //==============================================================================
  35. ApplicationCommandTarget::ApplicationCommandTarget()
  36. {
  37. }
  38. ApplicationCommandTarget::~ApplicationCommandTarget()
  39. {
  40. masterReference.clear();
  41. }
  42. //==============================================================================
  43. bool ApplicationCommandTarget::tryToInvoke (const InvocationInfo& info, const bool async)
  44. {
  45. if (isCommandActive (info.commandID))
  46. {
  47. if (async)
  48. {
  49. (new CommandMessage (this, info))->post();
  50. return true;
  51. }
  52. else
  53. {
  54. const bool success = perform (info);
  55. jassert (success); // Hmm.. your target claimed that it could perform this command, but failed to do so.
  56. // If it can't do it at the moment for some reason, it should clear the 'isActive' flag
  57. // when it returns the command's info.
  58. return success;
  59. }
  60. }
  61. return false;
  62. }
  63. ApplicationCommandTarget* ApplicationCommandTarget::findFirstTargetParentComponent()
  64. {
  65. if (Component* const c = dynamic_cast <Component*> (this))
  66. return c->findParentComponentOfClass<ApplicationCommandTarget>();
  67. return nullptr;
  68. }
  69. ApplicationCommandTarget* ApplicationCommandTarget::getTargetForCommand (const CommandID commandID)
  70. {
  71. ApplicationCommandTarget* target = this;
  72. int depth = 0;
  73. while (target != nullptr)
  74. {
  75. Array <CommandID> commandIDs;
  76. target->getAllCommands (commandIDs);
  77. if (commandIDs.contains (commandID))
  78. return target;
  79. target = target->getNextCommandTarget();
  80. ++depth;
  81. jassert (depth < 100); // could be a recursive command chain??
  82. jassert (target != this); // definitely a recursive command chain!
  83. if (depth > 100 || target == this)
  84. break;
  85. }
  86. if (target == nullptr)
  87. {
  88. target = JUCEApplication::getInstance();
  89. if (target != nullptr)
  90. {
  91. Array <CommandID> commandIDs;
  92. target->getAllCommands (commandIDs);
  93. if (commandIDs.contains (commandID))
  94. return target;
  95. }
  96. }
  97. return nullptr;
  98. }
  99. bool ApplicationCommandTarget::isCommandActive (const CommandID commandID)
  100. {
  101. ApplicationCommandInfo info (commandID);
  102. info.flags = ApplicationCommandInfo::isDisabled;
  103. getCommandInfo (commandID, info);
  104. return (info.flags & ApplicationCommandInfo::isDisabled) == 0;
  105. }
  106. //==============================================================================
  107. bool ApplicationCommandTarget::invoke (const InvocationInfo& info, const bool async)
  108. {
  109. ApplicationCommandTarget* target = this;
  110. int depth = 0;
  111. while (target != nullptr)
  112. {
  113. if (target->tryToInvoke (info, async))
  114. return true;
  115. target = target->getNextCommandTarget();
  116. ++depth;
  117. jassert (depth < 100); // could be a recursive command chain??
  118. jassert (target != this); // definitely a recursive command chain!
  119. if (depth > 100 || target == this)
  120. break;
  121. }
  122. if (target == nullptr)
  123. {
  124. target = JUCEApplication::getInstance();
  125. if (target != nullptr)
  126. return target->tryToInvoke (info, async);
  127. }
  128. return false;
  129. }
  130. bool ApplicationCommandTarget::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. //==============================================================================
  137. ApplicationCommandTarget::InvocationInfo::InvocationInfo (const CommandID command)
  138. : commandID (command),
  139. commandFlags (0),
  140. invocationMethod (direct),
  141. originatingComponent (nullptr),
  142. isKeyDown (false),
  143. millisecsSinceKeyPressed (0)
  144. {
  145. }