[Pas2js] LFM-to-PAS on-the-fly

warleyalex warleyalex at yahoo.com.br
Wed May 2 04:32:00 CEST 2018


The serialization mechanism (LFM-TO-PAS) comes from the TCompWriterPas
classes. 
...desperately, trying to serialize all forms (.LFM-to-PAS) from the
"Lazarus IDE" in one go, I came across with this wierd following code. 
------------------------------
type
  { TStreamAsPasForm }
  TStreamAsPasForm = class(TComponent)
  private
    FForm: TCustomForm;
  public
    procedure WriteComponents(AComponent: TComponent);
  published
    property FormName: TCustomForm read FForm write FForm;
  end;

procedure TStreamAsPasForm.WriteComponents(AComponent: TComponent);
var
  ms: TMemoryStream;
  s: string;
  aLazProject: TLazProject;
  aBaseDir: RawByteString;
begin
  If Assigned(LazarusIDE) and Assigned(LazarusIDE.ActiveProject) then
  begin
  aLazProject := LazarusIDE.ActiveProject;
    aBaseDir := ExtractFilePath( aLazProject.ProjectInfoFile);
    ms:=TMemoryStream.Create;
    try
      WriteComponentToPasStream(AComponent,ms);
      ms.Position:=0;
      SetLength(s,ms.Size);
      if s<>'' then
        ms.Read(s[1],length(s));
      ms.SaveToFile(aBaseDir + Self.FormName.Name +'.lfm.inc');
    finally
      ms.Free;
    end;
  end;
end


procedure doLFMtoPAS(Sender: TObject);

  procedure DFM2PAS(aFormName: TCustomForm);
  var
    StreamAsPasForm: TStreamAsPasForm;
  begin
    StreamAsPasForm := TStreamAsPasForm.Create(nil);
    try
      StreamAsPasForm.FormName := aFormName;
      StreamAsPasForm.FormName.Name := aFormName.Name;
      StreamAsPasForm.WriteComponents(StreamAsPasForm.FormName);
    finally
      StreamAsPasForm.Free;
    end;
  end;

var
  LazProject: TLazProject;
  LazFile: TLazProjectFile;
  aDesigner: TComponentEditorDesigner;
  CreateIncFile: Longint;
  i: Integer;
begin
  If Assigned(LazarusIDE) and Assigned(LazarusIDE.ActiveProject) then
  begin
  LazProject := LazarusIDE.ActiveProject;
  if LazProject <> nil then
    for i := 0 to LazProject.FileCount - 1 do
    begin
      LazFile := LazProject.Files[i];
      if LazFile.IsPartOfProject then
      begin
        if FilenameIsPascalUnit(LazFile.Filename) then
        begin
          aDesigner :=
           
TComponentEditorDesigner(LazarusIDE.GetDesignerWithProjectFile(LazFile,
True));
          if aDesigner = nil then
            exit; // this unit has no resource (form, frame, datamodule,
etc.)
          DFM2PAS(aDesigner.Form);
        end;
      end;
    end;
  end;
end;
------------------------------

It generates all corresponding (.LFM.INC) files. However, when we call the
doLFMtoPAS method from the "IDE", the components "event handlers are not
serialized" to pascal code as expected. f.i.
 
--// unit1.lfm //---------------
object Form1: TForm1
  Left = 200
  Height = 240
  Top = 100
  Width = 320
  Caption = 'Form1'
  ClientHeight = 240
  ClientWidth = 320
  object Button1: TButton
    Left = 20
    Height = 39
    Top = 17
    Width = 78
    Caption = 'Button1'
    OnClick = generateINCFiles
    TabOrder = 0
  end
  object Button2: TButton
    Left = 144
    Height = 89
    Top = 35
    Width = 102
    Caption = 'Button2'
    OnClick = Button2Click
    TabOrder = 1
  end
end
----------------------------------

--// Form1.lfm.inc //---------------
Button1:=TButton.Create(Self);
Button2:=TButton.Create(Self);
try
  Name:='Form1';
  Left:=200;
  Height:=240;
  Top:=100;
  Width:=320;
  Caption:='Form1';
  ClientHeight:=240;
  ClientWidth:=320;
  LCLVersion:='1.9.0.0';
  with Button1 do begin
    Name:='Button1';
    Parent:=Self;
    Left:=20;
    Height:=39;
    Top:=17;
    Width:=78;
    Caption:='Button1';
    TabOrder:=0;
    //  ---> did not generate the OnClick:=@generateINCFiles;
  end;
  with Button2 do begin
    Name:='Button2';
    Parent:=Self;
    Left:=144;
    Height:=89;
    Top:=35;
    Width:=102;
    Caption:='Button2';
    TabOrder:=1;
    //  ---> did not generate the OnClick:=@Button2Click;
  end;
finally
end;
------------------------------

 



--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list