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.

177 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A thread that automatically pops up a modal dialog box with a progress bar
  24. and cancel button while it's busy running.
  25. These are handy for performing some sort of task while giving the user feedback
  26. about how long there is to go, etc.
  27. E.g. @code
  28. class MyTask : public ThreadWithProgressWindow
  29. {
  30. public:
  31. MyTask() : ThreadWithProgressWindow ("busy...", true, true)
  32. {
  33. }
  34. void run()
  35. {
  36. for (int i = 0; i < thingsToDo; ++i)
  37. {
  38. // must check this as often as possible, because this is
  39. // how we know if the user's pressed 'cancel'
  40. if (threadShouldExit())
  41. break;
  42. // this will update the progress bar on the dialog box
  43. setProgress (i / (double) thingsToDo);
  44. // ... do the business here...
  45. }
  46. }
  47. };
  48. void doTheTask()
  49. {
  50. MyTask m;
  51. if (m.runThread())
  52. {
  53. // thread finished normally..
  54. }
  55. else
  56. {
  57. // user pressed the cancel button..
  58. }
  59. }
  60. @endcode
  61. @see Thread, AlertWindow
  62. @tags{GUI}
  63. */
  64. class JUCE_API ThreadWithProgressWindow : public Thread,
  65. private Timer
  66. {
  67. public:
  68. //==============================================================================
  69. /** Creates the thread.
  70. Initially, the dialog box won't be visible, it'll only appear when the
  71. runThread() method is called.
  72. @param windowTitle the title to go at the top of the dialog box
  73. @param hasProgressBar whether the dialog box should have a progress bar (see
  74. setProgress() )
  75. @param hasCancelButton whether the dialog box should have a cancel button
  76. @param timeOutMsWhenCancelling when 'cancel' is pressed, this is how long to wait for
  77. the thread to stop before killing it forcibly (see
  78. Thread::stopThread() )
  79. @param cancelButtonText the text that should be shown in the cancel button
  80. (if it has one). Leave this empty for the default "Cancel"
  81. @param componentToCentreAround if this is non-null, the window will be positioned
  82. so that it's centred around this component.
  83. */
  84. ThreadWithProgressWindow (const String& windowTitle,
  85. bool hasProgressBar,
  86. bool hasCancelButton,
  87. int timeOutMsWhenCancelling = 10000,
  88. const String& cancelButtonText = String(),
  89. Component* componentToCentreAround = nullptr);
  90. /** Destructor. */
  91. ~ThreadWithProgressWindow();
  92. //==============================================================================
  93. #if JUCE_MODAL_LOOPS_PERMITTED
  94. /** Starts the thread and waits for it to finish.
  95. This will start the thread, make the dialog box appear, and wait until either
  96. the thread finishes normally, or until the cancel button is pressed.
  97. Before returning, the dialog box will be hidden.
  98. @param priority the priority to use when starting the thread - see
  99. Thread::startThread() for values
  100. @returns true if the thread finished normally; false if the user pressed cancel
  101. */
  102. bool runThread (int priority = 5);
  103. #endif
  104. /** Starts the thread and returns.
  105. This will start the thread and make the dialog box appear in a modal state. When
  106. the thread finishes normally, or the cancel button is pressed, the window will be
  107. hidden and the threadComplete() method will be called.
  108. @param priority the priority to use when starting the thread - see
  109. Thread::startThread() for values
  110. */
  111. void launchThread (int priority = 5);
  112. /** The thread should call this periodically to update the position of the progress bar.
  113. @param newProgress the progress, from 0.0 to 1.0
  114. @see setStatusMessage
  115. */
  116. void setProgress (double newProgress);
  117. /** The thread can call this to change the message that's displayed in the dialog box. */
  118. void setStatusMessage (const String& newStatusMessage);
  119. /** Returns the AlertWindow that is being used. */
  120. AlertWindow* getAlertWindow() const noexcept { return alertWindow.get(); }
  121. //==============================================================================
  122. /** This method is called (on the message thread) when the operation has finished.
  123. You may choose to use this callback to delete the ThreadWithProgressWindow object.
  124. */
  125. virtual void threadComplete (bool userPressedCancel);
  126. private:
  127. //==============================================================================
  128. void timerCallback() override;
  129. double progress;
  130. std::unique_ptr<AlertWindow> alertWindow;
  131. String message;
  132. CriticalSection messageLock;
  133. const int timeOutMsWhenCancelling;
  134. bool wasCancelledByUser;
  135. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadWithProgressWindow)
  136. };
  137. } // namespace juce