DGH2112 / The-Open-Tools-API-Project

This is a collection of code and articles for my Open Tools API blog post.
17 stars 2 forks source link

OTAFormWizard using form inheritance. #1

Open giorgiobazzo opened 5 months ago

giorgiobazzo commented 5 months ago

Hello,

Your repository was recommended to me by Embarcadero support itself. I looked for something that could help me in your e-book "The Delphi IDE Open Tools API - Version 1.2", but I didn't find exactly what I was looking for. So I hope you read this Issue and can help me!

Let's go!

For some time I have been trying (but without success) to use OTAFormWizard and register a Wizard to create forms that are based on another base form.

The idea is to have a base form, with all the common visual parts (buttons, panels, etc.) already defined, and for the Wizard to create new forms from this base form.

I managed to get the Wizard to create the unit (.pas) with the desired code, but it does not inherit the visual part (.dfm) as expected, the new form is created empty, without the components that are in the base form.

I used the IOTAModuleCreator interface to create the .pas implementing the NewImplSource function, and that worked. But even implementing NewFormFile with the desired .dfm code, when I create the new form through File|New|Other|New I receive this message:

image

It seems that the Wizard doesn't understand that I want to create a form that inherits the components of its ancestor. Then he adds the unit to the project (.dpr) using:

...
contains
....
   Unit1 in 'Unit1.pas' {fmBaseForm1: TfmBaseForm};

rather than:

...
contains
....
   Unit1 in 'Unit1.pas' {fmBaseForm1};

that would be correct.

And also in the .dproj file something like:

         <DCCReference Include="Unit1.pas">
             <Form>fmBaseForm1</Form>
             <FormType>dfm</FormType>
             <DesignClass>TfmBaseForm</DesignClass>
         </DCCReference>

where the DesignClass element should not be added for it to work as expected.

I know I can get a similar result using the "Add to Repository" option, but I think that with wizard it would be better to standardize several IDE installations.

I'll attach an example of the approach I'm trying to use. InheritedFormWizard.zip

Finally, I would like to know if this is possible, and what might be missing in my example to make it work.

Grateful! Any help will be welcome!

DGH2112 commented 5 months ago

Its quite some time since I went into this code but the one thought that does occur is regarding the first line of the DFM in that it needs to be "inherited" as below from one of my apps where I inherit frames from frames with base properties. I could not see where the DFM was being defined (as I say, not looked at this for a very long time and I'm not at a dev machine).

inherited frmCalendars: TfrmCalendars
  Width = 755
  Height = 509
  HelpContext = 5060
  Font.Height = -13
  ParentFont = False
  ExplicitWidth = 755
  ExplicitHeight = 509
  ...
end
giorgiobazzo commented 5 months ago

Sorry, I sent the wrong version of the example, here is the correct version, with the dfm definition: InheritedFormWizard.zip

const
...
  DmfSource =
    'inherited %0:s: T%0:s'#13#10 +
    '  Caption = ''%0:s'''#13#10 +
    '  StyleElements = [seFont, seClient, seBorder]'#13#10 +
    '  TextHeight = 13'#13#10 +
    '  inherited Panel1: TPanel'#13#10 +
    '    StyleElements = [seFont, seClient, seBorder]'#13#10 +
    '  end'#13#10 +
    '  inherited Panel2: TPanel'#13#10 +
    '    StyleElements = [seFont, seClient, seBorder]'#13#10 +
    '  end'#13#10 +
    'end'#13#10;
...

function TBaseFormCreator.NewFormFile(const FormIdent, AncestorIdent: string): IOTAFile;
var
  LDfm : string;
begin
  LDfm := Format(DmfSource, [FormIdent]);
  Result := TUnitFile.Create(LDfm);
end;
...
DGH2112 commented 5 months ago

So the next thing to check would be that the base form is part of the project before trying to inherit from it? At the moment it looks like the base form is part of the wizard project but it needs to be referenced by the project in which you are inserting the inherited form (trusting I've understood what you are trying to do). regards Dave.

giorgiobazzo commented 5 months ago

"The next thing to check would be that the base form is part of the project before trying to inherit from it?"

No, my idea is that it is part of the wizard package registered in the IDE. Precisely so that I don't need to add it manually as part of each project (I don't know if this is possible). Of course, the path of this base form would be configured in the IDE's Library Path.

What's strange is that the implementation of TBaseFormCreator.NewFormFile seems to be of no use, as the .dfm code I'm generating in:

...
LDfm := Format(DmfSource, [FormIdent]);
...

It's not the code we see after the wizard creates the form.

I'm doing the tests by installing the Wizard, and trying to create a new form through the new wizard, in the wizard project itself, where the base form is part, but even there, I'm having the same problem I have in other projects.

Thank you very much for your attention Dave!

DGH2112 commented 5 months ago

I'll this open for the time being. If I get some time over the bank holiday weekend coming I'll see if I can reproduce the issue and if so I'll raise it to Embarcadero as a bug.

david-millington commented 5 months ago

Hey - I saw this in my github notifications. Both form inheritance and these APIs are tricky.

Precisely so that I don't need to add it manually as part of each project (I don't know if this is possible)

It should be, but it needs to be able to find the ancestor form in a way the uses clause can resolve. For example, find the source .pas and .dfm, or the project has a reference to the package they are located in.

For the issues here, if I was to debug them, I'd compare with a diff tool the project created by the wizard, and a project created manually that works, and see exactly what is different. That way you can see if the DFM contents are correct, if the uses clause is correct, if the project file contains the same references (diffing projects is annoying, sorry), etc.

giorgiobazzo commented 5 months ago

Hi @david-millington,

I don't know if I managed to understand this in the first comment on this issue, but I made this comparison.

I noticed 3 things that should be different for the inherited form using a wizard to be correct:

1 - That the .dfm code would be as defined in the string constant that I defined. Maybe this doesn't happen because of items 2 and 3 below.

2 - that in the .dpr the unit was added as:

Unit1 in 'Unit1.pas' {fmBaseForm1}; and not: Unit1 in 'Unit1.pas' {fmBaseForm1: TfmBaseForm};

3 - That in the .dproj in DCCReference Include="Unit1.pas" the DesignClass element was not added.

The problem seems to be how to make the OTA have this behavior, which interfaces or implementations to use, if they exist at all.

A good day to all!