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.

165 lines
5.8KB

  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. #ifndef __JUCE_THREADWITHPROGRESSWINDOW_JUCEHEADER__
  24. #define __JUCE_THREADWITHPROGRESSWINDOW_JUCEHEADER__
  25. #include "../windows/juce_AlertWindow.h"
  26. #include "../../../events/juce_Timer.h"
  27. #include "../../../../juce_core/threads/juce_Thread.h"
  28. //==============================================================================
  29. /**
  30. A thread that automatically pops up a modal dialog box with a progress bar
  31. and cancel button while it's busy running.
  32. These are handy for performing some sort of task while giving the user feedback
  33. about how long there is to go, etc.
  34. E.g. @code
  35. class MyTask : public ThreadWithProgressWindow
  36. {
  37. public:
  38. MyTask() : ThreadWithProgressWindow (T("busy..."), true, true)
  39. {
  40. }
  41. ~MyTask()
  42. {
  43. }
  44. void run()
  45. {
  46. for (int i = 0; i < thingsToDo; ++i)
  47. {
  48. // must check this as often as possible, because this is
  49. // how we know if the user's pressed 'cancel'
  50. if (threadShouldExit())
  51. break;
  52. // this will update the progress bar on the dialog box
  53. setProgress (i / (double) thingsToDo);
  54. // ... do the business here...
  55. }
  56. }
  57. };
  58. void doTheTask()
  59. {
  60. MyTask m;
  61. if (m.runThread())
  62. {
  63. // thread finished normally..
  64. }
  65. else
  66. {
  67. // user pressed the cancel button..
  68. }
  69. }
  70. @endcode
  71. @see Thread, AlertWindow
  72. */
  73. class JUCE_API ThreadWithProgressWindow : public Thread,
  74. private Timer
  75. {
  76. public:
  77. //==============================================================================
  78. /** Creates the thread.
  79. Initially, the dialog box won't be visible, it'll only appear when the
  80. runThread() method is called.
  81. @param windowTitle the title to go at the top of the dialog box
  82. @param hasProgressBar whether the dialog box should have a progress bar (see
  83. setProgress() )
  84. @param hasCancelButton whether the dialog box should have a cancel button
  85. @param timeOutMsWhenCancelling when 'cancel' is pressed, this is how long to wait for
  86. the thread to stop before killing it forcibly (see
  87. Thread::stopThread() )
  88. */
  89. ThreadWithProgressWindow (const String& windowTitle,
  90. const bool hasProgressBar,
  91. const bool hasCancelButton,
  92. const int timeOutMsWhenCancelling = 10000,
  93. const String& cancelButtonText = T("Cancel"));
  94. /** Destructor. */
  95. ~ThreadWithProgressWindow();
  96. //==============================================================================
  97. /** Starts the thread and waits for it to finish.
  98. This will start the thread, make the dialog box appear, and wait until either
  99. the thread finishes normally, or until the cancel button is pressed.
  100. Before returning, the dialog box will be hidden.
  101. @param threadPriority the priority to use when starting the thread - see
  102. Thread::startThread() for values
  103. @returns true if the thread finished normally; false if the user pressed cancel
  104. */
  105. bool runThread (const int threadPriority = 5);
  106. /** The thread should call this periodically to update the position of the progress bar.
  107. @param newProgress the progress, from 0.0 to 1.0
  108. @see setStatusMessage
  109. */
  110. void setProgress (const double newProgress);
  111. /** The thread can call this to change the message that's displayed in the dialog box.
  112. */
  113. void setStatusMessage (const String& newStatusMessage);
  114. //==============================================================================
  115. juce_UseDebuggingNewOperator
  116. private:
  117. void timerCallback();
  118. double progress;
  119. AlertWindow alertWindow;
  120. String message;
  121. const int timeOutMsWhenCancelling;
  122. ThreadWithProgressWindow (const ThreadWithProgressWindow&);
  123. const ThreadWithProgressWindow& operator= (const ThreadWithProgressWindow&);
  124. };
  125. #endif // __JUCE_THREADWITHPROGRESSWINDOW_JUCEHEADER__