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.

174 lines
6.6KB

  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). Leave this empty for the default "Cancel"
  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 = String(),
  86. Component* componentToCentreAround = nullptr);
  87. /** Destructor. */
  88. ~ThreadWithProgressWindow();
  89. //==============================================================================
  90. #if JUCE_MODAL_LOOPS_PERMITTED
  91. /** Starts the thread and waits for it to finish.
  92. This will start the thread, make the dialog box appear, and wait until either
  93. the thread finishes normally, or until the cancel button is pressed.
  94. Before returning, the dialog box will be hidden.
  95. @param threadPriority the priority to use when starting the thread - see
  96. Thread::startThread() for values
  97. @returns true if the thread finished normally; false if the user pressed cancel
  98. */
  99. bool runThread (int threadPriority = 5);
  100. #endif
  101. /** Starts the thread and returns.
  102. This will start the thread and make the dialog box appear in a modal state. When
  103. the thread finishes normally, or the cancel button is pressed, the window will be
  104. hidden and the threadComplete() method will be called.
  105. @param threadPriority the priority to use when starting the thread - see
  106. Thread::startThread() for values
  107. */
  108. void launchThread (int threadPriority = 5);
  109. /** The thread should call this periodically to update the position of the progress bar.
  110. @param newProgress the progress, from 0.0 to 1.0
  111. @see setStatusMessage
  112. */
  113. void setProgress (double newProgress);
  114. /** The thread can call this to change the message that's displayed in the dialog box. */
  115. void setStatusMessage (const String& newStatusMessage);
  116. /** Returns the AlertWindow that is being used. */
  117. AlertWindow* getAlertWindow() const noexcept { return alertWindow; }
  118. //==============================================================================
  119. /** This method is called (on the message thread) when the operation has finished.
  120. You may choose to use this callback to delete the ThreadWithProgressWindow object.
  121. */
  122. virtual void threadComplete (bool userPressedCancel);
  123. private:
  124. //==============================================================================
  125. void timerCallback() override;
  126. double progress;
  127. ScopedPointer<AlertWindow> alertWindow;
  128. String message;
  129. CriticalSection messageLock;
  130. const int timeOutMsWhenCancelling;
  131. bool wasCancelledByUser;
  132. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadWithProgressWindow)
  133. };
  134. #endif // JUCE_THREADWITHPROGRESSWINDOW_H_INCLUDED