* * *

Author Topic: Instantiate Form from IDE  (Read 836 times)

warleyalex

  • New member
  • *
  • Posts: 24
Instantiate Form from IDE
« on: April 13, 2018, 12:58:33 am »
As a newbie I ran into many issues in this 2 months using Lazarus IDE. The LFM serialization mechanism which Lazarus ships with may be pretty useful and pretty straightforward for win/linux applications. In my current pet project, we're using the Lazarus IDE to generate an simple web app using the GUI approach.

Take a look at this mini video. I'm trying to mimic the Delphi TMS Web Core with the Lazarus IDE.

https://youtu.be/1Y6JoWUFEXw

...desperately, I couldn't find a way to generate LFM-TO-PAS on-the-fly.
The idea is getting notified when a designer form is saved to generate respective pas code. Exactly, I have to serialize all .LFM forms from the current project to equivalent .pas code. I've been thinking this task can be performed "directly from the Lazarus IDE" (through a component or add on) we can stream forms as pascal code straightforward, I believe this will not add big overhead in output JS. However, I'm stuck on this plan, to create and initialize the components from a form directly from the IDE, we have to register and pass a form instance to some method, anyway.

For educational purpose, let's illustrate the issue. Suppose the current project has 5 forms:

Code: Pascal  [Select]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms, unit1, unit2, unit3, unit4, unit5
  11.   { you can add units after this };
  12.  
  13. {$R *.res}
  14.  
  15. begin
  16.   Application.Initialize;
  17.   Application.CreateForm(TForm1, Form1);
  18.   Application.CreateForm(TForm2, Form2);
  19.   Application.CreateForm(TForm3, Form3);
  20.   Application.Run;
  21. end.

On the Lazarus IDE, we have a custom component on the toolbar. In somewhere in this component we have a code to access the current/active project, for instance:

Code: Pascal  [Select]
  1. var
  2.   LazProject: TLazProject;
  3. begin
  4.  { get the current project here }
  5.   LazProject:=LazarusIDE.ActiveProject;
  6.   ...
  7.  

From this LazProject reference variable we can have a list of all forms used in the active project, for instance. However, I can not easily access/instantiate forms (or any TComponent) directly from this point "LazarusIDE.ActiveProject"

Any idea how to instantiate a form by its name from the IDE?

Code: Pascal  [Select]
  1. TFormClass(GetClass('T'+aFormName)).Create(nil);  

Any idea would greatly appreciated.

best regards,
warleyalex

warleyalex

  • New member
  • *
  • Posts: 24
Re: Instantiate Form from IDE
« Reply #1 on: April 25, 2018, 11:42:29 pm »
Since we can access the active project from the Lazarus IDE, it should be easy to access its form units. Exactly, we just have to load the file interface, we can check if the unit has resource (e.g. form) and open and load the designer to access the project forms, as instantiated TComponents.

Code: Pascal  [Select]
  1. aDesigner:= TComponentEditorDesigner(LazarusIDE.GetDesignerWithProjectFile(AFile,true));
  2. if aDesigner=nil then
  3.   exit;
  4.  
  5. aDesigner.Form;
  6.  
« Last Edit: April 25, 2018, 11:44:41 pm by warleyalex »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus