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.h 5.6KB

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