-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircularProgressItem.cs
1307 lines (1194 loc) · 48.5 KB
/
CircularProgressItem.cs
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Threading;
using DevComponents.DotNetBar.Rendering;
namespace DevComponents.DotNetBar
{
/// <summary>
/// Represents circular progress indicator.
/// </summary>
[ToolboxItem(false), Designer(typeof(DevComponents.DotNetBar.Design.SimpleItemDesigner))]
public class CircularProgressItem : BaseItem
{
#region Events
#endregion
#region Constructor
/// <summary>
/// Creates new instance of circular progress indicator.
/// </summary>
public CircularProgressItem() : this("", "") { }
/// <summary>
/// Creates new instance of circular progress indicator and assigns the name to it.
/// </summary>
/// <param name="sItemName">Item name.</param>
public CircularProgressItem(string sItemName) : this(sItemName, "") { }
/// <summary>
/// Creates new instance of circular progress indicator and assigns the name and text to it.
/// </summary>
/// <param name="sItemName">Item name.</param>
/// <param name="ItemText">item text.</param>
public CircularProgressItem(string sItemName, string ItemText)
: base(sItemName, ItemText)
{
_SpokeAngles = GetSpokeAngles(_SpokeCount);
}
/// <summary>
/// Returns copy of the item.
/// </summary>
public override BaseItem Copy()
{
CircularProgressItem objCopy = new CircularProgressItem(m_Name);
this.CopyToItem(objCopy);
return objCopy;
}
/// <summary>
/// Copies the ProgressBarItem specific properties to new instance of the item.
/// </summary>
/// <param name="copy">New ProgressBarItem instance.</param>
internal void InternalCopyToItem(ProgressBarItem copy)
{
CopyToItem(copy);
}
/// <summary>
/// Copies the ProgressBarItem specific properties to new instance of the item.
/// </summary>
/// <param name="copy">New ProgressBarItem instance.</param>
protected override void CopyToItem(BaseItem copy)
{
CircularProgressItem objCopy = copy as CircularProgressItem;
base.CopyToItem(objCopy);
}
protected override void Dispose(bool disposing)
{
Stop();
base.Dispose(disposing);
}
#endregion
#region Implementation
public override void Paint(ItemPaintArgs e)
{
//e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
//e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
if (_ProgressBarType == eCircularProgressType.Line)
{
PaintLineProgressBar(e);
}
else if (_ProgressBarType == eCircularProgressType.Dot)
{
PaintDotProgressBar(e);
}
else if (_ProgressBarType == eCircularProgressType.Donut)
{
PaintDonutProgressBar(e);
}
else if (_ProgressBarType == eCircularProgressType.Spoke)
{
PaintSpokeProgressBar(e);
}
else if (_ProgressBarType == eCircularProgressType.Pie)
{
PaintPieProgressBar(e);
}
PaintLabel(e);
if (this.Focused && this.DesignMode)
{
Rectangle r = this.DisplayRectangle;
r.Inflate(-1, -1);
DesignTime.DrawDesignTimeSelection(e.Graphics, r, e.Colors.ItemDesignTimeBorder);
}
this.DrawInsertMarker(e.Graphics);
}
private void PaintLabel(ItemPaintArgs e)
{
if (!_TextVisible || string.IsNullOrEmpty(this.Text))
return;
Font font = e.Font;
Graphics g = e.Graphics;
Color textColor = GetTextColor(e);
Rectangle textBounds = Rectangle.Empty;
Rectangle bounds = m_Rect;
Rectangle progressBounds = GetProgressBarBounds();
eTextFormat format = eTextFormat.Default | eTextFormat.NoClipping;
if (_TextPosition == eTextPosition.Left)
{
textBounds = new Rectangle(bounds.X + _TextPadding.Left, bounds.Y + _TextPadding.Top,
m_Rect.Width - _TextPadding.Horizontal - TextContentSpacing - progressBounds.Width,
m_Rect.Height - _TextPadding.Vertical);
format |= eTextFormat.VerticalCenter;
}
else if (_TextPosition == eTextPosition.Right)
{
textBounds = new Rectangle(bounds.X + _TextPadding.Left + TextContentSpacing + progressBounds.Width, bounds.Y + _TextPadding.Top,
m_Rect.Width - _TextPadding.Horizontal - TextContentSpacing - progressBounds.Width,
m_Rect.Height - _TextPadding.Vertical);
format |= eTextFormat.VerticalCenter;
}
else if (_TextPosition == eTextPosition.Top)
{
textBounds = new Rectangle(bounds.X + _TextPadding.Left, bounds.Y + _TextPadding.Top,
m_Rect.Width - _TextPadding.Horizontal,
m_Rect.Height - _TextPadding.Vertical - progressBounds.Height - TextContentSpacing);
format |= eTextFormat.HorizontalCenter;
}
else if (_TextPosition == eTextPosition.Bottom)
{
textBounds = new Rectangle(bounds.X + _TextPadding.Left, bounds.Y + _TextPadding.Top + TextContentSpacing + progressBounds.Height,
m_Rect.Width - _TextPadding.Horizontal,
m_Rect.Height - _TextPadding.Vertical - progressBounds.Height - TextContentSpacing);
format |= eTextFormat.HorizontalCenter;
}
if (_TextWidth > 0)
{
textBounds.Width = _TextWidth;
format |= eTextFormat.WordBreak;
}
//g.FillRectangle(Brushes.WhiteSmoke, textBounds);
TextDrawing.DrawString(g, this.Text, font, textColor, textBounds, format);
}
private Color GetTextColor(ItemPaintArgs e)
{
if (!_TextColor.IsEmpty) return _TextColor;
return LabelItem.GetTextColor(e, this.EffectiveStyle, this.GetEnabled(), _TextColor);
}
private bool RenderesProgressText
{
get
{
return _ProgressTextVisible && (!_IsEndlessProgressBar || !string.IsNullOrEmpty(_ProgressText));
}
}
private void PaintPieProgressBar(ItemPaintArgs e)
{
Graphics g = e.Graphics;
Rectangle bounds = GetProgressBarBounds();
PointF centerPoint = new PointF(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
float penWidth = (float)Math.Max(1.5f, bounds.Height * .2);
bounds.Inflate(-1, -1);
bounds.Width--;
bounds.Height--;
float borderWidth = 1f;
if (bounds.Height > 31)
borderWidth = bounds.Height * .05f;
if (!_IsEndlessProgressBar)
{
int value = GetValue();
Rectangle pieBounds = bounds;
pieBounds.Inflate(-(int)borderWidth, -(int)borderWidth);
int sweepAngle = (int)(360 * ((double)value / Math.Max(1, (_Maximum - _Minimum))));
using (SolidBrush brush = new SolidBrush(_ProgressColor))
{
g.FillPie(brush, pieBounds, 270, sweepAngle);
}
}
else
{
Rectangle pieBounds = bounds;
pieBounds.Inflate(-(int)borderWidth, -(int)borderWidth);
int startAngle = (int)(360 * _EndlessProgressValue / (double)_SpokeCount);
int sweepAngle = 90;
using (SolidBrush brush = new SolidBrush(_ProgressColor))
{
g.FillPie(brush, pieBounds, startAngle, sweepAngle);
}
}
Rectangle borderBounds = bounds;
borderBounds.Width--;
borderBounds.Height--;
borderBounds.Offset(1, 1);
using (Pen pen = new Pen(_PieBorderDark, borderWidth))
{
pen.Alignment = PenAlignment.Inset;
g.DrawEllipse(pen, borderBounds);
}
borderBounds.Offset(-1, -1);
//using (Pen pen = new Pen(_PieBorderLight, borderWidth))
// g.DrawEllipse(pen, borderBounds);
using (Pen pen = new Pen(_PieBorderLight, borderWidth + .5f))
{
pen.Alignment = PenAlignment.Inset;
g.DrawEllipse(pen, borderBounds);
g.DrawEllipse(pen, borderBounds);
}
if (RenderesProgressText)
{
bounds.Offset(1, 0);
PaintProgressText(g, bounds, (int)(bounds.Height * .4f), e.Font);
}
}
private void PaintSpokeProgressBar(ItemPaintArgs e)
{
Graphics g = e.Graphics;
Rectangle bounds = GetProgressBarBounds();
// Account for border and shade
bounds.Width -= 2;
bounds.Height -= 2;
PointF centerPoint = new PointF(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
float penWidth = (float)Math.Max(1.5f, bounds.Height * .2);
//bounds.Inflate(-(int)(penWidth / 2), -(int)(penWidth / 2));
GraphicsPath clipPath = new GraphicsPath();
Rectangle clipPathEllipse = bounds;
clipPathEllipse.Inflate(-(int)((float)bounds.Height / 3f), -(int)((float)bounds.Height / 3f));
clipPath.AddEllipse(clipPathEllipse);
Region oldClip = g.Clip;
g.SetClip(clipPath, CombineMode.Exclude);
if (!_IsEndlessProgressBar)
{
int value = GetValue();
int sweepAngle = (int)(360 * ((double)value / Math.Max(1, (_Maximum - _Minimum))));
using (SolidBrush brush = new SolidBrush(_ProgressColor))
{
g.FillPie(brush, bounds, 270, sweepAngle);
}
}
else
{
int startAngle = (int)(360 * _EndlessProgressValue / (double)_SpokeCount);
int sweepAngle = 90;
using (SolidBrush brush = new SolidBrush(_ProgressColor))
{
g.FillPie(brush, bounds, startAngle, sweepAngle);
}
}
int radius = bounds.Width / 2;
PointF shadeCenterPoint = centerPoint;
float shadeSpokeWidth = 1f;
float circleWidth = 1f;
float shadeCircleWidth = 1f;
//if (bounds.Height < 28)
//{
// shadeSpokeWidth = 1f;
// circleWidth = 1f;
// shadeCircleWidth = 1f;
//}
radius--;
using (Pen pen = new Pen(_SpokeBorderDark, shadeSpokeWidth))
{
pen.Alignment = PenAlignment.Right;
PointF p1 = GetCoordinate(centerPoint, radius, 315); p1.X++;
PointF p2 = GetCoordinate(shadeCenterPoint, radius, 135); p2.X++;
g.DrawLine(pen, p1, p2); g.DrawLine(pen, p1, p2);
p1 = GetCoordinate(centerPoint, radius, 270); p1.X++;
p2 = GetCoordinate(shadeCenterPoint, radius, 90); p2.X++;
g.DrawLine(pen, p1, p2);
p1 = GetCoordinate(centerPoint, radius, 225); p1.Y++;
p2 = GetCoordinate(shadeCenterPoint, radius, 45); p2.Y++;
g.DrawLine(pen, p1, p2);
p1 = GetCoordinate(centerPoint, radius, 180); p1.Y++;
p2 = GetCoordinate(shadeCenterPoint, radius, 0); p2.Y++;
g.DrawLine(pen, p1, p2);
}
using (Pen pen = new Pen(_SpokeBorderDark, shadeCircleWidth))
{
pen.Alignment = PenAlignment.Inset;
Rectangle shadeBounds = bounds;
shadeBounds.Offset(1, 1);
//shadeBounds.Width--;
//shadeBounds.Height--;
g.DrawEllipse(pen, shadeBounds);
}
using (Pen pen = new Pen(_SpokeBorderLight, shadeSpokeWidth))
{
g.DrawLine(pen, GetCoordinate(centerPoint, radius, 315), GetCoordinate(centerPoint, radius, 135));
g.DrawLine(pen, GetCoordinate(centerPoint, radius, 270), GetCoordinate(centerPoint, radius, 90));
g.DrawLine(pen, GetCoordinate(centerPoint, radius, 225), GetCoordinate(centerPoint, radius, 45));
g.DrawLine(pen, GetCoordinate(centerPoint, radius, 180), GetCoordinate(centerPoint, radius, 0));
}
using (Pen pen = new Pen(_SpokeBorderLight, circleWidth))
{
pen.Alignment = PenAlignment.Inset;
g.DrawEllipse(pen, bounds);
g.DrawEllipse(pen, bounds);
}
g.Clip = oldClip;
oldClip.Dispose();
float innerCircleWidth = 1f;
using (Pen pen = new Pen(Color.White, innerCircleWidth))
{
pen.Alignment = PenAlignment.Inset;
g.DrawEllipse(pen, clipPathEllipse);
g.DrawEllipse(pen, clipPathEllipse);
}
if (RenderesProgressText)
{
//bounds.Y--;
PaintProgressText(g, bounds, (int)(bounds.Height * .35f), e.Font);
}
}
private int GetValue()
{
int value = Math.Min(_Maximum, Math.Max(_Minimum, _Value));
if (this.DesignMode && value == _Minimum) value = (int)(_Maximum * .75d);
return value;
}
private void PaintDonutProgressBar(ItemPaintArgs e)
{
Graphics g = e.Graphics;
RectangleF bounds = GetProgressBarBounds();
PointF centerPoint = new PointF(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
float penWidth = (float)Math.Max(1.5f, bounds.Height * .2);
bounds.Inflate(-Math.Max(1, penWidth / 2), -Math.Max(1, penWidth / 2));
bounds.Width--;
bounds.Height--;
if (!_IsEndlessProgressBar)
{
int value = GetValue();
int sweepAngle = (int)(360 * ((double)value / Math.Max(1, (_Maximum - _Minimum))));
using (Pen pen = new Pen(_ProgressColor, penWidth))
{
g.DrawArc(pen, bounds, 270, sweepAngle);
}
}
else
{
int startAngle = (int)(360 * _EndlessProgressValue / (double)_SpokeCount);
int sweepAngle = 140;
using (Pen pen = new Pen(_ProgressColor, penWidth))
{
pen.EndCap = LineCap.Round;
pen.StartCap = LineCap.Round;
g.DrawArc(pen, bounds, startAngle, sweepAngle);
}
}
if (RenderesProgressText)
{
PaintProgressText(g, bounds, (int)(bounds.Height * .4f), e.Font);
}
}
private void PaintDotProgressBar(ItemPaintArgs e)
{
Graphics g = e.Graphics;
Rectangle bounds = GetProgressBarBounds();
PointF centerPoint = new PointF(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
int outerRadius = 14;
int circleSize = 2;
outerRadius = (int)Math.Round(bounds.Width * .40d);
circleSize = Math.Max(1, (int)Math.Round(outerRadius * .25d));
int value = GetValue();
if (!_IsEndlessProgressBar)
{
int spoke = (int)Math.Round(_SpokeCount * ((double)value / Math.Max(1, (_Maximum - _Minimum))));
for (int i = 0; i < spoke; i++)
{
PointF endPoint = GetCoordinate(centerPoint, outerRadius, _SpokeAngles[i]);
RectangleF circleBounds = new RectangleF(endPoint, Size.Empty);
circleBounds.Inflate(circleSize, circleSize);
using (SolidBrush brush = new SolidBrush(ColorFromSpokeIndex(i)))
{
g.FillEllipse(brush, circleBounds);
}
}
}
else if (_IsRunning) // Endless Progress Bar
{
int position = _EndlessProgressValue;
for (int i = 0; i < _SpokeCount; i++)
{
position = position % _SpokeCount;
PointF endPoint = GetCoordinate(centerPoint, outerRadius, _SpokeAngles[position]);
RectangleF circleBounds = new RectangleF(endPoint, Size.Empty);
circleBounds.Inflate(circleSize, circleSize);
using (SolidBrush brush = new SolidBrush(ColorFromSpokeIndex(i)))
{
g.FillEllipse(brush, circleBounds);
}
position++;
}
}
if (RenderesProgressText)
{
PaintProgressText(g, bounds, (int)(bounds.Height * .35f), e.Font);
}
}
private int _SpokeCount = 12;
private int _EndlessProgressValue = 0;
private void PaintLineProgressBar(ItemPaintArgs e)
{
//e.Graphics.DrawRectangle(Pens.Green, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
Graphics g = e.Graphics;
Rectangle bounds = GetProgressBarBounds();
PointF centerPoint = new PointF(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
int innerRadius = 6;
int outerRadius = 14;
int spokeSize = 2;
outerRadius = (int)Math.Round(bounds.Width * .45d);
innerRadius = (int)Math.Round(outerRadius * .45d);
spokeSize = Math.Max(2, (int)Math.Round(outerRadius * .15d));
int value = GetValue();
if (!_IsEndlessProgressBar)
{
int spoke = (int)Math.Round(_SpokeCount * ((double)value / Math.Max(1, (_Maximum - _Minimum))));
for (int i = 0; i < spoke; i++)
{
PointF startPoint = GetCoordinate(centerPoint, innerRadius, _SpokeAngles[i]);
PointF endPoint = GetCoordinate(centerPoint, outerRadius, _SpokeAngles[i]);
using (Pen pen = new Pen(ColorFromSpokeIndex(i), spokeSize))
{
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
g.DrawLine(pen, startPoint, endPoint);
}
}
}
else if (_IsRunning) // Endless Progress Bar
{
int position = _EndlessProgressValue;
for (int i = 0; i < _SpokeCount; i++)
{
position = position % _SpokeCount;
PointF startPoint = GetCoordinate(centerPoint, innerRadius, _SpokeAngles[position]);
PointF endPoint = GetCoordinate(centerPoint, outerRadius, _SpokeAngles[position]);
using (Pen pen = new Pen(ColorFromSpokeIndex(i), spokeSize))
{
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
g.DrawLine(pen, startPoint, endPoint);
}
position++;
}
}
if (RenderesProgressText)
{
PaintProgressText(g, bounds, innerRadius, e.Font);
}
}
private string GetProgressValueText()
{
try
{
return string.Format(_ProgressTextFormat, _Value);
}
catch
{
return "Format Error";
}
}
private void PaintProgressText(Graphics g, RectangleF bounds, int innerRadius, Font baseFont)
{
//StringFormat format = (StringFormat)StringFormat.GenericDefault.Clone();
//format.Alignment = StringAlignment.Center;
//format.LineAlignment = StringAlignment.Center;
//format.FormatFlags = StringFormatFlags.NoWrap;
//format.Trimming = StringTrimming.None;
eTextFormat format = eTextFormat.HorizontalCenter | eTextFormat.VerticalCenter | eTextFormat.NoPadding | eTextFormat.SingleLine | eTextFormat.NoClipping;
float fontSize = Math.Max(1f, innerRadius / 1.8f);
Color textColor = _ProgressTextColor.IsEmpty ? _ProgressColor : _ProgressTextColor;
using (Font font = new Font(baseFont.FontFamily, fontSize, FontStyle.Regular))
{
TextDrawing.DrawString(g, GetProgressValueText(), font, textColor, Rectangle.Round(bounds), format);
//using (SolidBrush brush = new SolidBrush(textColor))
// g.DrawString(string.Format("{0}%", _Value), font, brush, bounds, format);
}
}
private Color ColorFromSpokeIndex(int spokeIndex, int spokeCount)
{
return Color.FromArgb((int)(210 * (double)spokeIndex / spokeCount) + 45, _ProgressColor);
}
private Color ColorFromSpokeIndex(int spokeIndex)
{
return ColorFromSpokeIndex(spokeIndex, _SpokeCount);
}
private PointF GetCoordinate(PointF centerPoint, int radius, double angle)
{
double dblAngle = Math.PI * angle / 180;
return new PointF(centerPoint.X + radius * (float)Math.Cos(dblAngle),
centerPoint.Y + radius * (float)Math.Sin(dblAngle));
}
internal static readonly int TextContentSpacing = 3;
private Size _TextSize = Size.Empty;
public override void RecalcSize()
{
Rectangle r = new Rectangle(m_Rect.X, m_Rect.Y, _Diameter, _Diameter);
if (_TextVisible && !string.IsNullOrEmpty(this.Text))
{
Control parent = this.ContainerControl as Control;
if (parent != null)
{
Font font = parent.Font;
using (Graphics g = parent.CreateGraphics())
{
Size textSize = ButtonItemLayout.MeasureItemText(this, g, _TextWidth, font, (_TextWidth > 0 ? eTextFormat.WordBreak : eTextFormat.SingleLine), parent.RightToLeft == RightToLeft.Yes);
_TextSize = textSize;
textSize.Width += _TextPadding.Horizontal;
textSize.Height += _TextPadding.Vertical;
if (_TextPosition == eTextPosition.Left || _TextPosition == eTextPosition.Right)
{
textSize.Width += TextContentSpacing;
r.Width += textSize.Width;
r.Height = Math.Max(r.Height, textSize.Height);
}
else
{
textSize.Height += TextContentSpacing;
r.Height += textSize.Height;
r.Width = Math.Max(r.Width, textSize.Width);
}
}
}
}
m_Rect = r;
base.RecalcSize();
}
private Rectangle _ProgressBarBounds = Rectangle.Empty;
private Rectangle GetProgressBarBounds()
{
if (string.IsNullOrEmpty(Text) || !_TextVisible)
{
return new Rectangle(m_Rect.X + (m_Rect.Width - _Diameter) / 2, m_Rect.Y + (m_Rect.Height - _Diameter) / 2, _Diameter, _Diameter);
}
if (_TextPosition == eTextPosition.Top)
{
return new Rectangle(m_Rect.X + (m_Rect.Width - _Diameter) / 2, m_Rect.Y + (m_Rect.Height - _Diameter), _Diameter, _Diameter);
}
else if (_TextPosition == eTextPosition.Right)
{
return new Rectangle(m_Rect.X, m_Rect.Y + (m_Rect.Height - _Diameter) / 2, _Diameter, _Diameter);
}
else if (_TextPosition == eTextPosition.Left)
{
return new Rectangle(m_Rect.Right - _Diameter,
m_Rect.Y + (m_Rect.Height - _Diameter) / 2,
_Diameter, _Diameter);
}
else if (_TextPosition == eTextPosition.Bottom)
{
return new Rectangle(m_Rect.X + (m_Rect.Width - _Diameter) / 2, m_Rect.Y, _Diameter, _Diameter);
}
return new Rectangle(m_Rect.X, m_Rect.Y, _Diameter, _Diameter);
}
private double[] _SpokeAngles = null;
private double[] GetSpokeAngles(int numberOfSpokes)
{
double[] angles = new double[numberOfSpokes];
double angleStep = 360d / numberOfSpokes;
for (int i = 0; i < numberOfSpokes; i++)
angles[i] = (i == 0 ? 270 + angleStep : angles[i - 1] + angleStep);
return angles;
}
private eCircularProgressType _ProgressBarType = eCircularProgressType.Line;
/// <summary>
/// Gets or sets the circular progress bar type.
/// </summary>
[DefaultValue(eCircularProgressType.Line), Category("Appearance"), Description("Indicates circular progress bar type.")]
public eCircularProgressType ProgressBarType
{
get { return _ProgressBarType; }
set
{
if (value != _ProgressBarType)
{
eCircularProgressType oldValue = _ProgressBarType;
_ProgressBarType = value;
OnProgressBarTypeChanged(oldValue, value);
}
}
}
private void OnProgressBarTypeChanged(eCircularProgressType oldValue, eCircularProgressType newValue)
{
OnPropertyChanged(new PropertyChangedEventArgs("ProgressBarType"));
this.Refresh();
}
private int _Maximum = 100;
/// <summary>
/// Gets or sets the maximum value of the progress bar.
/// </summary>
[Description("Indicates maximum value of the progress bar."), Category("Behavior"), DefaultValue(100)]
public int Maximum
{
get { return _Maximum; }
set
{
if (value != _Maximum)
{
int oldValue = _Maximum;
_Maximum = value;
OnMaximumChanged(oldValue, value);
}
}
}
private void OnMaximumChanged(int oldValue, int newValue)
{
OnPropertyChanged(new PropertyChangedEventArgs("Maximum"));
CoerceValue();
}
private int _Minimum = 0;
/// <summary>
/// Gets or sets the minimum value of the progress bar.
/// </summary>
[Description("Indicates minimum value of the progress bar."), Category("Behavior"), DefaultValue(0)]
public int Minimum
{
get { return _Minimum; }
set
{
if (value != _Minimum)
{
int oldValue = _Minimum;
_Minimum = value;
OnMinimumChanged(oldValue, value);
}
}
}
private void OnMinimumChanged(int oldValue, int newValue)
{
OnPropertyChanged(new PropertyChangedEventArgs("Minimum"));
CoerceValue();
}
private void CoerceValue()
{
int newValue = _Value;
if (_Value < _Minimum)
newValue = _Minimum;
else if (_Value > _Maximum)
newValue = _Maximum;
Value = newValue;
}
private Color _ProgressTextColor = Color.Empty;
/// <summary>
/// Gets or sets the color of the progress percentage text.
/// </summary>
[Category("Appearance"), Description("Indicates color of progress percentage text")]
public Color ProgressTextColor
{
get { return _ProgressTextColor; }
set { _ProgressTextColor = value; this.Refresh(); }
}
/// <summary>
/// Gets whether property should be serialized.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeProgressTextColor()
{
return !_ProgressTextColor.IsEmpty;
}
/// <summary>
/// Resets property to its default value.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public void ResetProgressTextColor()
{
this.ProgressTextColor = Color.Empty;
}
private bool _ProgressTextVisible = false;
/// <summary>
/// Gets or sets whether text that displays the progress bar completion percentage text is visible. Default value is false.
/// </summary>
[DefaultValue(false), Category("Appearance"), Description("Indicates whether text that displays the progress bar completion percentage text is visible")]
public bool ProgressTextVisible
{
get { return _ProgressTextVisible; }
set
{
if (value != _ProgressTextVisible)
{
bool oldValue = _ProgressTextVisible;
_ProgressTextVisible = value;
OnProgressTextVisibleChanged(oldValue, value);
}
}
}
private void OnProgressTextVisibleChanged(bool oldValue, bool newValue)
{
OnPropertyChanged(new PropertyChangedEventArgs("ProgressTextVisible"));
this.Refresh();
}
private string _ProgressText = "";
/// <summary>
/// Gets or sets the text displayed on top of the circular progress bar.
/// </summary>
[DefaultValue(""), Category("Appearance"), Description("Indicates text displayed on top of the circular progress bar.")]
public string ProgressText
{
get { return _ProgressText; }
set
{
if (value != _ProgressText)
{
string oldValue = _ProgressText;
_ProgressText = value;
OnProgressTextChanged(oldValue, value);
}
}
}
private void OnProgressTextChanged(string oldValue, string newValue)
{
//OnPropertyChanged(new PropertyChangedEventArgs("ProgressText"));
Refresh();
}
private int _Value;
/// <summary>
/// Gets or sets the current value of the progress bar.
/// </summary>
[Description("Indicates current value of the progress bar."), Category("Behavior"), DefaultValue(0)]
public int Value
{
get { return _Value; }
set
{
value = Math.Min(_Maximum, Math.Max(value, _Minimum));
if (value != _Value)
{
int oldValue = _Value;
_Value = value;
OnValueChanged(oldValue, value);
}
}
}
private void OnValueChanged(int oldValue, int newValue)
{
if (!_IsEndlessProgressBar)
this.Refresh();
OnValueChanged(EventArgs.Empty);
OnPropertyChanged(new PropertyChangedEventArgs("Value"));
}
/// <summary>
/// Occurs when Value property has changed.
/// </summary>
public event EventHandler ValueChanged;
/// <summary>
/// Raises ValueChanged event.
/// </summary>
/// <param name="e">Provides event arguments.</param>
protected virtual void OnValueChanged(EventArgs e)
{
EventHandler handler = ValueChanged;
if (handler != null)
handler(this, e);
}
/// <summary>
/// Called when property on CircularProgressBar changes.
/// </summary>
/// <param name="propertyChangedEventArgs">Property Change Arguments</param>
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
}
private void MoveEndlessProgressBar()
{
if (!this.IsRunning) return;
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate()
{
MoveEndlessProgressBar();
}));
return;
}
_EndlessProgressValue = ++_EndlessProgressValue % _SpokeCount;
this.Refresh();
Control container = this.ContainerControl as Control;
if (container != null)
container.Update();
}
private bool _IsEndlessProgressBar = false;
private BackgroundWorker _LoopWorker = null;
/// <summary>
/// Starts the progress bar loop for endless type progress bar. Progress bar will continue to run until Stop() method is called.
/// </summary>
public void Start()
{
if (_IsRunning) return;
_IsEndlessProgressBar = true;
_IsRunning = true;
_LoopWorker = new BackgroundWorker();
_LoopWorker.WorkerSupportsCancellation = true;
_LoopWorker.DoWork += LoopWorkerDoWork;
_LoopWorker.RunWorkerAsync();
}
void LoopWorkerDoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
while (!worker.CancellationPending)
{
MoveEndlessProgressBar();
Thread.Sleep(_AnimationSpeed);
}
}
/// <summary>
/// Stops the progress bar loop for endless type progress bar.
/// </summary>
public void Stop()
{
if (!_IsRunning) return;
_IsEndlessProgressBar = false;
_IsRunning = false;
BackgroundWorker worker = _LoopWorker;
_LoopWorker = null;
worker.CancelAsync();
worker.DoWork -= LoopWorkerDoWork;
worker.Dispose();
this.Refresh();
}
private bool _IsRunning = false;
/// <summary>
/// Gets or sets whether endless type progress bar is running.
/// </summary>
[Browsable(false), DefaultValue(false)]
public bool IsRunning
{
get { return _IsRunning; }
set
{
if (_IsRunning != value)
{
if (value)
Start();
else
Stop();
}
}
}
private static readonly Color DefaultProgressColor = Color.DarkSlateGray;//Color.FromArgb(143, 223, 95);
private Color _ProgressColor = DefaultProgressColor;
/// <summary>
/// Gets or sets the color of the color of progress indicator.
/// </summary>
[Category("Columns"), Description("Indicates color of progress indicator.")]
public Color ProgressColor
{
get { return _ProgressColor; }
set { _ProgressColor = value; this.Refresh(); }
}
/// <summary>
/// Gets whether property should be serialized.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeProgressColor()
{
return _ProgressColor != DefaultProgressColor;
}
/// <summary>
/// Resets property to its default value.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public void ResetProgressColor()
{
this.ProgressColor = DefaultProgressColor;
}
private int _Diameter = 24;
/// <summary>
/// Gets or sets circular progress indicator diameter in pixels.
/// </summary>
[DefaultValue(24), Category("Appearance"), Description("Indicates circular progress indicator diameter in pixels.")]
public int Diameter
{
get { return _Diameter; }
set
{
if (value != _Diameter)
{
int oldValue = _Diameter;
_Diameter = value;
OnDiameterChanged(oldValue, value);
}
}
}
private void OnDiameterChanged(int oldValue, int newValue)
{
OnPropertyChanged(new PropertyChangedEventArgs("Diameter"));
NeedRecalcSize = true;
this.Refresh();
}
private eTextPosition _TextPosition = eTextPosition.Left;
/// <summary>
/// Gets or sets the text position in relation to the circular progress indicator.
/// </summary>
[DefaultValue(eTextPosition.Left), Category("Appearance"), Description("Indicatesd text position in relation to the circular progress indicator.")]
public eTextPosition TextPosition
{