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.

118 lines
3.3KB

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