{==============================================================================

  Copyright (C) 2000-2001 by Evgeny Kryukov
  All rights reserved

  All contents of this file and all other files included in this archive
  are Copyright (C) 2001 Evgeny Kryukov. Use and/or distribution of
  them requires acceptance of the License Agreement.

  See License.txt for licence information

===============================================================================}

unit KsConvertForm;

{$I KsCommonVer.Inc}
{$T-,W-,X+,P+}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, Buttons, KsConvert, CheckLst, ComCtrls;

type
  TfrmConvertForm = class(TForm)
    Pages: TNotebook;
    Image1: TImage;
    lbWelcome: TLabel;
    Bevel1: TBevel;
    btnCancel: TBitBtn;
    btnNext: TBitBtn;
    btnBack: TBitBtn;
    Label2: TLabel;
    Label3: TLabel;
    Bevel2: TBevel;
    Panel1: TPanel;
    lbWText: TLabel;
    lbWarning: TLabel;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    GroupBox1: TGroupBox;
    lbOldComps: TListBox;
    btnAddOld: TButton;
    Bevel3: TBevel;
    Panel2: TPanel;
    Label6: TLabel;
    LogMemo: TMemo;
    btnClose: TBitBtn;
    btnStart: TBitBtn;
    CProg: TProgressBar;
    lbNewComps: TCheckListBox;
    Label7: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure PagesPageChanged(Sender: TObject);
    procedure btnNextClick(Sender: TObject);
    procedure btnBackClick(Sender: TObject);
    procedure btnStartClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure lbNewCompsClick(Sender: TObject);
    procedure btnAddOldClick(Sender: TObject);
  private
    { Private declarations }
    FConvertClass: TKsConverterClass;
    FConvertName: string;
    FConvertForm: TCustomForm;
    FConverter: TKsCustomConverter;
    procedure SetConvertClass(const Value: TKsConverterClass);
    procedure SetConvertName(const Value: string);
    procedure SetConvertForm(const Value: TCustomForm);
    procedure DoConvert(Sender: TObject; Max, Pos: integer);
    procedure BuildNewCompsList;
    procedure BuildOldCompsList;
  protected
    procedure Convert; virtual;
  public
    { Public declarations }
    procedure InitConverter; virtual;

    property ConvertClass: TKsConverterClass read FConvertClass write SetConvertClass;
    property ConvertForm: TCustomForm read FConvertForm write SetConvertForm;
    property ConvertName: string read FConvertName write SetConvertName;
    property Converter: TKsCustomConverter read FConverter;
  end;

var
  frmConvertForm: TfrmConvertForm;

implementation

uses KsConvertFormAdd;

{$R *.DFM}

procedure TfrmConvertForm.FormCreate(Sender: TObject);
var
  i: integer;
begin
  { Initialization }
  Pages.PageIndex := 0;

  FConvertClass := TKsCustomConverter;
  FConvertName := 'Kernel';
end;

procedure TfrmConvertForm.FormDestroy(Sender: TObject);
begin
  FConverter.Free;
end;

