Program FileCopy;
{$D FileCopy}
{$I-}
uses WObjects, WinTypes, WinProcs;

Const
  AppName:PChar = 'FileCopy';
  rgb_Yellow = $0000FFFF;
  rgb_Blue   = $00FF0000;
  rgb_Red    = $000000FF;

Var
  SourceNumA : Integer;
  SourceNumB : Integer;
  SourceNumC : Integer;
  SourceNumD : Integer;
  TargetNumA : Integer;
  TargetNumB : Integer;
  TargetNumC : Integer;
  TargetNumD : Integer;
  Source     : Char;
  Target     : Char;
  SourceFile : String[30];
  TargetFile : String[30];
  Answer     : Boolean;
  NumColors  : Integer;

const
  id_Push1  = 101;
  id_Rad1   = 102;
  id_Rad2   = 103;
  id_Rad3   = 104;
  id_Rad4   = 105;
  id_Rad5   = 106;
  id_Rad6   = 107;
  id_Rad7   = 108;
  id_Rad8   = 109;
  id_Check1 = 110;
  id_SourceGroup = 111;
  id_TargetGroup = 112;
  id_Entry1 = 113;
  id_Entry2 = 114;
  Text1 = 115;
  Text2 = 116;

type
  CopyFile = object(TApplication)
    procedure InitMainWindow; virtual;
  end;

  PCopyWindow = ^CopyWindow;

  CopyWindow = object(TWindow)
    SourceA, SourceB: PRadioButton;
    SourceC, SourceD: PRadioButton;
    TargetA, TargetB: PRadioButton;
    TargetC, TargetD: PRadioButton;
    MyLogPalette:PLogPalette;
    BackGround:HBitmap;
    EntryField1 : PEdit;
    EntryField2 : PEdit;
    StartButton : PButton;
    Check1: PCheckBox;
    Text:PStatic;
    BlueBrush:HBrush;
    SourceGroup: PGroupBox;
    TargetGroup: PGroupBox;
    constructor Init(AParent: PWindowsObject; ATitle: PChar);
    procedure GetWindowClass(var AWndClass:TWndClass);Virtual;
    procedure PushStartButton(var Msg: TMessage);
      virtual id_First + id_Push1;
    procedure HandleSourceGroup(var Msg: TMessage);
      virtual id_First + id_SourceGroup;
    Procedure HandleTargetGroup(var Msg: TMessage);
      virtual id_First + id_TargetGroup;
    Procedure WMControlColor(var Msg:TMessage);
      virtual wm_First + wm_CtlColor;
  end;

{ -------- CopyWindow methods------------------ }
constructor CopyWindow.Init(AParent: PWindowsObject; ATitle: PChar);
var
  AButt: PButton;
begin
  TWindow.Init(AParent, ATitle);
  Attr.X:=105;
  Attr.Y:=90;
  Attr.W:=420;
  Attr.H:=280;
  Answer:=False;
  BlueBrush := CreateSolidBrush(rgb_Blue);

  StartButton := New(PButton, Init(@Self, id_Push1, 'Start Copy',
    250, 162, 100, 24, False));

  SourceGroup := New(PGroupBox, Init(@Self, id_SourceGroup, 'Source Drive',
    50, 30, 103, 80));
  SourceA := New(PRadioButton, Init(@Self, id_Rad1, 'A',
    65, 50, 38, 24, SourceGroup));
  SourceB := New(PRadioButton, Init(@Self, id_Rad2, 'B',
    65, 80, 38, 24, SourceGroup));
  SourceC := New(PRadioButton, Init(@Self, id_Rad3, 'C',
    110, 50, 38, 24, SourceGroup));
  SourceD := New(PRadioButton, Init(@Self, id_Rad4, 'D',
    110, 80, 38, 24, SourceGroup));

  Text := New(PStatic, Init(@Self, Text1, '------>',185,67,50,30,0));

  TargetGroup := New(PGroupBox, Init(@Self, id_TargetGroup, 'Target Drive',
    250, 30, 103, 80));
  TargetA := New(PRadioButton, Init(@Self, id_Rad5, 'A',
    265, 50, 38, 24, TargetGroup));
  TargetB := New(PRadioButton, Init(@Self, id_Rad6, 'B',
    265, 80, 38, 24, TargetGroup));
  TargetC := New(PRadioButton, Init(@Self, id_Rad7, 'C',
    310, 50, 38, 24, TargetGroup));
  TargetD := New(PRadioButton, Init(@Self, id_Rad8, 'D',
    310, 80, 38, 24, TargetGroup));

  Text := New(PStatic, Init(@Self, Text1, 'Source File Name',50,122,150,20,0));
  EntryField1 := New(PEdit, Init(@Self, id_Entry1, '',50,140,180,24,0,False));
  Text := New(PStatic, Init(@Self, Text2, 'Target File Name',50,172,150,20,0));
  EntryField2 := New(PEdit, Init(@Self, id_Entry2, '',50,190,180,24,0,False));

  MessageBox(HWindow,'Written By Adam A. Allard','FileCopy - ver 1.2',MB_OK);
