forked from rejetto/hfs2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progFrmLib.pas
203 lines (169 loc) · 5.08 KB
/
progFrmLib.pas
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
{
Copyright (C) 2002-2008 Massimo Melina (www.rejetto.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
unit progFrmLib;
interface
uses
ComCtrls, Forms, controls, ExtCtrls, buttons, graphics;
type
TprogressForm = class
private
prog: TProgressBar;
frm: Tform;
msgPnl: Tpanel;
cancelBtn: TbitBtn;
btnPnl: Tpanel;
stack: array of record ofs,length:real end;
partialLength: real;
canceled: boolean;
function getPos():real;
procedure setPos(x:real);
function getGlobalPos():real;
procedure setGlobalPos(x:real);
function getCaption():string;
procedure setCaption(x:string);
function getVisible():boolean;
procedure onCancel(Sender: TObject);
procedure onResize(Sender: TObject);
public
preventBackward: boolean;
constructor create;
procedure show(caption_:string=''; cancel:boolean=FALSE);
procedure hide();
property progress:real read getPos write setPos;
property globalPosition:real read getGlobalPos write setGlobalPos;
property caption:string read getCaption write setCaption;
property visible:boolean read getVisible;
property cancelRequested:boolean read canceled;
procedure push(sublength:real);
procedure pop();
procedure showCancel();
procedure hideCancel();
procedure reset();
end;
implementation
function max(a,b:integer):integer;
begin if a > b then result:=a else result:=b end;
constructor TprogressForm.create;
begin
frm:=Tform.create(NIL);
frm.Position:=poScreenCenter;
frm.Width:=200;
frm.BorderStyle:=bsNone;
frm.BorderWidth:=15;
frm.Height:=25+frm.BorderWidth*2;
frm.OnResize:=onResize;
//frm.FormStyle:=fsStayOnTop;
msgPnl:=Tpanel.create(frm);
msgPnl.Parent:=frm;
msgPnl.align:=alTop;
msgPnl.height:=20;
msgPnl.BevelOuter:=bvLowered;
prog:=TProgressBar.Create(frm);
prog.Parent:=frm;
prog.BorderWidth:=3;
prog.Min:=0;
prog.max:=100; // resolution
prog.Align:=alClient;
prog.smooth:=TRUE;
btnPnl:=Tpanel.create(frm);
btnPnl.parent:=frm;
btnPnl.Align:=alBottom;
btnPnl.BevelOuter:=bvLowered;
cancelBtn:=TbitBtn.create(frm);
cancelBtn.parent:=btnPnl;
cancelBtn.Kind:=bkCancel;
cancelBtn.top:=10;
cancelBtn.OnClick:=onCancel;
btnPnl.Height:=cancelBtn.Height+cancelBtn.top*2;
btnPnl.Hide();
partialLength:=1;
push(1); // init stack
frm.Height:=frm.Height+msgPnl.Height;
end; // constructor
function TprogressForm.getVisible():boolean;
begin result:=frm.Visible end;
procedure TprogressForm.showCancel();
begin
if btnPnl.visible then exit;
frm.Height:=frm.Height+btnPnl.Height;
btnPnl.show();
end; // showCancel
procedure TprogressForm.hideCancel();
begin
if not btnPnl.visible then exit;
frm.Height:=frm.Height-btnPnl.Height;
btnPnl.hide();
end; // hideCancel
procedure TprogressForm.show(caption_: string; cancel:boolean);
begin
canceled:=FALSE;
if not frm.visible then reset();
if caption_ > '' then caption:=caption_;
if cancel then showCancel();
frm.Show();
end; // show
procedure TprogressForm.hide();
begin
frm.hide();
hideCancel();
end;
function TprogressForm.getCaption():string;
begin result:=msgPnl.caption end;
procedure TprogressForm.setCaption(x:string);
begin
msgPnl.caption:=x;
frm.Width:=max(200,
frm.Canvas.TextWidth(x)+(msgPnl.BorderWidth+frm.BorderWidth)*2+20 );
end;
procedure TprogressForm.setGlobalPos(x:real);
begin
x:=x*prog.max;
if preventBackward and (prog.position > x) then x:=prog.position;
prog.position:=round(x);
end; // setGlobalPos
function TprogressForm.getGlobalPos():real;
begin result:=prog.position/prog.max end;
procedure TprogressForm.setPos(x:real);
begin setGlobalPos(stack[length(stack)-1].ofs + x*partialLength ) end;
function TprogressForm.getPos():real;
begin result:=getGlobalPos()/partialLength + stack[length(stack)-1].ofs end;
procedure TprogressForm.push(sublength:real);
var
i: integer;
begin
assert(sublength <= 1,'TprogressForm.push(X): X>1');
i:=length(stack);
setLength(stack, i+1);
stack[i].ofs:=globalPosition;
stack[i].length:=partialLength;
partialLength:=partialLength*sublength;
end; // push
procedure TprogressForm.pop();
var
i: integer;
begin
assert(length(stack) > 1, 'TprogressForm.pop(): empty stack');
progress:=1;
i:=length(stack)-1;
partialLength:=stack[i].length;
setlength(stack, i);
end; // pop
procedure TprogressForm.onCancel(Sender: TObject);
begin canceled:=TRUE end;
procedure TprogressForm.onResize(Sender: TObject);
begin cancelBtn.left:=(frm.width-cancelBtn.width) div 2-frm.borderWidth end;
procedure TprogressForm.reset();
begin prog.position:=0 end;
end.