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.

101 lines
3.4KB

  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. : 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, 0);
  31. // if there are no buttons, we won't allow the user to interrupt the thread.
  32. alertWindow->setEscapeKeyCancels (false);
  33. if (hasProgressBar)
  34. alertWindow->addProgressBarComponent (progress);
  35. }
  36. ThreadWithProgressWindow::~ThreadWithProgressWindow()
  37. {
  38. stopThread (timeOutMsWhenCancelling);
  39. }
  40. #if JUCE_MODAL_LOOPS_PERMITTED
  41. bool ThreadWithProgressWindow::runThread (const int priority)
  42. {
  43. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  44. startThread (priority);
  45. startTimer (100);
  46. {
  47. const ScopedLock sl (messageLock);
  48. alertWindow->setMessage (message);
  49. }
  50. const bool finishedNaturally = alertWindow->runModalLoop() != 0;
  51. stopThread (timeOutMsWhenCancelling);
  52. alertWindow->setVisible (false);
  53. return finishedNaturally;
  54. }
  55. #endif
  56. void ThreadWithProgressWindow::setProgress (const double newProgress)
  57. {
  58. progress = newProgress;
  59. }
  60. void ThreadWithProgressWindow::setStatusMessage (const String& newStatusMessage)
  61. {
  62. const ScopedLock sl (messageLock);
  63. message = newStatusMessage;
  64. }
  65. void ThreadWithProgressWindow::timerCallback()
  66. {
  67. if (! isThreadRunning())
  68. {
  69. // thread has finished normally..
  70. alertWindow->exitModalState (1);
  71. alertWindow->setVisible (false);
  72. }
  73. else
  74. {
  75. const ScopedLock sl (messageLock);
  76. alertWindow->setMessage (message);
  77. }
  78. }