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.

156 lines
5.7KB

  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_JUCEHEADER__
  18. #define __JUCE_THREADWITHPROGRESSWINDOW_JUCEHEADER__
  19. #include "../windows/juce_AlertWindow.h"
  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)
  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 = "Cancel",
  87. Component* componentToCentreAround = nullptr);
  88. /** Destructor. */
  89. ~ThreadWithProgressWindow();
  90. //==============================================================================
  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. /** The thread should call this periodically to update the position of the progress bar.
  101. @param newProgress the progress, from 0.0 to 1.0
  102. @see setStatusMessage
  103. */
  104. void setProgress (double newProgress);
  105. /** The thread can call this to change the message that's displayed in the dialog box. */
  106. void setStatusMessage (const String& newStatusMessage);
  107. /** Returns the AlertWindow that is being used. */
  108. AlertWindow* getAlertWindow() const noexcept { return alertWindow; }
  109. private:
  110. //==============================================================================
  111. void timerCallback() override;
  112. double progress;
  113. ScopedPointer <AlertWindow> alertWindow;
  114. String message;
  115. CriticalSection messageLock;
  116. const int timeOutMsWhenCancelling;
  117. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadWithProgressWindow)
  118. };
  119. #endif // __JUCE_THREADWITHPROGRESSWINDOW_JUCEHEADER__