|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2020 - Raw Material Software Limited
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- By using JUCE, you agree to the terms of both the JUCE 6 End-User License
- Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
-
- End User License Agreement: www.juce.com/juce-6-licence
- Privacy Policy: www.juce.com/juce-privacy-policy
-
- Or: You may also use this code under the terms of the GPL v3 (see
- www.gnu.org/licenses).
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- class ApplicationCommandTarget::CommandMessage : public MessageManager::MessageBase
- {
- public:
- CommandMessage (ApplicationCommandTarget* const target, const InvocationInfo& inf)
- : owner (target), info (inf)
- {
- }
-
- void messageCallback() override
- {
- if (ApplicationCommandTarget* const target = owner)
- target->tryToInvoke (info, false);
- }
-
- private:
- WeakReference<ApplicationCommandTarget> owner;
- const InvocationInfo info;
-
- JUCE_DECLARE_NON_COPYABLE (CommandMessage)
- };
-
- //==============================================================================
- ApplicationCommandTarget::ApplicationCommandTarget() {}
- ApplicationCommandTarget::~ApplicationCommandTarget() {}
-
- //==============================================================================
- bool ApplicationCommandTarget::tryToInvoke (const InvocationInfo& info, const bool async)
- {
- if (isCommandActive (info.commandID))
- {
- if (async)
- {
- (new CommandMessage (this, info))->post();
- return true;
- }
-
- if (perform (info))
- return true;
-
- // Hmm.. your target claimed that it could perform this command, but failed to do so.
- // If it can't do it at the moment for some reason, it should clear the 'isActive' flag
- // when it returns the command's info.
- jassertfalse;
- }
-
- return false;
- }
-
- ApplicationCommandTarget* ApplicationCommandTarget::findFirstTargetParentComponent()
- {
- if (Component* const c = dynamic_cast<Component*> (this))
- return c->findParentComponentOfClass<ApplicationCommandTarget>();
-
- return nullptr;
- }
-
- ApplicationCommandTarget* ApplicationCommandTarget::getTargetForCommand (const CommandID commandID)
- {
- ApplicationCommandTarget* target = this;
- int depth = 0;
-
- while (target != nullptr)
- {
- Array<CommandID> commandIDs;
- target->getAllCommands (commandIDs);
-
- if (commandIDs.contains (commandID))
- return target;
-
- target = target->getNextCommandTarget();
-
- ++depth;
- jassert (depth < 100); // could be a recursive command chain??
- jassert (target != this); // definitely a recursive command chain!
-
- if (depth > 100 || target == this)
- break;
- }
-
- if (target == nullptr)
- {
- target = JUCEApplication::getInstance();
-
- if (target != nullptr)
- {
- Array<CommandID> commandIDs;
- target->getAllCommands (commandIDs);
-
- if (commandIDs.contains (commandID))
- return target;
- }
- }
-
- return nullptr;
- }
-
- bool ApplicationCommandTarget::isCommandActive (const CommandID commandID)
- {
- ApplicationCommandInfo info (commandID);
- info.flags = ApplicationCommandInfo::isDisabled;
-
- getCommandInfo (commandID, info);
-
- return (info.flags & ApplicationCommandInfo::isDisabled) == 0;
- }
-
- //==============================================================================
- bool ApplicationCommandTarget::invoke (const InvocationInfo& info, const bool async)
- {
- ApplicationCommandTarget* target = this;
- int depth = 0;
-
- while (target != nullptr)
- {
- if (target->tryToInvoke (info, async))
- return true;
-
- target = target->getNextCommandTarget();
-
- ++depth;
- jassert (depth < 100); // could be a recursive command chain??
- jassert (target != this); // definitely a recursive command chain!
-
- if (depth > 100 || target == this)
- break;
- }
-
- if (target == nullptr)
- {
- target = JUCEApplication::getInstance();
-
- if (target != nullptr)
- return target->tryToInvoke (info, async);
- }
-
- return false;
- }
-
- bool ApplicationCommandTarget::invokeDirectly (const CommandID commandID, const bool asynchronously)
- {
- ApplicationCommandTarget::InvocationInfo info (commandID);
- info.invocationMethod = ApplicationCommandTarget::InvocationInfo::direct;
-
- return invoke (info, asynchronously);
- }
-
- //==============================================================================
- ApplicationCommandTarget::InvocationInfo::InvocationInfo (const CommandID command)
- : commandID (command),
- commandFlags (0),
- invocationMethod (direct),
- originatingComponent (nullptr),
- isKeyDown (false),
- millisecsSinceKeyPressed (0)
- {
- }
-
- } // namespace juce
|