PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 1ms

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