1{%MainUnit ../comctrls.pp}
2{******************************************************************************
3                                  TCustomProgressBar
4 ******************************************************************************
5
6 *****************************************************************************
7  This file is part of the Lazarus Component Library (LCL)
8
9  See the file COPYING.modifiedLGPL.txt, included in this distribution,
10  for details about the license.
11 *****************************************************************************
12
13  current design flaws:
14
15  - I decided to support some gtk-specific properties in this class. This
16    won't break Delphi compatibility but for 100% Delphi compatibility
17    a better approach would be to derive another class.
18    BTW: When porting to another widget library you can safely ignore
19
20           FBarShowText
21           FBarTextFormat
22
23  - FBarTextFormat is a fixed string by now, hard-coded in the gtk-interface
24  - lot's of properties are missing
25  - I spend no thought on the usage of type integer for the range for the bar,
26    maybe this can cause trouble some day (and already will when FMin < 0!)
27}
28
29
30{------------------------------------------------------------------------------
31  Method: TCustomProgressBar.Create
32  Params:  AOwner: the owner of the class
33  Returns: Nothing
34
35  Constructor for the progressbar.
36 ------------------------------------------------------------------------------}
37constructor TCustomProgressBar.Create (AOwner : TComponent);
38begin
39  inherited Create(AOwner);
40  fCompStyle := csProgressBar;
41  FPosition  := 0;
42  FStep := 10;
43  FMin := 0;
44  FMax := 100;
45  FSmooth := False;
46  FOrientation := pbHorizontal;
47  FBarShowText := False;
48  FBarTextFormat := '%v from [%l-%u] (=%p%%)';
49  FStyle := pbstNormal;
50  with GetControlClassDefaultSize do
51    SetInitialBounds(0, 0, CX, CY);
52end;
53
54{------------------------------------------------------------------------------
55  Method: TCustomProgressBar.InitializeWnd
56  Params: none
57  Returns: Nothing
58
59  Set all properties after visual component has been created. Will be called
60  from TWinControl.
61 ------------------------------------------------------------------------------}
62procedure TCustomProgressBar.InitializeWnd;
63begin
64  inherited InitializeWnd;
65  ApplyChanges;
66end;
67
68procedure TCustomProgressBar.Loaded;
69begin
70  inherited Loaded;
71  ApplyChanges;
72end;
73
74class function TCustomProgressBar.GetControlClassDefaultSize: TSize;
75begin
76  Result.CX := 100;
77  Result.CY := 20;
78end;
79
80{------------------------------------------------------------------------------
81  Method: TCustomProgressBar.GetMin
82  Params:  Nothing
83  Returns: actual minimum value of the progressbar
84
85  Retrieve the actual minimum value of the progressbar.
86 ------------------------------------------------------------------------------}
87function TCustomProgressBar.GetMin: Integer;
88begin
89  Result := FMin;
90end;
91
92{------------------------------------------------------------------------------
93  Method: TCustomProgressBar.GetMax
94  Params:  Nothing
95  Returns: actual maximum value of the progressbar
96
97  Retrieve the actual maximum value of the progressbar.
98 ------------------------------------------------------------------------------}
99function TCustomProgressBar.GetMax: Integer;
100begin
101  Result := FMax;
102end;
103
104{------------------------------------------------------------------------------
105  Method: TCustomProgressBar.GetPosition
106  Params:  Nothing
107  Returns: actual position of the progressbar
108
109  Retrieve the position of the progressbar.
110 ------------------------------------------------------------------------------}
111function TCustomProgressBar.GetPosition: Integer;
112begin
113  Result := FPosition;
114end;
115
116{------------------------------------------------------------------------------
117  Method: TCustomProgressBar.SetParams
118  Params:  Min & Max for the progressbar
119  Returns: Nothing
120
121  Set new minimum and maximum values for the progressbar.
122 ------------------------------------------------------------------------------}
123procedure TCustomProgressBar.SetParams(AMin, AMax: Integer);
124begin
125  SetMax(AMax);
126  SetMin(AMin);
127end;
128
129
130{------------------------------------------------------------------------------
131  Method: TCustomProgressBar.SetMin
132  Params:  Minimum value for the progressbar
133  Returns: Nothing
134
135  Set new minimum value for the progressbar.
136 ------------------------------------------------------------------------------}
137procedure TCustomProgressBar.SetMin(Value: Integer);
138begin
139  if FMin <> Value then
140  begin
141    FMin := Value;
142    ApplyChanges;
143  end;
144end;
145
146
147{------------------------------------------------------------------------------
148  Method: TCustomProgressBar.SetMax
149  Params:  Maximum value for the progressbar
150  Returns: Nothing
151
152  Set new maximum value for the progressbar.
153 ------------------------------------------------------------------------------}
154procedure TCustomProgressBar.SetMax(Value: Integer);
155begin
156  if FMax <> Value then
157  begin
158    FMax := Value;
159    ApplyChanges;
160  end;
161end;
162
163{------------------------------------------------------------------------------
164  Method: TCustomProgressBar.SetPosition
165  Params:  New acutal position for the progressbar
166  Returns: Nothing
167
168  Set new new acutal position for the progressbar
169 ------------------------------------------------------------------------------}
170procedure TCustomProgressBar.SetPosition(Value: Integer);
171begin
172  if FPosition <> Value then
173  begin
174    FPosition := Value;
175    ApplyChanges;
176  end;
177end;
178
179
180{------------------------------------------------------------------------------
181  Method: TCustomProgressBar.SetPosition
182  Params:  New stepping value for the progressbar
183  Returns: Nothing
184
185  Set new stepping value for the progressbar
186 ------------------------------------------------------------------------------}
187procedure TCustomProgressBar.SetStep(Value: Integer);
188begin
189  if FStep <> Value then
190  begin
191    FStep := Value;
192    ApplyChanges;
193  end;
194end;
195
196{------------------------------------------------------------------------------
197  Method: TCustomProgressBar.SetPosition
198  Params:  New orientation of the progressbar
199  Returns: Nothing
200
201  Set new orientation.
202 ------------------------------------------------------------------------------}
203procedure TCustomProgressBar.SetOrientation (Value : TProgressBarOrientation);
204begin
205  if FOrientation <> Value then
206  begin
207    Forientation := Value;
208    ApplyChanges;
209  end;
210end;
211
212procedure TCustomProgressBar.SetStyle(const AValue: TProgressBarStyle);
213begin
214  if FStyle <> AValue then
215  begin
216    FStyle := AValue;
217    if HandleAllocated then
218      TWSProgressBarClass(WidgetSetClass).SetStyle(Self, AValue);
219  end;
220end;
221
222class procedure TCustomProgressBar.WSRegisterClass;
223begin
224  inherited WSRegisterClass;
225  RegisterCustomProgressBar;
226end;
227
228{------------------------------------------------------------------------------
229  Method: TCustomProgressBar.StepIt
230  Params:  Nothing
231  Returns: Nothing
232
233  Let the progressbar proceed from actual position to actualposition + Step
234 ------------------------------------------------------------------------------}
235procedure TCustomProgressBar.StepIt;
236begin
237  inc(FPosition, FStep);
238  if FPosition > FMax then FPosition := FMax;
239  if FPosition < FMin then FPosition := FMin;
240  if HandleAllocated then
241    TWSProgressBarClass(WidgetSetClass).SetPosition(Self, FPosition);
242end;
243
244
245{------------------------------------------------------------------------------
246  Method: TCustomProgressBar.StepIt
247  Params:  Delta : the value to add to actual position
248  Returns: Nothing
249
250  Let the progressbar proceed from actual position to actualposition + Delta
251
252  Implementation detail:
253   StepBy is realized by faking the current position to get a solution
254   which is independant from the widget set in use.
255 ------------------------------------------------------------------------------}
256procedure TCustomProgressBar.StepBy(Delta: Integer);
257begin
258  FPosition := FPosition + Delta - FStep;
259  StepIt;
260end;
261
262{------------------------------------------------------------------------------
263  Method: TCustomProgressBar.ApplyChanges
264  Params:  Nothing
265  Returns: Nothing
266
267  Apply the current parameters to the object
268 ------------------------------------------------------------------------------}
269procedure TCustomProgressBar.ApplyChanges;
270begin
271  if not (csLoading in ComponentState) then
272  begin
273    if FMin > Max then FMin := Max;
274    if Position < Min then FPosition := Min;
275    if Position > Max then FPosition := Max;
276    if HandleAllocated then
277      TWSProgressBarClass(WidgetSetClass).ApplyChanges(Self);
278  end;
279end;
280
281
282{------------------------------------------------------------------------------
283  Method: TCustomProgressBar.SetSmooth
284  Params:  Value : Smoothing on or off
285  Returns: Nothing
286
287  Set the style of the progressbar
288 ------------------------------------------------------------------------------}
289procedure TCustomProgressBar.SetSmooth (Value : boolean);
290begin
291  if FSmooth <> value then
292  begin
293    FSmooth := value;
294    ApplyChanges;
295  end;
296end;
297
298
299{------------------------------------------------------------------------------
300  Method: TCustomProgressBar.SetBarShowText
301  Params:  Value : ShowText on or off
302  Returns: Nothing
303
304  Some widget sets can put a label on the progressbar which shows the
305  current position
306
307  Implementation detail:
308   This functionality is not Delphi-compatible
309 ------------------------------------------------------------------------------}
310procedure TCustomProgressBar.SetBarShowText (Value : boolean);
311begin
312  if FBarShowText <> Value then
313  begin
314    FBarShowText := Value;
315    ApplyChanges;
316  end;
317end;
318
319// included by comctrls.pp
320