The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

102 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. ThreadWithProgressWindow::ThreadWithProgressWindow (const String& title,
  19. const bool hasProgressBar,
  20. const bool hasCancelButton,
  21. const int timeOutMsWhenCancelling_,
  22. const String& cancelButtonText,
  23. Component* componentToCentreAround)
  24. : Thread ("Juce Progress Window"),
  25. progress (0.0),
  26. timeOutMsWhenCancelling (timeOutMsWhenCancelling_)
  27. {
  28. alertWindow = LookAndFeel::getDefaultLookAndFeel()
  29. .createAlertWindow (title, String::empty, cancelButtonText,
  30. String::empty, String::empty,
  31. AlertWindow::NoIcon, hasCancelButton ? 1 : 0,
  32. componentToCentreAround);
  33. // if there are no buttons, we won't allow the user to interrupt the thread.
  34. alertWindow->setEscapeKeyCancels (false);
  35. if (hasProgressBar)
  36. alertWindow->addProgressBarComponent (progress);
  37. }
  38. ThreadWithProgressWindow::~ThreadWithProgressWindow()
  39. {
  40. stopThread (timeOutMsWhenCancelling);
  41. }
  42. #if JUCE_MODAL_LOOPS_PERMITTED
  43. bool ThreadWithProgressWindow::runThread (const int priority)
  44. {
  45. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  46. startThread (priority);
  47. startTimer (100);
  48. {
  49. const ScopedLock sl (messageLock);
  50. alertWindow->setMessage (message);
  51. }
  52. const bool finishedNaturally = alertWindow->runModalLoop() != 0;
  53. stopThread (timeOutMsWhenCancelling);
  54. alertWindow->setVisible (false);
  55. return finishedNaturally;
  56. }
  57. #endif
  58. void ThreadWithProgressWindow::setProgress (const double newProgress)
  59. {
  60. progress = newProgress;
  61. }
  62. void ThreadWithProgressWindow::setStatusMessage (const String& newStatusMessage)
  63. {
  64. const ScopedLock sl (messageLock);
  65. message = newStatusMessage;
  66. }
  67. void ThreadWithProgressWindow::timerCallback()
  68. {
  69. if (! isThreadRunning())
  70. {
  71. // thread has finished normally..
  72. alertWindow->exitModalState (1);
  73. alertWindow->setVisible (false);
  74. }
  75. else
  76. {
  77. const ScopedLock sl (messageLock);
  78. alertWindow->setMessage (message);
  79. }
  80. }