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.

207 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A progress bar component.
  23. To use this, just create one and make it visible. It'll run its own timer to keep an eye on a
  24. variable that you give it, and will automatically redraw itself when the variable changes.
  25. Two styles of progress bars are supported: circular, and linear bar. If a style isn't given the
  26. look-and-feel will determine the style based on getDefaultProgressBarStyle().
  27. For an easy way of running a background task with a dialog box showing its progress, see
  28. the ThreadWithProgressWindow class.
  29. @see ThreadWithProgressWindow
  30. @tags{GUI}
  31. */
  32. class JUCE_API ProgressBar : public Component,
  33. public SettableTooltipClient,
  34. private Timer
  35. {
  36. public:
  37. /** The types of ProgressBar styles available.
  38. @see setStyle, getStyle, getResolvedStyle
  39. */
  40. enum class Style
  41. {
  42. linear, /**< A linear progress bar. */
  43. circular, /**< A circular progress indicator. */
  44. };
  45. //==============================================================================
  46. /** Creates a ProgressBar.
  47. The ProgressBar's style will initially be determined by the look-and-feel.
  48. @param progress pass in a reference to a double that you're going to
  49. update with your task's progress. The ProgressBar will
  50. monitor the value of this variable and will redraw itself
  51. when the value changes. The range is from 0 to 1.0 and JUCE
  52. LookAndFeel classes will draw a spinning animation for values
  53. outside this range. Obviously you'd better be careful not to
  54. delete this variable while the ProgressBar still exists!
  55. */
  56. explicit ProgressBar (double& progress);
  57. /** Creates a ProgressBar with a specific style.
  58. @param progress pass in a reference to a double that you're going to
  59. update with your task's progress. The ProgressBar will
  60. monitor the value of this variable and will redraw itself
  61. when the value changes. The range is from 0 to 1.0 and JUCE
  62. LookAndFeel classes will draw a spinning animation for values
  63. outside this range. Obviously you'd better be careful not to
  64. delete this variable while the ProgressBar still exists!
  65. @param style the style of the progress bar.
  66. */
  67. ProgressBar (double& progress, std::optional<Style> style);
  68. /** Destructor. */
  69. ~ProgressBar() override = default;
  70. //==============================================================================
  71. /** Turns the percentage display on or off.
  72. By default this is on, and the progress bar will display a text string showing
  73. its current percentage.
  74. */
  75. void setPercentageDisplay (bool shouldDisplayPercentage);
  76. /** Gives the progress bar a string to display inside it.
  77. If you call this, it will turn off the percentage display.
  78. @see setPercentageDisplay
  79. */
  80. void setTextToDisplay (const String& text);
  81. /** Sets the progress bar's current style.
  82. You can use this to force getResolvedStyle() to return a particular value.
  83. If a non-nullopt style is passed, that style will always be returned by
  84. getResolvedStyle(). Otherwise, if nullopt is passed, getResolvedStyle() will
  85. return its LookAndFeel's getDefaultProgressBarStyle().
  86. @see getStyle, getResolvedStyle
  87. */
  88. void setStyle (std::optional<Style> newStyle);
  89. /** Returns the progress bar's current style, as set in the constructor or in setStyle().
  90. @see setStyle, getResolvedStyle
  91. */
  92. std::optional<Style> getStyle() const { return style; }
  93. /** Returns the progress bar's current style if it has one, or a default style determined by
  94. the look-and-feel if it doesn't.
  95. Use this function in overrides of LookAndFeelMethods::drawProgressBar() in order to
  96. determine which style to draw.
  97. @see getStyle, setStyle, LookAndFeelMethods::getDefaultProgressBarStyle
  98. */
  99. Style getResolvedStyle() const;
  100. //==============================================================================
  101. /** A set of colour IDs to use to change the colour of various aspects of the bar.
  102. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  103. methods.
  104. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  105. */
  106. enum ColourIds
  107. {
  108. backgroundColourId = 0x1001900, /**< The background colour, behind the bar. */
  109. foregroundColourId = 0x1001a00, /**< The colour to use to draw the bar itself. LookAndFeel
  110. classes will probably use variations on this colour. */
  111. };
  112. //==============================================================================
  113. /** This abstract base class is implemented by LookAndFeel classes. */
  114. struct JUCE_API LookAndFeelMethods
  115. {
  116. virtual ~LookAndFeelMethods() = default;
  117. /** Draws a progress bar.
  118. If the progress value is less than 0 or greater than 1.0, this should draw a spinning
  119. bar that fills the whole space (i.e. to say that the app is still busy but the progress
  120. isn't known). It can use the current time as a basis for playing an animation.
  121. To determine which style of progress-bar to draw call getResolvedStyle().
  122. (Used by progress bars in AlertWindow).
  123. @see getResolvedStyle
  124. */
  125. virtual void drawProgressBar (Graphics&, ProgressBar&, int width, int height,
  126. double progress, const String& textToShow) = 0;
  127. virtual bool isProgressBarOpaque (ProgressBar&) = 0;
  128. /** Returns the default style a progress bar should use if one hasn't been set.
  129. @see setStyle, getResolvedStyle
  130. */
  131. virtual Style getDefaultProgressBarStyle (const ProgressBar&) = 0;
  132. };
  133. /** @internal */
  134. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
  135. protected:
  136. //==============================================================================
  137. /** @internal */
  138. void paint (Graphics&) override;
  139. /** @internal */
  140. void lookAndFeelChanged() override;
  141. /** @internal */
  142. void visibilityChanged() override;
  143. /** @internal */
  144. void colourChanged() override;
  145. private:
  146. double& progress;
  147. std::optional<Style> style;
  148. double currentValue { jlimit (0.0, 1.0, progress) };
  149. bool displayPercentage { true };
  150. String displayedMessage, currentMessage;
  151. uint32 lastCallbackTime { 0 };
  152. void timerCallback() override;
  153. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProgressBar)
  154. };
  155. } // namespace juce