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.

114 lines
3.0KB

  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. ProgressBar::ProgressBar (double& progress_)
  20. : progress (progress_),
  21. displayPercentage (true),
  22. lastCallbackTime (0)
  23. {
  24. currentValue = jlimit (0.0, 1.0, progress);
  25. }
  26. ProgressBar::~ProgressBar()
  27. {
  28. }
  29. //==============================================================================
  30. void ProgressBar::setPercentageDisplay (const bool shouldDisplayPercentage)
  31. {
  32. displayPercentage = shouldDisplayPercentage;
  33. repaint();
  34. }
  35. void ProgressBar::setTextToDisplay (const String& text)
  36. {
  37. displayPercentage = false;
  38. displayedMessage = text;
  39. }
  40. void ProgressBar::lookAndFeelChanged()
  41. {
  42. setOpaque (getLookAndFeel().isProgressBarOpaque (*this));
  43. }
  44. void ProgressBar::colourChanged()
  45. {
  46. lookAndFeelChanged();
  47. }
  48. void ProgressBar::paint (Graphics& g)
  49. {
  50. String text;
  51. if (displayPercentage)
  52. {
  53. if (currentValue >= 0 && currentValue <= 1.0)
  54. text << roundToInt (currentValue * 100.0) << '%';
  55. }
  56. else
  57. {
  58. text = displayedMessage;
  59. }
  60. getLookAndFeel().drawProgressBar (g, *this,
  61. getWidth(), getHeight(),
  62. currentValue, text);
  63. }
  64. void ProgressBar::visibilityChanged()
  65. {
  66. if (isVisible())
  67. startTimer (30);
  68. else
  69. stopTimer();
  70. }
  71. void ProgressBar::timerCallback()
  72. {
  73. double newProgress = progress;
  74. const uint32 now = Time::getMillisecondCounter();
  75. const int timeSinceLastCallback = (int) (now - lastCallbackTime);
  76. lastCallbackTime = now;
  77. if (currentValue != newProgress
  78. || newProgress < 0 || newProgress >= 1.0
  79. || currentMessage != displayedMessage)
  80. {
  81. if (currentValue < newProgress
  82. && newProgress >= 0 && newProgress < 1.0
  83. && currentValue >= 0 && currentValue < 1.0)
  84. {
  85. newProgress = jmin (currentValue + 0.0008 * timeSinceLastCallback,
  86. newProgress);
  87. }
  88. currentValue = newProgress;
  89. currentMessage = displayedMessage;
  90. repaint();
  91. }
  92. }