Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_ThreadWithProgressWindow.cpp 3.8KB

9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. ThreadWithProgressWindow::ThreadWithProgressWindow (const String& title,
  18. const bool hasProgressBar,
  19. const bool hasCancelButton,
  20. const int cancellingTimeOutMs,
  21. const String& cancelButtonText,
  22. Component* componentToCentreAround)
  23. : Thread ("ThreadWithProgressWindow"),
  24. progress (0.0),
  25. timeOutMsWhenCancelling (cancellingTimeOutMs),
  26. wasCancelledByUser (false)
  27. {
  28. alertWindow = LookAndFeel::getDefaultLookAndFeel()
  29. .createAlertWindow (title, String(),
  30. cancelButtonText.isEmpty() ? TRANS("Cancel")
  31. : cancelButtonText,
  32. String(), String(),
  33. AlertWindow::NoIcon, hasCancelButton ? 1 : 0,
  34. componentToCentreAround);
  35. // if there are no buttons, we won't allow the user to interrupt the thread.
  36. alertWindow->setEscapeKeyCancels (false);
  37. if (hasProgressBar)
  38. alertWindow->addProgressBarComponent (progress);
  39. }
  40. ThreadWithProgressWindow::~ThreadWithProgressWindow()
  41. {
  42. stopThread (timeOutMsWhenCancelling);
  43. }
  44. void ThreadWithProgressWindow::launchThread (int priority)
  45. {
  46. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  47. startThread (priority);
  48. startTimer (100);
  49. {
  50. const ScopedLock sl (messageLock);
  51. alertWindow->setMessage (message);
  52. }
  53. alertWindow->enterModalState();
  54. }
  55. void ThreadWithProgressWindow::setProgress (const double newProgress)
  56. {
  57. progress = newProgress;
  58. }
  59. void ThreadWithProgressWindow::setStatusMessage (const String& newStatusMessage)
  60. {
  61. const ScopedLock sl (messageLock);
  62. message = newStatusMessage;
  63. }
  64. void ThreadWithProgressWindow::timerCallback()
  65. {
  66. bool threadStillRunning = isThreadRunning();
  67. if (! (threadStillRunning && alertWindow->isCurrentlyModal()))
  68. {
  69. stopTimer();
  70. stopThread (timeOutMsWhenCancelling);
  71. alertWindow->exitModalState (1);
  72. alertWindow->setVisible (false);
  73. wasCancelledByUser = threadStillRunning;
  74. threadComplete (threadStillRunning);
  75. return; // (this may be deleted now)
  76. }
  77. const ScopedLock sl (messageLock);
  78. alertWindow->setMessage (message);
  79. }
  80. void ThreadWithProgressWindow::threadComplete (bool) {}
  81. #if JUCE_MODAL_LOOPS_PERMITTED
  82. bool ThreadWithProgressWindow::runThread (const int priority)
  83. {
  84. launchThread (priority);
  85. while (isTimerRunning())
  86. MessageManager::getInstance()->runDispatchLoopUntil (5);
  87. return ! wasCancelledByUser;
  88. }
  89. #endif