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.

159 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_THREADWITHPROGRESSWINDOW_JUCEHEADER__
  19. #define __JUCE_THREADWITHPROGRESSWINDOW_JUCEHEADER__
  20. #include "../windows/juce_AlertWindow.h"
  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. */
  63. class JUCE_API ThreadWithProgressWindow : public Thread,
  64. private Timer
  65. {
  66. public:
  67. //==============================================================================
  68. /** Creates the thread.
  69. Initially, the dialog box won't be visible, it'll only appear when the
  70. runThread() method is called.
  71. @param windowTitle the title to go at the top of the dialog box
  72. @param hasProgressBar whether the dialog box should have a progress bar (see
  73. setProgress() )
  74. @param hasCancelButton whether the dialog box should have a cancel button
  75. @param timeOutMsWhenCancelling when 'cancel' is pressed, this is how long to wait for
  76. the thread to stop before killing it forcibly (see
  77. Thread::stopThread() )
  78. @param cancelButtonText the text that should be shown in the cancel button
  79. (if it has one)
  80. @param componentToCentreAround if this is non-null, the window will be positioned
  81. so that it's centred around this component.
  82. */
  83. ThreadWithProgressWindow (const String& windowTitle,
  84. bool hasProgressBar,
  85. bool hasCancelButton,
  86. int timeOutMsWhenCancelling = 10000,
  87. const String& cancelButtonText = "Cancel",
  88. Component* componentToCentreAround = nullptr);
  89. /** Destructor. */
  90. ~ThreadWithProgressWindow();
  91. //==============================================================================
  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 threadPriority 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 threadPriority = 5);
  101. /** The thread should call this periodically to update the position of the progress bar.
  102. @param newProgress the progress, from 0.0 to 1.0
  103. @see setStatusMessage
  104. */
  105. void setProgress (double newProgress);
  106. /** The thread can call this to change the message that's displayed in the dialog box.
  107. */
  108. void setStatusMessage (const String& newStatusMessage);
  109. /** Returns the AlertWindow that is being used.
  110. */
  111. AlertWindow* getAlertWindow() const noexcept { return alertWindow; }
  112. private:
  113. //==============================================================================
  114. void timerCallback();
  115. double progress;
  116. ScopedPointer <AlertWindow> alertWindow;
  117. String message;
  118. CriticalSection messageLock;
  119. const int timeOutMsWhenCancelling;
  120. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadWithProgressWindow);
  121. };
  122. #endif // __JUCE_THREADWITHPROGRESSWINDOW_JUCEHEADER__