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.

113 lines
3.2KB

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