unit PCMacroDlg;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  PasEdit,PowerCad,DlgBase,commdlg,PCTypesUtils,FileCtrl,PCScript,Delphin,HALInit;

type

  TPCMacroDialog = class(TDlgBase)
  private
    Dial: TMacroForm;
    FMacroDirectory: string;
    procedure setdirectory(value: string);

    Procedure GetCommand( comId: integer; values: string; valueI: integer );
  protected
    MacroEngine: THalComp;
    Procedure SetControl(value: TPowerCad);override;
  public
    constructor create(Aowner: TComponent);override;
    Destructor Destroy;
    procedure show;override;
    Function getVisible(var hnd : THandle): Boolean;
    Procedure Locate(px,py:Integer);override;
    Procedure GetMacroFileNames(List:TStringlist);
    Procedure RunMacro(Macro:TStringlist);
    Procedure RunMacroText(Macro:String);
    Procedure RunMacroByFileName(MacroName:String);
    //Procedure AddPSCLConstant(ConstName:String;Value:Variant);
    //Procedure AddPSCLProcedure(ProcName:String;ProcAddr:TProcType;const Params:array of byte);
    //Procedure AddPSCLFunction(ProcName:String;ProcAddr:TProcType;const Params:array of byte);

  published
    Property MacroDirectory:string read FMacroDirectory write setdirectory;
    Property CadControl;
  end;

implementation
{$R *.DCR}

constructor TPCMacroDialog.create(Aowner: TComponent);
begin
  inherited create(Aowner);
  dial := TMacroForm.create(Application);
  oncommand := GetCommand;
  MacroEngine := THalComp.Create(nil);
end;

Destructor TPCMacroDialog.Destroy;
begin
  dial.free;
  MacroEngine.Free;
  inherited destroy;
end;

Procedure TPCMacroDialog.Show;
begin
  If FMacroDirectory = '' then
  begin
    showmessage('Macro directory is not specified');
    exit;
  end;
  dial.UpdateCombo;
  dial.InitCombo;
  dial.show;
end;

Procedure TPCMacroDialog.Locate(px,py:Integer);
begin
  Dial.Left := px;
  Dial.Top := py;
end;

Procedure TPCMacroDialog.GetMacroFileNames(List:TStringlist);
var path: string;
    FileRec:TWIN32FindData;
    a: integer;
    FHandle: THandle;
    MacroName: String;
begin
  path := Self.MacroDirectory;
  if not DirectoryExists(Path) then exit;
  if not assigned(list) then exit;
  FHandle := FindFirstFile(pchar(path+'*.cmf'),FileRec);
  if FHandle <> INVALID_HANDLE_VALUE then
  begin
    List.Add(Self.MacroDirectory+FileRec.cFileName);
    while FindNextFile(Fhandle,FileRec) do
    begin
      List.Add(Self.MacroDirectory+FileRec.cFileName);
    end;
  end;
end;

Function TPCMacroDialog.getVisible(var hnd : THandle): Boolean;
Begin
  hnd := Dial.Handle;
  result := Dial.visible;
end;


Procedure TPCMacroDialog.SetDirectory(value: string);
begin
  if (value <> '') and (copy(value,length(value),1) <> '\') then value := value +'\';
  FMacroDirectory := value;
  if (value <> '') then RegWrite('MacroDir',Value);
  if not (csDesigning in self.ComponentState) then
  begin
    dial.MDirectory := value;
    try
      dial.UpdateCombo;
      dial.InitCombo;
    except end;
  end;
end;


Procedure TPCMacroDialog.SetControl(value:TPowerCad);
begin
  inherited;
end;

Procedure TPCMacroDialog.GetCommand( comId: integer; values: string; valueI: integer );
begin
  case ComId of
    0 :
      begin
        RunMacroText(values);
      end;
  end;
end;

Procedure TPCMacroDialog.RunMacroByFileName(MacroName:String);
var macro: TStringList;
Begin
   SetControlForScripting(CadControl);
   macro := TStringlist.Create;
   if fileexists(MacroName) then
   begin
     Macro.LoadFromFile(Macroname);
     MacroEngine.script := Macro;
     MacroEngine.Compile('Self',MacroEngine);
     MacroEngine.Run;
   end;
   SetControlForScripting(nil);
End;

Procedure TPCMacroDialog.RunMacro(Macro:TStringlist);
Begin
   SetControlForScripting(CadControl);;
   MacroEngine.script := Macro;
   MacroEngine.Compile('Self',MacroEngine);
   MacroEngine.Run;
   SetControlForScripting(nil);
End;

Procedure TPCMacroDialog.RunMacroText(Macro:String);
var list:Tstringlist;
begin
   SetControlForScripting(CadControl);;
   list := TStringList.Create;
   list.text := Macro;
   MacroEngine.script := list;
   MacroEngine.Compile('Self',MacroEngine);
   MacroEngine.Run;
   List.Free;
   SetControlForScripting(nil);
end;

(*
Procedure TPCMacroDialog.AddPSCLConstant(ConstName:String;Value:Variant);
begin
  AddConst(ConstName,Value);
end;

Procedure TPCMacroDialog.AddPSCLProcedure(ProcName:String;ProcAddr:TProcType;const Params:array of byte);
begin
  AddProc(ProcName,ProcAddr,params);
end;

Procedure TPCMacroDialog.AddPSCLFunction(ProcName:String;ProcAddr:TProcType;const Params:array of byte);
begin
  AddFun(ProcName,ProcAddr,params);
end;
 *)


begin
  {$ifdef demo}
    if not delphiloaded then application.terminate;
  {$endif demo}
end.
