-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigIt_Form_BuildDuplex.pas
643 lines (562 loc) · 17.2 KB
/
DigIt_Form_BuildDuplex.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
(*******************************************************************************
** DigIt **
** **
** (s) 2024 Massimo Magnano **
** **
********************************************************************************
** Wizard to Build Front/Back Pages from a Front Only Source **
*******************************************************************************)
unit DigIt_Form_BuildDuplex;
{$mode objfpc}{$H+}
//{$define Test}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, ComCtrls, ExtCtrls, Buttons, StdCtrls,
Digit_Bridge_Intf, Digit_Bridge_Impl, BGRAAnimatedGif;
resourcestring
CAPTION_PREV ='&Back';
CAPTION_NEXT ='&Next';
CAPTION_END ='&End';
CAPTION_CANCEL ='&Cancel';
MSG_CONFIRM_CANCELSTEP ='Cancel the Operation?';
MSG_ERR_CANCELSTEP ='Operation aborted by User';
rsCancelFront = 'Do I cancel the Front Side acquisition I just made?';
rsCancelBack = 'Do I cancel the Back Side acquisition I just made?';
rsNoPages = 'No Pages in Front or Back Side';
type
{ TWizardBuildDuplex }
Exception_CancelledOp = class (Exception);
TWizardBuildDuplex = class(TForm, IDigIt_ROArray, IDigIt_ProgressCallback)
Image1: TImage;
Image2: TImage;
animTurn: TImage;
Image4: TImage;
Image5: TImage;
Label1: TLabel;
Label2: TLabel;
lbBackSide: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
lbBackSide1: TLabel;
Panel1: TPanel;
PageControl1: TPageControl;
Panel2: TPanel;
btNext: TBitBtn;
btPrev: TBitBtn;
btCancel: TBitBtn;
rbOneSided: TRadioButton;
rbTwoSided: TRadioButton;
TabSheet2: TTabSheet;
TabSheet1: TTabSheet;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
tbDestination: TToolButton;
tbSource: TToolButton;
tbWizard: TToolBar;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btNextClick(Sender: TObject);
procedure btPrevClick(Sender: TObject);
procedure btCancelClick(Sender: TObject);
private
StartPageIndex,
CurPageIndex,
MaxPageIndex: Integer;
InStepCode: Boolean;
CancelledOp: Boolean;
FrontFiles,
BackFiles,
AllFiles: TStringArray;
OneSided: Boolean;
procedure GUI_Adjust;
procedure InternalNext;
procedure InternalPrev;
function BeforeNextStep(index: Integer): Boolean;
procedure NextStep(index: Integer);
function PrevStep(index: Integer): Boolean;
function CancelStep: Boolean;
function EndStep: Boolean;
function Files_Add(var AFiles: TStringArray; AArray: IDigIt_ROArray): Integer; overload;
function Files_Add(var AFiles: TStringArray; AFileName: String): Integer; overload;
public
//IDigIt_ROArray
function GetCount: DWord; stdcall;
function Get(const aIndex: DWord; out aData: Pointer): Boolean; stdcall;
//IDigIt_ProgressCallback
procedure ProgressCancelClick(ATotalValue, ACurrentValue: Integer); stdcall;
function Execute(out aDataType: TDigItDataType; out aData: Pointer): TModalResult;
end;
var
WizardBuildDuplex: TWizardBuildDuplex = nil;
function WizardBuildDuplexExecute(out aDataType: TDigItDataType; out aData: Pointer): Integer;
implementation
{$R *.lfm}
uses DigIt_Form_Main;
procedure TWizardBuildDuplex.GUI_Adjust;
begin
btPrev.Enabled := (CurPageIndex > StartPageIndex);
btNext.Enabled :=True;
if (CurPageIndex = MaxPageIndex)
then btNext.Caption :=CAPTION_END
else btNext.Caption :=CAPTION_NEXT;
PageControl1.ActivePage :=PageControl1.Pages[CurPageIndex-1];
end;
procedure TWizardBuildDuplex.InternalNext;
begin
if BeforeNextStep(CurPageIndex+1)
then begin
if (CurPageIndex = MaxPageIndex)
then begin
if EndStep
then ModalResult :=mrOk;
end
else begin
inc(CurPageIndex);
GUI_Adjust;
NextStep(CurPageIndex);
end;
end
else if (CurPageIndex = (StartPageIndex-1)) //Cancelled Before the First Step Close with Cancel
then ModalResult :=mrCancel;
end;
procedure TWizardBuildDuplex.InternalPrev;
begin
if PrevStep(CurPageIndex-1) then
begin
dec(CurPageIndex);
GUI_Adjust;
end;
end;
function TWizardBuildDuplex.BeforeNextStep(index: Integer): Boolean;
var
btNext_Enabled,
btPrev_Enabled :Boolean;
curData: Pointer;
curDataType: TDigItDataType;
res, i,
oldLength: Integer;
curImageFile: PChar;
curArray: IDigIt_ROArray;
procedure CheckCancelledOp;
begin
if CancelledOp then raise Exception_CancelledOp.Create(MSG_ERR_CANCELSTEP);
end;
begin
InStepCode := True;
//Avoid User Can Click during Step
btNext_Enabled := btNext.Enabled;
btPrev_Enabled := btPrev.Enabled;
btNext.Enabled := False;
btPrev.Enabled := False;
Result :=True;
try
(* Put YOUR Code Here : index is the Page about to be Showed, return False to block *)
Case index of
1:try
except
on E:Exception do raise Exception.Create('Error on Step:'+#13#10+'['+E.Message+']');
end;
2:try
except
on E:Exception do raise Exception.Create('Error on Step:'+#13#10+'['+E.Message+']');
end;
3:try //Before EndStep if index=(MaxPageIndex+1)
res:= DigIt_Main.Source^.Inst.Take(takeActTake, curDataType, curData);
Result:= (res > 0) and (curData <> nil);
if Result then
begin
if (curDataType = diDataType_FileName) then
begin
if (res = 1)
then begin
curImageFile:= PChar(curData);
Files_Add(FrontFiles, curImageFile);
StrDispose(curImageFile);
end
else
if (res > 1)
then begin
curArray:= IDigIt_ROArray(curData);
Files_Add(FrontFiles, curArray);
end;
end;
end;
except
on E:Exception do raise Exception.Create('Error on Step:'+#13#10+'['+E.Message+']');
end;
4:try
animTurn.Visible:= False;
res:= DigIt_Main.Source^.Inst.Take(takeActTake, curDataType, curData);
Result:= (res > 0) and (curData <> nil);
if Result then
begin
if (curDataType = diDataType_FileName) then
begin
if (res = 1)
then begin
curImageFile:= PChar(curData);
Files_Add(BackFiles, curImageFile);
StrDispose(curImageFile);
end
else
if (res > 1)
then begin
curArray:= IDigIt_ROArray(curData);
Files_Add(BackFiles, curArray);
end;
end;
end;
except
on E:Exception do raise Exception.Create('Error on Step:'+#13#10+'['+E.Message+']');
end;
end;
except
on E:Exception_CancelledOp do
begin
(* do something to Cancel the Operation *)
Case index of
1:try
except
end;
2:try
except
end;
end;
btNext_Enabled :=False;
Dialogs.MessageDlg(E.Message, mtError, [mbOk], 0);
end;
on E:Exception do
begin
btNext_Enabled :=False;
Dialogs.MessageDlg(E.Message, mtError, [mbOk], 0);
end;
end;
InStepCode := False;
//User Can Now Click (return to Previous Button State)
btNext.Enabled := btNext_Enabled;
btPrev.Enabled := btPrev_Enabled;
end;
procedure TWizardBuildDuplex.NextStep(index: Integer);
var
btNext_Enabled,
btPrev_Enabled :Boolean;
procedure CheckCancelledOp;
begin
if CancelledOp then raise Exception_CancelledOp.Create(MSG_ERR_CANCELSTEP);
end;
begin
InStepCode := True;
//Avoid User Can Click during Step
btNext_Enabled := btNext.Enabled;
btPrev_Enabled := btPrev.Enabled;
btNext.Enabled := False;
btPrev.Enabled := False;
try
(* Put YOUR Code Here : index is the Page Showed *)
Case index of
1:try
except
on E:Exception do raise Exception.Create('Error on Step:'+#13#10+'['+E.Message+']');
end;
2:try
OneSided:= rbOneSided.Checked;
except
on E:Exception do raise Exception.Create('Error on Step:'+#13#10+'['+E.Message+']');
end;
3:try
animTurn.Visible:= not(OneSided);
lbBackSide.Visible:= not(OneSided);
except
on E:Exception do raise Exception.Create('Error on Step:'+#13#10+'['+E.Message+']');
end;
end;
except
on E:Exception_CancelledOp do
begin
(* do something to Cancel the Operation *)
Case index of
1:try
except
end;
2:try
except
end;
end;
btNext_Enabled :=False;
Dialogs.MessageDlg(E.Message, mtError, [mbOk], 0);
end;
on E:Exception do
begin
btNext_Enabled :=False;
Dialogs.MessageDlg(E.Message, mtError, [mbOk], 0);
end;
end;
InStepCode := False;
//User Can Now Click (return to Previous Button State)
btNext.Enabled := btNext_Enabled;
btPrev.Enabled := btPrev_Enabled;
end;
function TWizardBuildDuplex.PrevStep(index: Integer): Boolean;
begin
Result := (index >= StartPageIndex);
if Result then
try
(* Put YOUR Code Here : index is the Page about to be Showed, return False to block *)
Case index of
1:begin
end;
2:begin
if (Length(FrontFiles) > 0) then
begin
Case MessageDlg('DigIt', rsCancelFront,
mtConfirmation, [mbYes, mbNo, mbCancel], 0) of
mrYes: begin
FrontFiles:= nil;
DigIt_Main.Source^.Inst.Clear;
end;
mrCancel: Result:= False;
end;
end;
end;
3:begin
if (Length(BackFiles) > 0) then
begin
Case MessageDlg('DigIt', rsCancelBack,
mtConfirmation, [mbYes, mbNo, mbCancel], 0) of
mrYes: begin
BackFiles:= nil;
DigIt_Main.Source^.Inst.Clear;
end;
mrCancel: Result:= False;
end;
end;
if Result then
begin
animTurn.Visible:= not(OneSided);
lbBackSide.Visible:= not(OneSided);
end;
end;
4:begin
end;
end;
except
on E:Exception do begin
Dialogs.MessageDlg('Error :'+#13#10+'['+E.Message+']', mtError, [mbOk], 0);
Result :=False; //True if User can Go Back in each case, False to Stop it
end;
end;
end;
function TWizardBuildDuplex.CancelStep: Boolean;
begin
try
(* Put YOUR Code Here : User Click the Cancel Button, return False to block *)
Result :=True;
except
on E:Exception do begin
Dialogs.MessageDlg('Error :'+#13#10+
'['+E.Message+']', mtError, [mbOk], 0);
Result :=False;
end;
end;
end;
function TWizardBuildDuplex.EndStep: Boolean;
var
i,
iAll,
lenFront,
lenBack,
len,
lenOver: Integer;
begin
try
{$ifdef Test}
OneSided:= False;
SetLength(FrontFiles, 4);
SetLength(BackFiles, 7);
for i:=0 to Length(FrontFiles)-1 do FrontFiles[i]:=IntToStr(i)+' front';
for i:=0 to Length(BackFiles)-1 do BackFiles[i]:=IntToStr(Length(BackFiles)-i-1)+' back';
{$endif}
AllFiles:= nil;
lenFront:= Length(FrontFiles);
lenBack:= Length(BackFiles);
if (lenFront=0) or (lenBack=0) then raise Exception.Create(rsNoPages);
//Take the smallest length in len and the difference with the largest in lenOver
if (lenFront <= lenBack)
then begin
len:= lenFront;
lenOver:= lenBack-lenFront;
end
else begin
len:= lenBack;
lenOver:= lenFront-lenBack;
end;
//Allocates space for two pages at a time plus any extra pages
SetLength(AllFiles, (len*2)+lenOver);
if OneSided
then begin
//Take Front and Back in the same order
iAll:= 0;
for i:=0 to len-1 do
begin
AllFiles[iAll]:= FrontFiles[i];
inc(iAll);
AllFiles[iAll]:= BackFiles[i];
inc(iAll);
end;
//Take Extra Pages
if (lenFront < lenBack)
then for i:=0 to lenOver-1 do AllFiles[iAll+i]:= BackFiles[len+i]
else for i:=0 to lenOver-1 do AllFiles[iAll+i]:= FrontFiles[len+i];
end
else begin
//Take Front in the Same order, Back in reverse order
iAll:= 0;
for i:=0 to len-1 do
begin
AllFiles[iAll]:= FrontFiles[i];
inc(iAll);
AllFiles[iAll]:= BackFiles[lenBack-i-1];
inc(iAll);
end;
//Take Extra Pages, Front in the Same order, Back in reverse order
if (lenFront < lenBack)
then for i:=1 to lenOver do AllFiles[iAll+i-1]:= BackFiles[lenOver-i]
else for i:=0 to lenOver-1 do AllFiles[iAll+i]:= FrontFiles[len+i];
end;
Result :=True;
except
on E:Exception do begin
Dialogs.MessageDlg('Error :'+#13#10+
'['+E.Message+']', mtError, [mbOk], 0);
Result :=False;
end;
end;
end;
function TWizardBuildDuplex.Files_Add(var AFiles: TStringArray; AArray: IDigIt_ROArray): Integer;
var
oldLength, i: Integer;
curImageFile: PChar;
begin
Result:= 0;
if (AArray <> nil) then
begin
//Add files to end of Array AFiles
oldLength:= Length(AFiles);
Result:= AArray.GetCount;
if (Result > 0) then
begin
SetLength(AFiles, oldLength+Result);
for i:=0 to Result-1 do
begin
if AArray.Get(i, curImageFile) then
begin
AFiles[oldLength+i]:= curImageFile;
StrDispose(curImageFile);
end;
end;
end;
end;
end;
function TWizardBuildDuplex.Files_Add(var AFiles: TStringArray; AFileName: String): Integer;
var
oldLength: Integer;
begin
Result:= 0;
if (AFileName <> '') then
begin
//Add files to end of Array AFiles
oldLength:= Length(AFiles);
SetLength(AFiles, oldLength+1);
AFiles[oldLength]:= AFileName;
Result:= 1;
end;
end;
procedure TWizardBuildDuplex.FormCreate(Sender: TObject);
begin
StartPageIndex :=1;
InStepCode :=False;
CancelledOp :=False;
FrontFiles:= nil;
BackFiles:= nil;
AllFiles:= nil;
end;
procedure TWizardBuildDuplex.FormDestroy(Sender: TObject);
begin
FrontFiles:= nil;
BackFiles:= nil;
AllFiles:= nil;
end;
procedure TWizardBuildDuplex.FormShow(Sender: TObject);
begin
try
MaxPageIndex :=PageControl1.PageCount;
btPrev.Caption :=CAPTION_PREV;
btNext.Caption :=CAPTION_NEXT;
btCancel.Caption :=CAPTION_CANCEL;
CurPageIndex :=StartPageIndex-1;
InternalNext;
except
on E:Exception do
Dialogs.MessageDlg('Error Opening the Wizard :'+#13#10+
'['+E.Message+']', mtError, [mbOk], 0);
end;
end;
procedure TWizardBuildDuplex.btNextClick(Sender: TObject);
begin
InternalNext;
end;
procedure TWizardBuildDuplex.btPrevClick(Sender: TObject);
begin
InternalPrev;
end;
procedure TWizardBuildDuplex.btCancelClick(Sender: TObject);
begin
if InStepCode
then begin
if (CancelledOp = False)
then CancelledOp := (MessageDlg(Self.Caption, MSG_CONFIRM_CANCELSTEP, mtConfirmation, mbYesNo, 0) = mrYes);
end
else if not(CancelStep)
then begin
{ TODO 10 : Cancel the Modal Click Event }
end;
end;
function TWizardBuildDuplex.GetCount: DWord; stdcall;
begin
Result:= Length(AllFiles);
end;
function TWizardBuildDuplex.Get(const aIndex: DWord; out aData: Pointer): Boolean; stdcall;
begin
Result:= (aIndex < Length(AllFiles));
if Result
then aData:= StrNew(PChar(AllFiles[aIndex]))
else aData:= nil;
end;
procedure TWizardBuildDuplex.ProgressCancelClick(ATotalValue, ACurrentValue: Integer); stdcall;
begin
CancelledOp:= True;
end;
function TWizardBuildDuplex.Execute(out aDataType: TDigItDataType; out aData: Pointer): TModalResult;
begin
StartPageIndex :=1;
{$ifdef Test} EndStep; {$endif}
if (DigIt_Main.Source <> nil) and (DigIt_Main.Source^.Inst <> nil)
then Result:= Self.ShowModal
else Result:= mrCancel;
end;
function WizardBuildDuplexExecute(out aDataType: TDigItDataType; out aData: Pointer): Integer;
begin
Result:= 0;
aDataType:= diDataType_FileName;
if (WizardBuildDuplex = nil)
then WizardBuildDuplex :=TWizardBuildDuplex.Create(Application);
if (WizardBuildDuplex <> nil) and
(WizardBuildDuplex.Execute(aDataType, aData) = mrOk) then
begin
Result:= Length(WizardBuildDuplex.AllFiles);
aData:= WizardBuildDuplex as IDigIt_ROArray;
end;
end;
end.