PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/GammaJul.LgLcd/LcdGdiProgressBar.cs

#
C# | 147 lines | 97 code | 13 blank | 37 comment | 19 complexity | b539691590c5eddbd2f7c4c8830a14c4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Drawing;
  3. namespace GammaJul.LgLcd {
  4. /// <summary>
  5. /// Represents a progress bar on a <see cref="LcdGdiPage"/>.
  6. /// </summary>
  7. public class LcdGdiProgressBar : LcdGdiObject {
  8. private Brush _progressBrush;
  9. private int _minimum;
  10. private int _value;
  11. private int _maximum = 100;
  12. private bool _isVertical;
  13. /// <summary>
  14. /// Gets or sets the brush that is used to fill the progress indicator.
  15. /// <see cref="Brush"/> is used as a background to the bar,
  16. /// while <see cref="ProgressBrush"/> represents the progress part.
  17. /// </summary>
  18. public Brush ProgressBrush {
  19. get { return _progressBrush; }
  20. set {
  21. if (_progressBrush != value) {
  22. _progressBrush = value;
  23. HasChanged = true;
  24. }
  25. }
  26. }
  27. /// <summary>
  28. /// Gets or sets the minimum value of this progress bar.
  29. /// </summary>
  30. public int Minimum {
  31. get { return _minimum; }
  32. set {
  33. int newValue = Math.Min(value, _maximum);
  34. if (_minimum != newValue) {
  35. _minimum = newValue;
  36. _value = Math.Max(_value, newValue);
  37. HasChanged = true;
  38. }
  39. }
  40. }
  41. /// <summary>
  42. /// Gets or sets the maximum value of this progress bar.
  43. /// </summary>
  44. public int Maximum {
  45. get { return _maximum; }
  46. set {
  47. int newValue = Math.Max(value, _minimum);
  48. if (_maximum != newValue) {
  49. _maximum = newValue;
  50. _value = Math.Min(_value, newValue);
  51. HasChanged = true;
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// Gets or sets the current value of this progress bar.
  57. /// </summary>
  58. public int Value {
  59. get { return _value; }
  60. set {
  61. int newValue = Math.Max(Math.Min(value, _maximum), _minimum);
  62. if (_value != newValue) {
  63. _value = newValue;
  64. HasChanged = true;
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// Gets or sets whether this progress bar is vertical.
  70. /// The default value is <c>false</c>.
  71. /// </summary>
  72. public bool IsVertical {
  73. get { return _isVertical; }
  74. set {
  75. if (_isVertical != value) {
  76. _isVertical = value;
  77. HasChanged = true;
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// Draws the progress bar.
  83. /// </summary>
  84. /// <param name="page">Page where this object will be drawn.</param>
  85. /// <param name="graphics"><see cref="Graphics"/> to use for drawing.</param>
  86. protected internal override void Draw(LcdGdiPage page, Graphics graphics) {
  87. if (Brush != null)
  88. graphics.FillRectangle(Brush, AbsolutePosition.X, AbsolutePosition.Y, FinalSize.Width - 1.0f, FinalSize.Height - 1.0f);
  89. float penWidth;
  90. if (Pen != null) {
  91. graphics.DrawRectangle(Pen, AbsolutePosition.X, AbsolutePosition.Y, FinalSize.Width - 1.0f, FinalSize.Height - 1.0f);
  92. penWidth = Pen.Width;
  93. }
  94. else
  95. penWidth = 0.0f;
  96. if (_progressBrush != null) {
  97. int zeroBasedMaximum = _maximum - _minimum;
  98. int zeroBasedValue = _value - _minimum;
  99. float percent = zeroBasedMaximum == 0 ? 0.0f : zeroBasedValue / (float) zeroBasedMaximum;
  100. if (_isVertical) {
  101. float indicatorHeight = (FinalSize.Height - penWidth * 2) * percent;
  102. graphics.FillRectangle(_progressBrush, AbsolutePosition.X + penWidth,
  103. AbsolutePosition.Y + FinalSize.Height - penWidth - indicatorHeight,
  104. FinalSize.Width - penWidth * 2, indicatorHeight);
  105. }
  106. else {
  107. graphics.FillRectangle(_progressBrush, AbsolutePosition.X + penWidth, AbsolutePosition.Y + penWidth,
  108. (FinalSize.Width - penWidth * 2) * percent, FinalSize.Height - penWidth * 2);
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// Creates a new <see cref="LcdGdiProgressBar"/> with a black edge, white fill
  114. /// and black indicator part, which is the most common for monochrome devices.
  115. /// </summary>
  116. public LcdGdiProgressBar() {
  117. Pen = Pens.Black;
  118. Brush = Brushes.White;
  119. ProgressBrush = Brushes.Black;
  120. }
  121. /// <summary>
  122. /// Creates a new <see cref="LcdGdiProgressBar"/> with the specified pen for the edge,
  123. /// brush for the fill, and brush for the progression.
  124. /// </summary>
  125. /// <param name="pen">Pen to use to draw the edge of this object.</param>
  126. /// <param name="brush">Brush to use to draw the fill of this object.</param>
  127. /// <param name="progressBrush">Brush to use to fill the progress indicator.</param>
  128. public LcdGdiProgressBar(Pen pen, Brush brush, Brush progressBrush) {
  129. Pen = pen;
  130. Brush = brush;
  131. ProgressBrush = progressBrush;
  132. }
  133. }
  134. }