{ KOL MCK } // Do not remove this line!
{$DEFINE KOL_MCK}
unit Unit1;

interface

{$IF Defined(KOL_MCK)}
uses Windows, Messages, KOL, KOLEcmListEdit {$IF Defined(KOL_MCK)}{$ELSE}, mirror, Classes, Controls, mckCtrls, mckObjs, mckEcmListEdit, Graphics {$IFEND (place your units here->)}, KOLCryptedDBF;
{$ELSE}
{$I uses.inc}
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;
{$IFEND}

type
  {$IF Defined(KOL_MCK)}
  {$I MCKfakeClasses.inc}
  {$IFDEF KOLCLASSES} {$I TForm1class.inc} {$ELSE OBJECTS} PForm1 = ^TForm1; {$ENDIF CLASSES/OBJECTS}
  {$IFDEF KOLCLASSES}{$I TForm1.inc}{$ELSE} TForm1 = object(TObj) {$ENDIF}
    Form: PControl;
  {$ELSE not_KOL_MCK}
  TForm1 = class(TForm)
  {$IFEND KOL_MCK}
    KOLProj_: TKOLProject;
    KOLForm_: TKOLForm;
    Button1: TKOLButton;
    Button2: TKOLButton;
    Button3: TKOLButton;
    OSD: TKOLOpenSaveDialog;
    Label2: TKOLLabel;
    GroupBox1: TKOLGroupBox;
    DBFList: TKOLEcmListEdit;
    Label1: TKOLLabel;
    procedure KOLForm_Show(Sender: PObj);
    procedure Button1Click(Sender: PObj);
    procedure Button2Click(Sender: PObj);
    procedure Button3Click(Sender: PObj);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const strDescription =
  '201' { Windows_Russian_1251 } + #1 +
  'Column_1'#2'M'#1 +
  'Column_2'#2'M'#1 +
  'Column_3'#2'M'#1 +
  'Column_4'#2'M'#1 ;

var
  Form1 {$IFDEF KOL_MCK} : PForm1 {$ELSE} : TForm1 {$ENDIF} ;
  strPassword: string;
  DB: PCryptedDBF;
  
{$IFDEF KOL_MCK}
procedure NewForm1( var Result: PForm1; AParent: PControl );
{$ENDIF}

implementation
uses Unit2;

{$IF Defined(KOL_MCK)}{$ELSE}{$R *.DFM}{$IFEND}

{$IFDEF KOL_MCK}
{$I Unit1_1.inc}
{$ENDIF}

//-----------------------------------------------------
procedure TForm1.KOLForm_Show(Sender: PObj);
begin
 SendMessage( Form.Handle, WM_SETICON, ICON_SMALL,
  LoadIcon( 0, IDI_INFORMATION ) );
 Button1Click( Sender );
end;

//-----------------------------------------------------
procedure TForm1.Button1Click(Sender: PObj);
var
 i, j: Integer;
begin
 DBFList.Clear;
 for i:= 0 to 12 do
 begin
  DBFList.LVAdd( 'Item ' + Int2Str(i)+'x0' , 0, [lvisFocus], 0, 0, 0);
  for j:= 1 to 3 do
   DBFList.LVItems[i, j]:= 'Item ' + Int2Str(i)+'x'+Int2Str(j);
 end;
end;

//-----------------------------------------------------
procedure TForm1.Button2Click(Sender: PObj);
var
 i, j: Integer;
begin
 OSD.OpenDialog:= false;
 if ( OSD.Execute ) then
 begin
  strPassword:= '';
  NewForm2( Form2, Form );
  with ( Form2.Form^ ) do
  begin
   ShowModal;
   Free;
  end;
  if ( strPassword <> '' ) then
  begin
   DB:= NewCryptedDBF( '', true );
   if (not DB.NewDBF( OSD.Filename, strDescription, strPassword ) ) then
   begin
     MessageBox( Form.Handle, 'Ошибка создания БД!', 'Ошибка', MB_ICONERROR );
     DB.Free;
     Exit;
   end;

   with DB^ do
   begin
     for i:= 1 to DBFList.LVCount do
     begin
       Append;
       CurrentRecord:= RecordCount - 1;
       for j:= 0 to 3 do
        MemoAsString[j]:= DBFList.LVItems[i - 1, j];
     end;
     SetActive( false, strPassword );
     Free;
   end; // 'with DB^ do'

  end; // 'if ( strPassword <> '' )'
  strPassword:= '';

 end; // 'if ( OSD.Execute )'
end;

//-----------------------------------------------------
procedure TForm1.Button3Click(Sender: PObj);
var
 i, j: Integer;
begin
 OSD.OpenDialog:= true;
 if ( OSD.Execute ) then
 begin
  strPassword:= '';
  NewForm2( Form2, Form );
  with ( Form2.Form^ ) do
  begin
   ShowModal;
   Free;
  end;
  if ( strPassword <> '' ) then
  begin
   DB:= NewCryptedDBF( OSD.Filename, false, true );

   with DB^ do
   begin
     SetActive( true, strPassword );
     if ( ( Error <> eNoErrors ) or ( FileNameMemo = '' ) ) then
     begin
       MessageBox( Form.Handle, 'Ошибка открытия БД!', 'Ошибка', MB_ICONERROR );
       Free;
       Exit;
     end; // 'if ( ( Error <> eNoErrors ) or ( FileNameMemo = '' ) )'

     DBFList.Clear;
     for i:= 0 to RecordCount - 1 do
     begin
      CurrentRecord:= i;
      DBFList.LVAdd( '', 0, [], 0, 0, 0 );
      for j:= 0 to 3 do
       DBFList.LVItems[DBFList.LVCount - 1, j]:= MemoAsString[j];
     end; // 'for i:= 0 to RecordCount - 1 do'

     Free;
   end; // 'with DB^ do'

  end; // 'if ( strPassword <> '' )'
  strPassword:= '';
  
 end; // 'if ( OSD.Execute )'
end;

//-----------------------------------------------------

end.