End; (* Init Window *)


procedure CopyWindow.WMControlColor(var Msg: TMessage);
begin
  case Msg.LParamHi of

    ctlColor_Static:
      begin
        SetTextColor(Msg.WParam, rgb_Red);
        SetBkMode(Msg.WParam, TransParent);
        Msg.Result := GetStockObject(White_Brush);
      end;

    ctlcolor_Dlg:
      begin
        SetBkMode(Msg.WParam, Transparent);
        Msg.Result := BlueBrush;
      end;
  else
    DefWndProc(Msg);
  end; (* Case *)
end;

Procedure CopyWindow.GetWindowClass(var AWndClass:TWndClass);
Begin
  TWindow.GetWindowClass(AWndClass);
  AWndClass.hIcon:=LoadIcon(HInstance, AppName);
End;

Procedure CopyWindow.HandleSourceGroup(var Msg: TMessage);
Begin
  If (SourceA^.GetCheck <> 0) OR (SourceB^.GetCheck <> 0)
        OR (SourceC^.GetCheck <> 0) OR (SourceD^.GetCheck <> 0) Then
  Begin
    SourceNumA:=SourceA^.GetCheck;
    SourceNumB:=SourceB^.GetCheck;
    SourceNumC:=SourceC^.GetCheck;
    SourceNumD:=SourceD^.GetCheck;
    If SourceNumA = 1 then Source := 'A';
    If SourceNumB = 1 then Source := 'B';
    If SourceNumC = 1 then Source := 'C';
    If SourceNumD = 1 then Source := 'D';
  End; (* If *)
End; (* Procedure CopyWindow.HandleSourceGroup *)


Procedure CopyWindow.HandleTargetGroup(var Msg: TMessage);
Begin
  If (TargetA^.GetCheck <> 0) OR (TargetB^.GetCheck <> 0)
        OR (TargetC^.GetCheck <> 0) OR (TargetD^.GetCheck <> 0) Then
  Begin
    TargetNumA:=TargetA^.GetCheck;
    TargetNumB:=TargetB^.GetCheck;
    TargetNumC:=TargetC^.GetCheck;
    TargetNumD:=TargetD^.GetCheck;
    If TargetNumA = 1 then Target := 'A';
    If TargetNumB = 1 then Target := 'B';
    If TargetNumC = 1 then Target := 'C';
    If TargetNumD = 1 then Target := 'D';
  End; (* If *)
End; (* Procedure CopyWindow.HandleTargetGroup *)


Procedure CopyWindow.PushStartButton(var Msg: TMessage);
Var
  StartPos, EndPos:Integer;
  StartPos2,EndPos2:Integer;
  SourceFileName:Array[0..35] of Char;
  TargetFileName:Array[0..35] of Char;
  SourceFile,TargetFile:File;
  NumRead,NumWritten:Word;
  Buf:Array[1..2048] of Char;
  Reply,Reply2:Integer;
  CheckIt:Boolean;
  CheckA,CheckB,CheckC,CheckD:Integer;
  CheckAA,CheckBB,CheckCC,CheckDD:Integer;
