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.

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