LMMS
Loading...
Searching...
No Matches
juce_ModalComponentManager.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 ModalItem (Component* comp, bool shouldAutoDelete)
33 component (comp), autoDelete (shouldAutoDelete)
34 {
35 jassert (comp != nullptr);
36 }
37
38 ~ModalItem() override
39 {
40 if (autoDelete)
41 std::unique_ptr<Component> componentDeleter (component);
42 }
43
44 void componentMovedOrResized (bool, bool) override {}
45
47
48 void componentPeerChanged() override
49 {
51 }
52
54 {
55 if (! component->isShowing())
56 cancel();
57 }
58
60
62 {
64
65 if (component == &comp || comp.isParentOf (component))
66 {
67 autoDelete = false;
68 cancel();
69 }
70 }
71
72 void cancel()
73 {
74 if (isActive)
75 {
76 isActive = false;
77
78 if (auto* mcm = ModalComponentManager::getInstanceWithoutCreating())
79 mcm->triggerAsyncUpdate();
80 }
81 }
82
85 int returnValue = 0;
86 bool isActive = true, autoDelete;
87
89};
90
91//==============================================================================
95
97{
98 stack.clear();
99 clearSingletonInstance();
100}
101
103
104
105//==============================================================================
106void ModalComponentManager::startModal (Component* component, bool autoDelete)
107{
108 if (component != nullptr)
109 stack.add (new ModalItem (component, autoDelete));
110}
111
113{
114 if (callback != nullptr)
115 {
116 std::unique_ptr<Callback> callbackDeleter (callback);
117
118 for (int i = stack.size(); --i >= 0;)
119 {
120 auto* item = stack.getUnchecked (i);
121
122 if (item->component == component)
123 {
124 item->callbacks.add (callback);
125 callbackDeleter.release();
126 break;
127 }
128 }
129 }
130}
131
133{
134 for (int i = stack.size(); --i >= 0;)
135 {
136 auto* item = stack.getUnchecked (i);
137
138 if (item->component == component)
139 item->cancel();
140 }
141}
142
143void ModalComponentManager::endModal (Component* component, int returnValue)
144{
145 for (int i = stack.size(); --i >= 0;)
146 {
147 auto* item = stack.getUnchecked (i);
148
149 if (item->component == component)
150 {
151 item->returnValue = returnValue;
152 item->cancel();
153 }
154 }
155}
156
158{
159 int n = 0;
160
161 for (auto* item : stack)
162 if (item->isActive)
163 ++n;
164
165 return n;
166}
167
169{
170 int n = 0;
171
172 for (int i = stack.size(); --i >= 0;)
173 {
174 auto* item = stack.getUnchecked (i);
175
176 if (item->isActive)
177 if (n++ == index)
178 return item->component;
179 }
180
181 return nullptr;
182}
183
185{
186 for (auto* item : stack)
187 if (item->isActive && item->component == comp)
188 return true;
189
190 return false;
191}
192
197
199{
200 for (int i = stack.size(); --i >= 0;)
201 {
202 auto* item = stack.getUnchecked (i);
203
204 if (! item->isActive)
205 {
206 std::unique_ptr<ModalItem> deleter (stack.removeAndReturn (i));
207 Component::SafePointer<Component> compToDelete (item->autoDelete ? item->component : nullptr);
208
209 for (int j = item->callbacks.size(); --j >= 0;)
210 item->callbacks.getUnchecked (j)->modalStateFinished (item->returnValue);
211
212 compToDelete.deleteAndZero();
213 }
214 }
215}
216
218{
219 ComponentPeer* lastOne = nullptr;
220
221 for (int i = 0; i < getNumModalComponents(); ++i)
222 {
223 auto* c = getModalComponent (i);
224
225 if (c == nullptr)
226 break;
227
228 if (auto* peer = c->getPeer())
229 {
230 if (peer != lastOne)
231 {
232 if (lastOne == nullptr)
233 {
234 peer->toFront (topOneShouldGrabFocus);
235
236 if (topOneShouldGrabFocus)
237 peer->grabFocus();
238 }
239 else
240 {
241 peer->toBehind (lastOne);
242 }
243
244 lastOne = peer;
245 }
246 }
247 }
248}
249
251{
252 auto numModal = getNumModalComponents();
253
254 for (int i = numModal; --i >= 0;)
255 if (auto* c = getModalComponent (i))
256 c->exitModalState (0);
257
258 return numModal > 0;
259}
260
261//==============================================================================
262#if JUCE_MODAL_LOOPS_PERMITTED
263int ModalComponentManager::runEventLoopForCurrentComponent()
264{
265 // This can only be run from the message thread!
267
268 int returnValue = 0;
269
270 if (auto* currentlyModal = getModalComponent (0))
271 {
272 FocusRestorer focusRestorer;
273 bool finished = false;
274
275 attachCallback (currentlyModal, ModalCallbackFunction::create ([&] (int r) { returnValue = r; finished = true; }));
276
278 {
279 while (! finished)
280 {
281 if (! MessageManager::getInstance()->runDispatchLoopUntil (20))
282 break;
283 }
284 }
286 }
287
288 return returnValue;
289}
290#endif
291
292} // namespace juce
Definition juce_Component.h:2287
void deleteAndZero()
Definition juce_Component.h:2314
void componentBeingDeleted(Component &) override
Definition juce_ComponentMovementWatcher.cpp:103
virtual void componentMovedOrResized(bool wasMoved, bool wasResized)=0
virtual void componentVisibilityChanged()=0
ComponentMovementWatcher(Component *componentToWatch)
Definition juce_ComponentMovementWatcher.cpp:29
Definition juce_ComponentPeer.h:44
static ModalComponentManager::Callback * create(CallbackFn &&fn)
Definition juce_ModalComponentManager.h:174
Definition juce_ModalComponentManager.h:56
Definition juce_ModalComponentManager.h:44
OwnedArray< ModalItem > stack
Definition juce_ModalComponentManager.h:146
void handleAsyncUpdate() override
Definition juce_ModalComponentManager.cpp:198
bool isModal(const Component *component) const
Definition juce_ModalComponentManager.cpp:184
void bringModalComponentsToFront(bool topOneShouldGrabFocus=true)
Definition juce_ModalComponentManager.cpp:217
~ModalComponentManager() override
Definition juce_ModalComponentManager.cpp:96
void startModal(Component *, bool autoDelete)
Definition juce_ModalComponentManager.cpp:106
Component * getModalComponent(int index) const
Definition juce_ModalComponentManager.cpp:168
bool isFrontModalComponent(const Component *component) const
Definition juce_ModalComponentManager.cpp:193
void attachCallback(Component *component, Callback *callback)
Definition juce_ModalComponentManager.cpp:112
bool cancelAllModalComponents()
Definition juce_ModalComponentManager.cpp:250
friend class Component
Definition juce_ModalComponentManager.h:143
void endModal(Component *, int returnValue)
Definition juce_ModalComponentManager.cpp:143
ModalComponentManager()
Definition juce_ModalComponentManager.cpp:92
int getNumModalComponents() const
Definition juce_ModalComponentManager.cpp:157
Definition juce_OwnedArray.h:51
register unsigned j
Definition inflate.c:1576
register unsigned i
Definition inflate.c:1575
#define JUCE_TRY
Definition juce_ApplicationBase.h:329
#define JUCE_CATCH_EXCEPTION
Definition juce_ApplicationBase.h:330
#define JUCE_ASSERT_MESSAGE_THREAD
Definition juce_MessageManager.h:473
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE(className)
#define JUCE_IMPLEMENT_SINGLETON(Classname)
Definition juce_Singleton.h:201
static int finished(SordWorld *world, SordModel *sord, int status)
Definition sord_test.c:390
static int JUCE_CDECL comp(const void *a, const void *b)
Definition lsp.c:298
Definition carla_juce.cpp:31
Definition juce_Component.cpp:181
Definition juce_ModalComponentManager.cpp:30
OwnedArray< Callback > callbacks
Definition juce_ModalComponentManager.cpp:84
Component * component
Definition juce_ModalComponentManager.cpp:83
void cancel()
Definition juce_ModalComponentManager.cpp:72
ModalItem(Component *comp, bool shouldAutoDelete)
Definition juce_ModalComponentManager.cpp:31
void componentBeingDeleted(Component &comp) override
Definition juce_ModalComponentManager.cpp:61
~ModalItem() override
Definition juce_ModalComponentManager.cpp:38
bool isActive
Definition juce_ModalComponentManager.cpp:86
void componentVisibilityChanged() override
Definition juce_ModalComponentManager.cpp:53
bool autoDelete
Definition juce_ModalComponentManager.cpp:86
void componentMovedOrResized(bool, bool) override
Definition juce_ModalComponentManager.cpp:44
void componentPeerChanged() override
Definition juce_ModalComponentManager.cpp:48
int returnValue
Definition juce_ModalComponentManager.cpp:85
RECT const char void(* callback)(const char *droppath))) SWELL_API_DEFINE(BOOL
Definition swell-functions.h:1004
int n
Definition crypt.c:458
return c
Definition crypt.c:175
int r
Definition crypt.c:458
#define void
Definition unzip.h:396