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.

115 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "../../../../juce_core/basics/juce_StandardHeader.h"
  24. BEGIN_JUCE_NAMESPACE
  25. #include "juce_ThreadWithProgressWindow.h"
  26. #include "../lookandfeel/juce_LookAndFeel.h"
  27. #include "../../../../juce_core/text/juce_LocalisedStrings.h"
  28. //==============================================================================
  29. ThreadWithProgressWindow::ThreadWithProgressWindow (const String& title,
  30. const bool hasProgressBar,
  31. const bool hasCancelButton,
  32. const int timeOutMsWhenCancelling_,
  33. const String& cancelButtonText)
  34. : Thread ("Juce Progress Window"),
  35. progress (0.0),
  36. timeOutMsWhenCancelling (timeOutMsWhenCancelling_)
  37. {
  38. alertWindow = LookAndFeel::getDefaultLookAndFeel()
  39. .createAlertWindow (title, String::empty, cancelButtonText, String::empty, String::empty,
  40. AlertWindow::NoIcon, 1, 0);
  41. if (hasProgressBar)
  42. alertWindow->addProgressBarComponent (progress);
  43. if (hasCancelButton)
  44. alertWindow->addButton (cancelButtonText, 1);
  45. }
  46. ThreadWithProgressWindow::~ThreadWithProgressWindow()
  47. {
  48. stopThread (timeOutMsWhenCancelling);
  49. delete alertWindow;
  50. }
  51. bool ThreadWithProgressWindow::runThread (const int priority)
  52. {
  53. startThread (priority);
  54. startTimer (100);
  55. {
  56. const ScopedLock sl (messageLock);
  57. alertWindow->setMessage (message);
  58. }
  59. const bool wasCancelled = alertWindow->runModalLoop() != 0;
  60. stopThread (timeOutMsWhenCancelling);
  61. alertWindow->setVisible (false);
  62. return ! wasCancelled;
  63. }
  64. void ThreadWithProgressWindow::setProgress (const double newProgress)
  65. {
  66. progress = newProgress;
  67. }
  68. void ThreadWithProgressWindow::setStatusMessage (const String& newStatusMessage)
  69. {
  70. const ScopedLock sl (messageLock);
  71. message = newStatusMessage;
  72. }
  73. void ThreadWithProgressWindow::timerCallback()
  74. {
  75. if (! isThreadRunning())
  76. {
  77. // thread has finished normally..
  78. alertWindow->exitModalState (0);
  79. alertWindow->setVisible (false);
  80. }
  81. else
  82. {
  83. const ScopedLock sl (messageLock);
  84. alertWindow->setMessage (message);
  85. }
  86. }
  87. END_JUCE_NAMESPACE