summaryrefslogtreecommitdiff
path: root/src/gui/fpg_progressbar.pas
blob: e106577c6541ee5a2ee8251d10dccc3c311dbcfd (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
224
225
226
227
228
229
230
231
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      Defines a component for a progress bar.
}

unit fpg_progressbar;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  fpg_base,
  fpg_main,
  fpg_widget;
  
type
  TfpgCustomProgressBar = class(TfpgWidget)
  private
    FMax: longint;
    FMin: longint;
    FPosition: longint;
    FShowCaption: boolean;
    FStep: longint;
    FFont: TfpgFont;
    procedure   SetMax(const AValue: longint);
    procedure   SetMin(const AValue: longint);
    procedure   SetPBPosition(const AValue: longint);
    procedure   SetShowCaption(const AValue: boolean);
    procedure   SetStep(const AValue: longint);
  protected
    procedure   HandlePaint; override;
    property    Max: longint read FMax write SetMax default 100;
    property    Min: longint read FMin write SetMin default 0;
    property    Position: longint read FPosition write SetPBPosition default 0;
    property    Step: longint read FStep write SetStep;
//    property    FontName: string read GetFontName write SetFontName;
    property    ShowCaption: boolean read FShowCaption write SetShowCaption default False;
  public
    constructor Create(AOwner: TComponent); override;
    procedure   StepIt;
    procedure   StepBy(AStep: integer);
    property    Font: TfpgFont read FFont;
  end;
  
  
  TfpgProgressBar = class(TfpgCustomProgressBar)
  published
    property    Align;
    property    BackgroundColor default $c4c4c4;
    property    Enabled;
    property    Hint;
    property    ShowCaption;
    property    Max;
    property    Min;
    property    ParentShowHint;
    property    Position;
    property    ShowHint;
    property    Step;
    property    TextColor;
    property    OnShowHint;
  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.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.SetPBPosition(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
  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, 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(TextColor);
    Canvas.Font := FFont;
  	Canvas.DrawString(x, y, txt);
  end;
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;
  FTextColor := Parent.TextColor;
  FShowCaption := False;
  FFont     := fpgStyle.DefaultFont;
end;

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

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

end.