Begin
  EntryField1^.GetSelection(StartPos,EndPos);
  EntryField1^.GetText(SourceFileName, 35);
  Checkit:=EntryField1^.IsModified;
  If Checkit=False then
    Begin
      MessageBox(HWindow,'You Must Enter A Source File Name','Copy Status',MB_OK+MB_IconExclamation);
      Exit;
    End;
  CheckA:=SourceA^.GetCheck;
  CheckB:=SourceB^.GetCheck;
  CheckC:=SourceC^.GetCheck;
  CheckD:=SourceD^.GetCheck;
  If (CheckA=0) AND (CheckB=0) AND (CheckC=0) AND (CheckD=0) then
    Begin
      MessageBox(HWindow,'You Must Enter A Source Drive','Copy Status',MB_OK+MB_IconExclamation);
      Exit;
    End;
  CheckAA:=TargetA^.GetCheck;
  CheckBB:=TargetB^.GetCheck;
  CheckCC:=TargetC^.GetCheck;
  CheckDD:=TargetD^.GetCheck;
  If (CheckAA=0) AND (CheckBB=0) AND (CheckCC=0) AND (CheckDD=0) then
    Begin
      MessageBox(Hwindow,'You Must Enter A Target Drive','Copy Status',MB_OK+MB_IconExclamation);
      Exit;
    End;
  If Answer=False then
    Begin
      EntryField2^.SetText(SourceFileName);
      EntryField2^.GetText(TargetFileName,35);
      Answer:=True;
      Reply := MessageBox(HWindow, 'Change Target File Name?','Copy Status', MB_YesNo+MB_IconQuestion);
        If Reply = id_Yes then Exit;
    End;

  EntryField2^.GetText(TargetFileName,35);
  Assign(SourceFile,Source+':\'+SourceFileName);
  Reset(SourceFile,1);
  If IOResult <> 0 then
    Begin
      MessageBox(HWindow,'File Not Found','Copy Status',MB_OK+MB_IconStop);
        Answer:=False;
        EntryField1^.SetText('');
        EntryField2^.SetText('');
        SourceA^.SetCheck(0);
        SourceB^.SetCheck(0);
        SourceC^.SetCheck(0);
        SourceD^.SetCheck(0);
        TargetA^.SetCheck(0);
        TargetB^.SetCheck(0);
        TargetC^.SetCheck(0);
        TargetD^.SetCheck(0);
      Exit;
    End;
  Assign(TargetFile,Target+':\'+TargetFileName);
  ReWrite(TargetFile,1);
    Repeat
      BlockRead(SourceFile,Buf,SizeOf(Buf), NumRead);
      BlockWrite(TargetFile,Buf,NumRead,NumWritten);
    Until (NumRead = 0) or (NumWritten <> NumRead);
  Close(SourceFile);
  Close(TargetFile);

  MessageBox(HWindow, 'File Copy Completed', 'Copy Status', MB_OK+MB_IconExclamation);
  Reply2 := MessageBox(HWindow, 'Copy Another File?','Copy Status', MB_YesNo+MB_IconQuestion);
    If Reply2=Id_Yes then
      Begin
        Answer:=False;
        EntryField1^.SetText('');
        EntryField2^.SetText('');
        SourceA^.SetCheck(0);
        SourceB^.SetCheck(0);
        SourceC^.SetCheck(0);
        SourceD^.SetCheck(0);
        TargetA^.SetCheck(0);
        TargetB^.SetCheck(0);
        TargetC^.SetCheck(0);
        TargetD^.SetCheck(0);
      End;
    If Reply2 = Id_No then Halt;
End; (* Procedure CopyWindow.PushStartButton *)


{ ----------- CopyFile Methods------------ }
procedure CopyFile.InitMainWindow;
begin
  MainWindow := New(PCopyWindow, Init(nil, 'Adam Allard''s File Copy Program'));
end;

var
  AdamCopy : CopyFile;

begin
  AdamCopy.Init('File Copy Program');
  AdamCopy.Run;
  AdamCopy.Done;
end.
