forked from TES5Edit/TES3Edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmWaitForm.pas
246 lines (197 loc) · 6.26 KB
/
frmWaitForm.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{*******************************************************************************
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
*******************************************************************************}
unit frmWaitForm;
{$I wbDefines.inc}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,
wbInterface, StdCtrls, Buttons, ExtCtrls;
type
TwbProgress = class;
TfrmWait = class(TForm, IInterface, IwbWaitForm)
plMessage: TPanel;
Panel2: TPanel;
plCancel: TPanel;
bnCancel: TBitBtn;
procedure bnCancelClick(Sender: TObject);
protected {private}
fwRefCount: Integer;
fwCanceled : Boolean;
fwProgressHead : TwbProgress;
fwProgressTail : TwbProgress;
fwCanCancel : Boolean;
fwShowDelay : Integer;
fwUpdateDelay : Integer;
fwCreated : Cardinal;
fwLastUpdate : Cardinal;
procedure fwDoUpdate;
protected
{--- IInterface ---}
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
{--- IwbWaitForm ---}
function GetIsCanceled: Boolean;
function CreateProgress(const aCaption : string;
const aStatus : string;
aMax : Integer)
: IwbProgress;
public
constructor Create(const aCaption : string;
const aMessage : string;
aCanCancel : Boolean;
aShowDelay : Integer;
aUpdateDelay : Integer); reintroduce;
destructor Destroy; override;
end;
TwbProgress = class(TInterfacedObject, IwbProgress)
protected {private}
prgWait : TfrmWait;
prgWaitPrev : TwbProgress;
prgWaitNext : TwbProgress;
prgCaption : string;
prgMax : Integer;
prgPosition : Integer;
prgStatus : string;
procedure prgAddToWait;
procedure prgRemoveFromWait;
protected
{--- IwbProgress ---}
procedure UpdateStatus(aPosition: Integer; const aStatus: string);
public
constructor Create(aWait: TfrmWait; const aCaption, aStatus: string; aMax: Integer);
destructor Destroy; override;
end;
implementation
{$R *.dfm}
function _CreateWaitForm(const aCaption : string;
const aMessage : string;
aCanCancel : Boolean;
aShowDelay : Integer = 0;
aUpdateDelay : Integer = 0)
: IwbWaitForm;
begin
Result := TfrmWait.Create(aCaption, aMessage, aCanCancel, aShowDelay, aUpdateDelay);
end;
{ TfrmWait }
procedure TfrmWait.bnCancelClick(Sender: TObject);
begin
fwCanceled := True;
end;
constructor TfrmWait.Create(const aCaption, aMessage: string; aCanCancel: Boolean; aShowDelay, aUpdateDelay: Integer);
begin
inherited Create(nil);
Caption := aCaption;
fwCanCancel := aCanCancel;
fwShowDelay := aShowDelay;
fwUpdateDelay := aUpdateDelay;
fwCreated := GetTickCount;
fwLastUpdate := fwCreated;
plCancel.Visible := fwCanCancel;
plMessage.Caption := aMessage;
end;
function TfrmWait.CreateProgress(const aCaption, aStatus: string; aMax: Integer): IwbProgress;
begin
Result := TwbProgress.Create(Self, aCaption, aStatus, aMax);
end;
destructor TfrmWait.Destroy;
begin
while Assigned(fwProgressHead) do begin
fwProgressHead.prgWait := nil;
fwProgressHead := fwProgressHead.prgWaitNext;
end;
inherited;
end;
procedure TfrmWait.fwDoUpdate;
begin
if (Int64(GetTickCount) - fwUpdateDelay) > fwLastUpdate then begin
//!!!
fwLastUpdate := GetTickCount;
if not Visible then
if (Int64(GetTickCount) - fwShowDelay) > fwCreated then
Show;
Application.ProcessMessages;
end;
end;
function TfrmWait.GetIsCanceled: Boolean;
begin
Result := fwCanCancel and fwCanceled;
end;
function TfrmWait._AddRef: Integer;
begin
Result := InterlockedIncrement(fwRefCount);
end;
function TfrmWait._Release: Integer;
begin
Result := InterlockedDecrement(fwRefCount);
if Result = 0 then begin
Hide;
Release;
end;
end;
{ TwbProgress }
constructor TwbProgress.Create(aWait: TfrmWait; const aCaption, aStatus: string; aMax: Integer);
begin
inherited Create;
prgCaption := aCaption;
prgStatus := aStatus;
prgMax := aMax;
prgWait := aWait;
if Assigned(prgWait) then begin
prgAddToWait;
prgWait.fwDoUpdate;
end;
end;
destructor TwbProgress.Destroy;
begin
if Assigned(prgWait) then begin
prgRemoveFromWait;
prgWait.fwDoUpdate;
end;
inherited;
end;
procedure TwbProgress.prgAddToWait;
begin
prgWaitPrev := prgWait.fwProgressTail;
prgWait.fwProgressTail := Self;
if Assigned(prgWaitPrev) then
prgWaitPrev.prgWaitNext := Self;
if not Assigned(prgWait.fwProgressHead) then
prgWait.fwProgressHead := Self;
end;
procedure TwbProgress.prgRemoveFromWait;
begin
if Assigned(prgWaitNext) then
prgWaitNext.prgWaitPrev := prgWaitPrev
else
if prgWait.fwProgressTail = Self then
prgWait.fwProgressTail := prgWaitPrev;
if Assigned(prgWaitPrev) then
prgWaitPrev.prgWaitNext := prgWaitNext
else
if prgWait.fwProgressHead = Self then
prgWait.fwProgressHead := prgWaitNext;
prgWaitNext := nil;
prgWaitPrev := nil;
end;
procedure TwbProgress.UpdateStatus(aPosition: Integer; const aStatus: string);
begin
if not Assigned(prgWait) then
Exit;
if aPosition > prgMax then
aPosition := prgMax;
prgPosition := aPosition;
prgStatus := aStatus;
prgWait.fwDoUpdate;
end;
initialization
// wbCreateWaitForm := _CreateWaitForm;
end.