aboutsummaryrefslogtreecommitdiff
path: root/Histacom2.Engine/UI/IProgressBar.cs
blob: 2cd916d9dbb66aac4ba1b473cd1cd1c183c30c03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Histacom2.Engine.UI
{
    public class ProgressBar : Control
    {
        private Color _pColor = Color.DarkBlue;
        private ProgressBarStyle _style = ProgressBarStyle.Continuous;

        private Timer RedrawTimer = null;

        public Color ProgressColor
        {
            get
            {
                return _pColor;
            }
            set
            {
                _pColor = value; Refresh();
            }
        }


        public ProgressBarStyle Style
        {
            get
            {
                return _style;
            }
            set
            {
                _style = value; Refresh();
            }
        }


        private double _max = 100.0;
        private double _min = 0.0;

        public double Maximum
        {
            get
            {
                return _max;
            }
            set
            {
                _max = value; Refresh();
            }
        }
        public double Minimum
        {
            get
            {
                return _min;
            }
            set
            {
                _min = value; Refresh();
            }
        }


        private int _blockWidth = 14;

        public int BlockWidth
        {
            get
            {
                return _blockWidth;
            }
            set
            {
                _blockWidth = value;
                Refresh();
            }
        }

        private int _blockSpacing = 2;

        public int BlockSpacing
        {
            get
            {
                return _blockSpacing;
            }
            set
            {
                _blockSpacing = value;
                Refresh();
            }
        }

        public ProgressBar() : base()
        {
            MarqueeWidth = 125;
            BlockWidth = 20;
            BlockSpacing = 5;
            Minimum = 0.00;
            Maximum = 100.0;
            Style = ProgressBarStyle.Continuous;
            ProgressColor = Color.DarkBlue;
            RedrawTimer = new Timer();
            RedrawTimer.Tick += (o, a) =>
            {
                if (this.Style == ProgressBarStyle.Marquee)
                {
                    if (_marqueePos >= this.Width)
                        _marqueePos = 0;
                    else
                        _marqueePos++;
                    this.Refresh();
                }
            };
            RedrawTimer.Interval = 50;
        }

        private double _value = 0.00;

        public double Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (value < Minimum || value > Maximum)
                    throw new ArgumentOutOfRangeException("The value is outside the minimum and maximum range.");
                this.Refresh();
                _value = value;
            }
        }

        private int _marqueeWidth = 14;

        public int MarqueeWidth
        {
            get
            {
                return _marqueeWidth;
            }
            set
            {
                _marqueeWidth = value;
                Refresh();
            }
        }

        private bool _showText = false;

        public bool ShowText
        {
            get
            {
                return _showText;
            }
            set
            {
                _showText = value;
                Refresh();
            }
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            if (Visible)
                RedrawTimer.Start();
            else
                RedrawTimer.Stop();
        }

        private int _marqueePos = 0;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.Clear(BackColor);
            //Stolen from the ShiftOS code :3
            switch (Style)
            {
                case ProgressBarStyle.Continuous:
                    double width = linear(this.Value, this.Minimum, this.Maximum, 0, this.Width);
                    g.FillRectangle(new SolidBrush(ProgressColor), new RectangleF(0, 0, (float)width, this.Height));
                    break;
                case ProgressBarStyle.Blocks:
                    int block_count = this.Width / (this.BlockWidth + this.BlockSpacing);
                    int blocks = (int)linear(this.Value, this.Minimum, this.Maximum, 0, block_count);
                    for (int i = 0; i < blocks - 1; i++)
                    {
                        int position = i * (BlockWidth + BlockSpacing);
                        g.FillRectangle(new SolidBrush(ProgressColor), new Rectangle(position, 0, BlockWidth, this.Height));
                    }
                    break;
                case ProgressBarStyle.Marquee:
                    g.FillRectangle(new SolidBrush(ProgressColor), new Rectangle(_marqueePos, 0, MarqueeWidth, this.Height));
                    break;
            }
            if (ShowText)
            {
                var f = this.Font;
                var t = this.Text;
                var size = g.MeasureString(t, f);
                var loc = new PointF(
                        (this.Width - size.Width) / 2,
                        (this.Height - size.Height) / 2
                    );
                var color = this.ForeColor;
                g.DrawString(t, f, new SolidBrush(color), loc);
            }

        }

        ///<summary>
        /// Simple linear interpolation algorithm. (http://stackoverflow.com/questions/12838007/c-sharp-linear-interpolation)
        /// </summary>
        private double linear(double x, double x0, double x1, double y0, double y1)
        {
            if ((x1 - x0) == 0)
            {
                return (y0 + y1) / 2;
            }
            return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
        }
    }
}