LMMS
Loading...
Searching...
No Matches
juce_ApplicationCommandTarget.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30{
31public:
33 : owner (target), info (inf)
34 {
35 }
36
37 void messageCallback() override
38 {
39 if (ApplicationCommandTarget* const target = owner)
40 target->tryToInvoke (info, false);
41 }
42
43private:
46
48};
49
50//==============================================================================
53
54//==============================================================================
56{
57 if (isCommandActive (info.commandID))
58 {
59 if (async)
60 {
61 (new CommandMessage (this, info))->post();
62 return true;
63 }
64
65 if (perform (info))
66 return true;
67
68 // Hmm.. your target claimed that it could perform this command, but failed to do so.
69 // If it can't do it at the moment for some reason, it should clear the 'isActive' flag
70 // when it returns the command's info.
72 }
73
74 return false;
75}
76
78{
79 if (Component* const c = dynamic_cast<Component*> (this))
80 return c->findParentComponentOfClass<ApplicationCommandTarget>();
81
82 return nullptr;
83}
84
86{
87 ApplicationCommandTarget* target = this;
88 int depth = 0;
89
90 while (target != nullptr)
91 {
92 Array<CommandID> commandIDs;
93 target->getAllCommands (commandIDs);
94
95 if (commandIDs.contains (commandID))
96 return target;
97
98 target = target->getNextCommandTarget();
99
100 ++depth;
101 jassert (depth < 100); // could be a recursive command chain??
102 jassert (target != this); // definitely a recursive command chain!
103
104 if (depth > 100 || target == this)
105 break;
106 }
107
108 if (target == nullptr)
109 {
111
112 if (target != nullptr)
113 {
114 Array<CommandID> commandIDs;
115 target->getAllCommands (commandIDs);
116
117 if (commandIDs.contains (commandID))
118 return target;
119 }
120 }
121
122 return nullptr;
123}
124
126{
127 ApplicationCommandInfo info (commandID);
129
130 getCommandInfo (commandID, info);
131
132 return (info.flags & ApplicationCommandInfo::isDisabled) == 0;
133}
134
135//==============================================================================
137{
138 ApplicationCommandTarget* target = this;
139 int depth = 0;
140
141 while (target != nullptr)
142 {
143 if (target->tryToInvoke (info, async))
144 return true;
145
146 target = target->getNextCommandTarget();
147
148 ++depth;
149 jassert (depth < 100); // could be a recursive command chain??
150 jassert (target != this); // definitely a recursive command chain!
151
152 if (depth > 100 || target == this)
153 break;
154 }
155
156 if (target == nullptr)
157 {
159
160 if (target != nullptr)
161 return target->tryToInvoke (info, async);
162 }
163
164 return false;
165}
166
167bool ApplicationCommandTarget::invokeDirectly (const CommandID commandID, const bool asynchronously)
168{
171
172 return invoke (info, asynchronously);
173}
174
175//==============================================================================
185
186} // namespace juce
#define nullptr
Definition DistrhoDefines.h:75
Definition juce_ApplicationCommandTarget.cpp:30
CommandMessage(ApplicationCommandTarget *const target, const InvocationInfo &inf)
Definition juce_ApplicationCommandTarget.cpp:32
WeakReference< ApplicationCommandTarget > owner
Definition juce_ApplicationCommandTarget.cpp:44
void messageCallback() override
Definition juce_ApplicationCommandTarget.cpp:37
const InvocationInfo info
Definition juce_ApplicationCommandTarget.cpp:45
bool tryToInvoke(const InvocationInfo &, bool async)
Definition juce_ApplicationCommandTarget.cpp:55
ApplicationCommandTarget()
Definition juce_ApplicationCommandTarget.cpp:51
bool invokeDirectly(const CommandID commandID, const bool asynchronously)
Definition juce_ApplicationCommandTarget.cpp:167
virtual ApplicationCommandTarget * getNextCommandTarget()=0
virtual void getCommandInfo(CommandID commandID, ApplicationCommandInfo &result)=0
virtual void getAllCommands(Array< CommandID > &commands)=0
bool invoke(const InvocationInfo &invocationInfo, const bool asynchronously)
Definition juce_ApplicationCommandTarget.cpp:136
virtual bool perform(const InvocationInfo &info)=0
bool isCommandActive(const CommandID commandID)
Definition juce_ApplicationCommandTarget.cpp:125
ApplicationCommandTarget * findFirstTargetParentComponent()
Definition juce_ApplicationCommandTarget.cpp:77
virtual ~ApplicationCommandTarget()
Definition juce_ApplicationCommandTarget.cpp:52
ApplicationCommandTarget * getTargetForCommand(const CommandID commandID)
Definition juce_ApplicationCommandTarget.cpp:85
Definition juce_Array.h:56
bool contains(ParameterType elementToLookFor) const
Definition juce_Array.h:400
Definition juce_Component.h:36
static JUCEApplication *JUCE_CALLTYPE getInstance() noexcept
Definition juce_Application.cpp:33
Definition juce_MessageManager.h:182
Definition juce_WeakReference.h:78
struct backing_store_struct * info
Definition jmemsys.h:183
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE(className)
#define jassertfalse
Definition carla_juce.cpp:31
int CommandID
Definition juce_ApplicationCommandID.h:37
#define false
Definition ordinals.h:83
Definition juce_ApplicationCommandInfo.h:45
@ isDisabled
Definition juce_ApplicationCommandInfo.h:146
Definition juce_ApplicationCommandTarget.h:61
InvocationMethod invocationMethod
Definition juce_ApplicationCommandTarget.h:85
InvocationInfo(const CommandID commandID)
Definition juce_ApplicationCommandTarget.cpp:176
Component * originatingComponent
Definition juce_ApplicationCommandTarget.h:93
bool isKeyDown
Definition juce_ApplicationCommandTarget.h:108
@ direct
Definition juce_ApplicationCommandTarget.h:78
int commandFlags
Definition juce_ApplicationCommandTarget.h:72
CommandID commandID
Definition juce_ApplicationCommandTarget.h:67
int millisecsSinceKeyPressed
Definition juce_ApplicationCommandTarget.h:115
return c
Definition crypt.c:175
ZCONST char * post
Definition fileio.c:2493