LMMS
Loading...
Searching...
No Matches
juce_ApplicationCommandManager.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{
31 keyMappings.reset (new KeyPressMappingSet (*this));
32 Desktop::getInstance().addFocusChangeListener (this);
33}
34
36{
37 Desktop::getInstance().removeFocusChangeListener (this);
38 keyMappings.reset();
39}
40
41//==============================================================================
43{
44 commands.clear();
45 keyMappings->clearAllKeyPresses();
47}
48
50{
51 // zero isn't a valid command ID!
52 jassert (newCommand.commandID != 0);
53
54 // the name isn't optional!
55 jassert (newCommand.shortName.isNotEmpty());
56
57 if (auto* command = getMutableCommandForID (newCommand.commandID))
58 {
59 // Trying to re-register the same command ID with different parameters can often indicate a typo.
60 // This assertion is here because I've found it useful catching some mistakes, but it may also cause
61 // false alarms if you're deliberately updating some flags for a command.
62 jassert (newCommand.shortName == getCommandForID (newCommand.commandID)->shortName
63 && newCommand.categoryName == getCommandForID (newCommand.commandID)->categoryName
64 && newCommand.defaultKeypresses == getCommandForID (newCommand.commandID)->defaultKeypresses
67
68 *command = newCommand;
69 }
70 else
71 {
72 auto* newInfo = new ApplicationCommandInfo (newCommand);
73 newInfo->flags &= ~ApplicationCommandInfo::isTicked;
74 commands.add (newInfo);
75
76 keyMappings->resetToDefaultMapping (newCommand.commandID);
77
79 }
80}
81
83{
84 if (target != nullptr)
85 {
86 Array<CommandID> commandIDs;
87 target->getAllCommands (commandIDs);
88
89 for (int i = 0; i < commandIDs.size(); ++i)
90 {
92 target->getCommandInfo (info.commandID, info);
93
95 }
96 }
97}
98
100{
101 for (int i = commands.size(); --i >= 0;)
102 {
103 if (commands.getUnchecked (i)->commandID == commandID)
104 {
105 commands.remove (i);
107
108 const Array<KeyPress> keys (keyMappings->getKeyPressesAssignedToCommand (commandID));
109
110 for (int j = keys.size(); --j >= 0;)
111 keyMappings->removeKeyPress (keys.getReference (j));
112 }
113 }
114}
115
120
121//==============================================================================
123{
124 for (int i = commands.size(); --i >= 0;)
125 if (commands.getUnchecked(i)->commandID == commandID)
126 return commands.getUnchecked(i);
127
128 return nullptr;
129}
130
132{
133 return getMutableCommandForID (commandID);
134}
135
137{
138 if (auto* ci = getCommandForID (commandID))
139 return ci->shortName;
140
141 return {};
142}
143
145{
146 if (auto* ci = getCommandForID (commandID))
147 return ci->description.isNotEmpty() ? ci->description
148 : ci->shortName;
149
150 return {};
151}
152
154{
156
157 for (int i = 0; i < commands.size(); ++i)
158 s.addIfNotAlreadyThere (commands.getUnchecked(i)->categoryName, false);
159
160 return s;
161}
162
164{
165 Array<CommandID> results;
166
167 for (int i = 0; i < commands.size(); ++i)
168 if (commands.getUnchecked(i)->categoryName == categoryName)
169 results.add (commands.getUnchecked(i)->commandID);
170
171 return results;
172}
173
174//==============================================================================
175bool ApplicationCommandManager::invokeDirectly (CommandID commandID, bool asynchronously)
176{
179
180 return invoke (info, asynchronously);
181}
182
184{
185 // This call isn't thread-safe for use from a non-UI thread without locking the message
186 // manager first..
188
189 bool ok = false;
190 ApplicationCommandInfo commandInfo (0);
191
192 if (auto* target = getTargetForCommand (inf.commandID, commandInfo))
193 {
195 info.commandFlags = commandInfo.flags;
196
198 ok = target->invoke (info, asynchronously);
200 }
201
202 return ok;
203}
204
205//==============================================================================
211
216
218 ApplicationCommandInfo& upToDateInfo)
219{
220 auto* target = getFirstCommandTarget (commandID);
221
222 if (target == nullptr)
224
225 if (target != nullptr)
226 target = target->getTargetForCommand (commandID);
227
228 if (target != nullptr)
229 {
230 upToDateInfo.commandID = commandID;
231 target->getCommandInfo (commandID, upToDateInfo);
232 }
233
234 return target;
235}
236
237//==============================================================================
239{
240 auto* target = dynamic_cast<ApplicationCommandTarget*> (c);
241
242 if (target == nullptr && c != nullptr)
243 target = c->findParentComponentOfClass<ApplicationCommandTarget>();
244
245 return target;
246}
247
249{
251
252 if (c == nullptr)
253 {
254 if (auto* activeWindow = TopLevelWindow::getActiveTopLevelWindow())
255 {
256 if (auto* peer = activeWindow->getPeer())
257 {
258 c = peer->getLastFocusedSubcomponent();
259
260 if (c == nullptr)
261 c = activeWindow;
262 }
263 }
264 }
265
266 if (c == nullptr)
267 {
268 auto& desktop = Desktop::getInstance();
269
270 // getting a bit desperate now: try all desktop comps..
271 for (int i = desktop.getNumComponents(); --i >= 0;)
272 if (auto* component = desktop.getComponent (i))
273 if (isForegroundOrEmbeddedProcess (component))
274 if (auto* peer = component->getPeer())
275 if (auto* target = findTargetForComponent (peer->getLastFocusedSubcomponent()))
276 return target;
277 }
278
279 if (c != nullptr)
280 {
281 // if we're focused on a ResizableWindow, chances are that it's the content
282 // component that really should get the event. And if not, the event will
283 // still be passed up to the top level window anyway, so let's send it to the
284 // content comp.
285 if (auto* resizableWindow = dynamic_cast<ResizableWindow*> (c))
286 if (auto* content = resizableWindow->getContentComponent())
287 c = content;
288
289 if (auto* target = findTargetForComponent (c))
290 return target;
291 }
292
294}
295
296//==============================================================================
301
306
311
313{
314 listeners.call ([] (ApplicationCommandManagerListener& l) { l.applicationCommandListChanged(); });
315}
316
321
322} // namespace juce
void globalFocusChanged(Component *) override
Definition juce_ApplicationCommandManager.cpp:317
void addListener(ApplicationCommandManagerListener *listener)
Definition juce_ApplicationCommandManager.cpp:297
ApplicationCommandTarget * getTargetForCommand(CommandID commandID, ApplicationCommandInfo &upToDateInfo)
Definition juce_ApplicationCommandManager.cpp:217
static ApplicationCommandTarget * findTargetForComponent(Component *)
Definition juce_ApplicationCommandManager.cpp:238
std::unique_ptr< KeyPressMappingSet > keyMappings
Definition juce_ApplicationCommandManager.h:307
Array< CommandID > getCommandsInCategory(const String &categoryName) const
Definition juce_ApplicationCommandManager.cpp:163
String getNameOfCommand(CommandID commandID) const noexcept
Definition juce_ApplicationCommandManager.cpp:136
static ApplicationCommandTarget * findDefaultComponentTarget()
Definition juce_ApplicationCommandManager.cpp:248
StringArray getCommandCategories() const
Definition juce_ApplicationCommandManager.cpp:153
void setFirstCommandTarget(ApplicationCommandTarget *newTarget) noexcept
Definition juce_ApplicationCommandManager.cpp:212
void registerCommand(const ApplicationCommandInfo &newCommand)
Definition juce_ApplicationCommandManager.cpp:49
ApplicationCommandManager()
Definition juce_ApplicationCommandManager.cpp:29
bool invokeDirectly(CommandID commandID, bool asynchronously)
Definition juce_ApplicationCommandManager.cpp:175
void removeCommand(CommandID commandID)
Definition juce_ApplicationCommandManager.cpp:99
OwnedArray< ApplicationCommandInfo > commands
Definition juce_ApplicationCommandManager.h:305
ApplicationCommandInfo * getMutableCommandForID(CommandID) const noexcept
Definition juce_ApplicationCommandManager.cpp:122
~ApplicationCommandManager() override
Definition juce_ApplicationCommandManager.cpp:35
const ApplicationCommandInfo * getCommandForID(CommandID commandID) const noexcept
Definition juce_ApplicationCommandManager.cpp:131
bool invoke(const ApplicationCommandTarget::InvocationInfo &invocationInfo, bool asynchronously)
Definition juce_ApplicationCommandManager.cpp:183
void commandStatusChanged()
Definition juce_ApplicationCommandManager.cpp:116
ListenerList< ApplicationCommandManagerListener > listeners
Definition juce_ApplicationCommandManager.h:306
void clearCommands()
Definition juce_ApplicationCommandManager.cpp:42
void sendListenerInvokeCallback(const ApplicationCommandTarget::InvocationInfo &)
Definition juce_ApplicationCommandManager.cpp:307
ApplicationCommandTarget * firstTarget
Definition juce_ApplicationCommandManager.h:308
virtual ApplicationCommandTarget * getFirstCommandTarget(CommandID commandID)
Definition juce_ApplicationCommandManager.cpp:206
void removeListener(ApplicationCommandManagerListener *listener)
Definition juce_ApplicationCommandManager.cpp:302
void handleAsyncUpdate() override
Definition juce_ApplicationCommandManager.cpp:312
void registerAllCommandsForTarget(ApplicationCommandTarget *target)
Definition juce_ApplicationCommandManager.cpp:82
String getDescriptionOfCommand(CommandID commandID) const noexcept
Definition juce_ApplicationCommandManager.cpp:144
Definition juce_ApplicationCommandManager.h:330
Definition juce_ApplicationCommandTarget.h:47
virtual void getCommandInfo(CommandID commandID, ApplicationCommandInfo &result)=0
virtual void getAllCommands(Array< CommandID > &commands)=0
Definition juce_Array.h:56
ElementType getUnchecked(int index) const
Definition juce_Array.h:252
int size() const noexcept
Definition juce_Array.h:215
void add(const ElementType &newElement)
Definition juce_Array.h:418
void triggerAsyncUpdate()
Definition juce_AsyncUpdater.cpp:62
Definition juce_Component.h:36
static Component *JUCE_CALLTYPE getCurrentlyFocusedComponent() noexcept
Definition juce_Component.cpp:3092
static Desktop &JUCE_CALLTYPE getInstance()
Definition juce_Desktop.cpp:50
static JUCEApplication *JUCE_CALLTYPE getInstance() noexcept
Definition juce_Application.cpp:33
Definition juce_KeyPressMappingSet.h:89
Definition juce_ResizableWindow.h:52
Definition juce_StringArray.h:35
Definition juce_String.h:53
bool isNotEmpty() const noexcept
Definition juce_String.h:316
static TopLevelWindow * getActiveTopLevelWindow() noexcept
Definition juce_TopLevelWindow.cpp:330
int * l
Definition inflate.c:1579
register unsigned j
Definition inflate.c:1576
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
struct backing_store_struct * info
Definition jmemsys.h:183
#define JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
Definition juce_MessageManager.h:465
#define jassert(expression)
Definition carla_juce.cpp:31
int CommandID
Definition juce_ApplicationCommandID.h:37
static bool isForegroundOrEmbeddedProcess(Component *viewComponent)
Definition juce_gui_basics.cpp:120
Definition juce_ApplicationCommandInfo.h:45
int flags
Definition juce_ApplicationCommandInfo.h:187
Array< KeyPress > defaultKeypresses
Definition juce_ApplicationCommandInfo.h:127
CommandID commandID
Definition juce_ApplicationCommandInfo.h:87
String categoryName
Definition juce_ApplicationCommandInfo.h:117
@ wantsKeyUpDownCallbacks
Definition juce_ApplicationCommandInfo.h:163
@ readOnlyInKeyEditor
Definition juce_ApplicationCommandInfo.h:173
@ hiddenFromKeyEditor
Definition juce_ApplicationCommandInfo.h:168
String shortName
Definition juce_ApplicationCommandInfo.h:96
Definition juce_ApplicationCommandTarget.h:61
@ direct
Definition juce_ApplicationCommandTarget.h:78
CommandID commandID
Definition juce_ApplicationCommandTarget.h:67
return c
Definition crypt.c:175