LMMS
Loading...
Searching...
No Matches
juce_ThreadWithProgressWindow.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 const bool hasProgressBar,
31 const bool hasCancelButton,
32 const int cancellingTimeOutMs,
33 const String& cancelButtonText,
34 Component* componentToCentreAround)
35 : Thread ("ThreadWithProgressWindow"),
36 progress (0.0),
37 timeOutMsWhenCancelling (cancellingTimeOutMs),
39{
41 .createAlertWindow (title, {},
42 cancelButtonText.isEmpty() ? TRANS("Cancel")
43 : cancelButtonText,
44 {}, {}, MessageBoxIconType::NoIcon, hasCancelButton ? 1 : 0,
45 componentToCentreAround));
46
47 // if there are no buttons, we won't allow the user to interrupt the thread.
48 alertWindow->setEscapeKeyCancels (false);
49
50 if (hasProgressBar)
51 alertWindow->addProgressBarComponent (progress);
52}
53
58
60{
62
63 startThread (priority);
64 startTimer (100);
65
66 {
67 const ScopedLock sl (messageLock);
68 alertWindow->setMessage (message);
69 }
70
71 alertWindow->enterModalState();
72}
73
74void ThreadWithProgressWindow::setProgress (const double newProgress)
75{
76 progress = newProgress;
77}
78
80{
81 const ScopedLock sl (messageLock);
82 message = newStatusMessage;
83}
84
86{
87 bool threadStillRunning = isThreadRunning();
88
89 if (! (threadStillRunning && alertWindow->isCurrentlyModal (false)))
90 {
91 stopTimer();
93 alertWindow->exitModalState (1);
94 alertWindow->setVisible (false);
95
96 wasCancelledByUser = threadStillRunning;
97 threadComplete (threadStillRunning);
98 return; // (this may be deleted now)
99 }
100
101 const ScopedLock sl (messageLock);
102 alertWindow->setMessage (message);
103}
104
106
107#if JUCE_MODAL_LOOPS_PERMITTED
108bool ThreadWithProgressWindow::runThread (const int priority)
109{
110 launchThread (priority);
111
112 while (isTimerRunning())
113 MessageManager::getInstance()->runDispatchLoopUntil (5);
114
115 return ! wasCancelledByUser;
116}
117#endif
118
119} // namespace juce
Definition juce_Component.h:36
static LookAndFeel & getDefaultLookAndFeel() noexcept
Definition juce_LookAndFeel.cpp:107
static MessageManager * getInstance()
Definition juce_MessageManager.cpp:47
Definition juce_String.h:53
bool isEmpty() const noexcept
Definition juce_String.h:310
void startThread()
Definition juce_Thread.cpp:122
Thread(const String &threadName, size_t threadStackSize=0)
Definition juce_Thread.cpp:26
bool stopThread(int timeOutMilliseconds)
Definition juce_Thread.cpp:208
bool isThreadRunning() const
Definition juce_Thread.cpp:155
~ThreadWithProgressWindow() override
Definition juce_ThreadWithProgressWindow.cpp:54
void setStatusMessage(const String &newStatusMessage)
Definition juce_ThreadWithProgressWindow.cpp:79
void timerCallback() override
Definition juce_ThreadWithProgressWindow.cpp:85
bool wasCancelledByUser
Definition juce_ThreadWithProgressWindow.h:170
CriticalSection messageLock
Definition juce_ThreadWithProgressWindow.h:168
ThreadWithProgressWindow(const String &windowTitle, bool hasProgressBar, bool hasCancelButton, int timeOutMsWhenCancelling=10000, const String &cancelButtonText=String(), Component *componentToCentreAround=nullptr)
Definition juce_ThreadWithProgressWindow.cpp:29
void launchThread(int priority=5)
Definition juce_ThreadWithProgressWindow.cpp:59
double progress
Definition juce_ThreadWithProgressWindow.h:165
void setProgress(double newProgress)
Definition juce_ThreadWithProgressWindow.cpp:74
const int timeOutMsWhenCancelling
Definition juce_ThreadWithProgressWindow.h:169
std::unique_ptr< AlertWindow > alertWindow
Definition juce_ThreadWithProgressWindow.h:166
virtual void threadComplete(bool userPressedCancel)
Definition juce_ThreadWithProgressWindow.cpp:105
String message
Definition juce_ThreadWithProgressWindow.h:167
void stopTimer() noexcept
Definition juce_Timer.cpp:357
void startTimer(int intervalInMilliseconds) noexcept
Definition juce_Timer.cpp:332
static const char * title
Definition pugl.h:1747
#define TRANS(stringLiteral)
Definition juce_LocalisedStrings.h:208
#define JUCE_ASSERT_MESSAGE_THREAD
Definition juce_MessageManager.h:473
Definition carla_juce.cpp:31
CriticalSection::ScopedLockType ScopedLock
Definition juce_CriticalSection.h:186
@ NoIcon
Definition juce_MessageBoxOptions.h:32
#define false
Definition ordinals.h:83