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.

171 lines
6.4KB

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