Recent

Author Topic: Unable to register required units on adding a component onto a form  (Read 1846 times)

Alexey Daryin

  • Newbie
  • Posts: 3
Lazarus 0.9.30.4 + FPC 2.6.0 for Win32 release

I am developing a component that requires several units to be added into the unit uses clause when the component is dropped onto a form.

From this article I discovered the way to set the designer hooks and add required files into a project with the possibility to add the unit to the uses clause.

The code sample:
Code: [Select]
procedure TTestComponent.DoCompAdded(APersistent: TPersistent; Select: boolean);
var
  oProj: TLazProject;
  oFile: TLazProjectFile;
begin
  oProj := LazarusIDE.ActiveProject;
  oFile := oProj.CreateProjectFile('X:\Unit path\AddonUnit.pas');
  oFile.IsPartOfProject := True;
  oProj.AddFile(oFile, True);
end;

constructor TTestComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  GlobalDesignHook.AddHandlerPersistentAdded(@DoCompAdded);
end;

procedure Register;
begin
  RegisterComponents('AAA',[TTestComponent]);
end;
 

On debugging I made sure that the handler was called, a unit AddonUnit was added to the project (I iterated thru all the files inside oProj object) but that led to a zero result: actually nothing was added to the project or to the uses clause.

Moreover, I found that project files collection oProj.Files[index] returns currently opened editor files and not the files included into the project.

Am I missing something or is there another way to include required units to uses clause or to the project?