summaryrefslogtreecommitdiff
path: root/gui/progressbar.inc
blob: 15183c15a8ed23f416106c716c3334641dc24429 (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
{
    fpGUI  -  Free Pascal Graphical User Interface
    Copyright (C) 2006 by Graeme Geldenhuys
      member of the fpGUI development team.

    Progress Bar class declarations

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

    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.

 **********************************************************************}


{%mainunit fpgui.pp}

{
  Progress Bar implementation
}

{$IFDEF read_interface}

  { TCustomProgressBar }

  TCustomProgressBar = class(TCustomPanel)
  private
    FFillColor: TColor;
    FMax: integer;
    FMin: integer;
    FPosition: integer;
    FShowPercentage: Boolean;
    procedure   SetFillColor(const AValue: TColor);
    procedure   SetMax(const AValue: integer);
    procedure   SetMin(const AValue: integer);
    procedure   SetPosition(const AValue: integer);
    procedure   SetShowPercentage(const AValue: Boolean);
  protected
    procedure   Paint(Canvas: TFCanvas); override;
    property    FillColor: TColor read FFillColor write SetFillColor default clRed;
    property    Position: integer read FPosition write SetPosition;
    property    Min: integer read FMin write SetMin default 0;
    property    Max: integer read FMax write SetMax default 100;
    property    ShowPercentage: Boolean read FShowPercentage write SetShowPercentage default True;
  public
    constructor Create(const pText: string; pOwner: TComponent); overload;
  end;


  TProgressBar = class(TCustomProgressBar)
  published
    property    CanExpandWidth;
    property    CanExpandHeight;
    property    Enabled;
//    property    Text;
    property    FillColor;
    property    Position;
    property    Min;
    property    Max;
    property    ShowPercentage;
  end;

{$ENDIF read_interface}



{$IFDEF read_implementation}

procedure TCustomProgressBar.SetFillColor(const AValue: TColor);
begin
  if FFillColor = AValue then exit;
  FFillColor := AValue;
  Redraw;
end;

procedure TCustomProgressBar.SetMax(const AValue: integer);
begin
  if FMax = AValue then exit;
  FMax := AValue;
  if FPosition > FMax then
    FPosition := FMax;
  Redraw;
end;

procedure TCustomProgressBar.SetMin(const AValue: integer);
begin
  if FMin = AValue then exit;
  FMin := AValue;
  if FPosition < FMin then
    FPosition := FMin;
  Redraw;
end;

procedure TCustomProgressBar.SetPosition(const AValue: integer);
begin
  if FPosition = AValue then
    exit; //==>
  if (AValue >= Min) and (AValue <= Max) then
  begin
    FPosition := AValue;
    Redraw;
  end;
end;

procedure TCustomProgressBar.SetShowPercentage(const AValue: Boolean);
begin
  if FShowPercentage = AValue then
    Exit; //==>
  FShowPercentage := AValue;
  Redraw;
end;

procedure TCustomProgressBar.Paint(Canvas: TFCanvas);
var
  Pt: TPoint;
  r: TRect;
  p: integer;
  percent: integer;
  t: string;
begin
  FText := '';
  inherited Paint(Canvas);
  Canvas.SetColor(Style.GetUIColor(FFillColor));
  
  percent := (100 div (Max - Min)) * FPosition;
  p := (percent * (Width - 3)) div 100;
  
  r := Rect(
        ClientRect.Left + 3,
        ClientRect.Top + 3,
        p,
        ClientRect.Bottom - 3);
  Canvas.FillRect(r);
  
  if FShowPercentage then
  begin
    t := IntToStr(percent) + '%';
    Pt.x := (Width - Canvas.TextWidth(t)) div 2;
    Pt.y := (Height - Canvas.FontCellHeight) div 2;
    Canvas.SetColor(Style.GetUIColor(clBtnText));
    Style.DrawText(Canvas, Pt, t, WidgetState);
  end;
end;

constructor TCustomProgressBar.Create(const pText: string; pOwner: TComponent);
begin
  inherited Create(pText, pOwner);
  FCanExpandHeight  := False;
  FBevelStyle       := bsLowered;
  FFillColor        := clRed;
  FMin              := 0;
  FMax              := 100;
  FShowPercentage   := True;
end;


{$ENDIF read_implementation}