procedure TfrmConvertForm.InitConverter;
begin
  { Set control's property }
  lbWelcome.Caption := Format(lbWelcome.Caption, [FConvertName]);
  lbWText.Caption := Format(lbWText.Caption, [FConvertName]);

  FConverter := FConvertClass.Create(Self);

  BuildNewCompsList;
end;

procedure TfrmConvertForm.Convert;
begin
  Pages.PageIndex := Pages.PageIndex + 1;
  btnClose.Visible := true;
  btnClose.Enabled := false;
  btnNext.Visible := false;
  btnBack.Visible := false;
  btnStart.Visible := false;
  Application.ProcessMessages;
  try
    { Convertion }
    FConverter.OnProgress := DoConvert;
    FConverter.Log := LogMemo.Lines;

    FConverter.ConvertForm(FConvertForm);
  finally
    btnCancel.Visible := false;
    btnClose.Enabled := true;
  end;
end;

procedure TfrmConvertForm.BuildNewCompsList;
var
  i: integer;
  AObjectClass: TComponentClass;
  AObject: TComponent;
begin
  lbNewComps.Items.Clear;
  if FConverter = nil then Exit;

  for i := 0 to Integer(ckLast) - 1 do
  begin
    AObjectClass := FConverter.GetClass(TClassKind(i));
    if AObjectClass = nil then Continue;

    AObject := AObjectClass.Create(nil);
    try
      if AObject <> nil then
        lbNewComps.Items.AddObject(AObject.ClassName, Pointer(i));

      lbNewComps.Checked[i] := true;
    finally
      AObject.Free;
    end;
  end;
end;

procedure TfrmConvertForm.BuildOldCompsList;
var
  Kind: TClassKind;
  ClassList: string;
  i: integer;
begin
  { New Comps clicked }
  lbOldComps.Items.Clear;

  if (lbNewComps.ItemIndex < 0) or (lbNewComps.ItemIndex > lbNewComps.Items.Count - 1) then Exit;
  if FConverter = nil then Exit;

  Kind := TClassKind(lbNewComps.Items.Objects[lbNewComps.ItemIndex]);

  ClassList := FConverter.Rules[Kind];

  for i := 0 to GetClassCount(ClassList) - 1 do
    lbOldComps.Items.Add(GetClassName(ClassList, i));
end;

procedure TfrmConvertForm.PagesPageChanged(Sender: TObject);
begin
  case Pages.PageIndex of
    0: begin { Welcome }
      btnBack.Visible := false;
      btnNext.Visible := true;
      btnStart.Visible := false;
    end;
    1: begin { Comps }
      btnBack.Visible := true;
      btnNext.Visible := true;

      btnStart.Visible := true;
    end;
    2: begin { Convert }
      btnBack.Visible := true;
      btnNext.Visible := false;
    end;
  else
    btnBack.Visible := true;
    btnNext.Visible := true;
  end;
end;

procedure TfrmConvertForm.btnNextClick(Sender: TObject);
begin
  Pages.PageIndex := Pages.PageIndex + 1;
end;

procedure TfrmConvertForm.btnBackClick(Sender: TObject);
begin
  Pages.PageIndex := Pages.PageIndex - 1;
end;

procedure TfrmConvertForm.SetConvertClass(const Value: TKsConverterClass);
begin
  FConvertClass := Value;
end;

procedure TfrmConvertForm.SetConvertName(const Value: string);
begin
  FConvertName := Value;
end;

procedure TfrmConvertForm.btnStartClick(Sender: TObject);
begin
  Convert;
end;

procedure TfrmConvertForm.DoConvert(Sender: TObject; Max, Pos: integer);
begin
  CProg.Max := Max;
  CProg.Position := Pos;
end;

procedure TfrmConvertForm.SetConvertForm(const Value: TCustomForm);
begin
  FConvertForm := Value;
end;

procedure TfrmConvertForm.lbNewCompsClick(Sender: TObject);
begin
  BuildOldCompsList;
end;

procedure TfrmConvertForm.btnAddOldClick(Sender: TObject);
var
  NewClass: string;
  Kind: TClassKind;
  ClassList: string;
begin
  if FConverter = nil then Exit;

  frmAddClass := TfrmAddClass.Create(Self);
  if frmAddClass.ShowModal = mrOk then
    if frmAddClass.cbClassList.ItemIndex >= 0 then
    begin
      NewClass := frmAddClass.cbClassList.Items[frmAddClass.cbClassList.ItemIndex];
      Kind := TClassKind(lbNewComps.Items.Objects[lbNewComps.ItemIndex]);

      ClassList := FConverter.Rules[Kind];
      AddClassName(ClassList, NewClass);
      FConverter.Rules[Kind] := ClassList;

      BuildOldCompsList;
    end;
  frmAddClass.Free;
end;

end.
