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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 timeOutMsWhenCancelling_,
  21. const String& cancelButtonText,
  22. Component* componentToCentreAround)
  23. : Thread ("Juce Progress Window"),
  24. progress (0.0),
  25. timeOutMsWhenCancelling (timeOutMsWhenCancelling_)
  26. {
  27. alertWindow = LookAndFeel::getDefaultLookAndFeel()
  28. .createAlertWindow (title, String::empty, cancelButtonText,
  29. String::empty, String::empty,
  30. AlertWindow::NoIcon, hasCancelButton ? 1 : 0,
  31. componentToCentreAround);
  32. // if there are no buttons, we won't allow the user to interrupt the thread.
  33. alertWindow->setEscapeKeyCancels (false);
  34. if (hasProgressBar)
  35. alertWindow->addProgressBarComponent (progress);
  36. }
  37. ThreadWithProgressWindow::~ThreadWithProgressWindow()
  38. {
  39. stopThread (timeOutMsWhenCancelling);
  40. }
  41. #if JUCE_MODAL_LOOPS_PERMITTED
  42. bool ThreadWithProgressWindow::runThread (const int priority)
  43. {
  44. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  45. startThread (priority);
  46. startTimer (100);
  47. {
  48. const ScopedLock sl (messageLock);
  49. alertWindow->setMessage (message);
  50. }
  51. const bool finishedNaturally = alertWindow->runModalLoop() != 0;
  52. stopThread (timeOutMsWhenCancelling);
  53. alertWindow->setVisible (false);
  54. return finishedNaturally;
  55. }
  56. #endif
  57. void ThreadWithProgressWindow::setProgress (const double newProgress)
  58. {
  59. progress = newProgress;
  60. }
  61. void ThreadWithProgressWindow::setStatusMessage (const String& newStatusMessage)
  62. {
  63. const ScopedLock sl (messageLock);
  64. message = newStatusMessage;
  65. }
  66. void ThreadWithProgressWindow::timerCallback()
  67. {
  68. if (! isThreadRunning())
  69. {
  70. // thread has finished normally..
  71. alertWindow->exitModalState (1);
  72. alertWindow->setVisible (false);
  73. }
  74. else
  75. {
  76. const ScopedLock sl (messageLock);
  77. alertWindow->setMessage (message);
  78. }
  79. }