unit PCBlockDlg;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DlgBase,BlockFrm,FileCtrl,PCTypesUtils,GUIStrings;

type
  TPCBlockDlg = class(TDlgBase)
  private
    { Private declarations }
    Dial : TBlockForm;
    FBlockDir: String;
    Procedure GetCommand( comId: integer; values: string; valueI: integer );
    procedure SetBlockDir(Value: String);
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor create(Aowner: TComponent);override;
    destructor destroy;override;
    procedure show;override;
    Procedure SyncronizeContext;
    Procedure Syncronize;override;
    Procedure Locate(px,py:Integer);override;
    Procedure GetBlockFileNames(List:TStringlist);
  published
    { Published declarations }
    Property CadControl;
    Property BlockDirectory:String read FBlockDir write SetBlockDir;
  end;

implementation
{$R *.DCR}

var Filelist : TFileListBox;

constructor TPCBlockDlg.create(aowner: TComponent);
begin
  inherited create(Aowner);
  dial := TBlockForm.create(self);
  oncommand := GetCommand;
  DlgName := 'Block';
  Filelist := TFileListBox.Create(self);
  FileList.Parent := Dial;
  FileList.Visible := false;
end;

Procedure TPCBlockDlg.Show;
begin
  SyncronizeContext;
  dial.Show;
end;

Procedure TPCBlockDlg.Locate(px,py:Integer);
begin
  dial.Left := px;
  dial.top := py;
end;

Procedure TPCBlockDlg.SyncronizeContext;
var a: integer;
begin
  if assigned(CadControl) then
  begin
    with Dial do
    begin
      Cad := CadControl;
      Blckdir := BlockDirectory;
      Combobox1.Items.Clear;
      if (BlockDirectory <> '') and (DirectoryExists(BlockDirectory)) then begin
        Filelist.Directory := BlockDirectory;
        FileList.Mask := '*.pwl';
        FileList.Update;
        For a := 0 to FileList.Items.Count - 1 do
        begin
          Combobox1.Items.add(Copy(FileList.Items[a],1,Length(FileList.Items[a])-4));
        end;
        Combobox1.Items.add(asAllBlocks);
        If Combobox1.Items.Count > 0 then
        begin
          Combobox1.ItemIndex := 0;
          LoadBlockLib;
        end;
      end;
    end;
  end;
end;

Procedure TPCBlockDlg.Syncronize;
begin
  If Dial.Visible then SyncronizeContext;
end;

Procedure TPCBlockDlg.GetCommand(comId: integer; values: string; valueI: integer );
var blName: String;
begin
  If assigned(CadControl) then
  begin
    case ComId of
      0:  // NewLibrary added
          SyncronizeContext;
      1: CadControl.InsertBlockwithFileName(CadControl.ActiveLayer,values,-1,-1);
      2: begin
        if InputQuery('Make Block', 'Block Name', blname) then
        begin
          CadControl.MakeSelectionBlock(BlockDirectory+blname+'.pwb');
          Dial.AddBlockToCurrentLib(blName);
        end;

      end;
    end;
  end;
end;

procedure TPCBlockDlg.GetBlockFileNames(List: TStringlist);
var path: string;
    FileRec:TWIN32FindData;
    FHandle: THandle;
begin
  path := Self.BlockDirectory;
  if not DirectoryExists(Path) then exit;
  if not assigned(list) then exit;
  FHandle := FindFirstFile(pchar(path+'*.pwb'),FileRec);
  if FHandle <> INVALID_HANDLE_VALUE then
  begin
    List.Add(Self.BlockDirectory+FileRec.cFileName);
    while FindNextFile(Fhandle,FileRec) do
    begin
      List.Add(Self.BlockDirectory+FileRec.cFileName);
    end;
  end;
end;

procedure TPCBlockDlg.SetBlockDir(Value: String);
begin
  if (value <> '') and (copy(value,length(value),1) <> '\') then value := value +'\';
  FBlockDir := Value;
  if (Value <> '') then RegWrite('BlockDir',Value);
  if not (csDesigning in self.ComponentState) then SyncronizeContext;
end;

destructor TPCBlockDlg.destroy;
begin
  inherited;
end;

end.
