summaryrefslogtreecommitdiff
path: root/src/gui/gui_progressbar.pas
blob: 4717efaa8964e1975fcf388e24ca77bee1186d55 (plain)
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
unit gui_progressbar;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  gfxbase,
  fpgfx,
  gfx_widget;
  
type
  TfpgCustomProgressBar = class(TfpgWidget)
  private
    FBackgroundColor: TfpgColor;
    FMax: longint;
    FMin: longint;
    FPosition: longint;
    FShowCaption: boolean;
    FStep: longint;
    FFont: TfpgFont;
    procedure   SetBackgroundColor(const AValue: TfpgColor);
    procedure   SetMax(const AValue: longint);
    procedure   SetMin(const AValue: longint);
    procedure   SetPosition(const AValue: longint);
    procedure   SetShowCaption(const AValue: boolean);
    procedure   SetStep(const AValue: longint);
  protected
    procedure   HandlePaint; override;
    property    Max: longint read FMax write SetMax;
    property    Min: longint read FMin write SetMin;
    property    Position: longint read FPosition write SetPosition;
    property    Step: longint read FStep write SetStep;
//    property    FontName: string read GetFontName write SetFontName;
    property    BackgroundColor: TfpgColor read FBackgroundColor write SetBackgroundColor;
    property    ShowCaption: boolean read FShowCaption write SetShowCaption;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    procedure   StepIt;
    procedure   StepBy(AStep: integer);
    property    Font: TfpgFont read FFont;
  end;
  
  
  TfpgProgressBar = class(TfpgCustomProgressBar)
  published
    property    BackgroundColor;
    property    ShowCaption;
    property    Max;
    property    Min;
    property    Position;
    property    Step;
  end;

implementation

{ TfpgCustomProgressBar }

procedure TfpgCustomProgressBar.SetMax(const AValue: longint);
begin
  if FMax = AValue then
    Exit; //==>

  // correct wrong inputs
  if FMin > AValue then
    FMin := AValue - 1;
  if FPosition > AValue then
    FPosition := AValue;

  FMax := AValue;
	RePaint;
end;

procedure TfpgCustomProgressBar.SetBackgroundColor(const AValue: TfpgColor);
begin
  if FBackgroundColor = AValue then
    Exit; //==>
  FBackgroundColor := AValue;
  RePaint;
end;

procedure TfpgCustomProgressBar.SetMin(const AValue: longint);
begin
  if FMin = AValue then
    Exit; //==>

  // correct wrong inputs
  if AValue > FPosition then
    FPosition := AValue;
  if AValue > FMax then
    FMax := AValue+1;

  FMin := AValue;
	RePaint;
end;

procedure TfpgCustomProgressBar.SetPosition(const AValue: longint);
begin
  if FPosition = AValue then
    Exit; //==>

  // correct limits
  if AValue < Min then
    FPosition := Min
  else if AValue > Max then
    FPosition := Max
  else
    FPosition := AValue;

	RePaint;
end;

procedure TfpgCustomProgressBar.SetShowCaption(const AValue: boolean);
begin
  if FShowCaption = AValue then
    Exit; //==>
  FShowCaption := AValue;
  RePaint;
end;

procedure TfpgCustomProgressBar.SetStep(const AValue: longint);
begin
  if AValue < 1 then
    Exit; //==>
  if FStep = AValue then
    Exit; //==>
  FStep := AValue;
end;

procedure TfpgCustomProgressBar.HandlePaint;
var
  r: TfpgRect;
  diff: integer;
  aPos: integer;  // absolute position
  pos: integer;
  percent: integer;
  txt: string;
  x: TfpgCoord;
  y: TfpgCoord;
begin
  Canvas.BeginDraw;
//  inherited HandlePaint;
  Canvas.ClearClipRect;
  r.SetRect(0, 0, Width, Height);
  
  Canvas.Clear(BackgroundColor);
//  Canvas.SetColor(clInactiveWgFrame);

  // calculate position
  diff    := Max - Min; // diff..
  aPos    := Position - Min;	// absolute position
  percent := round(((100 / diff) * aPos));
  txt     := IntToStr(percent) + '%';
  pos     := round(percent * (Width-2) / 100);

  // Bluecurve theme  :)
  // outer dark border
  Canvas.SetColor(TfpgColor($999999));
  Canvas.SetLineStyle(1, lsSolid);
  Canvas.DrawRectangle(r);
  InflateRect(r, -1, -1);
  r.Width := pos;
  if FPosition > 0 then
  begin
    // left top
    Canvas.SetColor(TfpgColor($98b2ed));
    Canvas.DrawLine(r.Left, r.Bottom, r.Left, r.Top);  // left
    Canvas.DrawLine(r.Left, r.Top, r.Right, r.Top);    // top
    // right bottom
    Canvas.SetColor(TfpgColor($3b4c71));
    Canvas.DrawLine(r.Right, r.Top, r.Right, r.Bottom);   // right
    Canvas.DrawLine(r.Right, r.Bottom, r.Left-1, r.Bottom);   // bottom
    // inside gradient fill
    InflateRect(r, -1, -1);
    Canvas.GradientFill(r, TfpgColor($425d9b), TfpgColor($97b0e8), gdVertical);
  end;
  // paint percentage if required
  if FShowCaption then
  begin
  	x := (Width - FFont.TextWidth(txt)) div 2;
  	y := (Height - FFont.Height) div 2;
    Canvas.SetTextColor(clText1);
    Canvas.Font := FFont;
  	Canvas.DrawString(x, y, txt);
  end;
  Canvas.EndDraw;
end;

constructor TfpgCustomProgressBar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Focusable := False;
  Width := 150;
  Height := 22;
  FMin      := 0;
  FMax      := 100;
  FStep     := 1;
  FPosition := 0;
  FBackgroundColor := TfpgColor($c4c4c4); // clListBox;
  FShowCaption := False;
  FFont     := fpgStyle.DefaultFont;
end;

destructor TfpgCustomProgressBar.Destroy;
begin
  inherited Destroy;
end;

procedure TfpgCustomProgressBar.StepIt;
begin
  Position := Position + Step;
end;

procedure TfpgCustomProgressBar.StepBy(AStep: integer);
begin
  Position := Position + AStep;
end;

end.